mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 07:26:53 +00:00
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:
@@ -2416,44 +2416,39 @@ func TestExecutionTypeToOrderStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandles(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTC-USDT")
|
||||
t.Parallel()
|
||||
pair, err := currency.NewPairFromString("BTC-USDT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
startTime := time.Unix(1546300800, 0)
|
||||
end := time.Unix(1577836799, 0)
|
||||
_, err = b.GetHistoricCandles(context.Background(),
|
||||
currencyPair, asset.Spot, startTime, end, kline.OneDay)
|
||||
|
||||
_, err = b.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.OneDay, startTime, end)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = b.GetHistoricCandles(context.Background(),
|
||||
currencyPair, asset.Spot, startTime, end, kline.Interval(time.Hour*7))
|
||||
if err == nil {
|
||||
t.Fatal("unexpected result")
|
||||
_, err = b.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.Interval(time.Hour*7), startTime, end)
|
||||
if !errors.Is(err, kline.ErrRequestExceedsExchangeLimits) {
|
||||
t.Fatalf("received: '%v', but expected: '%v'", err, kline.ErrRequestExceedsExchangeLimits)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTC-USDT")
|
||||
t.Parallel()
|
||||
pair, err := currency.NewPairFromString("BTC-USDT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
startTime := time.Date(2020, 9, 1, 0, 0, 0, 0, time.UTC)
|
||||
end := time.Date(2021, 2, 15, 0, 0, 0, 0, time.UTC)
|
||||
_, err = b.GetHistoricCandlesExtended(context.Background(),
|
||||
currencyPair, asset.Spot, startTime, end, kline.OneDay)
|
||||
|
||||
_, err = b.GetHistoricCandlesExtended(context.Background(), pair, asset.Spot, kline.OneDay, startTime, end)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = b.GetHistoricCandlesExtended(context.Background(),
|
||||
currencyPair, asset.Spot, startTime, end, kline.Interval(time.Hour*7))
|
||||
if err == nil {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinance_FormatExchangeKlineInterval(t *testing.T) {
|
||||
|
||||
@@ -166,23 +166,23 @@ func (b *Binance) 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.EightHour.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.ThreeMin,
|
||||
kline.FiveMin,
|
||||
kline.FifteenMin,
|
||||
kline.ThirtyMin,
|
||||
kline.OneHour,
|
||||
kline.TwoHour,
|
||||
kline.FourHour,
|
||||
kline.SixHour,
|
||||
kline.EightHour,
|
||||
kline.TwelveHour,
|
||||
kline.OneDay,
|
||||
kline.ThreeDay,
|
||||
kline.OneWeek,
|
||||
kline.OneMonth,
|
||||
),
|
||||
ResultLimit: 1000,
|
||||
},
|
||||
},
|
||||
@@ -1680,83 +1680,70 @@ func (b *Binance) FormatExchangeKlineInterval(interval kline.Interval) string {
|
||||
}
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (b *Binance) 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
|
||||
}
|
||||
if kline.TotalCandlesPerInterval(start, end, interval) > float64(b.Features.Enabled.Kline.ResultLimit) {
|
||||
return kline.Item{}, errors.New(kline.ErrRequestExceedsExchangeLimits)
|
||||
}
|
||||
req := KlinesRequestParams{
|
||||
Interval: b.FormatExchangeKlineInterval(interval),
|
||||
Symbol: pair,
|
||||
StartTime: start,
|
||||
EndTime: end,
|
||||
Limit: int(b.Features.Enabled.Kline.ResultLimit),
|
||||
}
|
||||
ret := kline.Item{
|
||||
Exchange: b.Name,
|
||||
Pair: pair,
|
||||
Asset: a,
|
||||
Interval: interval,
|
||||
func (b *Binance) 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 nil, err
|
||||
}
|
||||
|
||||
candles, err := b.GetSpotKline(ctx, &req)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
if a != asset.Spot {
|
||||
// TODO: Add support for other asset types.
|
||||
return nil, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
candles, err := b.GetSpotKline(ctx, &KlinesRequestParams{
|
||||
Interval: b.FormatExchangeKlineInterval(req.ExchangeInterval),
|
||||
Symbol: req.Pair,
|
||||
StartTime: req.Start,
|
||||
EndTime: req.End,
|
||||
Limit: int(b.Features.Enabled.Kline.ResultLimit),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
timeSeries := make([]kline.Candle, len(candles))
|
||||
for x := range candles {
|
||||
ret.Candles = append(ret.Candles, kline.Candle{
|
||||
timeSeries[x] = kline.Candle{
|
||||
Time: candles[x].OpenTime,
|
||||
Open: candles[x].Open,
|
||||
High: candles[x].High,
|
||||
Low: candles[x].Low,
|
||||
Close: candles[x].Close,
|
||||
Volume: candles[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 (b *Binance) 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
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
Exchange: b.Name,
|
||||
Pair: pair,
|
||||
Asset: a,
|
||||
Interval: interval,
|
||||
}
|
||||
dates, err := kline.CalculateCandleDateRanges(start, end, interval, b.Features.Enabled.Kline.ResultLimit)
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set
|
||||
// time interval
|
||||
func (b *Binance) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
|
||||
req, err := b.GetKlineExtendedRequest(pair, a, interval, start, end)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
var candles []CandleStick
|
||||
for x := range dates.Ranges {
|
||||
req := KlinesRequestParams{
|
||||
Interval: b.FormatExchangeKlineInterval(interval),
|
||||
Symbol: pair,
|
||||
StartTime: dates.Ranges[x].Start.Time,
|
||||
EndTime: dates.Ranges[x].End.Time,
|
||||
Limit: int(b.Features.Enabled.Kline.ResultLimit),
|
||||
}
|
||||
|
||||
candles, err = b.GetSpotKline(ctx, &req)
|
||||
if a != asset.Spot {
|
||||
// TODO: Add support for other asset types.
|
||||
return nil, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
timeSeries := make([]kline.Candle, 0, req.Size())
|
||||
for x := range req.Ranges {
|
||||
var candles []CandleStick
|
||||
candles, err = b.GetSpotKline(ctx, &KlinesRequestParams{
|
||||
Interval: b.FormatExchangeKlineInterval(req.ExchangeInterval),
|
||||
Symbol: req.Pair,
|
||||
StartTime: req.Ranges[x].Start.Time,
|
||||
EndTime: req.Ranges[x].End.Time,
|
||||
Limit: int(b.Features.Enabled.Kline.ResultLimit),
|
||||
})
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range candles {
|
||||
for j := range ret.Candles {
|
||||
if ret.Candles[j].Time.Equal(candles[i].OpenTime) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
ret.Candles = append(ret.Candles, kline.Candle{
|
||||
timeSeries = append(timeSeries, kline.Candle{
|
||||
Time: candles[i].OpenTime,
|
||||
Open: candles[i].Open,
|
||||
High: candles[i].High,
|
||||
@@ -1766,16 +1753,7 @@ func (b *Binance) GetHistoricCandlesExtended(ctx context.Context, pair currency.
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
dates.SetHasDataFromCandles(ret.Candles)
|
||||
summary := dates.DataSummary(false)
|
||||
if len(summary) > 0 {
|
||||
log.Warnf(log.ExchangeSys, "%v - %v", b.Name, summary)
|
||||
}
|
||||
ret.RemoveDuplicateCandlesByTime()
|
||||
ret.RemoveOutsideRange(start, end)
|
||||
ret.SortCandlesByTimestamp(false)
|
||||
return ret, nil
|
||||
return req.ProcessResponse(timeSeries)
|
||||
}
|
||||
|
||||
func compatibleOrderVars(side, status, orderType string) OrderVars {
|
||||
|
||||
Reference in New Issue
Block a user