mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-08 15:11:07 +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:
@@ -113,7 +113,7 @@ func TestGetHistoricRatesGranularityCheck(t *testing.T) {
|
||||
end := time.Now()
|
||||
start := end.Add(-time.Hour * 2)
|
||||
_, err := c.GetHistoricCandles(context.Background(),
|
||||
testPair, asset.Spot, start, end, kline.OneHour)
|
||||
testPair, asset.Spot, kline.OneHour, start, end)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -124,7 +124,7 @@ func TestCoinbasePro_GetHistoricCandlesExtended(t *testing.T) {
|
||||
end := time.Unix(1577836799, 0)
|
||||
|
||||
_, err := c.GetHistoricCandlesExtended(context.Background(),
|
||||
testPair, asset.Spot, start, end, kline.OneDay)
|
||||
testPair, asset.Spot, kline.OneDay, start, end)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1053,67 +1053,6 @@ func TestParseTime(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckInterval(t *testing.T) {
|
||||
interval := time.Minute
|
||||
i, err := checkInterval(interval)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if i != 60 {
|
||||
t.Fatal("incorrect return")
|
||||
}
|
||||
interval = time.Minute * 5
|
||||
i, err = checkInterval(interval)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if i != 300 {
|
||||
t.Fatal("incorrect return")
|
||||
}
|
||||
|
||||
interval = time.Minute * 15
|
||||
i, err = checkInterval(interval)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if i != 900 {
|
||||
t.Fatal("incorrect return")
|
||||
}
|
||||
|
||||
interval = time.Hour
|
||||
i, err = checkInterval(interval)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if i != 3600 {
|
||||
t.Fatal("incorrect return")
|
||||
}
|
||||
|
||||
interval = time.Hour * 6
|
||||
i, err = checkInterval(interval)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if i != 21600 {
|
||||
t.Fatal("incorrect return")
|
||||
}
|
||||
|
||||
interval = time.Hour * 24
|
||||
i, err = checkInterval(interval)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if i != 86400 {
|
||||
t.Fatal("incorrect return")
|
||||
}
|
||||
|
||||
interval = time.Hour * 1337
|
||||
_, err = checkInterval(interval)
|
||||
if err == nil {
|
||||
t.Fatal("error cannot be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRecentTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := c.GetRecentTrades(context.Background(), testPair, asset.Spot)
|
||||
|
||||
@@ -117,14 +117,14 @@ func (c *CoinbasePro) SetDefaults() {
|
||||
Enabled: exchange.FeaturesEnabled{
|
||||
AutoPairUpdates: true,
|
||||
Kline: kline.ExchangeCapabilitiesEnabled{
|
||||
Intervals: map[string]bool{
|
||||
kline.OneMin.Word(): true,
|
||||
kline.FiveMin.Word(): true,
|
||||
kline.FifteenMin.Word(): true,
|
||||
kline.OneHour.Word(): true,
|
||||
kline.SixHour.Word(): true,
|
||||
kline.OneDay.Word(): true,
|
||||
},
|
||||
Intervals: kline.DeployExchangeIntervals(
|
||||
kline.OneMin,
|
||||
kline.FiveMin,
|
||||
kline.FifteenMin,
|
||||
kline.OneHour,
|
||||
kline.SixHour,
|
||||
kline.OneDay,
|
||||
),
|
||||
ResultLimit: 300,
|
||||
},
|
||||
},
|
||||
@@ -887,65 +887,26 @@ func (c *CoinbasePro) GetOrderHistory(ctx context.Context, req *order.GetOrdersR
|
||||
return req.Filter(c.Name, orders), nil
|
||||
}
|
||||
|
||||
// checkInterval checks allowable interval
|
||||
func checkInterval(i time.Duration) (int64, error) {
|
||||
switch i.Seconds() {
|
||||
case 60:
|
||||
return 60, nil
|
||||
case 300:
|
||||
return 300, nil
|
||||
case 900:
|
||||
return 900, nil
|
||||
case 3600:
|
||||
return 3600, nil
|
||||
case 21600:
|
||||
return 21600, nil
|
||||
case 86400:
|
||||
return 86400, nil
|
||||
}
|
||||
return 0, fmt.Errorf("interval not allowed %v", i.Seconds())
|
||||
}
|
||||
|
||||
// GetHistoricCandles returns a set of candle between two time periods for a
|
||||
// designated time period
|
||||
func (c *CoinbasePro) GetHistoricCandles(ctx context.Context, p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if err := c.ValidateKline(p, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
if kline.TotalCandlesPerInterval(start, end, interval) > float64(c.Features.Enabled.Kline.ResultLimit) {
|
||||
return kline.Item{}, errors.New(kline.ErrRequestExceedsExchangeLimits)
|
||||
}
|
||||
|
||||
gran, err := strconv.ParseInt(c.FormatExchangeKlineInterval(interval), 10, 64)
|
||||
func (c *CoinbasePro) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
|
||||
req, err := c.GetKlineRequest(pair, a, interval, start, end)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
formatP, err := c.FormatExchangeCurrency(p, a)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
history, err := c.GetHistoricRates(ctx,
|
||||
formatP.String(),
|
||||
req.RequestFormatted.String(),
|
||||
start.Format(time.RFC3339),
|
||||
end.Format(time.RFC3339),
|
||||
gran)
|
||||
int64(req.ExchangeInterval.Duration().Seconds()))
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
candles := kline.Item{
|
||||
Exchange: c.Name,
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Interval: interval,
|
||||
Candles: make([]kline.Candle, len(history)),
|
||||
return nil, err
|
||||
}
|
||||
|
||||
timeSeries := make([]kline.Candle, len(history))
|
||||
for x := range history {
|
||||
candles.Candles[x] = kline.Candle{
|
||||
timeSeries[x] = kline.Candle{
|
||||
Time: history[x].Time,
|
||||
Low: history[x].Low,
|
||||
High: history[x].High,
|
||||
@@ -954,51 +915,30 @@ func (c *CoinbasePro) GetHistoricCandles(ctx context.Context, p currency.Pair, a
|
||||
Volume: history[x].Volume,
|
||||
}
|
||||
}
|
||||
|
||||
candles.SortCandlesByTimestamp(false)
|
||||
return candles, nil
|
||||
return req.ProcessResponse(timeSeries)
|
||||
}
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (c *CoinbasePro) GetHistoricCandlesExtended(ctx context.Context, p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if err := c.ValidateKline(p, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
gran, err := strconv.ParseInt(c.FormatExchangeKlineInterval(interval), 10, 64)
|
||||
func (c *CoinbasePro) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
|
||||
req, err := c.GetKlineExtendedRequest(pair, a, interval, start, end)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
dates, err := kline.CalculateCandleDateRanges(start, end, interval, c.Features.Enabled.Kline.ResultLimit)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
formattedPair, err := c.FormatExchangeCurrency(p, a)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
Exchange: c.Name,
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Interval: interval,
|
||||
}
|
||||
|
||||
for x := range dates.Ranges {
|
||||
timeSeries := make([]kline.Candle, 0, req.Size())
|
||||
for x := range req.Ranges {
|
||||
var history []History
|
||||
history, err = c.GetHistoricRates(ctx,
|
||||
formattedPair.String(),
|
||||
dates.Ranges[x].Start.Time.Format(time.RFC3339),
|
||||
dates.Ranges[x].End.Time.Format(time.RFC3339),
|
||||
gran)
|
||||
req.RequestFormatted.String(),
|
||||
req.Ranges[x].Start.Time.Format(time.RFC3339),
|
||||
req.Ranges[x].End.Time.Format(time.RFC3339),
|
||||
int64(req.ExchangeInterval.Duration().Seconds()))
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range history {
|
||||
ret.Candles = append(ret.Candles, kline.Candle{
|
||||
timeSeries = append(timeSeries, kline.Candle{
|
||||
Time: history[i].Time,
|
||||
Low: history[i].Low,
|
||||
High: history[i].High,
|
||||
@@ -1008,15 +948,7 @@ func (c *CoinbasePro) GetHistoricCandlesExtended(ctx context.Context, p currency
|
||||
})
|
||||
}
|
||||
}
|
||||
dates.SetHasDataFromCandles(ret.Candles)
|
||||
summary := dates.DataSummary(false)
|
||||
if len(summary) > 0 {
|
||||
log.Warnf(log.ExchangeSys, "%v - %v", c.Name, summary)
|
||||
}
|
||||
ret.RemoveDuplicateCandlesByTime()
|
||||
ret.RemoveOutsideRange(start, end)
|
||||
ret.SortCandlesByTimestamp(false)
|
||||
return ret, nil
|
||||
return req.ProcessResponse(timeSeries)
|
||||
}
|
||||
|
||||
// ValidateCredentials validates current credentials used for wrapper
|
||||
|
||||
Reference in New Issue
Block a user