Skip to content

Commit

Permalink
Binance,Okx: Add Leverage, MarginType, Positions and CollateralMode s…
Browse files Browse the repository at this point in the history
…upport (#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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
gloriousCode and thrasher- authored Sep 26, 2023
1 parent a2ae99e commit 5f2f6f8
Show file tree
Hide file tree
Showing 67 changed files with 12,476 additions and 5,393 deletions.
6 changes: 4 additions & 2 deletions backtester/data/kline/live/live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 5 additions & 7 deletions backtester/engine/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions backtester/eventhandlers/exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/collateral"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
"github.com/thrasher-corp/gocryptotrader/exchanges/fundingrate"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
Expand Down Expand Up @@ -920,15 +921,15 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
Pair: p,
}
var positionSummaryResponse *order.PositionSummary
positionSummaryResponse, err = e.GetPositionSummary(context.TODO(), positionSummaryRequest)
positionSummaryResponse, err = e.GetFuturesPositionSummary(context.TODO(), positionSummaryRequest)
msg = ""
if err != nil {
msg = err.Error()
responseContainer.ErrorCount++
}
responseContainer.EndpointResponses = append(responseContainer.EndpointResponses, EndpointResponse{
SentParams: jsonifyInterface([]interface{}{positionSummaryRequest}),
Function: "GetPositionSummary",
Function: "GetFuturesPositionSummary",
Error: msg,
Response: jsonifyInterface([]interface{}{positionSummaryResponse}),
})
Expand Down Expand Up @@ -968,7 +969,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
LockedCollateral: decimal.NewFromInt(1337),
UnrealisedPNL: decimal.NewFromInt(1337),
}
var scaleCollateralResponse *order.CollateralByCurrency
var scaleCollateralResponse *collateral.ByCurrency
scaleCollateralResponse, err = e.ScaleCollateral(context.TODO(), collateralCalculator)
msg = ""
if err != nil {
Expand Down Expand Up @@ -999,21 +1000,21 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
Response: jsonifyInterface([]interface{}{calculateTotalCollateralResponse}),
})

var futuresPositionsResponse []order.PositionDetails
var futuresPositionsResponse []order.PositionResponse
futuresPositionsRequest := &order.PositionsRequest{
Asset: assetTypes[i],
Pairs: currency.Pairs{p},
StartDate: time.Now().Add(-time.Hour),
}
futuresPositionsResponse, err = e.GetFuturesPositions(context.TODO(), futuresPositionsRequest)
futuresPositionsResponse, err = e.GetFuturesPositionOrders(context.TODO(), futuresPositionsRequest)
msg = ""
if err != nil {
msg = err.Error()
responseContainer.ErrorCount++
}
responseContainer.EndpointResponses = append(responseContainer.EndpointResponses, EndpointResponse{
SentParams: jsonifyInterface([]interface{}{futuresPositionsRequest}),
Function: "GetFuturesPositions",
Function: "GetFuturesPositionOrders",
Error: msg,
Response: jsonifyInterface([]interface{}{futuresPositionsResponse}),
})
Expand Down
63 changes: 57 additions & 6 deletions cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/collateral"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/margin"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
Expand Down Expand Up @@ -263,13 +265,21 @@ var (
stringParam = reflect.TypeOf((*string)(nil)).Elem()
feeBuilderParam = reflect.TypeOf((**exchange.FeeBuilder)(nil)).Elem()
credentialsParam = reflect.TypeOf((**account.Credentials)(nil)).Elem()
orderSideParam = reflect.TypeOf((*order.Side)(nil)).Elem()
collateralModeParam = reflect.TypeOf((*collateral.Mode)(nil)).Elem()
marginTypeParam = reflect.TypeOf((*margin.Type)(nil)).Elem()
int64Param = reflect.TypeOf((*int64)(nil)).Elem()
float64Param = reflect.TypeOf((*float64)(nil)).Elem()
// types with asset in params
assetParam = reflect.TypeOf((*asset.Item)(nil)).Elem()
orderSubmitParam = reflect.TypeOf((**order.Submit)(nil)).Elem()
orderModifyParam = reflect.TypeOf((**order.Modify)(nil)).Elem()
orderCancelParam = reflect.TypeOf((**order.Cancel)(nil)).Elem()
orderCancelsParam = reflect.TypeOf((*[]order.Cancel)(nil)).Elem()
getOrdersRequestParam = reflect.TypeOf((**order.MultiOrderRequest)(nil)).Elem()
assetParam = reflect.TypeOf((*asset.Item)(nil)).Elem()
orderSubmitParam = reflect.TypeOf((**order.Submit)(nil)).Elem()
orderModifyParam = reflect.TypeOf((**order.Modify)(nil)).Elem()
orderCancelParam = reflect.TypeOf((**order.Cancel)(nil)).Elem()
orderCancelsParam = reflect.TypeOf((*[]order.Cancel)(nil)).Elem()
getOrdersRequestParam = reflect.TypeOf((**order.MultiOrderRequest)(nil)).Elem()
positionChangeRequestParam = reflect.TypeOf((**margin.PositionChangeRequest)(nil)).Elem()
positionSummaryRequestParam = reflect.TypeOf((**order.PositionSummaryRequest)(nil)).Elem()
positionsRequestParam = reflect.TypeOf((**order.PositionsRequest)(nil)).Elem()
)

