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

@@ -94,43 +94,33 @@ func TestGetChartCandles(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTC-USD")
if err != nil {
t.Fatal(err)
}
startTime := time.Now().Add(-time.Hour * 24)
end := time.Now()
_, err = h.GetHistoricCandles(context.Background(),
currencyPair, asset.Spot, startTime, end, kline.OneMin)
if err != nil {
t.Fatal(err)
}
t.Parallel()
_, err = h.GetHistoricCandles(context.Background(),
currencyPair, asset.Spot, startTime, end, kline.Interval(time.Hour*7))
if err == nil {
t.Fatal("unexpected result")
pair, err := currency.NewPairFromString("BTC-USD")
if err != nil {
t.Fatal(err)
}
startTime := time.Now().Add(-time.Hour * 6)
end := time.Now()
_, err = h.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.OneMin, startTime, end)
if err != nil {
t.Fatal(err)
}
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTC-USD")
t.Parallel()
pair, err := currency.NewPairFromString("BTC-USD")
if err != nil {
t.Fatal(err)
}
startTime := time.Unix(1546300800, 0)
end := time.Unix(1577836799, 0)
_, err = h.GetHistoricCandlesExtended(context.Background(),
currencyPair, asset.Spot, startTime, end, kline.OneHour)
_, err = h.GetHistoricCandlesExtended(context.Background(), pair, asset.Spot, kline.OneHour, startTime, end)
if err != nil {
t.Fatal(err)
}
_, err = h.GetHistoricCandlesExtended(context.Background(),
currencyPair, asset.Spot, startTime, end, kline.Interval(time.Hour*7))
if err == nil {
t.Fatal("unexpected result")
}
}
func TestGetCurrencies(t *testing.T) {

View File

@@ -115,16 +115,18 @@ func (h *HitBTC) 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.ThirtyMin.Word(): true,
kline.OneHour.Word(): true,
kline.FourHour.Word(): true,
kline.OneDay.Word(): true,
kline.SevenDay.Word(): true,
},
Intervals: kline.DeployExchangeIntervals(
kline.OneMin,
kline.ThreeMin,
kline.FiveMin,
kline.FifteenMin,
kline.ThirtyMin,
kline.OneHour,
kline.FourHour,
kline.OneDay,
kline.SevenDay,
kline.OneMonth,
),
ResultLimit: 1000,
},
},
@@ -861,82 +863,58 @@ func (h *HitBTC) FormatExchangeKlineInterval(in kline.Interval) string {
}
// GetHistoricCandles returns candles between a time period for a set time interval
func (h *HitBTC) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if err := h.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := h.FormatExchangeCurrency(pair, a)
func (h *HitBTC) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
req, err := h.GetKlineRequest(pair, a, interval, start, end)
if err != nil {
return kline.Item{}, err
return nil, err
}
data, err := h.GetCandles(ctx,
formattedPair.String(),
req.RequestFormatted.String(),
strconv.FormatInt(int64(h.Features.Enabled.Kline.ResultLimit), 10),
h.FormatExchangeKlineInterval(interval),
start,
end)
h.FormatExchangeKlineInterval(req.ExchangeInterval),
req.Start,
req.End)
if err != nil {
return kline.Item{}, err
return nil, err
}
ret := kline.Item{
Exchange: h.Name,
Pair: pair,
Asset: a,
Interval: interval,
}
timeSeries := make([]kline.Candle, len(data))
for x := range data {
ret.Candles = append(ret.Candles, kline.Candle{
timeSeries[x] = kline.Candle{
Time: data[x].Timestamp,
Open: data[x].Open,
High: data[x].Max,
Low: data[x].Min,
Close: data[x].Close,
Volume: data[x].Volume,
})
}
}
ret.SortCandlesByTimestamp(false)
return ret, nil
return req.ProcessResponse(timeSeries)
}
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (h *HitBTC) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if err := h.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{
Exchange: h.Name,
Pair: pair,
Asset: a,
Interval: interval,
func (h *HitBTC) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
req, err := h.GetKlineExtendedRequest(pair, a, interval, start, end)
if err != nil {
return nil, err
}
dates, err := kline.CalculateCandleDateRanges(start, end, interval, h.Features.Enabled.Kline.ResultLimit)
if err != nil {
return kline.Item{}, err
}
formattedPair, err := h.FormatExchangeCurrency(pair, a)
if err != nil {
return kline.Item{}, err
}
for y := range dates.Ranges {
timeSeries := make([]kline.Candle, 0, req.Size())
for y := range req.Ranges {
var data []ChartData
data, err = h.GetCandles(ctx,
formattedPair.String(),
req.RequestFormatted.String(),
strconv.FormatInt(int64(h.Features.Enabled.Kline.ResultLimit), 10),
h.FormatExchangeKlineInterval(interval),
dates.Ranges[y].Start.Time,
dates.Ranges[y].End.Time)
h.FormatExchangeKlineInterval(req.ExchangeInterval),
req.Ranges[y].Start.Time,
req.Ranges[y].End.Time)
if err != nil {
return kline.Item{}, err
return nil, err
}
for i := range data {
ret.Candles = append(ret.Candles, kline.Candle{
timeSeries = append(timeSeries, kline.Candle{
Time: data[i].Timestamp,
Open: data[i].Open,
High: data[i].Max,
@@ -946,13 +924,5 @@ func (h *HitBTC) GetHistoricCandlesExtended(ctx context.Context, pair currency.P
})
}
}
dates.SetHasDataFromCandles(ret.Candles)
summary := dates.DataSummary(false)
if len(summary) > 0 {
log.Warnf(log.ExchangeSys, "%v - %v", h.Name, summary)
}
ret.RemoveDuplicateCandlesByTime()
ret.RemoveOutsideRange(start, end)
ret.SortCandlesByTimestamp(false)
return ret, nil
return req.ProcessResponse(timeSeries)
}