indicator: clean up bollinger band indicator api usage

This commit is contained in:
c9s 2022-07-14 13:05:44 +08:00
parent a5715c6aee
commit 2ef8ecf3d9
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
6 changed files with 13 additions and 12 deletions

View File

@ -53,7 +53,7 @@ func (inc *BOLL) GetSMA() types.SeriesExtend {
}
func (inc *BOLL) GetStdDev() types.SeriesExtend {
return types.NewSeries(inc.StdDev)
return inc.StdDev
}
func (inc *BOLL) LastUpBand() float64 {

View File

@ -4,9 +4,10 @@ import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/stretchr/testify/assert"
)
/*
@ -60,8 +61,8 @@ func TestBOLL(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
boll := BOLL{IntervalWindow: types.IntervalWindow{Window: tt.window}, K: tt.k}
boll.CalculateAndUpdate(tt.kLines)
assert.InDelta(t, tt.up, boll.LastUpBand(), Delta)
assert.InDelta(t, tt.down, boll.LastDownBand(), Delta)
assert.InDelta(t, tt.up, boll.UpBand.Last(), Delta)
assert.InDelta(t, tt.down, boll.DownBand.Last(), Delta)
})
}

View File

@ -278,8 +278,8 @@ func (s *Strategy) placeOrders(ctx context.Context, midPrice fixedpoint.Value, k
baseBalance, hasBaseBalance := balances[s.Market.BaseCurrency]
quoteBalance, hasQuoteBalance := balances[s.Market.QuoteCurrency]
downBand := s.defaultBoll.LastDownBand()
upBand := s.defaultBoll.LastUpBand()
downBand := s.defaultBoll.DownBand.Last()
upBand := s.defaultBoll.UpBand.Last()
sma := s.defaultBoll.SMA.Last()
log.Infof("%s bollinger band: up %f sma %f down %f", s.Symbol, upBand, sma, downBand)
@ -349,7 +349,7 @@ func (s *Strategy) placeOrders(ctx context.Context, midPrice fixedpoint.Value, k
// WHEN: price breaks the upper band (price > window 2) == strongUpTrend
// THEN: we apply strongUpTrend skew
if s.TradeInBand {
if !inBetween(midPrice.Float64(), s.neutralBoll.LastDownBand(), s.neutralBoll.LastUpBand()) {
if !inBetween(midPrice.Float64(), s.neutralBoll.DownBand.Last(), s.neutralBoll.UpBand.Last()) {
log.Infof("tradeInBand is set, skip placing orders when the price is outside of the band")
return
}

View File

@ -12,7 +12,7 @@ const (
)
func detectPriceTrend(inc *indicator.BOLL, price float64) PriceTrend {
if inBetween(price, inc.LastDownBand(), inc.LastUpBand()) {
if inBetween(price, inc.DownBand.Last(), inc.UpBand.Last()) {
return NeutralTrend
}

View File

@ -336,8 +336,8 @@ func (s *Strategy) placeOrders(ctx context.Context, midPrice fixedpoint.Value, k
// baseBalance, hasBaseBalance := balances[s.Market.BaseCurrency]
// quoteBalance, hasQuoteBalance := balances[s.Market.QuoteCurrency]
downBand := s.defaultBoll.LastDownBand()
upBand := s.defaultBoll.LastUpBand()
downBand := s.defaultBoll.DownBand.Last()
upBand := s.defaultBoll.UpBand.Last()
sma := s.defaultBoll.SMA.Last()
log.Infof("bollinger band: up %f sma %f down %f", upBand, sma, downBand)

View File

@ -305,8 +305,8 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
var pips = s.Pips
if s.EnableBollBandMargin {
lastDownBand := fixedpoint.NewFromFloat(s.boll.LastDownBand())
lastUpBand := fixedpoint.NewFromFloat(s.boll.LastUpBand())
lastDownBand := fixedpoint.NewFromFloat(s.boll.DownBand.Last())
lastUpBand := fixedpoint.NewFromFloat(s.boll.UpBand.Last())
if lastUpBand.IsZero() || lastDownBand.IsZero() {
log.Warnf("bollinger band value is zero, skipping")