forked from rodrigo-brito/ninjabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ninjabot_test.go
115 lines (96 loc) · 2.55 KB
/
ninjabot_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package ninjabot
import (
"context"
"github.com/rodrigo-brito/ninjabot/strategy"
"testing"
"github.com/markcheno/go-talib"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/rodrigo-brito/ninjabot/exchange"
"github.com/rodrigo-brito/ninjabot/service"
"github.com/rodrigo-brito/ninjabot/storage"
)
type fakeStrategy struct{}
func (e fakeStrategy) Timeframe() string {
return "1d"
}
func (e fakeStrategy) WarmupPeriod() int {
return 9
}
func (e fakeStrategy) Indicators(df *Dataframe) []strategy.ChartIndicator {
df.Metadata["ema9"] = talib.Ema(df.Close, 9)
return nil
}
func (e *fakeStrategy) OnCandle(df *Dataframe, broker service.Broker) {
closePrice := df.Close.Last(0)
assetPosition, quotePosition, err := broker.Position(df.Pair)
if err != nil {
log.Error(err)
}
if quotePosition > 0 && df.Close.Crossover(df.Metadata["ema9"]) {
_, err := broker.CreateOrderMarket(SideTypeBuy, df.Pair, quotePosition/closePrice*0.5)
if err != nil {
log.Fatal(err)
}
}
if assetPosition > 0 &&
df.Close.Crossunder(df.Metadata["ema9"]) {
_, err := broker.CreateOrderMarket(SideTypeSell, df.Pair, assetPosition)
if err != nil {
log.Fatal(err)
}
}
}
func TestMarketOrder(t *testing.T) {
ctx := context.Background()
storage, err := storage.FromMemory()
require.NoError(t, err)
strategy := new(fakeStrategy)
csvFeed, err := exchange.NewCSVFeed(
strategy.Timeframe(),
exchange.PairFeed{
Pair: "BTCUSDT",
File: "testdata/btc-1h.csv",
Timeframe: "1h",
},
exchange.PairFeed{
Pair: "ETHUSDT",
File: "testdata/eth-1h.csv",
Timeframe: "1h",
},
)
require.NoError(t, err)
paperWallet := exchange.NewPaperWallet(
ctx,
"USDT",
exchange.WithPaperAsset("USDT", 10000),
exchange.WithDataFeed(csvFeed),
)
bot, err := NewBot(ctx, Settings{
Pairs: []string{
"BTCUSDT",
"ETHUSDT",
},
},
paperWallet,
strategy,
WithStorage(storage),
WithBacktest(paperWallet),
WithLogLevel(log.ErrorLevel),
)
require.NoError(t, err)
require.NoError(t, bot.Run(ctx))
assets, quote, err := bot.paperWallet.Position("BTCUSDT")
require.NoError(t, err)
require.Equal(t, assets, 0.0)
require.InDelta(t, quote, 26694.6741, 0.001)
results := bot.orderController.Results["BTCUSDT"]
require.InDelta(t, 7424.3705, results.Profit(), 0.001)
require.Len(t, results.Win, 6)
require.Len(t, results.Lose, 11)
results = bot.orderController.Results["ETHUSDT"]
require.InDelta(t, 9270.3036, results.Profit(), 0.001)
require.Len(t, results.Win, 9)
require.Len(t, results.Lose, 8)
bot.Summary()
}