okex: fix kline query

This commit is contained in:
c9s 2022-05-03 11:14:53 +08:00
parent f22327eb75
commit d742aea633
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 26 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package okex
import ( import (
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -268,3 +269,10 @@ func toGlobalOrderType(orderType okexapi.OrderType) (types.OrderType, error) {
} }
return "", fmt.Errorf("unknown or unsupported okex order type: %s", orderType) return "", fmt.Errorf("unknown or unsupported okex order type: %s", orderType)
} }
func toLocalInterval(src string, ) string {
var re = regexp.MustCompile("\\d+[hdw]")
return re.ReplaceAllStringFunc(src, func(w string) string {
return strings.ToUpper(w)
})
}

View File

@ -8,12 +8,15 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"golang.org/x/time/rate"
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi" "github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
) )
var marketDataLimiter = rate.NewLimiter(rate.Every(time.Second/20), 1)
// OKB is the platform currency of OKEx, pre-allocate static string here // OKB is the platform currency of OKEx, pre-allocate static string here
const OKB = "OKB" const OKB = "OKB"
@ -272,15 +275,21 @@ func (e *Exchange) NewStream() types.Stream {
} }
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) { func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
if err := marketDataLimiter.Wait(ctx) ; err != nil {
return nil, err
}
intervalParam := toLocalInterval(interval.String())
req := e.client.MarketDataService.NewCandlesticksRequest(toLocalSymbol(symbol)) req := e.client.MarketDataService.NewCandlesticksRequest(toLocalSymbol(symbol))
req.Bar(interval.String()) req.Bar(intervalParam)
if options.StartTime != nil { if options.StartTime != nil {
req.After(options.StartTime.UnixNano() / int64(time.Millisecond)) req.After(options.StartTime.Unix())
} }
if options.EndTime != nil { if options.EndTime != nil {
req.Before(options.EndTime.UnixNano() / int64(time.Millisecond)) req.Before(options.EndTime.Unix())
} }
candles, err := req.Do(ctx) candles, err := req.Do(ctx)

View File

@ -25,15 +25,15 @@ type Candle struct {
type CandlesticksRequest struct { type CandlesticksRequest struct {
client *RestClient client *RestClient
instId string instId string `param:"instId"`
limit *int limit *int `param:"limit"`
bar *string bar *string `param:"bar"`
after *int64 after *int64 `param:"after,seconds"`
before *int64 before *int64 `param:"before,seconds"`
} }
func (r *CandlesticksRequest) After(after int64) *CandlesticksRequest { func (r *CandlesticksRequest) After(after int64) *CandlesticksRequest {