Binance,Okx: Add Leverage, MarginType, Positions and CollateralMode support (#1220)

* init

* surprise train commit

* basic distinctions

* the terms of binance are confusing

* renames and introduction of allocatedMargin

* add new margin funcs

* pulling out wires

* implement proper getposition stuff

* bad coding day

* investigate order manager next

* a broken mess, but a progressing one

* finally completes some usdtmargined stuff

* coinMfutures eludes me

* expand to okx

* imports fix

* completes okx wrapper implementations

* cleans and polishes before rpc implementations

* rpc setup, order manager features, exch features

* more rpc, collateral and margin things

* mini test

* looking at rpc response, expansion of features

* reorganising before the storm

* changing how futures requests work

* cleanup and tests of cli usage

* remove silly client side logic

* cleanup

* collateral package, typo fix, margin err, rpc derive

* uses convert.StringToFloat ONLY ON STRUCTS FROM THIS PR

* fix binance order history bug

* niteroos

* adds new funcs to exchange standards testing

* more post merge fixes

* fix binance

* replace simepletimeformat

* fix for merge

* merge fixes

* micro fixes

* order side now required for leverage

* fix up the rest

* global -> portfolio collateral

* Update exchanges/collateral/collateral_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* adds fields and todos

* rm field redundancy

* lint fix oopsie daisy

* fixes panic, expands error and cli explanations (sorry shaz)

* ensures casing is appropriate for underlying

* Adds a shiny TODO

---------

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Scott
2023-09-26 16:16:31 +10:00
committed by GitHub
parent a2ae99ed7f
commit 5f2f6f884b
67 changed files with 11558 additions and 4475 deletions

View File

@@ -14,7 +14,7 @@ import (
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
)
const testExchange = "binanceus"
const testExchange = "okx"
func TestLoadCandles(t *testing.T) {
t.Parallel()
@@ -73,7 +73,9 @@ func TestLoadTrades(t *testing.T) {
ConfigFormat: pFormat,
}
var data *gctkline.Item
data, err = LoadData(context.Background(), time.Now().Add(-interval.Duration()*10), exch, common.DataTrade, interval.Duration(), cp, currency.EMPTYPAIR, a, true)
// start is 10 mins in the past to ensure there are some trades to pull from the exchange
start := time.Now().Add(-time.Minute * 10)
data, err = LoadData(context.Background(), start, exch, common.DataTrade, interval.Duration(), cp, currency.EMPTYPAIR, a, true)
if err != nil {
t.Fatal(err)
}

View File

@@ -205,13 +205,11 @@ func (bt *BackTest) SetupFromConfig(cfg *config.Config, templatePath, output str
}
}
bt.orderManager, err = engine.SetupOrderManager(
bt.exchangeManager,
&engine.CommunicationManager{},
&sync.WaitGroup{},
verbose,
trackFuturesPositions,
0)
bt.orderManager, err = engine.SetupOrderManager(bt.exchangeManager, &engine.CommunicationManager{}, &sync.WaitGroup{}, &gctconfig.OrderManager{
Verbose: verbose,
ActivelyTrackFuturesPositions: trackFuturesPositions,
RespectOrderHistoryLimits: convert.BoolPtr(true),
})
if err != nil {
return err
}

View File

@@ -17,6 +17,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/backtester/funding"
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/convert"
gctconfig "github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/engine"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
@@ -198,7 +199,7 @@ func TestPlaceOrder(t *testing.T) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
bot.ExchangeManager = em
bot.OrderManager, err = engine.SetupOrderManager(em, &engine.CommunicationManager{}, &bot.ServicesWG, false, false, 0)
bot.OrderManager, err = engine.SetupOrderManager(em, &engine.CommunicationManager{}, &bot.ServicesWG, &gctconfig.OrderManager{})
if !errors.Is(err, nil) {
t.Errorf("received '%v' expected '%v'", err, nil)
}
@@ -256,7 +257,7 @@ func TestExecuteOrder(t *testing.T) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
bot.ExchangeManager = em
bot.OrderManager, err = engine.SetupOrderManager(em, &engine.CommunicationManager{}, &bot.ServicesWG, false, false, 0)
bot.OrderManager, err = engine.SetupOrderManager(em, &engine.CommunicationManager{}, &bot.ServicesWG, &gctconfig.OrderManager{})
if !errors.Is(err, nil) {
t.Errorf("received: %v, expected: %v", err, nil)
}
@@ -392,7 +393,7 @@ func TestExecuteOrderBuySellSizeLimit(t *testing.T) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
bot.ExchangeManager = em
bot.OrderManager, err = engine.SetupOrderManager(em, &engine.CommunicationManager{}, &bot.ServicesWG, false, false, 0)
bot.OrderManager, err = engine.SetupOrderManager(em, &engine.CommunicationManager{}, &bot.ServicesWG, &gctconfig.OrderManager{})
if !errors.Is(err, nil) {
t.Errorf("received: %v, expected: %v", err, nil)
}