// generateMethodArg determines the argument type and returns a pre-made
Expand Down Expand Up @@ -459,6 +469,39 @@ func generateMethodArg(ctx context.Context, t *testing.T, argGenerator *MethodAr
AssetType: argGenerator.AssetParams.Asset,
Pairs: currency.Pairs{argGenerator.AssetParams.Pair},
})
case argGenerator.MethodInputType.AssignableTo(marginTypeParam):
input = reflect.ValueOf(margin.Isolated)
case argGenerator.MethodInputType.AssignableTo(collateralModeParam):
input = reflect.ValueOf(collateral.SingleMode)
case argGenerator.MethodInputType.AssignableTo(positionChangeRequestParam):
input = reflect.ValueOf(&margin.PositionChangeRequest{
Exchange: argGenerator.Exchange.GetName(),
Pair: argGenerator.AssetParams.Pair,
Asset: argGenerator.AssetParams.Asset,
MarginType: margin.Isolated,
OriginalAllocatedMargin: 1337,
NewAllocatedMargin: 1338,
})
case argGenerator.MethodInputType.AssignableTo(positionSummaryRequestParam):
input = reflect.ValueOf(&order.PositionSummaryRequest{
Asset: argGenerator.AssetParams.Asset,
Pair: argGenerator.AssetParams.Pair,
Direction: order.Buy,
})
case argGenerator.MethodInputType.AssignableTo(positionsRequestParam):
input = reflect.ValueOf(&order.PositionsRequest{
Asset: argGenerator.AssetParams.Asset,
Pairs: currency.Pairs{argGenerator.AssetParams.Pair},
StartDate: argGenerator.Start,
EndDate: argGenerator.End,
RespectOrderHistoryLimits: true,
})
case argGenerator.MethodInputType.AssignableTo(orderSideParam):
input = reflect.ValueOf(order.Long)
case argGenerator.MethodInputType.AssignableTo(int64Param):
input = reflect.ValueOf(1337)
case argGenerator.MethodInputType.AssignableTo(float64Param):
input = reflect.ValueOf(13.37)
default:
input = reflect.Zero(argGenerator.MethodInputType)
}
Expand Down Expand Up @@ -509,6 +552,14 @@ var excludedMethodNames = map[string]struct{}{
"CalculateTotalCollateral": {},
"ScaleCollateral": {},
"GetPositionSummary": {},
"GetFuturesPositionSummary": {},
"GetFuturesPositionOrders": {},
"SetCollateralMode": {},
"GetCollateralMode": {},
"SetLeverage": {},
"GetLeverage": {},
"SetMarginType": {},
"ChangePositionMargin": {},
"GetLatestFundingRate": {},
}

Expand Down
18 changes: 18 additions & 0 deletions cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/margin"
"github.com/thrasher-corp/gocryptotrader/gctrpc"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -1420,6 +1421,11 @@ var submitOrderCommand = &cli.Command{
Name: "asset",
Usage: "required asset type",
},
&cli.StringFlag{
Name: "margintype",
Usage: "required asset type",
Required: false,
},
},
}

Expand All @@ -1436,6 +1442,7 @@ func submitOrder(c *cli.Context) error {
var price float64
var clientID string
var assetType string
var marginType string

if c.IsSet("exchange") {
exchangeName = c.String("exchange")
Expand Down Expand Up @@ -1510,11 +1517,22 @@ func submitOrder(c *cli.Context) error {
assetType = c.Args().Get(7)
}

if c.IsSet("margintype") {
marginType = c.String("margintype")
} else {
marginType = c.Args().Get(8)
}

assetType = strings.ToLower(assetType)
if !validAsset(assetType) {
return errInvalidAsset
}

marginType = strings.ToLower(marginType)
if !margin.IsValidString(marginType) {
return margin.ErrInvalidMarginType
}

p, err := currency.NewPairDelimiter(currencyPair, pairDelimiter)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 5f2f6f8

Please sign in to comment.