fix kline tail method

This commit is contained in:
c9s 2020-12-08 10:26:20 +08:00
parent 3aa8d70622
commit 0222c33330
4 changed files with 12 additions and 6 deletions

View File

@ -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

View File

@ -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]
}

View File

@ -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)
}

View File

@ -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
}