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:
Ryan O'Hara-Reid
2023-01-17 16:22:33 +11:00
committed by GitHub
parent 72f36d70d1
commit 83cfefa45c
110 changed files with 11312 additions and 5768 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
@@ -55,21 +56,14 @@ func TestMain(m *testing.M) {
log.Fatal("Bittrex Setup values not set correctly")
}
os.Exit(m.Run())
}
func TestStart(t *testing.T) {
t.Parallel()
err := b.Start(nil)
if !errors.Is(err, common.ErrNilPointer) {
t.Fatalf("received: '%v' but expected: '%v'", err, common.ErrNilPointer)
}
var testWg sync.WaitGroup
err = b.Start(&testWg)
var wg sync.WaitGroup
err = b.Start(&wg)
if err != nil {
t.Fatal(err)
log.Fatal(err)
}
testWg.Wait()
wg.Wait()
os.Exit(m.Run())
}
func TestGetMarkets(t *testing.T) {
@@ -701,3 +695,30 @@ func TestGetHistoricTrades(t *testing.T) {
t.Fatal(err)
}
}
func TestGetHistoricCandles(t *testing.T) {
pair, err := currency.NewPairFromString("btc-usdt")
if err != nil {
t.Fatal(err)
}
start := time.Unix(1546300800, 0)
end := time.Unix(1577836799, 0)
_, err = b.GetHistoricCandles(context.Background(), pair, asset.Spot, kline.OneDay, start, end)
if err != nil {
t.Fatal(err)
}
}
func TestGetHistoricCandlesExtended(t *testing.T) {
pair, err := currency.NewPairFromString("btc-usdt")
if err != nil {
t.Fatal(err)
}
start := time.Unix(1546300800, 0)
end := time.Unix(1577836799, 0)
_, err = b.GetHistoricCandlesExtended(context.Background(), pair, asset.Spot, kline.OneDay, start, end)
if !errors.Is(err, common.ErrNotYetImplemented) {
t.Fatal(err)
}
}

View File

@@ -110,12 +110,13 @@ func (b *Bittrex) SetDefaults() {
Enabled: exchange.FeaturesEnabled{
AutoPairUpdates: true,
Kline: kline.ExchangeCapabilitiesEnabled{
Intervals: map[string]bool{
kline.OneMin.Word(): true,
kline.FiveMin.Word(): true,
kline.OneHour.Word(): true,
kline.OneDay.Word(): true,
},
Intervals: kline.DeployExchangeIntervals(
kline.OneMin,
kline.FiveMin,
kline.OneHour,
kline.OneDay,
),
ResultLimit: 1000,
},
},
}
@@ -951,50 +952,40 @@ func (b *Bittrex) FormatExchangeKlineInterval(in kline.Interval) string {
// - 1 day interval: candles for 366 days
// This implementation rounds returns candles up to the next interval or to the end
// time (whichever comes first)
func (b *Bittrex) 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
}
candleInterval := b.FormatExchangeKlineInterval(interval)
if candleInterval == "notfound" {
return kline.Item{}, errors.New("invalid interval")
}
formattedPair, err := b.FormatExchangeCurrency(pair, a)
func (b *Bittrex) 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
}
ret := kline.Item{
Exchange: b.Name,
Pair: pair,
Asset: a,
Interval: interval,
candleInterval := b.FormatExchangeKlineInterval(req.ExchangeInterval)
if candleInterval == "notfound" {
return nil, errors.New("invalid interval")
}
year, month, day := start.Date()
year, month, day := req.Start.Date()
curYear, curMonth, curDay := time.Now().Date()
getHistoric := false // nolint:ifshort,nolintlint // false positive and triggers only on Windows
getRecent := false // nolint:ifshort,nolintlint // false positive and triggers only on Windows
switch interval {
switch req.ExchangeInterval {
case kline.OneMin, kline.FiveMin:
if time.Since(start) > 24*time.Hour {
if time.Since(req.Start) > 24*time.Hour {
getHistoric = true
}
if year >= curYear && month >= curMonth && day >= curDay {
getRecent = true
}
case kline.OneHour:
if time.Since(start) > 31*24*time.Hour {
if time.Since(req.Start) > 31*24*time.Hour {
getHistoric = true
}
if year >= curYear && month >= curMonth {
getRecent = true
}
case kline.OneDay:
if time.Since(start) > 366*24*time.Hour {
if time.Since(req.Start) > 366*24*time.Hour {
getHistoric = true
}
if year >= curYear {
@@ -1006,36 +997,37 @@ func (b *Bittrex) GetHistoricCandles(ctx context.Context, pair currency.Pair, a
if getHistoric {
var historicData []CandleData
historicData, err = b.GetHistoricalCandles(ctx,
formattedPair.String(),
b.FormatExchangeKlineInterval(interval),
req.RequestFormatted.String(),
b.FormatExchangeKlineInterval(req.ExchangeInterval),
"TRADE",
year,
int(month),
day)
if err != nil {
return kline.Item{}, err
return nil, err
}
ohlcData = append(ohlcData, historicData...)
}
if getRecent {
var recentData []CandleData
recentData, err = b.GetRecentCandles(ctx,
formattedPair.String(),
b.FormatExchangeKlineInterval(interval),
req.RequestFormatted.String(),
b.FormatExchangeKlineInterval(req.ExchangeInterval),
"TRADE")
if err != nil {
return kline.Item{}, err
return nil, err
}
ohlcData = append(ohlcData, recentData...)
}
timeSeries := make([]kline.Candle, 0, len(ohlcData))
for x := range ohlcData {
timestamp := ohlcData[x].StartsAt
if timestamp.Before(start) || timestamp.After(end) {
if ohlcData[x].StartsAt.Before(req.Start) ||
ohlcData[x].StartsAt.After(req.End) {
continue
}
ret.Candles = append(ret.Candles, kline.Candle{
Time: timestamp,
timeSeries = append(timeSeries, kline.Candle{
Time: ohlcData[x].StartsAt,
Open: ohlcData[x].Open,
High: ohlcData[x].High,
Low: ohlcData[x].Low,
@@ -1043,12 +1035,10 @@ func (b *Bittrex) GetHistoricCandles(ctx context.Context, pair currency.Pair, a
Volume: ohlcData[x].Volume,
})
}
ret.SortCandlesByTimestamp(false)
ret.RemoveDuplicateCandlesByTime()
return ret, nil
return req.ProcessResponse(timeSeries)
}
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (b *Bittrex) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
return kline.Item{}, common.ErrNotYetImplemented
func (b *Bittrex) GetHistoricCandlesExtended(_ context.Context, _ currency.Pair, _ asset.Item, _ kline.Interval, _, _ time.Time) (*kline.Item, error) {
return nil, common.ErrNotYetImplemented
}