Skip to content

Commit 51972e9

Browse files
committed
bbgo: fix indicator load key duplicate issue
1 parent 70fb6d1 commit 51972e9

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

pkg/bbgo/standard_indicator_set.go

+25-17
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,23 @@ type StandardIndicatorSet struct {
2424
// Standard indicators
2525
// interval -> window
2626
iwbIndicators map[types.IntervalWindowBandWidth]*indicator.BOLL
27-
iwIndicators map[types.IntervalWindow]indicator.KLinePusher
27+
iwIndicators map[indicatorKey]indicator.KLinePusher
2828

2929
stream types.Stream
3030
store *MarketDataStore
3131
}
3232

33+
type indicatorKey struct {
34+
iw types.IntervalWindow
35+
id string
36+
}
37+
3338
func NewStandardIndicatorSet(symbol string, stream types.Stream, store *MarketDataStore) *StandardIndicatorSet {
3439
return &StandardIndicatorSet{
3540
Symbol: symbol,
3641
store: store,
3742
stream: stream,
38-
iwIndicators: make(map[types.IntervalWindow]indicator.KLinePusher),
43+
iwIndicators: make(map[indicatorKey]indicator.KLinePusher),
3944
iwbIndicators: make(map[types.IntervalWindowBandWidth]*indicator.BOLL),
4045
}
4146
}
@@ -50,74 +55,77 @@ func (s *StandardIndicatorSet) initAndBind(inc indicator.KLinePusher, interval t
5055
s.stream.OnKLineClosed(types.KLineWith(s.Symbol, interval, inc.PushK))
5156
}
5257

53-
func (s *StandardIndicatorSet) allocateSimpleIndicator(t indicator.KLinePusher, iw types.IntervalWindow) indicator.KLinePusher {
54-
inc, ok := s.iwIndicators[iw]
58+
func (s *StandardIndicatorSet) allocateSimpleIndicator(t indicator.KLinePusher, iw types.IntervalWindow, id string) indicator.KLinePusher {
59+
k := indicatorKey{
60+
iw: iw,
61+
id: id,
62+
}
63+
inc, ok := s.iwIndicators[k]
5564
if ok {
5665
return inc
5766
}
5867

5968
inc = t
6069
s.initAndBind(inc, iw.Interval)
61-
s.iwIndicators[iw] = inc
70+
s.iwIndicators[k] = inc
6271
return t
6372
}
6473

6574
// SMA is a helper function that returns the simple moving average indicator of the given interval and the window size.
6675
func (s *StandardIndicatorSet) SMA(iw types.IntervalWindow) *indicator.SMA {
67-
inc := s.allocateSimpleIndicator(&indicator.SMA{IntervalWindow: iw}, iw)
76+
inc := s.allocateSimpleIndicator(&indicator.SMA{IntervalWindow: iw}, iw, "sma")
6877
return inc.(*indicator.SMA)
6978
}
7079

7180
// EWMA is a helper function that returns the exponential weighed moving average indicator of the given interval and the window size.
7281
func (s *StandardIndicatorSet) EWMA(iw types.IntervalWindow) *indicator.EWMA {
73-
inc := s.allocateSimpleIndicator(&indicator.EWMA{IntervalWindow: iw}, iw)
82+
inc := s.allocateSimpleIndicator(&indicator.EWMA{IntervalWindow: iw}, iw, "ewma")
7483
return inc.(*indicator.EWMA)
7584
}
7685

7786
// VWMA
7887
func (s *StandardIndicatorSet) VWMA(iw types.IntervalWindow) *indicator.VWMA {
79-
inc := s.allocateSimpleIndicator(&indicator.VWMA{IntervalWindow: iw}, iw)
88+
inc := s.allocateSimpleIndicator(&indicator.VWMA{IntervalWindow: iw}, iw, "vwma")
8089
return inc.(*indicator.VWMA)
8190
}
8291

83-
8492
func (s *StandardIndicatorSet) PivotHigh(iw types.IntervalWindow) *indicator.PivotHigh {
85-
inc := s.allocateSimpleIndicator(&indicator.PivotHigh{IntervalWindow: iw}, iw)
93+
inc := s.allocateSimpleIndicator(&indicator.PivotHigh{IntervalWindow: iw}, iw, "pivothigh")
8694
return inc.(*indicator.PivotHigh)
8795
}
8896

8997
func (s *StandardIndicatorSet) PivotLow(iw types.IntervalWindow) *indicator.PivotLow {
90-
inc := s.allocateSimpleIndicator(&indicator.PivotLow{IntervalWindow: iw}, iw)
98+
inc := s.allocateSimpleIndicator(&indicator.PivotLow{IntervalWindow: iw}, iw, "pivotlow")
9199
return inc.(*indicator.PivotLow)
92100
}
93101

94102
func (s *StandardIndicatorSet) ATR(iw types.IntervalWindow) *indicator.ATR {
95-
inc := s.allocateSimpleIndicator(&indicator.ATR{IntervalWindow: iw}, iw)
103+
inc := s.allocateSimpleIndicator(&indicator.ATR{IntervalWindow: iw}, iw, "atr")
96104
return inc.(*indicator.ATR)
97105
}
98106

99107
func (s *StandardIndicatorSet) ATRP(iw types.IntervalWindow) *indicator.ATRP {
100-
inc := s.allocateSimpleIndicator(&indicator.ATRP{IntervalWindow: iw}, iw)
108+
inc := s.allocateSimpleIndicator(&indicator.ATRP{IntervalWindow: iw}, iw, "atrp")
101109
return inc.(*indicator.ATRP)
102110
}
103111

104112
func (s *StandardIndicatorSet) EMV(iw types.IntervalWindow) *indicator.EMV {
105-
inc := s.allocateSimpleIndicator(&indicator.EMV{IntervalWindow: iw}, iw)
113+
inc := s.allocateSimpleIndicator(&indicator.EMV{IntervalWindow: iw}, iw, "emv")
106114
return inc.(*indicator.EMV)
107115
}
108116

109117
func (s *StandardIndicatorSet) CCI(iw types.IntervalWindow) *indicator.CCI {
110-
inc := s.allocateSimpleIndicator(&indicator.CCI{IntervalWindow: iw}, iw)
118+
inc := s.allocateSimpleIndicator(&indicator.CCI{IntervalWindow: iw}, iw, "cci")
111119
return inc.(*indicator.CCI)
112120
}
113121

114122
func (s *StandardIndicatorSet) HULL(iw types.IntervalWindow) *indicator.HULL {
115-
inc := s.allocateSimpleIndicator(&indicator.HULL{IntervalWindow: iw}, iw)
123+
inc := s.allocateSimpleIndicator(&indicator.HULL{IntervalWindow: iw}, iw, "hull")
116124
return inc.(*indicator.HULL)
117125
}
118126

119127
func (s *StandardIndicatorSet) STOCH(iw types.IntervalWindow) *indicator.STOCH {
120-
inc := s.allocateSimpleIndicator(&indicator.STOCH{IntervalWindow: iw}, iw)
128+
inc := s.allocateSimpleIndicator(&indicator.STOCH{IntervalWindow: iw}, iw, "stoch")
121129
return inc.(*indicator.STOCH)
122130
}
123131

0 commit comments

Comments
 (0)