mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-03 07:26:45 +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:
@@ -206,7 +206,7 @@ func (g *Gateio) GetOrderbook(ctx context.Context, symbol string) (*Orderbook, e
|
||||
}
|
||||
|
||||
// GetSpotKline returns kline data for the most recent time period
|
||||
func (g *Gateio) GetSpotKline(ctx context.Context, arg KlinesRequestParams) (kline.Item, error) {
|
||||
func (g *Gateio) GetSpotKline(ctx context.Context, arg KlinesRequestParams) ([]kline.Candle, error) {
|
||||
urlPath := fmt.Sprintf("/%s/%s/%s?group_sec=%s&range_hour=%d",
|
||||
gateioAPIVersion,
|
||||
gateioKline,
|
||||
@@ -220,58 +220,55 @@ func (g *Gateio) GetSpotKline(ctx context.Context, arg KlinesRequestParams) (kli
|
||||
}{}
|
||||
|
||||
if err := g.SendHTTPRequest(ctx, exchange.RestSpotSupplementary, urlPath, &resp); err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
if resp.Result != "true" || len(resp.Data) == 0 {
|
||||
return kline.Item{}, errors.New("rawKlines unexpected data returned")
|
||||
}
|
||||
|
||||
result := kline.Item{
|
||||
Exchange: g.Name,
|
||||
return nil, errors.New("rawKlines unexpected data returned")
|
||||
}
|
||||
|
||||
timeSeries := make([]kline.Candle, len(resp.Data))
|
||||
for x := range resp.Data {
|
||||
if len(resp.Data[x]) < 6 {
|
||||
return kline.Item{}, fmt.Errorf("unexpected kline data length")
|
||||
return nil, fmt.Errorf("unexpected kline data length")
|
||||
}
|
||||
otString, err := strconv.ParseFloat(resp.Data[x][0], 64)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
ot, err := convert.TimeFromUnixTimestampFloat(otString)
|
||||
if err != nil {
|
||||
return kline.Item{}, fmt.Errorf("cannot parse Kline.OpenTime. Err: %s", err)
|
||||
return nil, fmt.Errorf("cannot parse Kline.OpenTime. Err: %s", err)
|
||||
}
|
||||
_vol, err := convert.FloatFromString(resp.Data[x][1])
|
||||
if err != nil {
|
||||
return kline.Item{}, fmt.Errorf("cannot parse Kline.Volume. Err: %s", err)
|
||||
return nil, fmt.Errorf("cannot parse Kline.Volume. Err: %s", err)
|
||||
}
|
||||
_close, err := convert.FloatFromString(resp.Data[x][2])
|
||||
if err != nil {
|
||||
return kline.Item{}, fmt.Errorf("cannot parse Kline.Close. Err: %s", err)
|
||||
return nil, fmt.Errorf("cannot parse Kline.Close. Err: %s", err)
|
||||
}
|
||||
_high, err := convert.FloatFromString(resp.Data[x][3])
|
||||
if err != nil {
|
||||
return kline.Item{}, fmt.Errorf("cannot parse Kline.High. Err: %s", err)
|
||||
return nil, fmt.Errorf("cannot parse Kline.High. Err: %s", err)
|
||||
}
|
||||
_low, err := convert.FloatFromString(resp.Data[x][4])
|
||||
if err != nil {
|
||||
return kline.Item{}, fmt.Errorf("cannot parse Kline.Low. Err: %s", err)
|
||||
return nil, fmt.Errorf("cannot parse Kline.Low. Err: %s", err)
|
||||
}
|
||||
_open, err := convert.FloatFromString(resp.Data[x][5])
|
||||
if err != nil {
|
||||
return kline.Item{}, fmt.Errorf("cannot parse Kline.Open. Err: %s", err)
|
||||
return nil, fmt.Errorf("cannot parse Kline.Open. Err: %s", err)
|
||||
}
|
||||
result.Candles = append(result.Candles, kline.Candle{
|
||||
timeSeries[x] = kline.Candle{
|
||||
Time: ot,
|
||||
Volume: _vol,
|
||||
Close: _close,
|
||||
High: _high,
|
||||
Low: _low,
|
||||
Open: _open,
|
||||
})
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
return timeSeries, nil
|
||||
}
|
||||
|
||||
// GetBalances obtains the users account balance
|
||||
|
||||
@@ -765,27 +765,27 @@ func TestParseTime(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.Now().Add(-time.Hour * 6)
|
||||
_, err = g.GetHistoricCandles(context.Background(),
|
||||
currencyPair, asset.Spot, startTime, time.Now(), kline.OneMin)
|
||||
_, err = g.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.OneMin, startTime, time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
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.Now().Add(-time.Minute * 2)
|
||||
_, err = g.GetHistoricCandlesExtended(context.Background(),
|
||||
currencyPair, asset.Spot, startTime, time.Now(), kline.OneMin)
|
||||
if err != nil {
|
||||
startTime := time.Now().Add(-time.Hour * 2)
|
||||
_, err = g.GetHistoricCandlesExtended(context.Background(), pair, asset.Spot, kline.OneMin, startTime, time.Now())
|
||||
if !errors.Is(err, common.ErrNotYetImplemented) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,19 +114,20 @@ func (g *Gateio) 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,
|
||||
},
|
||||
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,
|
||||
),
|
||||
ResultLimit: 1001,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -899,39 +900,26 @@ func (g *Gateio) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
}
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (g *Gateio) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if err := g.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
hours := time.Since(start).Hours()
|
||||
formattedPair, err := g.FormatExchangeCurrency(pair, a)
|
||||
func (g *Gateio) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
|
||||
req, err := g.GetKlineRequest(pair, a, interval, start, end)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := KlinesRequestParams{
|
||||
Symbol: formattedPair.String(),
|
||||
GroupSec: g.FormatExchangeKlineInterval(interval),
|
||||
HourSize: int(hours),
|
||||
}
|
||||
|
||||
klineData, err := g.GetSpotKline(ctx, params)
|
||||
timeSeries, err := g.GetSpotKline(ctx, KlinesRequestParams{
|
||||
Symbol: req.RequestFormatted.String(),
|
||||
GroupSec: g.FormatExchangeKlineInterval(req.ExchangeInterval),
|
||||
HourSize: int(time.Since(req.Start).Hours()),
|
||||
})
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
return nil, err
|
||||
}
|
||||
klineData.Interval = interval
|
||||
klineData.Pair = pair
|
||||
klineData.Asset = a
|
||||
|
||||
klineData.SortCandlesByTimestamp(false)
|
||||
klineData.RemoveOutsideRange(start, end)
|
||||
return klineData, nil
|
||||
return req.ProcessResponse(timeSeries)
|
||||
}
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (g *Gateio) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
return g.GetHistoricCandles(ctx, pair, a, start, end, interval)
|
||||
func (g *Gateio) GetHistoricCandlesExtended(_ context.Context, _ currency.Pair, _ asset.Item, _ kline.Interval, _, _ time.Time) (*kline.Item, error) {
|
||||
return nil, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// GetAvailableTransferChains returns the available transfer blockchains for the specific
|
||||
|
||||
Reference in New Issue
Block a user