exchanges: add setTimeWindow boolean to GetKlineRequest param (#1160)

* exchanges: add setTimeWindow boolean to GetKlineRequest params to differentiate between a set time period return from endpoint.

* glorious: nits

* exchange: conjugation

* Update exchanges/exchange.go

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

* glorious: nits and an assortment of differences

* exchanges: remove some comments

* glorious: nits

* cleanup

* tests: fix

* Update exchanges/hitbtc/hitbtc_wrapper.go

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

* Update exchanges/kline/kline.go

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

* Update exchanges/kline/kline_test.go

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

* glorious: nits

* kline: fix test

* rm unused variables

* almost: nits

* glorious: nits

* linter: fix

* rm unused variable

* Refactored comment in the okex tests to ensure that it accurately reflects the variable name and the issue related to the time window, as requested by GloriousCode. The previous comment did not align with the identifier assigned to the property, which could cause confusion and misunderstanding among other programmers or stakeholders. The updated comment will improve the clarity and readability of the codebase and make it easier to understand the intended purpose of the associated variables. The change was made with the aim of improving the overall quality and maintainability of the code.

---------

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-04-27 10:10:19 +10:00
committed by GitHub
parent 668d083749
commit 42475bf2b8
36 changed files with 809 additions and 471 deletions

View File

@@ -1500,7 +1500,7 @@ func (b *Base) IsPerpetualFutureCurrency(asset.Item, currency.Pair) (bool, error
// GetKlineRequest returns a helper for the fetching of candle/kline data for
// a single request within a pre-determined time window.
func (b *Base) GetKlineRequest(pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Request, error) {
func (b *Base) GetKlineRequest(pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time, fixedAPICandleLength bool) (*kline.Request, error) {
if pair.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
@@ -1516,17 +1516,6 @@ func (b *Base) GetKlineRequest(pair currency.Pair, a asset.Item, interval kline.
return nil, err
}
// NOTE: This check is here to make sure a client is notified that using
// this functionality will result in error if the total candles cannot be
// theoretically retrieved.
if count := kline.TotalCandlesPerInterval(start, end, exchangeInterval); count >
int64(b.Features.Enabled.Kline.ResultLimit) {
return nil, fmt.Errorf("candles count: %d, max limit: %d %w",
count,
b.Features.Enabled.Kline.ResultLimit,
kline.ErrRequestExceedsExchangeLimits)
}
err = b.ValidateKline(pair, a, exchangeInterval)
if err != nil {
return nil, err
@@ -1537,7 +1526,49 @@ func (b *Base) GetKlineRequest(pair currency.Pair, a asset.Item, interval kline.
return nil, err
}
return kline.CreateKlineRequest(b.Name, pair, formatted, a, interval, exchangeInterval, start, end)
limit, err := b.Features.Enabled.Kline.GetIntervalResultLimit(exchangeInterval)
if err != nil {
return nil, err
}
req, err := kline.CreateKlineRequest(b.Name, pair, formatted, a, interval, exchangeInterval, start, end, limit)
if err != nil {
return nil, err
}
// NOTE: The checks below makes sure a client is notified that using this
// functionality will result in error if the total candles cannot be
// theoretically retrieved.
if fixedAPICandleLength {
origCount := kline.TotalCandlesPerInterval(req.Start, req.End, interval)
modifiedCount := kline.TotalCandlesPerInterval(req.Start, time.Now(), exchangeInterval)
if modifiedCount > limit {
errMsg := fmt.Sprintf("for %v %v candles between %v-%v. ",
origCount,
interval,
start.Format(common.SimpleTimeFormatWithTimezone),
end.Format(common.SimpleTimeFormatWithTimezone))
if interval != exchangeInterval {
errMsg += fmt.Sprintf("Request converts to %v %v candles. ",
modifiedCount,
exchangeInterval)
}
boundary := time.Now().Add(-exchangeInterval.Duration() * time.Duration(limit))
return nil, fmt.Errorf("%w %v, exceeding the limit of %v %v candles up to %v. Please reduce timeframe or use GetHistoricCandlesExtended",
kline.ErrRequestExceedsExchangeLimits,
errMsg,
limit,
exchangeInterval,
boundary.Format(common.SimpleTimeFormatWithTimezone))
}
} else if count := kline.TotalCandlesPerInterval(req.Start, req.End, exchangeInterval); count > limit {
return nil, fmt.Errorf("candle count exceeded: %d. The endpoint has a set candle limit return of %d candles. Candle data will be incomplete: %w",
count,
limit,
kline.ErrRequestExceedsExchangeLimits)
}
return req, nil
}
// GetKlineExtendedRequest returns a helper for the fetching of candle/kline
@@ -1566,12 +1597,18 @@ func (b *Base) GetKlineExtendedRequest(pair currency.Pair, a asset.Item, interva
return nil, err
}
r, err := kline.CreateKlineRequest(b.Name, pair, formatted, a, interval, exchangeInterval, start, end)
limit, err := b.Features.Enabled.Kline.GetIntervalResultLimit(exchangeInterval)
if err != nil {
return nil, err
}
r, err := kline.CreateKlineRequest(b.Name, pair, formatted, a, interval, exchangeInterval, start, end, limit)
if err != nil {
return nil, err
}
r.IsExtended = true
dates, err := r.GetRanges(b.Features.Enabled.Kline.ResultLimit)
dates, err := r.GetRanges(uint32(limit))
if err != nil {
return nil, err
}