Skip to content

Commit

Permalink
fix market data kline registration
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 5, 2020
1 parent 900f822 commit c5d002a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/buyandhold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ exchangeStrategies:
symbol: "BTCUSDT"
interval: "1m"
baseQuantity: 0.001
minDropPercentage: -0.001
minDropPercentage: -0.01
5 changes: 3 additions & 2 deletions pkg/bbgo/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
}

session.lastPrices[kline.Symbol] = kline.Close
session.marketDataStores[kline.Symbol].AddKLine(kline)
})

session.Stream.OnTradeUpdate(func(trade types.Trade) {
Expand All @@ -167,8 +166,10 @@ func (environ *Environment) Init(ctx context.Context) (err error) {

var lastPriceTime time.Time
for interval := range types.SupportedIntervals {
// avoid querying the last unclosed kline
endTime := environ.startTime.Add(- interval.Duration())
kLines, err := session.Exchange.QueryKLines(ctx, symbol, interval, types.KLineQueryOptions{
EndTime: &environ.startTime,
EndTime: &endTime,
Limit: 500, // indicators need at least 100
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/boll.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (inc *BOLL) calculateAndUpdate(kLines []types.KLine) {
}

var recentK = kLines[index-(inc.Window-1) : index+1]
sma, err := calculateSMA(recentK, inc.Window)
sma, err := calculateSMA(recentK, inc.Window, KLineClosePriceMapper)
if err != nil {
log.WithError(err).Error("SMA error")
return
Expand Down
9 changes: 6 additions & 3 deletions pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ func (inc *EWMA) calculateAndUpdate(allKLines []types.KLine) {
// for the first value, we should use the close price
inc.Values = []float64{priceF(allKLines[0])}
} else {
// from = len(inc.Values)

// update ewma with the existing values
for i := dataLen - 1; i > 0; i-- {
if allKLines[i].StartTime.After(inc.LastOpenTime) {
var k = allKLines[i]
if k.StartTime.After(inc.LastOpenTime) {
from = i
} else {
break
Expand All @@ -57,13 +60,13 @@ func (inc *EWMA) calculateAndUpdate(allKLines []types.KLine) {
}

if len(inc.Values) != dataLen {
log.Warnf("EMA %s (%d) value length (%d) != all kline data length (%d)", inc.Interval, inc.Window, len(inc.Values), dataLen)
log.Warnf("%s EMA (%d) value length (%d) != all kline data length (%d)", inc.Interval, inc.Window, len(inc.Values), dataLen)
}

v1 := math.Floor(inc.Values[len(inc.Values)-1]*100.0) / 100.0
v2 := math.Floor(CalculateKLineEWMA(allKLines, priceF, inc.Window)*100.0) / 100.0
if v1 != v2 {
log.Warnf("ACCUMULATED EMA %s (%d) %f != EMA %f", inc.Interval, inc.Window, v1, v2)
log.Warnf("ACCUMULATED %s EMA (%d) %f != EMA %f", inc.Interval, inc.Window, v1, v2)
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (inc *SMA) calculateAndUpdate(kLines []types.KLine) {

var recentK = kLines[index-(inc.Window-1) : index+1]

sma, err := calculateSMA(recentK, inc.Window)
sma, err := calculateSMA(recentK, inc.Window, KLineClosePriceMapper)
if err != nil {
log.WithError(err).Error("SMA error")
return
Expand All @@ -67,15 +67,15 @@ func (inc *SMA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

func calculateSMA(kLines []types.KLine, window int) (float64, error) {
func calculateSMA(kLines []types.KLine, window int, priceF KLinePriceMapper) (float64, error) {
length := len(kLines)
if length == 0 || length < window {
return 0.0, fmt.Errorf("insufficient elements for calculating SMA with window = %d", window)
}

sum := 0.0
for _, k := range kLines {
sum += k.Close
sum += priceF(k)
}

avg := sum / float64(window)
Expand Down

0 comments on commit c5d002a

Please sign in to comment.