mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 07:26:53 +00:00
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
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -14,12 +15,12 @@ import (
|
||||
)
|
||||
|
||||
// LoadData retrieves data from a GoCryptoTrader exchange wrapper which calls the exchange's API
|
||||
func LoadData(dataType int64, startDate, endDate time.Time, interval time.Duration, exch exchange.IBotExchange, fPair currency.Pair, a asset.Item) (*kline.Item, error) {
|
||||
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(
|
||||
candles, err = exch.GetHistoricCandlesExtended(ctx,
|
||||
fPair,
|
||||
a,
|
||||
startDate,
|
||||
@@ -30,7 +31,7 @@ func LoadData(dataType int64, startDate, endDate time.Time, interval time.Durati
|
||||
}
|
||||
case common.DataTrade:
|
||||
var trades []trade.Data
|
||||
trades, err = exch.GetHistoricTrades(
|
||||
trades, err = exch.GetHistoricTrades(ctx,
|
||||
fPair,
|
||||
a,
|
||||
startDate,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -37,7 +38,8 @@ func TestLoadCandles(t *testing.T) {
|
||||
interval := gctkline.OneMin
|
||||
a := asset.Spot
|
||||
var data *gctkline.Item
|
||||
data, err = LoadData(common.DataCandle, tt1, tt2, interval.Duration(), exch, cp, a)
|
||||
data, err = LoadData(context.Background(),
|
||||
common.DataCandle, tt1, tt2, interval.Duration(), exch, cp, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -45,7 +47,8 @@ func TestLoadCandles(t *testing.T) {
|
||||
t.Error("expected candles")
|
||||
}
|
||||
|
||||
_, err = LoadData(-1, tt1, tt2, interval.Duration(), exch, cp, a)
|
||||
_, err = LoadData(context.Background(),
|
||||
-1, tt1, tt2, interval.Duration(), exch, cp, a)
|
||||
if !errors.Is(err, common.ErrInvalidDataType) {
|
||||
t.Errorf("expected '%v' received '%v'", err, common.ErrInvalidDataType)
|
||||
}
|
||||
@@ -73,7 +76,8 @@ func TestLoadTrades(t *testing.T) {
|
||||
tt2 := time.Now().Round(interval.Duration())
|
||||
a := asset.Spot
|
||||
var data *gctkline.Item
|
||||
data, err = LoadData(common.DataTrade, tt1, tt2, interval.Duration(), exch, cp, a)
|
||||
data, err = LoadData(context.Background(),
|
||||
common.DataTrade, tt1, tt2, interval.Duration(), exch, cp, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -14,12 +15,12 @@ import (
|
||||
)
|
||||
|
||||
// LoadData retrieves data from a GoCryptoTrader exchange wrapper which calls the exchange's API for the latest interval
|
||||
func LoadData(exch exchange.IBotExchange, dataType int64, interval time.Duration, fPair currency.Pair, a asset.Item) (*kline.Item, error) {
|
||||
func LoadData(ctx context.Context, exch exchange.IBotExchange, dataType int64, interval time.Duration, fPair currency.Pair, a asset.Item) (*kline.Item, error) {
|
||||
var candles kline.Item
|
||||
var err error
|
||||
switch dataType {
|
||||
case common.DataCandle:
|
||||
candles, err = exch.GetHistoricCandles(
|
||||
candles, err = exch.GetHistoricCandles(ctx,
|
||||
fPair,
|
||||
a,
|
||||
time.Now().Add(-interval*2), // multiplied by 2 to ensure the latest candle is always included
|
||||
@@ -30,7 +31,11 @@ func LoadData(exch exchange.IBotExchange, dataType int64, interval time.Duration
|
||||
}
|
||||
case common.DataTrade:
|
||||
var trades []trade.Data
|
||||
trades, err = exch.GetHistoricTrades(fPair, a, time.Now().Add(-interval*2), time.Now()) // multiplied by 2 to ensure the latest candle is always included
|
||||
trades, err = exch.GetHistoricTrades(ctx,
|
||||
fPair,
|
||||
a,
|
||||
time.Now().Add(-interval*2), // multiplied by 2 to ensure the latest candle is always included
|
||||
time.Now())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -41,10 +46,10 @@ func LoadData(exch exchange.IBotExchange, dataType int64, interval time.Duration
|
||||
}
|
||||
base := exch.GetBase()
|
||||
if len(candles.Candles) <= 1 && base.GetSupportedFeatures().RESTCapabilities.TradeHistory {
|
||||
trades, err = exch.GetHistoricTrades(
|
||||
trades, err = exch.GetHistoricTrades(ctx,
|
||||
fPair,
|
||||
a,
|
||||
time.Now().Add(-interval), // multiplied by 2 to ensure the latest candle is always included
|
||||
time.Now().Add(-interval),
|
||||
time.Now())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve live trade data for %v %v %v, %v", exch.GetName(), a, fPair, err)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
@@ -36,14 +37,15 @@ func TestLoadCandles(t *testing.T) {
|
||||
ConfigFormat: pFormat,
|
||||
}
|
||||
var data *gctkline.Item
|
||||
data, err = LoadData(exch, common.DataCandle, interval.Duration(), cp1, a)
|
||||
data, err = LoadData(context.Background(),
|
||||
exch, common.DataCandle, interval.Duration(), cp1, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(data.Candles) == 0 {
|
||||
t.Error("expected candles")
|
||||
}
|
||||
_, err = LoadData(exch, -1, interval.Duration(), cp1, a)
|
||||
_, err = LoadData(context.Background(), exch, -1, interval.Duration(), cp1, a)
|
||||
if !errors.Is(err, common.ErrInvalidDataType) {
|
||||
t.Errorf("expected '%v' received '%v'", err, common.ErrInvalidDataType)
|
||||
}
|
||||
@@ -71,7 +73,7 @@ func TestLoadTrades(t *testing.T) {
|
||||
ConfigFormat: pFormat,
|
||||
}
|
||||
var data *gctkline.Item
|
||||
data, err = LoadData(exch, common.DataTrade, interval.Duration(), cp1, a)
|
||||
data, err = LoadData(context.Background(), exch, common.DataTrade, interval.Duration(), cp1, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user