kline/exchanges: automatic creation of unsupported candle intervals (#1091)

* kline: Add builder and testing

* Ideas

* kline: deploy builder functionality across GCT

* exchanges: implement across gct

* exchanges: Add tests and fix implementations before kline package testing and veri.

* kline: Add tests and start to fix ConvertToNewInterval

* kline: fix ConvertToNewInterval add tests

* kline: complete overarching tests now on to exchanges

* kline: finish exchange tests and implement limits

* exchanges: more fixes

* linter: fix

* engine: fix tests

* kraken: fix recent trades and other fixes

* zb: fix tests

* bithumb: fix empty insertion

* kline: refactor/optimize CreateKline function

* kline: remove the mooos!

* kline: prealloc CalculateCandleDateRanges

* linter: fix

* exchanges: prealloc extended

* fix whoopsie

* reverse fix because this is a whoopsie

* okx: fix risidual issues

* linter: fix

* kline: initial nits from @gloriouscode

* kline: rename builder -> request and cascade change

* linter: fix + test

* kline: update forced alignment on start and end times when CreateKlineRequest is called.

* nits: more more more

* NITS: Addressed

* tests: fix race issue

* Update exchanges/kline/request.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* kline: add method AddPadding() to automatically fill in holes in kline.Request functionality and reject if missing data when converting

* kline: Add params start and end to addPadding() to insert blanks in between block

* kline: remove test comment code as it's not needed anymore

* kline: fix lint and test

* kline: sort slice without extra bool check every iteration

* okx: fix issues with timeing and candles and such from niterinos & address typo

* Update exchanges/kline/kline.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: niterinos

* Update exchanges/poloniex/poloniex_wrapper.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits now onto conflicts YAYA!!!

* Update exchanges/exchange_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits again

* thrasher: nitters

* thrasher: niterinos - adds partial flag for incomplete recent candles and fetching.

* kline: rm fmtizzle packageizzle

* glorious: nitters

* glorious: more niterinos

* fix last niterinos

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
Ryan O'Hara-Reid
2023-01-17 16:22:33 +11:00
committed by GitHub
parent 72f36d70d1
commit 83cfefa45c
110 changed files with 11312 additions and 5768 deletions

View File

@@ -3093,28 +3093,28 @@ func (ok *Okx) GetIntervalEnum(interval kline.Interval) string {
return "2H"
case kline.FourHour:
return "4H"
case kline.SixHour:
return "6H"
case kline.SixHour: // NOTE: Cases here and below force UTC return instead of hong Kong time.
return "6Hutc"
case kline.EightHour:
return "8H"
return "8Hutc"
case kline.TwelveHour:
return "12H"
return "12Hutc"
case kline.OneDay:
return "1D"
return "1Dutc"
case kline.TwoDay:
return "2D"
return "2Dutc"
case kline.ThreeDay:
return "3D"
return "3Dutc"
case kline.OneWeek:
return "1W"
return "1Wutc"
case kline.OneMonth:
return "1M"
return "1Mutc"
case kline.ThreeMonth:
return "3M"
return "3Mutc"
case kline.SixMonth:
return "6M"
return "6Mutc"
case kline.OneYear:
return "1Y"
return "1Yutc"
default:
return ""
}
@@ -3149,10 +3149,10 @@ func (ok *Okx) GetCandlestickData(ctx context.Context, instrumentID string, inte
}
params.Set("instId", instrumentID)
var resp [][7]string
if limit <= 100 {
if limit <= 300 {
params.Set("limit", strconv.FormatInt(limit, 10))
} else if limit > 100 {
return nil, fmt.Errorf("%w limit can not exceed 100", errLimitExceedsMaximumResultPerRequest)
} else if limit > 300 {
return nil, fmt.Errorf("%w can not exceed 300", errLimitExceedsMaximumResultPerRequest)
}
if !before.IsZero() {
params.Set("before", strconv.FormatInt(before.UnixMilli(), 10))
@@ -3164,7 +3164,7 @@ func (ok *Okx) GetCandlestickData(ctx context.Context, instrumentID string, inte
if bar != "" {
params.Set("bar", bar)
}
err := ok.SendHTTPRequest(ctx, exchange.RestSpot, getCandlesticksEPL, http.MethodGet, common.EncodeURLValues(marketCandles, params), nil, &resp, false)
err := ok.SendHTTPRequest(ctx, exchange.RestSpot, getCandlesticksEPL, http.MethodGet, common.EncodeURLValues(route, params), nil, &resp, false)
if err != nil {
return nil, err
}

View File

@@ -127,13 +127,6 @@ func TestGetCandlesticks(t *testing.T) {
t.Error("Okx GetCandlesticks() error", err)
}
}
func TestGetHistoricCandlesExtended(t *testing.T) {
t.Parallel()
currencyPair := currency.NewPair(currency.BTC, currency.USDT)
if _, err := ok.GetHistoricCandlesExtended(context.Background(), currencyPair, asset.Spot, time.Now().Add(-time.Hour), time.Now(), kline.OneMin); err != nil {
t.Errorf("%s GetHistoricCandlesExtended() error: %v", ok.Name, err)
}
}
func TestGetCandlesticksHistory(t *testing.T) {
t.Parallel()
@@ -2309,15 +2302,25 @@ func TestValidateCredentials(t *testing.T) {
func TestGetHistoricCandles(t *testing.T) {
t.Parallel()
pair := currency.NewPair(currency.BTC, currency.USDT)
startTime := time.Date(2020, 9, 1, 0, 0, 0, 0, time.UTC)
endTime := time.Date(2021, 2, 15, 0, 0, 0, 0, time.UTC)
_, err := ok.GetHistoricCandles(context.Background(), pair, asset.Spot, startTime, endTime, kline.Interval(time.Hour*5))
if err != nil && !strings.Contains(err.Error(), "interval not supported") {
t.Errorf("Okx GetHistoricCandles() expected %s, but found %v", "interval not supported", err)
}
_, err = ok.GetHistoricCandles(context.Background(), pair, asset.Spot, time.Time{}, time.Time{}, kline.Interval(time.Hour*4))
startTime := time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC)
endTime := time.Date(2021, 9, 15, 0, 0, 0, 0, time.UTC)
_, err := ok.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.OneDay, startTime, endTime)
if err != nil {
t.Error("Okx GetHistoricCandles() error", err)
t.Fatal(err)
}
_, err = ok.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.Interval(time.Hour*4), startTime, endTime)
if !errors.Is(err, kline.ErrRequestExceedsExchangeLimits) {
t.Errorf("received: '%v' but expected: '%v'", err, kline.ErrRequestExceedsExchangeLimits)
}
}
func TestGetHistoricCandlesExtended(t *testing.T) {
t.Parallel()
currencyPair := currency.NewPair(currency.BTC, currency.USDT)
_, err := ok.GetHistoricCandlesExtended(context.Background(), currencyPair, asset.Spot, kline.OneMin, time.Now().Add(-time.Hour), time.Now())
if err != nil {
t.Errorf("%s GetHistoricCandlesExtended() error: %v", ok.Name, err)
}
}

View File

@@ -130,25 +130,25 @@ func (ok *Okx) SetDefaults() {
Enabled: exchange.FeaturesEnabled{
AutoPairUpdates: true,
Kline: kline.ExchangeCapabilitiesEnabled{
Intervals: map[string]bool{
kline.OneMin.Word(): true,
kline.ThreeMin.Word(): true,
kline.FiveMin.Word(): true,
kline.FifteenMin.Word(): true,
kline.ThirtyMin.Word(): true,
kline.OneHour.Word(): true,
kline.TwoHour.Word(): true,
kline.FourHour.Word(): true,
kline.SixHour.Word(): true,
kline.TwelveHour.Word(): true,
kline.OneDay.Word(): true,
kline.ThreeDay.Word(): true,
kline.OneWeek.Word(): true,
kline.OneMonth.Word(): true,
kline.ThreeMonth.Word(): true,
kline.SixMonth.Word(): true,
kline.OneYear.Word(): true,
},
Intervals: kline.DeployExchangeIntervals(
kline.OneMin,
kline.ThreeMin,
kline.FiveMin,
kline.FifteenMin,
kline.ThirtyMin,
kline.OneHour,
kline.TwoHour,
kline.FourHour,
kline.SixHour,
kline.TwelveHour,
kline.OneDay,
kline.ThreeDay,
kline.OneWeek,
kline.OneMonth,
kline.ThreeMonth,
kline.SixMonth,
kline.OneYear,
),
ResultLimit: 300,
},
},
@@ -1361,75 +1361,67 @@ func (ok *Okx) ValidateCredentials(ctx context.Context, assetType asset.Item) er
}
// GetHistoricCandles returns candles between a time period for a set time interval
func (ok *Okx) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if err := ok.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
if kline.TotalCandlesPerInterval(start, end, interval) > 100 {
return kline.Item{}, errors.New(kline.ErrRequestExceedsExchangeLimits)
}
format, err := ok.GetPairFormat(a, false)
func (ok *Okx) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
req, err := ok.GetKlineRequest(pair, a, interval, start, end)
if err != nil {
return kline.Item{}, err
return nil, err
}
if !pair.IsPopulated() {
return kline.Item{}, errIncompleteCurrencyPair
}
instrumentID := format.Format(pair)
candles, err := ok.GetCandlesticksHistory(ctx, instrumentID, interval, start, end, 100)
candles, err := ok.GetCandlesticksHistory(ctx,
req.RequestFormatted.Base.String()+
currency.DashDelimiter+
req.RequestFormatted.Quote.String(),
req.ExchangeInterval,
start.Add(-time.Nanosecond), // Start time not inclusive of candle.
end,
300)
if err != nil {
return kline.Item{}, err
}
response := kline.Item{
Exchange: ok.Name,
Pair: pair,
Asset: a,
Interval: interval,
return nil, err
}
timeSeries := make([]kline.Candle, len(candles))
for x := range candles {
response.Candles = append(response.Candles, kline.Candle{
timeSeries[x] = kline.Candle{
Time: candles[x].OpenTime,
Open: candles[x].OpenPrice,
High: candles[x].HighestPrice,
Low: candles[x].LowestPrice,
Close: candles[x].ClosePrice,
Volume: candles[x].Volume,
})
}
}
response.SortCandlesByTimestamp(false)
return response, nil
return req.ProcessResponse(timeSeries)
}
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (ok *Okx) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
format, err := ok.GetPairFormat(a, false)
func (ok *Okx) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
req, err := ok.GetKlineExtendedRequest(pair, a, interval, start, end)
if err != nil {
return kline.Item{}, err
return nil, err
}
err = ok.ValidateKline(pair, a, interval)
if err != nil {
return kline.Item{}, err
if count := kline.TotalCandlesPerInterval(start, end, req.ExchangeInterval); count > 1440 {
return nil,
fmt.Errorf("candles count: %d max lookback: %d, %w",
count, 1440, kline.ErrRequestExceedsMaxLookback)
}
instrumentID := format.Format(pair)
if err != nil {
return kline.Item{}, err
}
ret := kline.Item{
Exchange: ok.Name,
Pair: pair,
Interval: interval,
}
dates, err := kline.CalculateCandleDateRanges(start, end, interval, 100)
if err != nil {
return kline.Item{}, err
}
for y := range dates.Ranges {
candles, err := ok.GetCandlesticksHistory(ctx, instrumentID, interval, dates.Ranges[y].Start.Time, dates.Ranges[y].End.Time, 100)
timeSeries := make([]kline.Candle, 0, req.Size())
for y := range req.Ranges {
var candles []CandleStick
candles, err = ok.GetCandlesticksHistory(ctx,
req.RequestFormatted.Base.String()+
currency.DashDelimiter+
req.RequestFormatted.Quote.String(),
req.ExchangeInterval,
req.Ranges[y].Start.Time.Add(-time.Nanosecond), // Start time not inclusive of candle.
req.Ranges[y].End.Time,
300)
if err != nil {
return kline.Item{}, err
return nil, err
}
for x := range candles {
ret.Candles = append(ret.Candles, kline.Candle{
timeSeries = append(timeSeries, kline.Candle{
Time: candles[x].OpenTime,
Open: candles[x].OpenPrice,
High: candles[x].HighestPrice,
@@ -1439,10 +1431,7 @@ func (ok *Okx) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pai
})
}
}
ret.RemoveDuplicateCandlesByTime()
ret.RemoveOutsideRange(start, end)
ret.SortCandlesByTimestamp(false)
return ret, nil
return req.ProcessResponse(timeSeries)
}
// GetAvailableTransferChains returns the available transfer blockchains for the specific