exchange.SetMarginLevel
Sets the leverage value for the trading pair or contract specified by the symbol parameter. It is also compatible with a calling method that passes only the marginLevel parameter, which is used to set the leverage value of the current trading pair or contract of the exchange object.
exchange.SetMarginLevel(symbol, marginLevel)
exchange.SetMarginLevel(marginLevel)Examples
Set leverage for the current contract, then for a specified symbol.
javascript
function main() {
exchange.SetMarginLevel(10)
// Set the leverage of BTC's USDT-margined perpetual contract to 15
exchange.SetMarginLevel("BTC_USDT.swap", 15)
}
python
def main():
exchange.SetMarginLevel(10)
exchange.SetMarginLevel("BTC_USDT.swap", 15)
rust
fn main() {
exchange.SetMarginLevel(10);
// In the Rust SDK, the SetMarginLevel function does not support the symbol parameter; it only
// sets the leverage value of the current trading pair or contract
// To set the leverage of the BTC_USDT.swap instrument to 15, you need to switch to that trading
// pair or contract first and then call exchange.SetMarginLevel(15)
}Arguments
| Name | Type | Required | Description |
symbol | string | No | The instrument whose leverage is adjusted, for example |
marginLevel | number | Yes | Leverage value. The leverage value of an exchange is usually an integer, and some exchanges also support setting the leverage value in floating-point form. |
See Also
Remarks
The exchange.SetMarginLevel() function only supports cryptocurrency futures contract exchange objects. The backtesting system supports calling the exchange.SetMarginLevel() function to set the leverage value.
For cryptocurrency futures contracts, the leverage mechanisms of different cryptocurrency futures contract exchanges are not unified.
On some exchanges, the leverage value of a futures contract is a parameter in the order-placing interface. In this case, calling the exchange.SetMarginLevel() function does not generate a network request; it merely sets the underlying leverage variable in the FMZ system (used for passing parameters to the order-placing interface).
On other exchanges, the leverage value of a futures contract is an independent setting of the exchange, which needs to be set through the exchange's website page or API interface. In this case, calling the exchange.SetMarginLevel() function will generate a network request and may fail to set the value. There can be various reasons for failure, for example: there are currently open positions or pending orders, which prevents a new leverage value from being set for that trading pair or contract.
Exchanges that do not support this function: Futures_Deribit, Futures_EdgeX, Futures_Boros, Futures_Polymarket, Futures_Futu, Futures_IB. The call fails with an error and returns null; for all exchanges see Capability Matrix.