Skip to content

Commit

Permalink
feat(chart): include OCO target and stop in plots / compress JS (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-brito authored Oct 19, 2021
1 parent 175a5ed commit 4055ee5
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 232 deletions.
5 changes: 4 additions & 1 deletion examples/backtesting/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ func main() {
exchange.WithDataFeed(csvFeed),
)

chart := plot.NewChart(plot.WithIndicators(
chart, err := plot.NewChart(plot.WithIndicators(
indicator.EMA(8, "red"),
indicator.EMA(21, "#000"),
indicator.RSI(14, "purple"),
indicator.Stoch(8, 3, "red", "blue"),
))
if err != nil {
log.Fatal(err)
}

bot, err := ninjabot.NewBot(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion examples/strategies/ocosell.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (e *OCOSell) OnCandle(df *model.Dataframe, broker service.Broker) {
}).Error(err)
}

_, err = broker.CreateOrderOCO(model.SideTypeSell, df.Pair, size, closePrice*1.05, closePrice*0.95, closePrice*0.95)
_, err = broker.CreateOrderOCO(model.SideTypeSell, df.Pair, size, closePrice*1.1, closePrice*0.95, closePrice*0.95)
if err != nil {
log.WithFields(map[string]interface{}{
"pair": df.Pair,
Expand Down
15 changes: 10 additions & 5 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"strings"
"sync"

"github.com/rodrigo-brito/ninjabot/service"

"github.com/rodrigo-brito/ninjabot/model"
"github.com/rodrigo-brito/ninjabot/service"

"github.com/StudioSol/set"
log "github.com/sirupsen/logrus"
)

Expand All @@ -27,7 +27,7 @@ type DataFeed struct {

type DataFeedSubscription struct {
exchange service.Exchange
Feeds []string
Feeds *set.LinkedHashSetString
DataFeeds map[string]*DataFeed
SubscriptionsByDataFeed map[string][]Subscription
SubscriptionsFinish []func()
Expand All @@ -43,6 +43,7 @@ type DataFeedConsumer func(model.Candle)
func NewDataFeed(exchange service.Exchange) *DataFeedSubscription {
return &DataFeedSubscription{
exchange: exchange,
Feeds: set.NewLinkedHashSetString(),
DataFeeds: make(map[string]*DataFeed),
SubscriptionsByDataFeed: make(map[string][]Subscription),
}
Expand All @@ -59,7 +60,7 @@ func (d *DataFeedSubscription) pairTimeframeFromKey(key string) (pair, timeframe

func (d *DataFeedSubscription) Subscribe(pair, timeframe string, consumer DataFeedConsumer, onCandleClose bool) {
key := d.feedKey(pair, timeframe)
d.Feeds = append(d.Feeds, key)
d.Feeds.Add(key)
d.SubscriptionsByDataFeed[key] = append(d.SubscriptionsByDataFeed[key], Subscription{
onCandleClose: onCandleClose,
consumer: consumer,
Expand All @@ -74,6 +75,10 @@ func (d *DataFeedSubscription) Preload(pair, timeframe string, candles []model.C
log.Infof("[SETUP] preloading %d candles for %s-%s", len(candles), pair, timeframe)
key := d.feedKey(pair, timeframe)
for _, candle := range candles {
if !candle.Complete {
continue
}

for _, subscription := range d.SubscriptionsByDataFeed[key] {
subscription.consumer(candle)
}
Expand All @@ -82,7 +87,7 @@ func (d *DataFeedSubscription) Preload(pair, timeframe string, candles []model.C

func (d *DataFeedSubscription) Connect() {
log.Infof("Connecting to the exchange.")
for _, feed := range d.Feeds {
for feed := range d.Feeds.Iter() {
pair, timeframe := d.pairTimeframeFromKey(feed)
ccandle, cerr := d.exchange.CandlesSubscription(context.Background(), pair, timeframe)
d.DataFeeds[feed] = &DataFeed{
Expand Down
6 changes: 5 additions & 1 deletion exchange/paperwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (p *PaperWallet) OnCandle(candle model.Candle) {
}

for i, order := range p.orders {
if order.Status != model.OrderStatusTypeNew {
if order.Pair != candle.Pair || order.Status != model.OrderStatusTypeNew {
continue
}

Expand All @@ -168,6 +168,7 @@ func (p *PaperWallet) OnCandle(candle model.Candle) {
walletValue := p.avgPrice[candle.Pair] * actualQty

p.volume[candle.Pair] += orderVolume
p.orders[i].UpdatedAt = candle.Time
p.orders[i].Status = model.OrderStatusTypeFilled
p.avgPrice[candle.Pair] = (walletValue + orderVolume) / (actualQty + order.Quantity)
p.assets[asset].Free = p.assets[asset].Free + order.Quantity
Expand Down Expand Up @@ -196,6 +197,7 @@ func (p *PaperWallet) OnCandle(candle model.Candle) {
if groupOrder.GroupID != nil && *groupOrder.GroupID == *order.GroupID &&
groupOrder.ExchangeID != order.ExchangeID {
p.orders[j].Status = model.OrderStatusTypeCanceled
p.orders[j].UpdatedAt = candle.Time
break
}
}
Expand Down Expand Up @@ -270,6 +272,7 @@ func (p *PaperWallet) CreateOrderOCO(side model.SideType, pair string,
Price: price,
Quantity: size,
GroupID: &groupID,
RefPrice: p.lastCandle[pair].Close,
}

stopOrder := model.Order{
Expand All @@ -284,6 +287,7 @@ func (p *PaperWallet) CreateOrderOCO(side model.SideType, pair string,
Stop: &stop,
Quantity: size,
GroupID: &groupID,
RefPrice: p.lastCandle[pair].Close,
}
p.orders = append(p.orders, limitMaker, stopOrder)

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module github.com/rodrigo-brito/ninjabot
go 1.16

require (
github.com/StudioSol/set v0.0.0-20211001132805-52fe71d0afcf
github.com/adshao/go-binance/v2 v2.3.2
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/evanw/esbuild v0.13.7
github.com/gorilla/websocket v1.4.2 // indirect
github.com/jpillora/backoff v1.0.0
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/markcheno/go-talib v0.0.0-20190307022042-cd53a9264d70
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -19,8 +21,6 @@ require (
github.com/tidwall/gjson v1.9.2 // indirect
github.com/urfave/cli/v2 v2.3.0
github.com/xhit/go-str2duration/v2 v2.0.0
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/tucnak/telebot.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect
)
21 changes: 16 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/StudioSol/set v0.0.0-20211001132805-52fe71d0afcf h1:J0UFkeov8c7HXBx/9LT0k5NZgUMXQWnl9o3B15Qja5w=
github.com/StudioSol/set v0.0.0-20211001132805-52fe71d0afcf/go.mod h1:gHtdo3acqCLmt09I95lUZsaYbD5EkgaXXb/+IRbU3jg=
github.com/adshao/go-binance/v2 v2.3.2 h1:qIXuCUDma9yrnQ1qP0PQZrHwA9d/4b8UaFxw+5TQKFU=
github.com/adshao/go-binance/v2 v2.3.2/go.mod h1:TfcBwfGtmRibSljDDR0XCaPkfBt1kc2N9lnNMYC3dCQ=
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
Expand All @@ -8,9 +10,12 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dR
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evanw/esbuild v0.13.7 h1:ijdfXsbVKc70+JclIgYLSuyEgGj8nIpjGSO1KbULosk=
github.com/evanw/esbuild v0.13.7/go.mod h1:GG+zjdi59yh3ehDn4ZWfPcATxjPDUH53iU4ZJbp7dkY=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
Expand Down Expand Up @@ -45,12 +50,14 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.13.1 h1:wXr2uRxZTJXHLly6qhJabee5JqIhTRoLBhDOA74hDEQ=
github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/markcheno/go-talib v0.0.0-20190307022042-cd53a9264d70 h1:+iG37/Aw61Oc+ZJ4DSxQF2+K0e4ZiMidI7ytWuW4/cI=
Expand All @@ -70,6 +77,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
Expand All @@ -83,6 +92,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/btree v0.6.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4=
Expand Down Expand Up @@ -117,8 +127,8 @@ github.com/xhit/go-str2duration/v2 v2.0.0 h1:uFtk6FWB375bP7ewQl+/1wBcn840GPhnySO
github.com/xhit/go-str2duration/v2 v2.0.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365 h1:6wSTsvPddg9gc/mVEEyk9oOAoxn+bT4Z9q1zx+4RwA4=
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -127,6 +137,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/tucnak/telebot.v2 v2.4.0 h1:nOeqOWnOAD3dzbKW+NRumd8zjj5vrWwSa0WRTxvgfag=
gopkg.in/tucnak/telebot.v2 v2.4.0/go.mod h1:BgaIIx50PSRS9pG59JH+geT82cfvoJU/IaI5TJdN3v8=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
5 changes: 3 additions & 2 deletions model/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ type Order struct {
GroupID *int64 `db:"group_id" json:"group_id"`

// Internal use (Plot)
Profit float64 `json:"-"`
Candle Candle `json:"-"`
RefPrice float64 `json:"-"`
Profit float64 `json:"-"`
Candle Candle `json:"-"`
}

func (o Order) String() string {
Expand Down
8 changes: 8 additions & 0 deletions model/priorityqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ func NewPriorityQueue(data []Item) *PriorityQueue {
func (q *PriorityQueue) Push(item Item) {
q.Lock()
defer q.Unlock()

q.data = append(q.data, item)
q.length++
q.up(q.length - 1)
}
func (q *PriorityQueue) Pop() Item {
q.Lock()
defer q.Unlock()

if q.length == 0 {
return nil
}
Expand All @@ -47,15 +49,21 @@ func (q *PriorityQueue) Pop() Item {
q.data = q.data[:len(q.data)-1]
return top
}

func (q *PriorityQueue) Peek() Item {
q.Lock()
defer q.Unlock()

if q.length == 0 {
return nil
}
return q.data[0]
}

func (q *PriorityQueue) Len() int {
q.Lock()
defer q.Unlock()

return q.length
}
func (q *PriorityQueue) down(pos int) {
Expand Down
14 changes: 8 additions & 6 deletions ninjabot.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func (n *NinjaBot) processCandles() {

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()
Expand All @@ -252,13 +253,14 @@ func (n *NinjaBot) Run(ctx context.Context) error {
// TODO: include onCandleClose=false to improve precision in OCO orders (backtesting)
n.dataFeed.Subscribe(pair, n.strategy.Timeframe(), n.onCandle, true)

// preload candles to warmup strategy
candles, err := n.exchange.CandlesByLimit(ctx, pair, n.strategy.Timeframe(), n.strategy.WarmupPeriod()+1)
if err != nil {
return err
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.dataFeed.Preload(pair, n.strategy.Timeframe(), candles)
}

n.orderFeed.Start()
Expand Down
Loading

0 comments on commit 4055ee5

Please sign in to comment.