bbgo: move rightWindow to the IntervalWindow struct

This commit is contained in:
c9s 2022-08-24 17:43:28 +08:00
parent f43f9af20f
commit 469c6bfb28
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 20 additions and 19 deletions

View File

@ -23,8 +23,8 @@ type StandardIndicatorSet struct {
// Standard indicators
// interval -> window
boll map[types.IntervalWindowBandWidth]*indicator.BOLL
simples map[types.IntervalWindow]indicator.KLinePusher
iwbIndicators map[types.IntervalWindowBandWidth]*indicator.BOLL
iwIndicators map[types.IntervalWindow]indicator.KLinePusher
stream types.Stream
store *MarketDataStore
@ -32,33 +32,33 @@ type StandardIndicatorSet struct {
func NewStandardIndicatorSet(symbol string, stream types.Stream, store *MarketDataStore) *StandardIndicatorSet {
return &StandardIndicatorSet{
Symbol: symbol,
store: store,
stream: stream,
simples: make(map[types.IntervalWindow]indicator.KLinePusher),
boll: make(map[types.IntervalWindowBandWidth]*indicator.BOLL),
Symbol: symbol,
store: store,
stream: stream,
iwIndicators: make(map[types.IntervalWindow]indicator.KLinePusher),
iwbIndicators: make(map[types.IntervalWindowBandWidth]*indicator.BOLL),
}
}
func (s *StandardIndicatorSet) initAndBind(inc indicator.KLinePusher, iw types.IntervalWindow) {
if klines, ok := s.store.KLinesOfInterval(iw.Interval); ok {
func (s *StandardIndicatorSet) initAndBind(inc indicator.KLinePusher, interval types.Interval) {
if klines, ok := s.store.KLinesOfInterval(interval); ok {
for _, k := range *klines {
inc.PushK(k)
}
}
s.stream.OnKLineClosed(types.KLineWith(s.Symbol, iw.Interval, inc.PushK))
s.stream.OnKLineClosed(types.KLineWith(s.Symbol, interval, inc.PushK))
}
func (s *StandardIndicatorSet) allocateSimpleIndicator(t indicator.KLinePusher, iw types.IntervalWindow) indicator.KLinePusher {
inc, ok := s.simples[iw]
inc, ok := s.iwIndicators[iw]
if ok {
return inc
}
inc = t
s.initAndBind(inc, iw)
s.simples[iw] = inc
s.initAndBind(inc, iw.Interval)
s.iwIndicators[iw] = inc
return t
}
@ -112,17 +112,17 @@ func (s *StandardIndicatorSet) STOCH(iw types.IntervalWindow) *indicator.STOCH {
// BOLL returns the bollinger band indicator of the given interval, the window and bandwidth
func (s *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64) *indicator.BOLL {
iwb := types.IntervalWindowBandWidth{IntervalWindow: iw, BandWidth: bandWidth}
inc, ok := s.boll[iwb]
inc, ok := s.iwbIndicators[iwb]
if !ok {
inc = &indicator.BOLL{IntervalWindow: iw, K: bandWidth}
s.initAndBind(inc, iw)
s.initAndBind(inc, iw.Interval)
if debugBOLL {
inc.OnUpdate(func(sma float64, upBand float64, downBand float64) {
logrus.Infof("%s BOLL %s: sma=%f up=%f down=%f", s.Symbol, iw.String(), sma, upBand, downBand)
})
}
s.boll[iwb] = inc
s.iwbIndicators[iwb] = inc
}
return inc

View File

@ -12,8 +12,6 @@ type PivotLow struct {
types.IntervalWindow
RightWindow int `json:"rightWindow"`
Lows types.Float64Slice
Values types.Float64Slice
EndTime time.Time

View File

@ -77,8 +77,11 @@ type IntervalWindow struct {
// The interval of kline
Interval Interval `json:"interval"`
// The windows size of the indicator (EWMA and SMA)
// The windows size of the indicator (for example, EWMA and SMA)
Window int `json:"window"`
// RightWindow is used by the pivot indicator
RightWindow int `json:"rightWindow"`
}
type IntervalWindowBandWidth struct {