fix max kline calculation

This commit is contained in:
c9s 2020-08-14 13:08:09 +08:00
parent 8f2c0b1680
commit 7c5fa2f191
2 changed files with 67 additions and 15 deletions

View File

@ -450,3 +450,34 @@ func convertRemoteTrade(t binance.TradeV3) (*types.Trade, error) {
Time: mts, Time: mts,
}, nil }, nil
} }
func (e *Exchange) BatchQueryKLines(ctx context.Context, symbol, interval string, startTime, endTime time.Time) ([]types.KLine, error) {
var allKLines []types.KLine
for startTime.Before(endTime) {
klines, err := e.QueryKLines(ctx, symbol, interval, types.KLineQueryOptions{
StartTime: &startTime,
Limit: 1000,
})
if err != nil {
return nil, err
}
for _, kline := range klines {
t := time.Unix(0, kline.EndTime*int64(time.Millisecond))
if t.After(endTime) {
return allKLines, nil
}
allKLines = append(allKLines, kline)
startTime = t
}
// avoid rate limit
time.Sleep(100 * time.Millisecond)
}
return allKLines, nil
}

View File

@ -2,13 +2,16 @@ package types
import ( import (
"fmt" "fmt"
"github.com/c9s/bbgo/pkg/util"
"github.com/slack-go/slack"
"math" "math"
"time" "time"
"github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/util"
) )
type KLineOrWindow interface { type KLineOrWindow interface {
GetInterval() string
GetTrend() int GetTrend() int
GetChange() float64 GetChange() float64
GetMaxChange() float64 GetMaxChange() float64
@ -42,9 +45,9 @@ type KLine struct {
Symbol string `json:"s"` Symbol string `json:"s"`
Interval string `json:"i"` Interval string `json:"i"`
Open string `json:"o"` Open string `json:"o"`
Close string `json:"c"` Close string `json:"c"`
High string `json:"h"` High string `json:"h"`
Low string `json:"l"` Low string `json:"l"`
Volume string `json:"V"` // taker buy base asset volume (like 10 BTC) Volume string `json:"V"` // taker buy base asset volume (like 10 BTC)
@ -55,6 +58,10 @@ type KLine struct {
Closed bool `json:"x"` Closed bool `json:"x"`
} }
func (k KLine) GetInterval() string {
return k.Interval
}
func (k KLine) Mid() float64 { func (k KLine) Mid() float64 {
return (k.GetHigh() + k.GetLow()) / 2 return (k.GetHigh() + k.GetLow()) / 2
} }
@ -143,6 +150,14 @@ func (k KLine) GetChange() float64 {
return k.GetClose() - k.GetOpen() return k.GetClose() - k.GetOpen()
} }
func (k KLine) GetStartTime() time.Time {
return time.Unix(0, k.StartTime*int64(time.Millisecond))
}
func (k KLine) GetEndTime() time.Time {
return time.Unix(0, k.EndTime*int64(time.Millisecond))
}
func (k KLine) String() string { func (k KLine) String() string {
return fmt.Sprintf("%s %s Open: % 14s Close: % 14s High: % 14s Low: % 14s Volume: % 15s Change: % 11f Max Change: % 11f", k.Symbol, k.Interval, k.Open, k.Close, k.High, k.Low, k.Volume, k.GetChange(), k.GetMaxChange()) return fmt.Sprintf("%s %s Open: % 14s Close: % 14s High: % 14s Low: % 14s Volume: % 15s Change: % 11f Max Change: % 11f", k.Symbol, k.Interval, k.Open, k.Close, k.High, k.Low, k.Volume, k.GetChange(), k.GetMaxChange())
} }
@ -195,8 +210,16 @@ func (k KLineWindow) Len() int {
return len(k) return len(k)
} }
func (k KLineWindow) First() KLine {
return k[0]
}
func (k KLineWindow) GetInterval() string {
return k.First().Interval
}
func (k KLineWindow) GetOpen() float64 { func (k KLineWindow) GetOpen() float64 {
return k[0].GetOpen() return k.First().GetOpen()
} }
func (k KLineWindow) GetClose() float64 { func (k KLineWindow) GetClose() float64 {
@ -207,22 +230,18 @@ func (k KLineWindow) GetClose() float64 {
func (k KLineWindow) GetHigh() float64 { func (k KLineWindow) GetHigh() float64 {
high := k.GetOpen() high := k.GetOpen()
for _, line := range k { for _, line := range k {
val := line.GetHigh() high = math.Max(high, line.GetHigh())
if val > high {
high = val
}
} }
return high return high
} }
func (k KLineWindow) GetLow() float64 { func (k KLineWindow) GetLow() float64 {
low := k.GetOpen() low := k.GetOpen()
for _, line := range k { for _, line := range k {
val := line.GetLow() low = math.Min(low, line.GetLow())
if val < low {
low = val
}
} }
return low return low
} }
@ -353,9 +372,11 @@ func (k KLineWindow) GetLowerShadowHeight() float64 {
func (k KLineWindow) SlackAttachment() slack.Attachment { func (k KLineWindow) SlackAttachment() slack.Attachment {
var first KLine var first KLine
var end KLine
var windowSize = len(k) var windowSize = len(k)
if windowSize > 0 { if windowSize > 0 {
first = k[0] first = k[0]
end = k[windowSize-1]
} }
return slack.Attachment{ return slack.Attachment{
@ -393,7 +414,7 @@ func (k KLineWindow) SlackAttachment() slack.Attachment {
Short: true, Short: true,
}, },
}, },
Footer: "", Footer: fmt.Sprintf("Since %s til %s", first.GetStartTime(), end.GetEndTime()),
FooterIcon: "", FooterIcon: "",
} }
} }