-
Notifications
You must be signed in to change notification settings - Fork 0
/
baregone_test.go
82 lines (63 loc) · 1.68 KB
/
baregone_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package baregone
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"strconv"
"testing"
"time"
lop "github.com/samber/lo"
)
type MD struct {
Price string
Time string
}
type JsonFile struct {
Date int
MarketData []MD
}
func TestBacktest(t *testing.T) {
file, _ := ioutil.ReadFile("BTCUSDT.json")
data := JsonFile{}
_ = json.Unmarshal([]byte(file), &data)
// log.Print("original", data.MarketData[1])
marketData := lop.Map(data.MarketData, func(md MD, i int) BarData {
dateUnix, _ := strconv.ParseInt(md.Time, 10, 64)
price, _ := strconv.ParseInt(md.Price, 10, 64)
return BarData{
date: time.Unix(dateUnix/1000, 0),
close: int(price),
volume: 0,
}
})
// log.Print("parsed", marketData)
const demoTargetPrice = 8100
backtestArgs := BacktestArgs{
marketData: marketData,
option: BackTestOptions{
capital: 1000,
debug: true,
},
strategy: Strategy{
analysePosition: func(args AnalysePositionArgs) {
// log.Print("analysePosition ----------------", args.bar.close)
log.Print("analysePosition ----------------", args.position.tradeType)
if args.position.profit <= 0 {
log.Print("----------------closing position ----------------")
args.exitPosition()
}
},
onMarketTick: func(args OnMarketTickArgs) {
if args.bar.close >= demoTargetPrice {
args.enterPosition("BUY")
}
// log.Print("currentBar ----------------", args.bar.close)
},
},
}
results := Backtest(backtestArgs)
fmt.Println("totalTrades ---------------------------", results.totalTrades)
fmt.Println("profit --------------------------------", results.profit)
fmt.Println("profit --------------------------------", results.trades)
}