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:
@@ -704,29 +704,27 @@ func TestBitstamp_OHLC(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBitstamp_GetHistoricCandles(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSD")
|
||||
pair, err := currency.NewPairFromString("BTCUSD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Unix(1546300800, 0)
|
||||
end := time.Unix(1577836799, 0)
|
||||
|
||||
_, err = b.GetHistoricCandles(context.Background(),
|
||||
currencyPair, asset.Spot, start, end, kline.OneDay)
|
||||
_, err = b.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.OneDay, start, end)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBitstamp_GetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSD")
|
||||
pair, err := currency.NewPairFromString("BTCUSD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Unix(1546300800, 0)
|
||||
end := time.Unix(1577836799, 0)
|
||||
_, err = b.GetHistoricCandlesExtended(context.Background(),
|
||||
currencyPair, asset.Spot, start, end, kline.OneDay)
|
||||
|
||||
_, err = b.GetHistoricCandlesExtended(context.Background(), pair, asset.Spot, kline.OneDay, start, end)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -110,20 +110,20 @@ func (b *Bitstamp) 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,
|
||||
},
|
||||
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,
|
||||
),
|
||||
ResultLimit: 1000,
|
||||
},
|
||||
},
|
||||
@@ -853,42 +853,30 @@ func (b *Bitstamp) ValidateCredentials(ctx context.Context, assetType asset.Item
|
||||
}
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (b *Bitstamp) 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
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
Exchange: b.Name,
|
||||
Pair: pair,
|
||||
Asset: a,
|
||||
Interval: interval,
|
||||
}
|
||||
|
||||
formattedPair, err := b.FormatExchangeCurrency(pair, a)
|
||||
func (b *Bitstamp) 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
|
||||
}
|
||||
|
||||
candles, err := b.OHLC(ctx,
|
||||
formattedPair.Lower().String(),
|
||||
start,
|
||||
end,
|
||||
b.FormatExchangeKlineInterval(interval),
|
||||
strconv.FormatInt(int64(b.Features.Enabled.Kline.ResultLimit), 10),
|
||||
)
|
||||
|
||||
req.RequestFormatted.String(),
|
||||
req.Start,
|
||||
req.End,
|
||||
b.FormatExchangeKlineInterval(req.ExchangeInterval),
|
||||
strconv.FormatInt(int64(b.Features.Enabled.Kline.ResultLimit), 10))
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
timeSeries := make([]kline.Candle, 0, len(candles.Data.OHLCV))
|
||||
for x := range candles.Data.OHLCV {
|
||||
if time.Unix(candles.Data.OHLCV[x].Timestamp, 0).Before(start) ||
|
||||
time.Unix(candles.Data.OHLCV[x].Timestamp, 0).After(end) {
|
||||
timestamp := time.Unix(candles.Data.OHLCV[x].Timestamp, 0)
|
||||
if timestamp.Before(req.Start) || timestamp.After(req.End) {
|
||||
continue
|
||||
}
|
||||
ret.Candles = append(ret.Candles, kline.Candle{
|
||||
Time: time.Unix(candles.Data.OHLCV[x].Timestamp, 0),
|
||||
timeSeries = append(timeSeries, kline.Candle{
|
||||
Time: timestamp,
|
||||
Open: candles.Data.OHLCV[x].Open,
|
||||
High: candles.Data.OHLCV[x].High,
|
||||
Low: candles.Data.OHLCV[x].Low,
|
||||
@@ -896,52 +884,37 @@ func (b *Bitstamp) GetHistoricCandles(ctx context.Context, pair currency.Pair, a
|
||||
Volume: candles.Data.OHLCV[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 *Bitstamp) 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)
|
||||
func (b *Bitstamp) 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
|
||||
}
|
||||
formattedPair, err := b.FormatExchangeCurrency(pair, a)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for x := range dates.Ranges {
|
||||
timeSeries := make([]kline.Candle, 0, req.Size())
|
||||
for x := range req.Ranges {
|
||||
var candles OHLCResponse
|
||||
candles, err = b.OHLC(ctx,
|
||||
formattedPair.Lower().String(),
|
||||
dates.Ranges[x].Start.Time,
|
||||
dates.Ranges[x].End.Time,
|
||||
b.FormatExchangeKlineInterval(interval),
|
||||
req.RequestFormatted.String(),
|
||||
req.Ranges[x].Start.Time,
|
||||
req.Ranges[x].End.Time,
|
||||
b.FormatExchangeKlineInterval(req.ExchangeInterval),
|
||||
strconv.FormatInt(int64(b.Features.Enabled.Kline.ResultLimit), 10),
|
||||
)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range candles.Data.OHLCV {
|
||||
if time.Unix(candles.Data.OHLCV[i].Timestamp, 0).Before(start) ||
|
||||
time.Unix(candles.Data.OHLCV[i].Timestamp, 0).After(end) {
|
||||
timstamp := time.Unix(candles.Data.OHLCV[i].Timestamp, 0)
|
||||
if timstamp.Before(req.Ranges[x].Start.Time) ||
|
||||
timstamp.After(req.Ranges[x].End.Time) {
|
||||
continue
|
||||
}
|
||||
ret.Candles = append(ret.Candles, kline.Candle{
|
||||
timeSeries = append(timeSeries, kline.Candle{
|
||||
Time: time.Unix(candles.Data.OHLCV[i].Timestamp, 0),
|
||||
Open: candles.Data.OHLCV[i].Open,
|
||||
High: candles.Data.OHLCV[i].High,
|
||||
@@ -951,13 +924,5 @@ func (b *Bitstamp) 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user