forked from rodrigo-brito/ninjabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathninjabot.go
282 lines (238 loc) · 6.67 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
package ninjabot
import (
"bytes"
"context"
"fmt"
"strconv"
"sync"
"sync/atomic"
"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/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
)
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
pendingCandles int64
startBacktest sync.WaitGroup
}
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 _, option := range options {
option(bot)
}
var err error
if bot.storage == nil {
bot.storage, err = storage.FromFile(defaultDatabase)
if err != nil {
return nil, err
}
}
if bot.orderController == nil {
bot.orderController = order.NewController(ctx, exch, bot.storage, bot.orderFeed, bot.notifier)
}
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
}
func WithBacktest(wallet *exchange.PaperWallet) Option {
return func(bot *NinjaBot) {
bot.backtest = true
bot.startBacktest.Add(1)
// load paper wallet
opt := WithPaperWallet(wallet)
opt(bot)
}
}
func WithStorage(storage storage.Storage) Option {
return func(bot *NinjaBot) {
bot.storage = storage
}
}
func WithLogLevel(level log.Level) Option {
return func(bot *NinjaBot) {
log.SetLevel(level)
}
}
func WithNotifier(notifier service.Notifier) Option {
return func(bot *NinjaBot) {
bot.notifier = notifier
bot.SubscribeOrder(notifier)
}
}
func WithCandleSubscription(subscriber CandleSubscriber) Option {
return func(bot *NinjaBot) {
bot.SubscribeCandle(subscriber)
}
}
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
}
func (n *NinjaBot) Summary() string {
var (
total float64
wins int
loses int
volume float64
)
buffer := bytes.NewBuffer(nil)
table := tablewriter.NewWriter(buffer)
table.SetHeader([]string{"Pair", "Trades", "Win", "Loss", "% Win", "Payoff", "Profit", "Volume"})
table.SetFooterAlignment(tablewriter.ALIGN_RIGHT)
avgPayoff := 0.0
for _, summary := range n.orderController.Results {
avgPayoff += summary.Payoff() * 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("%.2f", summary.Profit()),
fmt.Sprintf("%.2f", summary.Volume),
})
total += summary.Profit()
wins += len(summary.Win)
loses += len(summary.Lose)
volume += summary.Volume
}
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("%.2f", total),
fmt.Sprintf("%.2f", volume),
})
table.Render()
return buffer.String()
}
func (n *NinjaBot) onCandle(candle model.Candle) {
n.priorityQueueCandle.Push(candle)
atomic.AddInt64(&n.pendingCandles, 1)
}
func (n *NinjaBot) processCandles() {
// when backtesting, we need to wait all candles load
// to avoid sync issues between multiple coins
if n.backtest {
n.startBacktest.Wait()
}
for atomic.AddInt64(&n.pendingCandles, -1) >= 0 {
item := n.priorityQueueCandle.Pop()
candle := item.(model.Candle)
if n.paperWallet != nil {
n.paperWallet.OnCandle(candle)
}
if candle.Complete {
n.strategiesControllers[candle.Pair].OnCandle(candle)
}
}
}
func (n *NinjaBot) Run(ctx context.Context) error {
for _, pair := range n.settings.Pairs {
pair := pair
// setup and subscribe strategy to data feed (candles)
strategyController := strategy.NewStrategyController(pair, n.strategy, n.orderController)
strategyController.Start()
n.strategiesControllers[pair] = strategyController
// link to ninja bot controller
n.dataFeed.Subscribe(pair, n.strategy.Timeframe(), n.onCandle, false)
if !n.backtest {
// preload candles to warmup strategy
candles, err := n.exchange.CandlesByLimit(ctx, pair, n.strategy.Timeframe(), n.strategy.WarmupPeriod()+1)
if err != nil {
return err
}
n.dataFeed.Preload(pair, n.strategy.Timeframe(), candles)
}
}
n.orderFeed.Start()
n.orderController.Start()
defer n.orderController.Stop()
if n.telegram != nil {
n.telegram.Start()
}
n.dataFeed.OnFinish(func() {
if n.backtest {
n.startBacktest.Done()
}
})
go n.dataFeed.Start()
n.processCandles()
return nil
}