strategy/bollmaker: preload dynamic spreads

This commit is contained in:
Andy Cheng 2022-08-25 13:44:38 +08:00
parent 702ce5220b
commit fcaa6466b6
2 changed files with 18 additions and 4 deletions

View File

@ -11,8 +11,7 @@ import (
type DynamicSpreadSettings struct { type DynamicSpreadSettings struct {
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
// Window is the window of the SMAs of spreads types.IntervalWindow
Window int `json:"window"`
// AskSpreadScale is used to define the ask spread range with the given percentage. // AskSpreadScale is used to define the ask spread range with the given percentage.
AskSpreadScale *bbgo.PercentageScale `json:"askSpreadScale"` AskSpreadScale *bbgo.PercentageScale `json:"askSpreadScale"`
@ -45,6 +44,19 @@ func (ds *DynamicSpreadSettings) Update(kline types.KLine) {
} }
} }
// Initialize dynamic spreads and preload SMAs
func (ds *DynamicSpreadSettings) Initialize(symbol string, session *bbgo.ExchangeSession) {
ds.DynamicBidSpread = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: ds.Interval, Window: ds.Window}}
ds.DynamicAskSpread = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: ds.Interval, Window: ds.Window}}
kLineStore, _ := session.MarketDataStore(symbol)
if klines, ok := kLineStore.KLinesOfInterval(ds.Interval); ok {
for i := 0; i < len(*klines); i++ {
ds.Update((*klines)[i])
}
}
}
// GetAskSpread returns current ask spread // GetAskSpread returns current ask spread
func (ds *DynamicSpreadSettings) GetAskSpread() (askSpread float64, err error) { func (ds *DynamicSpreadSettings) GetAskSpread() (askSpread float64, err error) {
if !ds.Enabled { if !ds.Enabled {

View File

@ -434,8 +434,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// Setup dynamic spread // Setup dynamic spread
if s.DynamicSpread.Enabled { if s.DynamicSpread.Enabled {
s.DynamicSpread.DynamicBidSpread = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: s.DynamicSpread.Window}} if s.DynamicSpread.Interval == "" {
s.DynamicSpread.DynamicAskSpread = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: s.DynamicSpread.Window}} s.DynamicSpread.Interval = s.Interval
}
s.DynamicSpread.Initialize(s.Symbol, s.session)
} }
if s.DisableShort { if s.DisableShort {