Files
gocryptotrader/backtester/data/kline/api/api.go
Ryan O'Hara-Reid d636049fb2 exchanges: Initial context propagation (#744)
* gct: phase one context awareness pass

* exchanges: context propagation pass

* common/requester: force context requirement

* gctcli/exchanges: linter fix

* rpcserver: fix test using dummy rpc server

* backtester: fix comments

* grpc: add correct cancel and timeout for commands

* rpcserver_test: add comment on dummy server

* common: deprecated SendHTTPGetRequest

* linter: fix

* linter: turn on no context check

* apichecker: fix context linter issue

* binance: use param context

* common: remove checks as this gets executed before main

* common: change mutex to RW as clients can be used by multiple go routines.

* common: remove init and JIT default client. Unexport global variables and add protection.

* common: Add comments

* bithumb: after dinner mints fix
2021-09-11 13:52:07 +10:00

54 lines
1.7 KiB
Go

package api
import (
"context"
"fmt"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/backtester/common"
"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/trade"
)
// LoadData retrieves data from a GoCryptoTrader exchange wrapper which calls the exchange's API
func LoadData(ctx context.Context, dataType int64, startDate, endDate time.Time, interval time.Duration, exch exchange.IBotExchange, fPair currency.Pair, a asset.Item) (*kline.Item, error) {
var candles kline.Item
var err error
switch dataType {
case common.DataCandle:
candles, err = exch.GetHistoricCandlesExtended(ctx,
fPair,
a,
startDate,
endDate,
kline.Interval(interval))
if err != nil {
return nil, fmt.Errorf("could not retrieve candle data for %v %v %v, %v", exch.GetName(), a, fPair, err)
}
case common.DataTrade:
var trades []trade.Data
trades, err = exch.GetHistoricTrades(ctx,
fPair,
a,
startDate,
endDate)
if err != nil {
return nil, fmt.Errorf("could not retrieve trade data for %v %v %v, %v", exch.GetName(), a, fPair, err)
}
candles, err = trade.ConvertTradesToCandles(kline.Interval(interval), trades...)
if err != nil {
return nil, fmt.Errorf("could not convert trade data to candles for %v %v %v, %v", exch.GetName(), a, fPair, err)
}
default:
return nil, fmt.Errorf("could not retrieve data for %v %v %v, %w", exch.GetName(), a, fPair, common.ErrInvalidDataType)
}
candles.Exchange = strings.ToLower(candles.Exchange)
return &candles, nil
}