indicator: Support difference bandwidth on boll indicator and can dynamic create BOLL

This commit is contained in:
Lee 2022-01-15 05:22:29 +08:00
parent 8a3f1c4dba
commit 266400d925
2 changed files with 29 additions and 9 deletions

View File

@ -36,7 +36,7 @@ type StandardIndicatorSet struct {
// interval -> window // interval -> window
sma map[types.IntervalWindow]*indicator.SMA sma map[types.IntervalWindow]*indicator.SMA
ewma map[types.IntervalWindow]*indicator.EWMA ewma map[types.IntervalWindow]*indicator.EWMA
boll map[types.IntervalWindow]*indicator.BOLL boll map[types.IntervalWindowBandWidth]*indicator.BOLL
stoch map[types.IntervalWindow]*indicator.STOCH stoch map[types.IntervalWindow]*indicator.STOCH
volatility map[types.IntervalWindow]*indicator.VOLATILITY volatility map[types.IntervalWindow]*indicator.VOLATILITY
@ -48,7 +48,7 @@ func NewStandardIndicatorSet(symbol string, store *MarketDataStore) *StandardInd
Symbol: symbol, Symbol: symbol,
sma: make(map[types.IntervalWindow]*indicator.SMA), sma: make(map[types.IntervalWindow]*indicator.SMA),
ewma: make(map[types.IntervalWindow]*indicator.EWMA), ewma: make(map[types.IntervalWindow]*indicator.EWMA),
boll: make(map[types.IntervalWindow]*indicator.BOLL), boll: make(map[types.IntervalWindowBandWidth]*indicator.BOLL),
stoch: make(map[types.IntervalWindow]*indicator.STOCH), stoch: make(map[types.IntervalWindow]*indicator.STOCH),
volatility: make(map[types.IntervalWindow]*indicator.VOLATILITY), volatility: make(map[types.IntervalWindow]*indicator.VOLATILITY),
store: store, store: store,
@ -81,22 +81,37 @@ func NewStandardIndicatorSet(symbol string, store *MarketDataStore) *StandardInd
// setup boll indicator, we may refactor boll indicator by subscribing SMA indicator, // setup boll indicator, we may refactor boll indicator by subscribing SMA indicator,
// however, since general used BOLLINGER band use window 21, which is not in the existing SMA indicator sets. // however, since general used BOLLINGER band use window 21, which is not in the existing SMA indicator sets.
// Pull out the bandwidth configuration as the boll Key // Pull out the bandwidth configuration as the boll Key
iw := types.IntervalWindow{Interval: interval, Window: 21} iwTmp := types.IntervalWindow{Interval: interval, Window: 21}
set.boll[iw] = &indicator.BOLL{IntervalWindow: iw, K: 2.0} iwb := types.IntervalWindowBandWidth{IntervalWindow: iwTmp, BandWidth: 1.0}
set.boll[iw].Bind(store) set.boll[iwb] = &indicator.BOLL{IntervalWindow: iwTmp, K: 1.0}
set.boll[iwb].Bind(store)
} }
return set return set
} }
// BOLL returns the bollinger band indicator of the given interval and the window, // BOLL returns the bollinger band indicator of the given interval, the window and bandwidth
// Please note that the K for std dev is fixed and defaults to 2.0
func (set *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64) *indicator.BOLL { func (set *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64) *indicator.BOLL {
inc, ok := set.boll[iw]
iwb := types.IntervalWindowBandWidth{IntervalWindow: iw, BandWidth: bandWidth}
inc, ok := set.boll[iwb]
if !ok { if !ok {
inc = &indicator.BOLL{IntervalWindow: iw, K: bandWidth} inc = &indicator.BOLL{IntervalWindow: iw, K: bandWidth}
// Bind tmp data store
tmpDataStore := NewMarketDataStore("TMP")
inc.Bind(tmpDataStore)
// Trigger tmp data store to calculate indicator
Klines := set.store.KLineWindows[iw.Interval]
for _, kline := range Klines {
tmpDataStore.AddKLine(kline)
}
// Bind real store
inc.Bind(set.store) inc.Bind(set.store)
set.boll[iw] = inc set.boll[iwb] = inc
} }
return inc return inc

View File

@ -75,6 +75,11 @@ type IntervalWindow struct {
Window int `json:"window"` Window int `json:"window"`
} }
type IntervalWindowBandWidth struct {
IntervalWindow
BandWidth float64 `json:"bandWidth"`
}
func (iw IntervalWindow) String() string { func (iw IntervalWindow) String() string {
return fmt.Sprintf("%s (%d)", iw.Interval, iw.Window) return fmt.Sprintf("%s (%d)", iw.Interval, iw.Window)
} }