mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-01 23:16:51 +00:00
* orderbook: export orderbook nodes for external strategy inspection * orderbook: Add in methods for locking and unlocking multiple books at the same time e.g. book1.LockWith(book2); defer book1.UnlockWith(book2) * include waiting functionality for depth change alert * backtester: add word. * log: include logger changes to impl with downstream integration * engine: reduce params for loading exchange * assort: rm verbose in tests, change wording in ob, expose sync.waitgroup for ext. sync options * ticker: reduce map look ups and contention when using RW mutex when there are over 80% writes adds find last function to get the latest rate * engine/syncmanager: add in waitgroup for step over for external package calls * cleaup * engine: linter fix * currency/fx: include all references to fiat currencies to default * orderbook: Add in fields to Unsafe type for strategies to detect potential out of sync book operations * syncmanager: changed config variable to display correct time * ordermanager: Add time when none provided * currency/manager: update getasset param to get enabled assets for minor optimizations * ftx: use get all wallet balances for a better accounts breakdown * orderbook: unlock in reverse order * bithumb: fixes bug on market buy and sell orders * bithumb: fix bug for nonce is also time window sensitive * bithumb: get orders add required parameter * bithumb: Add asset type to account struct * currency: improve log output when checking currency and it fails * bithumb: Add error return on incomplete pair * ticker:unexport all service related methods * ticker/currency: fixes * orderbook: fix comment * engine: revert variable name in LoadExchange method * sync_manager: fix panic when enabling disabling manager * engine: fix naming convention of exported function and comments * engine: update comment * orderbook: fix comment for unsafe type
228 lines
5.1 KiB
Go
228 lines
5.1 KiB
Go
package exchange
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/engine"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
)
|
|
|
|
// change these if you wish to test another exchange and/or currency pair
|
|
const (
|
|
exchName = "BTC Markets" // change to test on another exchange
|
|
exchAPIKEY = ""
|
|
exchAPISECRET = ""
|
|
exchClientID = ""
|
|
pairs = "BTC-AUD" // change to test another currency pair
|
|
delimiter = "-"
|
|
assetType = asset.Spot
|
|
orderID = "1234"
|
|
orderType = order.Limit
|
|
orderSide = order.Buy
|
|
orderClientID = ""
|
|
orderPrice = 1
|
|
orderAmount = 1
|
|
)
|
|
|
|
var (
|
|
settings = engine.Settings{
|
|
ConfigFile: filepath.Join("..", "..", "..", "..", "testdata", "configtest.json"),
|
|
EnableDryRun: true,
|
|
DataDir: filepath.Join("..", "..", "..", "..", "testdata", "gocryptotrader"),
|
|
Verbose: false,
|
|
EnableGRPC: false,
|
|
EnableDeprecatedRPC: false,
|
|
EnableWebsocketRPC: false,
|
|
}
|
|
exchangeTest = Exchange{}
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
var t int
|
|
err := setupEngine()
|
|
if err != nil {
|
|
fmt.Printf("Failed to configure exchange test cannot continue: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
t = m.Run()
|
|
cleanup()
|
|
os.Exit(t)
|
|
}
|
|
|
|
func TestExchange_Exchanges(t *testing.T) {
|
|
t.Parallel()
|
|
x := exchangeTest.Exchanges(false)
|
|
y := len(x)
|
|
if y != 1 {
|
|
t.Fatalf("expected 1 received %v", y)
|
|
}
|
|
}
|
|
|
|
func TestExchange_GetExchange(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := exchangeTest.GetExchange(exchName)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = exchangeTest.GetExchange("hello world")
|
|
if err == nil {
|
|
t.Fatal("unexpected error message received nil")
|
|
}
|
|
}
|
|
|
|
func TestExchange_IsEnabled(t *testing.T) {
|
|
t.Parallel()
|
|
x := exchangeTest.IsEnabled(exchName)
|
|
if !x {
|
|
t.Fatal("expected return to be true")
|
|
}
|
|
x = exchangeTest.IsEnabled("fake_exchange")
|
|
if x {
|
|
t.Fatal("expected return to be false")
|
|
}
|
|
}
|
|
|
|
func TestExchange_Ticker(t *testing.T) {
|
|
t.Parallel()
|
|
c, err := currency.NewPairDelimiter(pairs, delimiter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = exchangeTest.Ticker(exchName, c, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_Orderbook(t *testing.T) {
|
|
t.Parallel()
|
|
c, err := currency.NewPairDelimiter(pairs, delimiter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = exchangeTest.Orderbook(exchName, c, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_Pairs(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := exchangeTest.Pairs(exchName, false, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = exchangeTest.Pairs(exchName, true, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_AccountInformation(t *testing.T) {
|
|
if !configureExchangeKeys() {
|
|
t.Skip("no exchange configured test skipped")
|
|
}
|
|
_, err := exchangeTest.AccountInformation(exchName, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_QueryOrder(t *testing.T) {
|
|
if !configureExchangeKeys() {
|
|
t.Skip("no exchange configured test skipped")
|
|
}
|
|
t.Parallel()
|
|
_, err := exchangeTest.QueryOrder(exchName, orderID, currency.Pair{}, assetType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_SubmitOrder(t *testing.T) {
|
|
if !configureExchangeKeys() {
|
|
t.Skip("no exchange configured test skipped")
|
|
}
|
|
|
|
t.Parallel()
|
|
c, err := currency.NewPairDelimiter(pairs, delimiter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tempOrder := &order.Submit{
|
|
Pair: c,
|
|
Type: orderType,
|
|
Side: orderSide,
|
|
TriggerPrice: 0,
|
|
TargetAmount: 0,
|
|
Price: orderPrice,
|
|
Amount: orderAmount,
|
|
ClientID: orderClientID,
|
|
Exchange: exchName,
|
|
AssetType: asset.Spot,
|
|
}
|
|
_, err = exchangeTest.SubmitOrder(tempOrder)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestExchange_CancelOrder(t *testing.T) {
|
|
if !configureExchangeKeys() {
|
|
t.Skip("no exchange configured test skipped")
|
|
}
|
|
t.Parallel()
|
|
cp := currency.NewPair(currency.BTC, currency.USD)
|
|
a := asset.Spot
|
|
_, err := exchangeTest.CancelOrder(exchName, orderID, cp, a)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestOHLCV(t *testing.T) {
|
|
t.Parallel()
|
|
cp := currency.NewPair(currency.BTC, currency.AUD)
|
|
cp.Delimiter = currency.DashDelimiter
|
|
calvinKline, err := exchangeTest.OHLCV(exchName, cp, assetType, time.Now().Add(-time.Hour*24).UTC(), time.Now().UTC(), kline.OneHour)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if calvinKline.Exchange != exchName {
|
|
t.Error("unexpected response")
|
|
}
|
|
}
|
|
|
|
func setupEngine() (err error) {
|
|
engine.Bot, err = engine.NewFromSettings(&settings, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
em := engine.SetupExchangeManager()
|
|
engine.Bot.ExchangeManager = em
|
|
|
|
return engine.Bot.LoadExchange(exchName, nil)
|
|
}
|
|
|
|
func cleanup() {
|
|
err := os.RemoveAll(settings.DataDir)
|
|
if err != nil {
|
|
fmt.Printf("Clean up failed to remove file: %v manual removal may be required", err)
|
|
}
|
|
}
|
|
|
|
func configureExchangeKeys() bool {
|
|
ex := engine.Bot.GetExchangeByName(exchName).GetBase()
|
|
ex.SetAPIKeys(exchAPIKEY, exchAPISECRET, exchClientID)
|
|
ex.SkipAuthCheck = true
|
|
return ex.ValidateAPICredentials()
|
|
}
|