diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index f82dc4e4f..c2bda78d6 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -194,6 +194,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) { // update last prices by the given kline lastKLine := kLines[len(kLines)-1] + log.Infof("last kline: %+v", lastKLine) if lastPriceTime == emptyTime { session.lastPrices[symbol] = lastKLine.Close lastPriceTime = lastKLine.EndTime diff --git a/pkg/indicator/ewma.go b/pkg/indicator/ewma.go index 0690fdbff..86b332e37 100644 --- a/pkg/indicator/ewma.go +++ b/pkg/indicator/ewma.go @@ -19,6 +19,10 @@ type EWMA struct { } func (inc *EWMA) Last() float64 { + if len(inc.Values) == 0 { + return 0 + } + return inc.Values[len(inc.Values)-1] } diff --git a/pkg/strategy/movingstop/strategy.go b/pkg/strategy/movingstop/strategy.go index 3bda7ba57..6e2441c29 100644 --- a/pkg/strategy/movingstop/strategy.go +++ b/pkg/strategy/movingstop/strategy.go @@ -202,7 +202,6 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se session.Stream.OnOrderUpdate(s.handleOrderUpdate) - // session.Stream.OnKLineClosed sourceSession.Stream.OnKLineClosed(func(kline types.KLine) { // skip k-lines from other symbols @@ -227,7 +226,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se s.clear(ctx, session) }) - if lastPrice, ok := session.LastPrice(s.Symbol) ; ok { + if lastPrice, ok := session.LastPrice(s.Symbol); ok { s.place(ctx, &orderExecutor, session, indicator, lastPrice) } diff --git a/pkg/types/kline.go b/pkg/types/kline.go index b9bc123a9..b45bb67d0 100644 --- a/pkg/types/kline.go +++ b/pkg/types/kline.go @@ -169,7 +169,8 @@ func (k KLine) GetChange() float64 { } func (k KLine) String() string { - return fmt.Sprintf("%s %s %s Open: %.8f Close: %.8f High: %.8f Low: %.8f Volume: %.8f Change: %.4f Max Change: %.4f", + return fmt.Sprintf("%s %s %s %s Open: %.8f Close: %.8f High: %.8f Low: %.8f Volume: %.8f Change: %.4f Max Change: %.4f", + k.Exchange, k.StartTime.Format("2006-01-02 15:04"), k.Symbol, k.Interval, k.Open, k.Close, k.High, k.Low, k.Volume, k.GetChange(), k.GetMaxChange()) } @@ -346,14 +347,15 @@ func (k KLineWindow) Take(size int) KLineWindow { } func (k KLineWindow) Tail(size int) KLineWindow { - if len(k) <= size { - win := make(KLineWindow, len(k)) + length := len(k) + if length <= size { + win := make(KLineWindow, length) copy(win, k) return win } win := make(KLineWindow, size) - copy(win, k[len(k)-size:]) + copy(win, k[length-1-size:]) return win }