From 3d2a27fc10649b5ccce5bc814ab979286594f55d Mon Sep 17 00:00:00 2001 From: zenix Date: Wed, 26 May 2021 00:20:31 +0000 Subject: [PATCH 1/2] Fix: nil pointer exception in indicator creation, add stoch util func --- pkg/bbgo/session.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index bf6808d45..db23247cd 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -16,15 +16,16 @@ import ( "github.com/c9s/bbgo/pkg/util" ) -var fiatCurrencies = []string{"USDC", "USDT", "USD", "TWD", "EUR", "GBP"} +var fiatCurrencies = []string{"USDC", "USDT", "USD", "TWD", "EUR", "GBP", "BUSD"} type StandardIndicatorSet struct { Symbol string // Standard indicators // interval -> window - sma map[types.IntervalWindow]*indicator.SMA - ewma map[types.IntervalWindow]*indicator.EWMA - boll map[types.IntervalWindow]*indicator.BOLL + sma map[types.IntervalWindow]*indicator.SMA + ewma map[types.IntervalWindow]*indicator.EWMA + boll map[types.IntervalWindow]*indicator.BOLL + stoch map[types.IntervalWindow]*indicator.STOCH store *MarketDataStore } @@ -35,6 +36,7 @@ func NewStandardIndicatorSet(symbol string, store *MarketDataStore) *StandardInd sma: make(map[types.IntervalWindow]*indicator.SMA), ewma: make(map[types.IntervalWindow]*indicator.EWMA), boll: make(map[types.IntervalWindow]*indicator.BOLL), + stoch: make(map[types.IntervalWindow]*indicator.STOCH), store: store, } @@ -65,7 +67,7 @@ func NewStandardIndicatorSet(symbol string, store *MarketDataStore) *StandardInd func (set *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64) *indicator.BOLL { inc, ok := set.boll[iw] if !ok { - inc := &indicator.BOLL{IntervalWindow: iw, K: bandWidth} + inc = &indicator.BOLL{IntervalWindow: iw, K: bandWidth} inc.Bind(set.store) set.boll[iw] = inc } @@ -77,7 +79,7 @@ func (set *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64 func (set *StandardIndicatorSet) SMA(iw types.IntervalWindow) *indicator.SMA { inc, ok := set.sma[iw] if !ok { - inc := &indicator.SMA{IntervalWindow: iw} + inc = &indicator.SMA{IntervalWindow: iw} inc.Bind(set.store) set.sma[iw] = inc } @@ -89,7 +91,7 @@ func (set *StandardIndicatorSet) SMA(iw types.IntervalWindow) *indicator.SMA { func (set *StandardIndicatorSet) EWMA(iw types.IntervalWindow) *indicator.EWMA { inc, ok := set.ewma[iw] if !ok { - inc := &indicator.EWMA{IntervalWindow: iw} + inc = &indicator.EWMA{IntervalWindow: iw} inc.Bind(set.store) set.ewma[iw] = inc } @@ -97,6 +99,17 @@ func (set *StandardIndicatorSet) EWMA(iw types.IntervalWindow) *indicator.EWMA { return inc } +func (set *StandardIndicatorSet) STOCH(iw types.IntervalWindow) *indicator.STOCH { + inc, ok := set.stoch[iw] + if !ok { + inc = &indicator.STOCH{IntervalWindow: iw} + inc.Bind(set.store) + set.stoch[iw] = inc + } + + return inc +} + // ExchangeSession presents the exchange connection Session // It also maintains and collects the data returned from the stream. type ExchangeSession struct { @@ -385,7 +398,7 @@ func (session *ExchangeSession) initSymbol(ctx context.Context, environ *Environ var lastPriceTime time.Time for interval := range usedKLineIntervals { // avoid querying the last unclosed kline - endTime := environ.startTime.Add(- interval.Duration()) + endTime := environ.startTime.Add(-interval.Duration()) kLines, err := session.Exchange.QueryKLines(ctx, symbol, interval, types.KLineQueryOptions{ EndTime: &endTime, Limit: 1000, // indicators need at least 100 @@ -536,7 +549,7 @@ func (session *ExchangeSession) FormatOrder(order types.SubmitOrder) (types.Subm } func (session *ExchangeSession) UpdatePrices(ctx context.Context) (err error) { - if session.lastPriceUpdatedAt.After(time.Now().Add(- time.Hour)) { + if session.lastPriceUpdatedAt.After(time.Now().Add(-time.Hour)) { return nil } From 698ec9911f08287ae3a3da875d2ed1bea4463c27 Mon Sep 17 00:00:00 2001 From: zenix Date: Wed, 26 May 2021 00:57:35 +0000 Subject: [PATCH 2/2] Fix error formating on depth load fail --- pkg/exchange/binance/depthframe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/exchange/binance/depthframe.go b/pkg/exchange/binance/depthframe.go index acc5b3a56..bb89ccd3b 100644 --- a/pkg/exchange/binance/depthframe.go +++ b/pkg/exchange/binance/depthframe.go @@ -164,7 +164,7 @@ func (f *DepthFrame) PushEvent(e DepthEvent) { go f.once.Do(func() { if err := f.loadDepthSnapshot(); err != nil { - log.WithError(err).Error("%s depth snapshot load failed, resetting..", f.Symbol) + log.WithError(err).Errorf("%s depth snapshot load failed, resetting..", f.Symbol) f.emitReset() } })