mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
binance: integerate isolated margin / cross margin transfer
This commit is contained in:
parent
92691eda24
commit
8b1cefc699
|
@ -16,6 +16,11 @@ func (t *TransferCrossMarginAccountRequest) Asset(asset string) *TransferCrossMa
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TransferCrossMarginAccountRequest) TransferType(transferType int) *TransferCrossMarginAccountRequest {
|
||||||
|
t.transferType = transferType
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TransferCrossMarginAccountRequest) Amount(amount string) *TransferCrossMarginAccountRequest {
|
func (t *TransferCrossMarginAccountRequest) Amount(amount string) *TransferCrossMarginAccountRequest {
|
||||||
t.amount = amount
|
t.amount = amount
|
||||||
return t
|
return t
|
||||||
|
@ -41,6 +46,11 @@ func (t *TransferCrossMarginAccountRequest) GetParameters() (map[string]interfac
|
||||||
|
|
||||||
// assign parameter of asset
|
// assign parameter of asset
|
||||||
params["asset"] = asset
|
params["asset"] = asset
|
||||||
|
// check transferType field -> json key type
|
||||||
|
transferType := t.transferType
|
||||||
|
|
||||||
|
// assign parameter of transferType
|
||||||
|
params["type"] = transferType
|
||||||
// check amount field -> json key amount
|
// check amount field -> json key amount
|
||||||
amount := t.amount
|
amount := t.amount
|
||||||
|
|
||||||
|
|
|
@ -373,34 +373,53 @@ func (e *Exchange) QueryMarginBorrowHistory(ctx context.Context, asset string) e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exchange) TransferMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
func (e *Exchange) TransferMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
||||||
if e.IsMargin && !e.IsIsolatedMargin {
|
if e.IsMargin {
|
||||||
return e.transferCrossMarginAccountAsset(ctx, asset, amount, io)
|
if e.IsIsolatedMargin {
|
||||||
|
return e.transferIsolatedMarginAccountAsset(ctx, asset, amount, io)
|
||||||
|
} else {
|
||||||
|
return e.transferCrossMarginAccountAsset(ctx, asset, amount, io)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("isolated margin transfer is not supported")
|
return errors.New("isolated margin transfer is not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Exchange) transferIsolatedMarginAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
||||||
|
req := e.client2.NewTransferIsolatedMarginAccountRequest()
|
||||||
|
req.Symbol(e.IsolatedMarginSymbol)
|
||||||
|
|
||||||
|
switch io {
|
||||||
|
case types.TransferIn:
|
||||||
|
req.TransFrom(binanceapi.AccountTypeSpot)
|
||||||
|
req.TransTo(binanceapi.AccountTypeIsolatedMargin)
|
||||||
|
|
||||||
|
case types.TransferOut:
|
||||||
|
req.TransFrom(binanceapi.AccountTypeIsolatedMargin)
|
||||||
|
req.TransTo(binanceapi.AccountTypeSpot)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Asset(asset)
|
||||||
|
req.Amount(amount.String())
|
||||||
|
resp, err := req.Do(ctx)
|
||||||
|
return logResponse(resp, err, req)
|
||||||
|
}
|
||||||
|
|
||||||
// transferCrossMarginAccountAsset transfer asset to the cross margin account or to the main account
|
// transferCrossMarginAccountAsset transfer asset to the cross margin account or to the main account
|
||||||
func (e *Exchange) transferCrossMarginAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
func (e *Exchange) transferCrossMarginAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
||||||
req := e.client.NewMarginTransferService()
|
req := e.client2.NewTransferCrossMarginAccountRequest()
|
||||||
req.Asset(asset)
|
req.Asset(asset)
|
||||||
req.Amount(amount.String())
|
req.Amount(amount.String())
|
||||||
|
|
||||||
if io == types.TransferIn {
|
if io == types.TransferIn {
|
||||||
req.Type(binance.MarginTransferTypeToMargin)
|
req.TransferType(int(binance.MarginTransferTypeToMargin))
|
||||||
} else if io == types.TransferOut {
|
} else if io == types.TransferOut {
|
||||||
req.Type(binance.MarginTransferTypeToMain)
|
req.TransferType(int(binance.MarginTransferTypeToMain))
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("unexpected transfer direction: %d given", io)
|
return fmt.Errorf("unexpected transfer direction: %d given", io)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := req.Do(ctx)
|
resp, err := req.Do(ctx)
|
||||||
if err != nil {
|
return logResponse(resp, err, req)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("cross margin transfer %f %s, transaction id = %d", amount.Float64(), asset, resp.TranID)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exchange) QueryCrossMarginAccount(ctx context.Context) (*types.Account, error) {
|
func (e *Exchange) QueryCrossMarginAccount(ctx context.Context) (*types.Account, error) {
|
||||||
|
@ -1450,3 +1469,13 @@ func calculateMarginTolerance(marginLevel fixedpoint.Value) fixedpoint.Value {
|
||||||
// = 1.0 - (1.1 / marginLevel)
|
// = 1.0 - (1.1 / marginLevel)
|
||||||
return fixedpoint.One.Sub(fixedpoint.NewFromFloat(1.1).Div(marginLevel))
|
return fixedpoint.One.Sub(fixedpoint.NewFromFloat(1.1).Div(marginLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logResponse(resp interface{}, err error, req interface{}) error {
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Errorf("%T: error %+v", req, resp)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("%T: response: %+v", req, resp)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user