exchanges: add setTimeWindow boolean to GetKlineRequest param (#1160)

* exchanges: add setTimeWindow boolean to GetKlineRequest params to differentiate between a set time period return from endpoint.

* glorious: nits

* exchange: conjugation

* Update exchanges/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits and an assortment of differences

* exchanges: remove some comments

* glorious: nits

* cleanup

* tests: fix

* Update exchanges/hitbtc/hitbtc_wrapper.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/kline/kline.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/kline/kline_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* kline: fix test

* rm unused variables

* almost: nits

* glorious: nits

* linter: fix

* rm unused variable

* Refactored comment in the okex tests to ensure that it accurately reflects the variable name and the issue related to the time window, as requested by GloriousCode. The previous comment did not align with the identifier assigned to the property, which could cause confusion and misunderstanding among other programmers or stakeholders. The updated comment will improve the clarity and readability of the codebase and make it easier to understand the intended purpose of the associated variables. The change was made with the aim of improving the overall quality and maintainability of the code.

---------

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-04-27 10:10:19 +10:00
committed by GitHub
parent 668d083749
commit 42475bf2b8
36 changed files with 809 additions and 471 deletions

View File

@@ -206,7 +206,7 @@ func (h *HitBTC) GetCandles(ctx context.Context, currencyPair, limit, period str
}
var resp []ChartData
path := fmt.Sprintf("/%s/%s?%s", apiV2Candles, currencyPair, vals.Encode())
path := "/" + apiV2Candles + "/" + currencyPair + "?" + vals.Encode()
return resp, h.SendHTTPRequest(ctx, exchange.RestSpot, path, &resp)
}

View File

@@ -116,7 +116,6 @@ func TestGetHistoricCandlesExtended(t *testing.T) {
}
startTime := time.Unix(1546300800, 0)
end := time.Unix(1577836799, 0)
_, err = h.GetHistoricCandlesExtended(context.Background(), pair, asset.Spot, kline.OneHour, startTime, end)
if err != nil {
t.Fatal(err)
@@ -979,6 +978,7 @@ func TestWsTrades(t *testing.T) {
}
func Test_FormatExchangeKlineInterval(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
interval kline.Interval
@@ -1000,18 +1000,20 @@ func Test_FormatExchangeKlineInterval(t *testing.T) {
"D7",
},
{
"AllOther",
"OneMonth",
kline.OneMonth,
"",
"1M",
},
}
for x := range testCases {
test := testCases[x]
t.Run(test.name, func(t *testing.T) {
ret := h.FormatExchangeKlineInterval(test.interval)
t.Parallel()
ret, err := h.FormatExchangeKlineInterval(test.interval)
if err != nil {
t.Fatal(err)
}
if ret != test.output {
t.Fatalf("unexpected result return expected: %v received: %v", test.output, ret)
}

View File

@@ -116,18 +116,18 @@ func (h *HitBTC) SetDefaults() {
AutoPairUpdates: true,
Kline: kline.ExchangeCapabilitiesEnabled{
Intervals: kline.DeployExchangeIntervals(
kline.OneMin,
kline.ThreeMin,
kline.FiveMin,
kline.FifteenMin,
kline.ThirtyMin,
kline.OneHour,
kline.FourHour,
kline.OneDay,
kline.SevenDay,
kline.OneMonth,
kline.IntervalCapacity{Interval: kline.OneMin},
kline.IntervalCapacity{Interval: kline.ThreeMin},
kline.IntervalCapacity{Interval: kline.FiveMin},
kline.IntervalCapacity{Interval: kline.FifteenMin},
kline.IntervalCapacity{Interval: kline.ThirtyMin},
kline.IntervalCapacity{Interval: kline.OneHour},
kline.IntervalCapacity{Interval: kline.FourHour},
kline.IntervalCapacity{Interval: kline.OneDay},
kline.IntervalCapacity{Interval: kline.SevenDay},
kline.IntervalCapacity{Interval: kline.OneMonth},
),
ResultLimit: 1000,
GlobalResultLimit: 1000,
},
},
}
@@ -850,30 +850,48 @@ func (h *HitBTC) ValidateCredentials(ctx context.Context, assetType asset.Item)
}
// FormatExchangeKlineInterval returns Interval to exchange formatted string
func (h *HitBTC) FormatExchangeKlineInterval(in kline.Interval) string {
func (h *HitBTC) FormatExchangeKlineInterval(in kline.Interval) (string, error) {
switch in {
case kline.OneMin, kline.ThreeMin,
kline.FiveMin, kline.FifteenMin, kline.ThirtyMin:
return "M" + in.Short()[:len(in.Short())-1]
case kline.OneMin:
return "M1", nil
case kline.ThreeMin:
return "M3", nil
case kline.FiveMin:
return "M5", nil
case kline.FifteenMin:
return "M15", nil
case kline.ThirtyMin:
return "M30", nil
case kline.OneHour:
return "H1", nil
case kline.FourHour:
return "H4", nil
case kline.OneDay:
return "D1"
case kline.SevenDay:
return "D7"
return "D1", nil
case kline.OneWeek:
return "D7", nil
case kline.OneMonth:
return "1M", nil
}
return ""
return "", fmt.Errorf("%w %v", kline.ErrInvalidInterval, in)
}
// GetHistoricCandles returns candles between a time period for a set time interval
func (h *HitBTC) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
req, err := h.GetKlineRequest(pair, a, interval, start, end)
req, err := h.GetKlineRequest(pair, a, interval, start, end, false)
if err != nil {
return nil, err
}
formattedInterval, err := h.FormatExchangeKlineInterval(req.ExchangeInterval)
if err != nil {
return nil, err
}
data, err := h.GetCandles(ctx,
req.RequestFormatted.String(),
strconv.FormatInt(int64(h.Features.Enabled.Kline.ResultLimit), 10),
h.FormatExchangeKlineInterval(req.ExchangeInterval),
strconv.FormatInt(req.RequestLimit, 10),
formattedInterval,
req.Start,
req.End)
if err != nil {
@@ -901,13 +919,18 @@ func (h *HitBTC) GetHistoricCandlesExtended(ctx context.Context, pair currency.P
return nil, err
}
formattedInterval, err := h.FormatExchangeKlineInterval(req.ExchangeInterval)
if err != nil {
return nil, err
}
timeSeries := make([]kline.Candle, 0, req.Size())
for y := range req.RangeHolder.Ranges {
var data []ChartData
data, err = h.GetCandles(ctx,
req.RequestFormatted.String(),
strconv.FormatInt(int64(h.Features.Enabled.Kline.ResultLimit), 10),
h.FormatExchangeKlineInterval(req.ExchangeInterval),
strconv.FormatInt(req.RequestLimit, 10),
formattedInterval,
req.RangeHolder.Ranges[y].Start.Time,
req.RangeHolder.Ranges[y].End.Time)
if err != nil {