forked from rodrigo-brito/ninjabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ninjabot.go
393 lines (329 loc) · 10.9 KB
/
ninjabot.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package ninjabot
import (
"bytes"
"context"
"fmt"
"os"
"strconv"
"github.com/aybabtme/uniplot/histogram"
"github.com/rodrigo-brito/ninjabot/exchange"
"github.com/rodrigo-brito/ninjabot/model"
"github.com/rodrigo-brito/ninjabot/notification"
"github.com/rodrigo-brito/ninjabot/order"
"github.com/rodrigo-brito/ninjabot/service"
"github.com/rodrigo-brito/ninjabot/storage"
"github.com/rodrigo-brito/ninjabot/strategy"
"github.com/rodrigo-brito/ninjabot/tools/log"
"github.com/rodrigo-brito/ninjabot/tools/metrics"
"github.com/olekukonko/tablewriter"
"github.com/schollz/progressbar/v3"
)
const defaultDatabase = "ninjabot.db"
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04",
})
}
type OrderSubscriber interface {
OnOrder(model.Order)
}
type CandleSubscriber interface {
OnCandle(model.Candle)
}
type NinjaBot struct {
storage storage.Storage
settings model.Settings
exchange service.Exchange
strategy strategy.Strategy
notifier service.Notifier
telegram service.Telegram
orderController *order.Controller
priorityQueueCandle *model.PriorityQueue
strategiesControllers map[string]*strategy.Controller
orderFeed *order.Feed
dataFeed *exchange.DataFeedSubscription
paperWallet *exchange.PaperWallet
backtest bool
}
type Option func(*NinjaBot)
func NewBot(ctx context.Context, settings model.Settings, exch service.Exchange, str strategy.Strategy,
options ...Option) (*NinjaBot, error) {
bot := &NinjaBot{
settings: settings,
exchange: exch,
strategy: str,
orderFeed: order.NewOrderFeed(),
dataFeed: exchange.NewDataFeed(exch),
strategiesControllers: make(map[string]*strategy.Controller),
priorityQueueCandle: model.NewPriorityQueue(nil),
}
for _, pair := range settings.Pairs {
asset, quote := exchange.SplitAssetQuote(pair)
if asset == "" || quote == "" {
return nil, fmt.Errorf("invalid pair: %s", pair)
}
}
for _, option := range options {
option(bot)
}
var err error
if bot.storage == nil {
bot.storage, err = storage.FromFile(defaultDatabase)
if err != nil {
return nil, err
}
}
bot.orderController = order.NewController(ctx, exch, bot.storage, bot.orderFeed)
if settings.Telegram.Enabled {
bot.telegram, err = notification.NewTelegram(bot.orderController, settings)
if err != nil {
return nil, err
}
// register telegram as notifier
WithNotifier(bot.telegram)(bot)
}
return bot, nil
}
// WithBacktest sets the bot to run in backtest mode, it is required for backtesting environments
// Backtest mode optimize the input read for CSV and deal with race conditions
func WithBacktest(wallet *exchange.PaperWallet) Option {
return func(bot *NinjaBot) {
bot.backtest = true
opt := WithPaperWallet(wallet)
opt(bot)
}
}
// WithStorage sets the storage for the bot, by default it uses a local file called ninjabot.db
func WithStorage(storage storage.Storage) Option {
return func(bot *NinjaBot) {
bot.storage = storage
}
}
// WithLogLevel sets the log level. eg: log.DebugLevel, log.InfoLevel, log.WarnLevel, log.ErrorLevel, log.FatalLevel
func WithLogLevel(level log.Level) Option {
return func(bot *NinjaBot) {
log.SetLevel(level)
}
}
// WithNotifier registers a notifier to the bot, currently only email and telegram are supported
func WithNotifier(notifier service.Notifier) Option {
return func(bot *NinjaBot) {
bot.notifier = notifier
bot.orderController.SetNotifier(notifier)
bot.SubscribeOrder(notifier)
}
}
// WithCandleSubscription subscribes a given struct to the candle feed
func WithCandleSubscription(subscriber CandleSubscriber) Option {
return func(bot *NinjaBot) {
bot.SubscribeCandle(subscriber)
}
}
// WithPaperWallet sets the paper wallet for the bot (used for backtesting and live simulation)
func WithPaperWallet(wallet *exchange.PaperWallet) Option {
return func(bot *NinjaBot) {
bot.paperWallet = wallet
}
}
func (n *NinjaBot) SubscribeCandle(subscriptions ...CandleSubscriber) {
for _, pair := range n.settings.Pairs {
for _, subscription := range subscriptions {
n.dataFeed.Subscribe(pair, n.strategy.Timeframe(), subscription.OnCandle, false)
}
}
}
func WithOrderSubscription(subscriber OrderSubscriber) Option {
return func(bot *NinjaBot) {
bot.SubscribeOrder(subscriber)
}
}
func (n *NinjaBot) SubscribeOrder(subscriptions ...OrderSubscriber) {
for _, pair := range n.settings.Pairs {
for _, subscription := range subscriptions {
n.orderFeed.Subscribe(pair, subscription.OnOrder, false)
}
}
}
func (n *NinjaBot) Controller() *order.Controller {
return n.orderController
}
// Summary function displays all trades, accuracy and some bot metrics in stdout
// To access the raw data, you may access `bot.Controller().Results`
func (n *NinjaBot) Summary() {
var (
total float64
wins int
loses int
volume float64
sqn float64
)
buffer := bytes.NewBuffer(nil)
table := tablewriter.NewWriter(buffer)
table.SetHeader([]string{"Pair", "Trades", "Win", "Loss", "% Win", "Payoff", "Pr Fact.", "SQN", "Profit", "Volume"})
table.SetFooterAlignment(tablewriter.ALIGN_RIGHT)
avgPayoff := 0.0
avgProfitFactor := 0.0
returns := make([]float64, 0)
for _, summary := range n.orderController.Results {
avgPayoff += summary.Payoff() * float64(len(summary.Win())+len(summary.Lose()))
avgProfitFactor += summary.ProfitFactor() * float64(len(summary.Win())+len(summary.Lose()))
table.Append([]string{
summary.Pair,
strconv.Itoa(len(summary.Win()) + len(summary.Lose())),
strconv.Itoa(len(summary.Win())),
strconv.Itoa(len(summary.Lose())),
fmt.Sprintf("%.1f %%", float64(len(summary.Win()))/float64(len(summary.Win())+len(summary.Lose()))*100),
fmt.Sprintf("%.3f", summary.Payoff()),
fmt.Sprintf("%.3f", summary.ProfitFactor()),
fmt.Sprintf("%.1f", summary.SQN()),
fmt.Sprintf("%.2f", summary.Profit()),
fmt.Sprintf("%.2f", summary.Volume),
})
total += summary.Profit()
sqn += summary.SQN()
wins += len(summary.Win())
loses += len(summary.Lose())
volume += summary.Volume
returns = append(returns, summary.WinPercent()...)
returns = append(returns, summary.LosePercent()...)
}
table.SetFooter([]string{
"TOTAL",
strconv.Itoa(wins + loses),
strconv.Itoa(wins),
strconv.Itoa(loses),
fmt.Sprintf("%.1f %%", float64(wins)/float64(wins+loses)*100),
fmt.Sprintf("%.3f", avgPayoff/float64(wins+loses)),
fmt.Sprintf("%.3f", avgProfitFactor/float64(wins+loses)),
fmt.Sprintf("%.1f", sqn/float64(len(n.orderController.Results))),
fmt.Sprintf("%.2f", total),
fmt.Sprintf("%.2f", volume),
})
table.Render()
fmt.Println(buffer.String())
fmt.Println("------ RETURN -------")
totalReturn := 0.0
returnsPercent := make([]float64, len(returns))
for _, p := range returns {
returnsPercent = append(returnsPercent, p*100)
totalReturn += p
}
hist := histogram.Hist(15, returnsPercent)
histogram.Fprint(os.Stdout, hist, histogram.Linear(10))
fmt.Println()
fmt.Println("------ CONFIDENCE INTERVAL (95%) -------")
for pair, summary := range n.orderController.Results {
fmt.Printf("| %s |\n", pair)
returns := append(summary.WinPercent(), summary.LosePercent()...)
returnsInterval := metrics.Bootstrap(returns, metrics.Mean, 10000, 0.95)
payoffInterval := metrics.Bootstrap(returns, metrics.Payoff, 10000, 0.95)
profitFactorInterval := metrics.Bootstrap(returns, metrics.ProfitFactor, 10000, 0.95)
fmt.Printf("RETURN: %.2f%% (%.2f%% ~ %.2f%%)\n",
returnsInterval.Mean*100, returnsInterval.Lower*100, returnsInterval.Upper*100)
fmt.Printf("PAYOFF: %.2f (%.2f ~ %.2f)\n",
payoffInterval.Mean, payoffInterval.Lower, payoffInterval.Upper)
fmt.Printf("PROF.FACTOR: %.2f (%.2f ~ %.2f)\n",
profitFactorInterval.Mean, profitFactorInterval.Lower, profitFactorInterval.Upper)
}
fmt.Println()
if n.paperWallet != nil {
n.paperWallet.Summary()
}
}
func (n NinjaBot) SaveReturns(outputDir string) error {
for _, summary := range n.orderController.Results {
outputFile := fmt.Sprintf("%s/%s.csv", outputDir, summary.Pair)
if err := summary.SaveReturns(outputFile); err != nil {
return err
}
}
return nil
}
func (n *NinjaBot) onCandle(candle model.Candle) {
n.priorityQueueCandle.Push(candle)
}
func (n *NinjaBot) processCandle(candle model.Candle) {
if n.paperWallet != nil {
n.paperWallet.OnCandle(candle)
}
n.strategiesControllers[candle.Pair].OnPartialCandle(candle)
if candle.Complete {
n.strategiesControllers[candle.Pair].OnCandle(candle)
n.orderController.OnCandle(candle)
}
}
// Process pending candles in buffer
func (n *NinjaBot) processCandles() {
for item := range n.priorityQueueCandle.PopLock() {
n.processCandle(item.(model.Candle))
}
}
// Start the backtest process and create a progress bar
// backtestCandles will process candles from a prirority queue in chronological order
func (n *NinjaBot) backtestCandles() {
log.Info("[SETUP] Starting backtesting")
progressBar := progressbar.Default(int64(n.priorityQueueCandle.Len()))
for n.priorityQueueCandle.Len() > 0 {
item := n.priorityQueueCandle.Pop()
candle := item.(model.Candle)
if n.paperWallet != nil {
n.paperWallet.OnCandle(candle)
}
n.strategiesControllers[candle.Pair].OnPartialCandle(candle)
if candle.Complete {
n.strategiesControllers[candle.Pair].OnCandle(candle)
}
if err := progressBar.Add(1); err != nil {
log.Warnf("update progressbar fail: %v", err)
}
}
}
// Before Ninjabot start, we need to load the necessary data to fill strategy indicators
// Then, we need to get the time frame and warmup period to fetch the necessary candles
func (n *NinjaBot) preload(ctx context.Context, pair string) error {
if n.backtest {
return nil
}
candles, err := n.exchange.CandlesByLimit(ctx, pair, n.strategy.Timeframe(), n.strategy.WarmupPeriod())
if err != nil {
return err
}
for _, candle := range candles {
n.processCandle(candle)
}
n.dataFeed.Preload(pair, n.strategy.Timeframe(), candles)
return nil
}
// Run will initialize the strategy controller, order controller, preload data and start the bot
func (n *NinjaBot) Run(ctx context.Context) error {
for _, pair := range n.settings.Pairs {
// setup and subscribe strategy to data feed (candles)
n.strategiesControllers[pair] = strategy.NewStrategyController(pair, n.strategy, n.orderController)
// preload candles for warmup period
err := n.preload(ctx, pair)
if err != nil {
return err
}
// link to ninja bot controller
n.dataFeed.Subscribe(pair, n.strategy.Timeframe(), n.onCandle, false)
// start strategy controller
n.strategiesControllers[pair].Start()
}
// start order feed and controller
n.orderFeed.Start()
n.orderController.Start()
defer n.orderController.Stop()
if n.telegram != nil {
n.telegram.Start()
}
// start data feed and receives new candles
n.dataFeed.Start(n.backtest)
// start processing new candles for production or backtesting environment
if n.backtest {
n.backtestCandles()
} else {
n.processCandles()
}
return nil
}