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

@@ -180,49 +180,33 @@ func TestFormatExchangeKlineInterval(t *testing.T) {
func TestGetHistoricCandles(t *testing.T) {
t.Parallel()
curr, err := currency.NewPairFromString(testSPOTPair)
pair, err := currency.NewPairFromString(testSPOTPair)
if err != nil {
t.Fatal(err)
}
_, err = b.GetHistoricCandles(context.Background(),
curr, asset.Spot,
time.Time{}, time.Time{},
kline.OneMin)
start := time.Now().AddDate(0, 0, -1)
_, err = b.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.OneHour, start, time.Now())
if err != nil {
t.Fatal(err)
}
_, err = b.GetHistoricCandles(context.Background(),
curr, asset.Spot,
time.Time{}, time.Time{},
kline.OneDay)
_, err = b.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.OneDay, start, time.Now())
if err != nil {
t.Fatal(err)
}
curr.Quote = currency.XRP
_, err = b.GetHistoricCandles(context.Background(),
curr, asset.Spot,
time.Time{}, time.Time{},
kline.OneMin)
if err == nil {
t.Fatal("expected error when requesting with disabled pair")
}
}
func TestGetHistoricCandlesExtended(t *testing.T) {
t.Parallel()
curr, err := currency.NewPairFromString(testSPOTPair)
pair, err := currency.NewPairFromString(testSPOTPair)
if err != nil {
t.Fatal(err)
}
_, err = b.GetHistoricCandlesExtended(context.Background(),
curr, asset.Spot,
time.Time{}, time.Time{},
kline.OneMin)
if err != nil {
start := time.Now().AddDate(0, 0, -1)
_, err = b.GetHistoricCandlesExtended(context.Background(), pair, asset.Spot, kline.OneMin, start, time.Now())
if !errors.Is(err, common.ErrNotYetImplemented) {
t.Fatal(err)
}
}

View File

@@ -129,22 +129,15 @@ func (b *BTSE) 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,
},
Intervals: kline.DeployExchangeIntervals(
kline.OneMin,
kline.FiveMin,
kline.FifteenMin,
kline.ThirtyMin,
kline.OneHour,
kline.SixHour,
kline.OneDay,
),
ResultLimit: 300,
},
},
@@ -977,111 +970,51 @@ func (b *BTSE) FormatExchangeKlineInterval(in kline.Interval) string {
}
// GetHistoricCandles returns candles between a time period for a set time interval
func (b *BTSE) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
fPair, err := b.FormatExchangeCurrency(pair, a)
func (b *BTSE) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
req, err := b.GetKlineRequest(pair, a, interval, start, end)
if err != nil {
return kline.Item{}, err
return nil, err
}
intervalInt, err := strconv.Atoi(b.FormatExchangeKlineInterval(interval))
intervalInt, err := strconv.Atoi(b.FormatExchangeKlineInterval(req.ExchangeInterval))
if err != nil {
return kline.Item{}, err
return nil, err
}
klineRet := kline.Item{
Exchange: b.Name,
Pair: fPair,
Asset: a,
Interval: interval,
}
switch a {
var timeSeries []kline.Candle
switch req.Asset {
case asset.Spot:
req, err := b.OHLCV(ctx,
fPair.String(),
start,
end,
req.RequestFormatted.String(),
req.Start,
req.End,
intervalInt)
if err != nil {
return kline.Item{}, err
return nil, err
}
timeSeries = make([]kline.Candle, len(req))
for x := range req {
klineRet.Candles = append(klineRet.Candles, kline.Candle{
timeSeries[x] = kline.Candle{
Time: time.Unix(int64(req[x][0]), 0),
Open: req[x][1],
High: req[x][2],
Low: req[x][3],
Close: req[x][4],
Volume: req[x][5],
})
}
}
case asset.Futures:
return kline.Item{}, common.ErrNotYetImplemented
return nil, common.ErrNotYetImplemented
default:
return kline.Item{}, fmt.Errorf("asset %v not supported", a.String())
return nil, fmt.Errorf("asset %s not supported", req.Asset)
}
klineRet.SortCandlesByTimestamp(false)
return klineRet, nil
return req.ProcessResponse(timeSeries)
}
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (b *BTSE) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
if kline.TotalCandlesPerInterval(start, end, interval) > float64(b.Features.Enabled.Kline.ResultLimit) {
return kline.Item{}, errors.New(kline.ErrRequestExceedsExchangeLimits)
}
fPair, err := b.FormatExchangeCurrency(pair, a)
if err != nil {
return kline.Item{}, err
}
intervalInt, err := strconv.Atoi(b.FormatExchangeKlineInterval(interval))
if err != nil {
return kline.Item{}, err
}
klineRet := kline.Item{
Exchange: b.Name,
Pair: fPair,
Asset: a,
Interval: interval,
}
switch a {
case asset.Spot:
req, err := b.OHLCV(ctx,
fPair.String(),
start,
end,
intervalInt)
if err != nil {
return kline.Item{}, err
}
for x := range req {
klineRet.Candles = append(klineRet.Candles, kline.Candle{
Time: time.Unix(int64(req[x][0]), 0),
Open: req[x][1],
High: req[x][2],
Low: req[x][3],
Close: req[x][4],
Volume: req[x][5],
})
}
case asset.Futures:
return kline.Item{}, common.ErrNotYetImplemented
default:
return kline.Item{}, fmt.Errorf("asset %v not supported", a.String())
}
klineRet.SortCandlesByTimestamp(false)
return klineRet, nil
func (b *BTSE) GetHistoricCandlesExtended(_ context.Context, _ currency.Pair, _ asset.Item, _ kline.Interval, _, _ time.Time) (*kline.Item, error) {
return nil, common.ErrNotYetImplemented
}
func (b *BTSE) seedOrderSizeLimits(ctx context.Context) error {