bbgo_origin/pkg/bbgo/indicator_set.go

115 lines
3.5 KiB
Go
Raw Permalink Normal View History

2023-06-29 09:44:36 +00:00
package bbgo
import (
2023-06-30 02:35:34 +00:00
"github.com/sirupsen/logrus"
2024-03-23 09:17:40 +00:00
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
2023-06-29 09:44:36 +00:00
"github.com/c9s/bbgo/pkg/types"
)
// IndicatorSet is the v2 standard indicator set
// This will replace StandardIndicator in the future
type IndicatorSet struct {
Symbol string
stream types.Stream
store *MarketDataStore
// caches
2023-07-10 08:54:22 +00:00
kLines map[types.Interval]*indicatorv2.KLineStream
closePrices map[types.Interval]*indicatorv2.PriceStream
2023-06-29 09:44:36 +00:00
}
2023-06-29 09:49:04 +00:00
func NewIndicatorSet(symbol string, stream types.Stream, store *MarketDataStore) *IndicatorSet {
2023-06-29 09:44:36 +00:00
return &IndicatorSet{
Symbol: symbol,
store: store,
stream: stream,
2023-07-10 08:54:22 +00:00
kLines: make(map[types.Interval]*indicatorv2.KLineStream),
closePrices: make(map[types.Interval]*indicatorv2.PriceStream),
2023-06-29 09:44:36 +00:00
}
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) KLines(interval types.Interval) *indicatorv2.KLineStream {
2023-06-29 09:44:36 +00:00
if kLines, ok := i.kLines[interval]; ok {
return kLines
}
2023-07-10 08:54:22 +00:00
kLines := indicatorv2.KLines(i.stream, i.Symbol, interval)
2023-06-29 09:44:36 +00:00
if kLinesWindow, ok := i.store.KLinesOfInterval(interval); ok {
2023-06-30 02:38:38 +00:00
kLines.BackFill(*kLinesWindow)
2023-06-30 02:35:34 +00:00
} else {
logrus.Warnf("market data store %s kline history not found, unable to backfill the kline stream data", interval)
2023-06-29 09:44:36 +00:00
}
i.kLines[interval] = kLines
return kLines
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) OPEN(interval types.Interval) *indicatorv2.PriceStream {
return indicatorv2.OpenPrices(i.KLines(interval))
2023-06-29 09:44:36 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) HIGH(interval types.Interval) *indicatorv2.PriceStream {
return indicatorv2.HighPrices(i.KLines(interval))
2023-06-29 09:44:36 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) LOW(interval types.Interval) *indicatorv2.PriceStream {
return indicatorv2.LowPrices(i.KLines(interval))
2023-06-29 09:44:36 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) CLOSE(interval types.Interval) *indicatorv2.PriceStream {
2023-06-29 09:44:36 +00:00
if closePrices, ok := i.closePrices[interval]; ok {
return closePrices
}
2023-07-10 08:54:22 +00:00
closePrices := indicatorv2.ClosePrices(i.KLines(interval))
2023-06-29 09:44:36 +00:00
i.closePrices[interval] = closePrices
return closePrices
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) VOLUME(interval types.Interval) *indicatorv2.PriceStream {
return indicatorv2.Volumes(i.KLines(interval))
2023-06-30 02:37:42 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) RSI(iw types.IntervalWindow) *indicatorv2.RSIStream {
return indicatorv2.RSI2(i.CLOSE(iw.Interval), iw.Window)
2023-06-29 09:44:36 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) EMA(iw types.IntervalWindow) *indicatorv2.EWMAStream {
2023-06-29 09:44:36 +00:00
return i.EWMA(iw)
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) EWMA(iw types.IntervalWindow) *indicatorv2.EWMAStream {
return indicatorv2.EWMA2(i.CLOSE(iw.Interval), iw.Window)
2023-06-29 09:44:36 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) STOCH(iw types.IntervalWindow, dPeriod int) *indicatorv2.StochStream {
return indicatorv2.Stoch(i.KLines(iw.Interval), iw.Window, dPeriod)
2023-06-29 09:44:36 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) BOLL(iw types.IntervalWindow, k float64) *indicatorv2.BOLLStream {
return indicatorv2.BOLL(i.CLOSE(iw.Interval), iw.Window, k)
2023-06-29 09:44:36 +00:00
}
2024-02-03 11:28:51 +00:00
func (i *IndicatorSet) Keltner(iw types.IntervalWindow, atrLength int) *indicatorv2.KeltnerStream {
return indicatorv2.Keltner(i.KLines(iw.Interval), iw.Window, atrLength)
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) MACD(interval types.Interval, shortWindow, longWindow, signalWindow int) *indicatorv2.MACDStream {
return indicatorv2.MACD2(i.CLOSE(interval), shortWindow, longWindow, signalWindow)
2023-06-29 09:44:36 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) ATR(interval types.Interval, window int) *indicatorv2.ATRStream {
return indicatorv2.ATR2(i.KLines(interval), window)
2023-06-29 09:44:36 +00:00
}
2023-07-10 08:54:22 +00:00
func (i *IndicatorSet) ATRP(interval types.Interval, window int) *indicatorv2.ATRPStream {
return indicatorv2.ATRP2(i.KLines(interval), window)
2023-06-29 09:44:36 +00:00
}
2024-03-23 09:17:40 +00:00
func (i *IndicatorSet) ADX(interval types.Interval, window int) *indicatorv2.ADXStream {
return indicatorv2.ADX(i.KLines(interval), window)
}