Futures
exchange.GetPositions
The exchange.GetPositions() function is used to get position information; GetPositions() function is a member function of the exchange object exchange.
The GetPositions() function gets the position information of the exchange account bound to the exchange object exchange. The purpose of member functions (methods) of the exchange object is only related to exchange, which will not be repeated in subsequent documentation.
exchange.GetPositions()
exchange.GetPositions(symbol)Examples
Use futures exchange object to place market orders for multiple different trading pairs and contract codes. Query positions in various ways.
javascript
/*backtest
start: 2024-05-21 00:00:00
end: 2024-09-05 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
function main() {
var arrSymbol = ["BTC_USDT.swap", "BTC_USDT.quarter", "ETH_USDT.swap", "ETH_USDT.quarter"]
for (var symbol of arrSymbol) {
exchange.CreateOrder(symbol, "buy", -1, 1)
exchange.CreateOrder(symbol, "sell", -1, 1)
}
var defaultPositions = exchange.GetPositions()
var swapPositions = exchange.GetPositions("USDT.swap")
var futuresPositions = exchange.GetPositions("USDT.futures")
var btcUsdtSwapPositions = exchange.GetPositions("BTC_USDT.swap")
var tbls = []
var arr = [defaultPositions, swapPositions, futuresPositions, btcUsdtSwapPositions]
var tblDesc = ["defaultPositions", "swapPositions", "futuresPositions", "btcUsdtSwapPositions"]
for (var index in arr) {
var positions = arr[index]
var tbl = {type: "table", title: tblDesc[index], cols: ["Symbol", "MarginLevel", "Amount", "FrozenAmount", "Price", "Profit", "Type", "ContractType", "Margin"], rows: [] }
for (var pos of positions) {
tbl.rows.push([pos.Symbol, pos.MarginLevel, pos.Amount, pos.FrozenAmount, pos.Price, pos.Profit, pos.Type, pos.ContractType, pos.Margin])
}
tbls.push(tbl)
}
LogStatus("`" + JSON.stringify(tbls) + "`")
// Print output information once and return to prevent subsequent order execution during backtesting, which would affect data observation
return
}
python
'''backtest
start: 2024-05-21 00:00:00
end: 2024-09-05 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
'''
import json
def main():
arrSymbol = ["BTC_USDT.swap", "BTC_USDT.quarter", "ETH_USDT.swap", "ETH_USDT.quarter"]
for symbol in arrSymbol:
exchange.CreateOrder(symbol, "buy", -1, 1)
exchange.CreateOrder(symbol, "sell", -1, 1)
defaultPositions = exchange.GetPositions()
swapPositions = exchange.GetPositions("USDT.swap")
futuresPositions = exchange.GetPositions("USDT.futures")
btcUsdtSwapPositions = exchange.GetPositions("BTC_USDT.swap")
tbls = []
arr = [defaultPositions, swapPositions, futuresPositions, btcUsdtSwapPositions]
tblDesc = ["defaultPositions", "swapPositions", "futuresPositions", "btcUsdtSwapPositions"]
for index in range(len(arr)):
positions = arr[index]
tbl = {"type": "table", "title": tblDesc[index], "cols": ["Symbol", "MarginLevel", "Amount", "FrozenAmount", "Price", "Profit", "Type", "ContractType", "Margin"], "rows": []}
for pos in positions:
tbl["rows"].append([pos["Symbol"], pos["MarginLevel"], pos["Amount"], pos["FrozenAmount"], pos["Price"], pos["Profit"], pos["Type"], pos["ContractType"], pos["Margin"]])
tbls.append(tbl)
LogStatus("`" + json.dumps(tbls) + "`")
return
c++
/*backtest
start: 2024-05-21 00:00:00
end: 2024-09-05 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
void main() {
auto arrSymbol = {"BTC_USDT.swap", "BTC_USDT.quarter", "ETH_USDT.swap", "ETH_USDT.quarter"};
for (const auto& symbol : arrSymbol) {
exchange.CreateOrder(symbol, "buy", -1, 1);
exchange.CreateOrder(symbol, "sell", -1, 1);
}
auto defaultPositions = exchange.GetPositions();
auto swapPositions = exchange.GetPositions("USDT.swap");
auto futuresPositions = exchange.GetPositions("USDT.futures");
auto btcUsdtSwapPositions = exchange.GetPositions("BTC_USDT.swap");
json tbls = R"([])"_json;
std::vector<std::vector<Position>> arr = {defaultPositions, swapPositions, futuresPositions, btcUsdtSwapPositions};
std::string tblDesc[] = {"defaultPositions", "swapPositions", "futuresPositions", "btcUsdtSwapPositions"};
for (int index = 0; index < arr.size(); index++) {
auto positions = arr[index];
json tbl = R"({
"type": "table",
"cols": ["Symbol", "MarginLevel", "Amount", "FrozenAmount", "Price", "Profit", "Type", "ContractType", "Margin"],
"rows": []
})"_json;
tbl["title"] = tblDesc[index];
for (const auto& pos : positions) {
json arrJson = R"([])"_json;
arrJson.push_back(pos.Symbol);
arrJson.push_back(pos.MarginLevel);
arrJson.push_back(pos.Amount);
arrJson.push_back(pos.FrozenAmount);
arrJson.push_back(pos.Price);
arrJson.push_back(pos.Profit);
arrJson.push_back(pos.Type);
arrJson.push_back(pos.ContractType);
arrJson.push_back(pos.Margin);
tbl["rows"].push_back(arrJson);
}
tbls.push_back(tbl);
}
LogStatus(_D(), "\n", "`" + tbls.dump() + "`");
return;
}Returns
| Type | Description |
| The |
Arguments
| Name | Type | Required | Description |
symbol | string | No | The When the |
See Also
Remarks
Cryptocurrency futures contracts differ from cryptocurrency spot trading, where spot only has a logical position concept. In the FMZ quantitative trading platform system, specific cryptocurrency futures contract varieties are identified by both **trading pair** and **contract code**. Please refer to exchange.SetCurrency and exchange.SetContractType functions.
Summary of symbol parameter usage scenarios in the GetPositions function:
| Exchange Object Category | symbol Parameter | Query Scope | Notes |
|---|---|---|---|
| Futures | No symbol parameter | Query all trading varieties within current trading pair and contract code dimension | If current trading pair is BTC_USDT and contract code is swap, it queries all USDT-margined perpetual contracts. Equivalent to calling GetPositions("USDT.swap") |
| Futures | Specify trading variety, symbol parameter: "BTC_USDT.swap" | Query specified BTC USDT-margined perpetual contract | For futures exchange objects, the symbol parameter format is: FMZ platform-defined trading pair and contract code combination, separated by . character. |
| Futures | Specify trading variety range, symbol parameter: "USDT.swap" | Query all USDT-margined perpetual contracts | - |
| Futures exchanges supporting options | No symbol parameter | Query all option contracts within current trading pair dimension | If current trading pair is BTC_USDT and contract is set to option contract, e.g., Binance option contract: BTC-240108-40000-C |
| Futures exchanges supporting options | Specify specific trading variety | Query specified option contract | For example, for Binance futures exchange, symbol parameter: BTC_USDT.BTC-240108-40000-C |
| Futures exchanges supporting options | Specify trading variety range, symbol parameter: "USDT.option" | Query all USDT-margined option contracts | - |
Summary of futures exchange object query dimension ranges in the GetPositions function:
| symbol Parameter | Request Scope Definition | Notes |
|---|---|---|
| USDT.swap | USDT-margined perpetual contract range. | For dimensions not supported by exchange API interfaces, calls will return an error with null value. |
| USDT.futures | USDT-margined delivery contract range. | - |
| USD.swap | Coin-margined perpetual contract range. | - |
| USD.futures | Coin-margined delivery contract range. | - |
| USDT.option | USDT-margined option contract range. | - |
| USD.option | Coin-margined option contract range. | - |
| USDT.futures_combo | Spread combination contract range. | Futures_Deribit exchange |
| USD.futures_ff | Multi-collateral delivery contract range. | Futures_Kraken exchange |
| USD.swap_pf | Multi-collateral perpetual contract range. | Futures_Kraken exchange |
Compatible with exchange.GetPosition() call, GetPosition and GetPositions usage is completely identical.
When the account represented by the exchange object exchange has no positions within the query scope or for the specified trading variety, the exchange.GetPositions() function returns an empty array, for example: [].
exchange.SetMarginLevel
The exchange.SetMarginLevel() function is used to set the leverage value for the trading pair or contract specified by the symbol parameter. It supports passing only the marginLevel parameter to set the leverage value for the current trading pair or contract of the exchange exchange object.
exchange.SetMarginLevel(symbol, marginLevel)
exchange.SetMarginLevel(marginLevel)Examples
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)
c++
void main() {
exchange.SetMarginLevel(10);
exchange.SetMarginLevel("BTC_USDT.swap", 15);
}Arguments
| Name | Type | Required | Description |
symbol | string | No | The |
marginLevel | number | Yes | The |
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 leverage values.
For cryptocurrency futures contracts, the leverage mechanisms across exchanges are not unified.
For some exchanges, the futures contract leverage value is a parameter in the order placement interface. In this case, calling the exchange.SetMarginLevel() function will not generate a network request, but only sets the underlying leverage variable in the FMZ system (used for order interface parameter passing).
For some exchanges, the futures contract leverage value is an exchange setting that needs to be configured through the exchange website page or using API interfaces. In this case, calling the exchange.SetMarginLevel() function will generate a network request and may fail to set. Reasons for failure may include: existing positions or pending orders that prevent the trading pair or contract from being set to a new leverage value.
Exchanges that do not support the exchange.SetMarginLevel() function:
| Function Name | Unsupported Spot Exchanges | Unsupported Futures Exchanges |
|---|---|---|
| SetMarginLevel | -- | Futures_dYdX / Futures_Deribit / Futures_edgeX |
exchange.SetDirection
The exchange.SetDirection() function is used to set the order direction when placing futures contract orders with the exchange.Buy function and exchange.Sell function.
exchange.SetDirection(direction)Examples
javascript
function main(){
// 示例:设置为OKX期货当周合约
exchange.SetContractType("this_week")
// 设置杠杆为5倍
exchange.SetMarginLevel(5)
// 设置下单类型为做多
exchange.SetDirection("buy")
// 以10000的价格,合约数量为2张下单
exchange.Buy(10000, 2)
exchange.SetMarginLevel(5)
exchange.SetDirection("closebuy")
exchange.Sell(1000, 2)
}
python
def main():
exchange.SetContractType("this_week")
exchange.SetMarginLevel(5)
exchange.SetDirection("buy")
exchange.Buy(10000, 2)
exchange.SetMarginLevel(5)
exchange.SetDirection("closebuy")
exchange.Sell(1000, 2)
c++
void main() {
exchange.SetContractType("this_week");
exchange.SetMarginLevel(5);
exchange.SetDirection("buy");
exchange.Buy(10000, 2);
exchange.SetMarginLevel(5);
exchange.SetDirection("closebuy");
exchange.Sell(1000, 2);
}Arguments
| Name | Type | Required | Description |
direction | string | Yes | The |
See Also
Remarks
The correspondence between the direction set by the exchange.SetDirection() function for futures contract trading and the order placement functions:
| Order Function | Direction Set by SetDirection Parameter | Notes |
|---|---|---|
| exchange.Buy | "buy" | Buy to open long position |
| exchange.Buy | "closesell" | Buy to close short position |
| exchange.Sell | "sell" | Sell to open short position |
| exchange.Sell | "closebuy" | Sell to close long position |
exchange.SetContractType
The exchange.SetContractType() function is used to set the current contract code for the exchange exchange object.
exchange.SetContractType(symbol)Examples
-
Set the current contract to the current week contract:
javascriptfunction main() { // Set to current week contract exchange.SetContractType("this_week") }pythondef main(): exchange.SetContractType("this_week")c++void main() { exchange.SetContractType("this_week"); } -
When setting a contract with
USDTas margin, you need to switch the trading pair in the code (you can also set the trading pair directly when adding the exchange object):javascriptfunction main() { // Default trading pair is BTC_USD, set contract to current week, contract is coin-margined contract exchange.SetContractType("this_week") Log("ticker:", exchange.GetTicker()) // Switch trading pair, then set contract, switch to USDT-margined contract, different from coin-margined contract exchange.IO("currency", "BTC_USDT") exchange.SetContractType("swap") Log("ticker:", exchange.GetTicker()) }pythondef main(): exchange.SetContractType("this_week") Log("ticker:", exchange.GetTicker()) exchange.IO("currency", "BTC_USDT") exchange.SetContractType("swap") Log("ticker:", exchange.GetTicker())c++void main() { exchange.SetContractType("this_week"); Log("ticker:", exchange.GetTicker()); exchange.IO("currency", "BTC_USDT"); exchange.SetContractType("swap"); Log("ticker:", exchange.GetTicker()); } -
Print the return value of the
exchange.SetContractType()function:javascriptfunction main(){ // Set contract to current week var ret = exchange.SetContractType("this_week") // Return the information of the current week contract Log(ret) }pythondef main(): ret = exchange.SetContractType("this_week") Log(ret)c++void main() { auto ret = exchange.SetContractType("this_week"); Log(ret); }
Returns
| Type | Description |
object | The |
Arguments
| Name | Type | Required | Description |
symbol | string | Yes | The Unless otherwise specified, delivery contract codes in cryptocurrency futures contracts generally include:
Unless otherwise specified, perpetual contract codes in cryptocurrency futures contracts generally include:
|
See Also
Remarks
In cryptocurrency futures contract strategies, taking switching to the BTC_USDT trading pair as an example:
When using the exchange.SetCurrency("BTC_USDT") or exchange.IO("currency", "BTC_USDT") function to switch trading pairs, after switching you need to use the exchange.SetContractType() function to reset the contract in order to determine the current contract to operate under the new trading pair. The system determines whether it is a coin-margined contract or USDT-margined contract based on the trading pair.
For example: When the trading pair is set to BTC_USDT, using the exchange.SetContractType("swap") function to set the contract code to swap sets it to the USDT-margined perpetual contract for BTC. If the trading pair is BTC_USD, using the exchange.SetContractType("swap") function to set the contract code to swap sets it to the coin-margined perpetual contract for BTC.
Detailed introduction to supported cryptocurrency futures contract exchanges. The contract naming for each exchange is as follows:
-
Futures_OKCoin (OKX)
Set to perpetual contract:exchange.SetContractType("swap")
Set to current week contract:exchange.SetContractType("this_week")
Set to next week contract:exchange.SetContractType("next_week")
Set to monthly contract:exchange.SetContractType("month")
Set to next month contract:exchange.SetContractType("next_month")
Set to quarterly contract:exchange.SetContractType("quarter")
Set to next quarter contract:exchange.SetContractType("next_quarter")OKX supports pre-market trading contracts: The contract delivery date is fixed, and an example of the exchange-defined contract code is:
HMSTR-USDT-250207. On the FMZ platform, set the trading pair toHMSTR_USDT, then useexchange.SetContractType("HMSTR-USDT-250207")to set this contract.
For functions that support thesymbolparameter, such as:exchange.GetTicker(),exchange.CreateOrder(), etc., you can specify thesymbolparameter as:HMSTR_USDT.HMSTR-USDT-250207to get market data for this contract or execute orders. -
Futures_HuobiDM (Huobi Futures)
Set to current week contract:exchange.SetContractType("this_week").
Set to next week contract:exchange.SetContractType("next_week").
Set to quarterly contract:exchange.SetContractType("quarter").
Set to next quarter contract:exchange.SetContractType("next_quarter").
Set to perpetual contract:exchange.SetContractType("swap").
Supports contracts withUSDTas margin. TakingBTCcontract as an example: useexchange.IO("currency", "BTC_USDT")to switch to contracts withUSDTas margin,
or directly set the current trading pair toBTC_USDTwhen configuring live trading parameters and adding exchange objects. After switching trading pairs, you need to call theexchange.SetContractType()function again to set the contract. -
Futures_BitMEX (BitMEX)
Set to perpetual contract:exchange.SetContractType("swap").
Futures_BitMEX exchange's delivery contracts are monthly contracts, with contract codes as follows (January to December):code"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"Set delivery contract:
exchange.SetContractType("December"). For example, when the trading pair is set toXBT_USDT, callingexchange.SetContractType("December")function sets the BTC USDT-margined December delivery contract (the actual contract code isXBTUSDTZ23).Futures_BitMEX Contract Information Summary
Futures_BitMEX Defined Contract Code Corresponding Trading Pair on FMZ Corresponding Contract Code on FMZ Notes DOGEUSD DOGE_USD swap USD quoted, XBT settled. XBT is BTC. DOGEUSDT DOGE_USDT swap USDT quoted, USDT settled. XBTETH XBT_ETH swap ETH quoted, XBT settled. XBTEUR XBT_EUR swap EUR quoted, XBT settled. USDTUSDC USDT_USDC swap USDC quoted, XBT settled. ETHUSD_ETH ETH_USD_ETH swap USD quoted, ETH settled. XBTH24 XBT_USD March Expiry: March 2024, month code: H; USD quoted, XBT settled. ETHUSDZ23 ETH_USD December Expiry: December 2023, month code: Z; USD quoted, XBT settled. XBTUSDTZ23 XBT_USDT December Expiry: December 2023, month code: Z; USDT quoted, USDT settled. ADAZ23 ADA_XBT December Expiry: December 2023, month code: Z; XBT quoted, XBT settled. P_XBTETFX23 USDT_XXX P_XBTETFX23 Expiry: November 2023; percentage quoted, USDT settled. -
Futures_GateIO
Set to current week contract:exchange.SetContractType("this_week").
Set to next week contract:exchange.SetContractType("next_week").
Set to quarterly contract:exchange.SetContractType("quarter").
Set to next quarter contract:exchange.SetContractType("next_quarter").
Set to perpetual contract:exchange.SetContractType("swap").
Supports contracts withUSDTas margin. TakingBTCcontract as an example, useexchange.IO("currency", "BTC_USDT")to switch to contracts withUSDTas margin,
or directly set the current trading pair toBTC_USDTwhen configuring live trading parameters and adding exchange objects. After switching trading pairs, you need to call theexchange.SetContractType()function again to set the contract. -
Futures_Deribit
Set to perpetual contract:exchange.SetContractType("swap").
Supports Deribit'sUSDCcontracts.
Delivery contracts include:"this_week","next_week","month","quarter","next_quarter","third_quarter","fourth_quarter".
Spread contracts (future_combo):"this_week,swap","next_week,swap","next_quarter,this_week","third_quarter,this_week","month,next_week", and various other combinations.
For options contracts, you need to pass the specific option contract code defined by the exchange. Please refer to the Deribit official website for details. -
Futures_KuCoin
Coin-margined contracts, for example, set the trading pair toBTC_USD, then set the contract code for coin-margined contracts:
Set to perpetual contract:exchange.SetContractType("swap").
Set to current quarter contract:exchange.SetContractType("quarter").
Set to next quarter contract:exchange.SetContractType("next_quarter").Contracts with USDT as margin:
For example, set the trading pair toBTC_USDT, then set the contract code for USDT-margined contracts.
Set to perpetual contract:exchange.SetContractType("swap"). -
Futures_Binance
Binance Futures exchange defaults to the perpetual contract of the current trading pair, contract code:swap.
Set to perpetual contract:exchange.SetContractType("swap"). Binance perpetual contracts supportUSDTas margin, for example,BTC'sUSDT-margined perpetual contract, set the trading pair toBTC_USDT. Binance also supports coin-margined perpetual contracts, for example,BTC's coin-margined perpetual contract, set the trading pair toBTC_USD.
Set to quarterly contract:exchange.SetContractType("quarter"). Delivery contracts support coin-margined contracts (i.e., using coins as margin), for example,BTC's quarterly contract, set the trading pair toBTC_USD, then set the contractexchange.SetContractType("quarter")to setBTC's coin-margined quarterly contract.
Set to next quarter contract:exchange.SetContractType("next_quarter"). For example,BTC's coin-margined next quarter contract, set the trading pair toBTC_USD, then set the contractexchange.SetContractType("next_quarter").
Binance supports some delivery contracts withUSDTas margin. TakingBTCas an example, set the trading pair toBTC_USDT, then set the contract code.Supports Binance options contracts:
The option contract code format follows the exchange-defined option contract codes:BTC-241227-15000-C,XRP-240112-0.5-C,BTC-241227-15000-P. Taking Binance option contract codeBTC-241227-15000-Pas an example: BTC is the option currency code, 241227 is the exercise date, 15000 is the strike price, P indicates a put option, and C indicates a call option.
For specific information about option types (European options/American options), please refer to the exchange's option contract documentation.
The exchange may restrict option sellers, requiring separate qualification application. Binance options require seller qualification application. -
Futures_Bibox
Bibox perpetual contract code:swap.
Set to perpetual contract:exchange.SetContractType("swap"). -
Futures_Bybit
Defaults to the perpetual contract of the current trading pair, contract code:swap.
Current week contract code:this_week.
Next week contract code:next_week.
Third week contract code:third_week.
Monthly contract code:month.
Next month contract code:next_month.
Quarterly contract code:quarter.
Next quarter contract code:next_quarter.
Third quarter contract code:third_quarter.
Direct use of exchange contract naming: For exampleETHUSDT-04APR25, since some contract types on Bybit exchange do not have clear periodicity, the exchange-defined contract code naming is used directly. -
Futures_Kraken
Defaults to the perpetual contract of the current trading pair, contract code:swap.
swap: Perpetual contract.
month: Current month contract.
quarter: Quarterly contract.
next_quarter: Next quarter contract.
third_quarter: Third quarter contract.
swap_pf: Multi-collateral perpetual contract.
quarter_ff: Multi-collateral quarterly contract.
month_ff: Multi-collateral current month contract.
next_quarter_ff: Multi-collateral next quarter contract.
third_quarter_ff: Multi-collateral third quarter contract.
Direct use of exchange contract naming: For exampleFF_ETHUSD_250307, since some contract types on Kraken exchange do not have clear periodicity, the exchange-defined contract code naming is used directly. -
Futures_Bitfinex
Defaults to the perpetual contract of the current trading pair, contract code:swap. -
Futures_Bitget
Defaults to the perpetual contract of the current trading pair, contract code:swap.
Setting the trading pair toBTC_USDis for coin-margined contracts, setting the trading pair toBTC_USDTis forUSDT-settled contracts. Simulated contracts can set the trading pair to:SBTC_USD,BTC_SUSDT. -
Futures_dYdX (v4)
dYdX perpetual contract code:swap.
Set to perpetual contract:exchange.SetContractType("swap"), dYdX only has theUSD.swapinstrument dimension, using USDC as margin. -
Futures_MEXC
MEXC perpetual contract code:swap.
Set to perpetual contract:exchange.SetContractType("swap"). Setting the trading pair toBTC_USDis for coin-margined contracts, setting the trading pair toBTC_USDTis forUSDT-settled contracts. -
Futures_Crypto
Tokens in the crypto.com exchange account can be converted to USD-denominated credit for use as margin in contract trading.
Set to perpetual contract:exchange.SetContractType("swap"). For example, when the trading pair is set toBTC_USD, callingexchange.SetContractType("swap")function sets the BTC perpetual contract.
crypto.com exchange's delivery contracts are monthly contracts, with contract codes as follows (January to December):code"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"Set delivery contract:
exchange.SetContractType("October"). For example, when the trading pair is set toBTC_USD, callingexchange.SetContractType("October")function sets the BTC October delivery contract.
The contract code corresponding to the current moment is:BTCUSD-231027. -
Futures_WOO
Futures_WOO exchange supportsUSDT-margined contracts, perpetual contract code isswap. For example, when the trading pair is set toBTC_USDT, callingexchange.SetContractType("swap")function sets the current contract to BTC's USDT-margined perpetual contract. -
Futures_Hyperliquid
Futures_Hyperliquid exchange supportsUSDC-margined contracts, perpetual contract code isswap. For example, when the trading pair is set toETH_USD, callingexchange.SetContractType("swap")function sets the current contract to ETH's USDC-margined perpetual contract.
Futures_Hyperliquid only has theUSD.swapinstrument dimension, using USDC as margin.
Futures_Hyperliquid supports HIP-3 instruments. -
Futures_Lighter
Futures_Lighter exchange supportsUSDC-margined contracts, perpetual contract code isswap. For example, when the trading pair is set toBTC_USDC, callingexchange.SetContractType("swap")function sets the current contract to BTC's USDC-margined perpetual contract.
Futures_Lighter only supports perpetual contracts. -
Futures_Backpack
Futures_Backpack exchange supportsUSDC-margined contracts, perpetual contract code isswap. For example, when the trading pair is set toETH_USDC, callingexchange.SetContractType("swap")function sets the current contract to ETH's USDC-margined perpetual contract. -
Futures_edgeX
Futures_edgeX exchange supportsUSDT-margined contracts, perpetual contract code isswap. For example, when the trading pair is set toBTC_USDT, callingexchange.SetContractType("swap")function sets the current contract to BTC's USDT-margined perpetual contract. -
Futures_WOOFI
Futures_WOOFI exchange supportsUSDC-margined contracts, perpetual contract code isswap. For example, when the trading pair is set toETH_USDC, callingexchange.SetContractType("swap")function sets the current contract to ETH's USDC-margined perpetual contract. -
Futures_Coinw
Futures_Coinw exchange supportsUSDT-margined contracts, perpetual contract code isswap. For example, when the trading pair is set toETH_USDT, callingexchange.SetContractType("swap")function sets the current contract to ETH's USDT-margined perpetual contract. -
Futures_Aster
Futures_Aster exchange supportsUSDT-margined contracts, perpetual contract code isswap. For example, when the trading pair is set toETH_USDT, callingexchange.SetContractType("swap")function sets the current contract to ETH's USDT-margined perpetual contract. -
Futures_DeepCoin
Coin-margined contracts, for example, set the trading pair toBTC_USD, then set the contract code for coin-margined contracts:
Set to perpetual contract:exchange.SetContractType("swap").Contracts with USDT as margin:
For example, set the trading pair toBTC_USDT, then set the contract code for USDT-margined contracts.
Set to perpetual contract:exchange.SetContractType("swap").
exchange.GetContractType
The exchange.GetContractType() function is used to get the contract code currently set for the exchange exchange object.
exchange.GetContractType()Examples
javascript
function main () {
Log(exchange.SetContractType("this_week"))
Log(exchange.GetContractType())
}
python
def main():
Log(exchange.SetContractType("this_week"))
Log(exchange.GetContractType())
c++
void main() {
Log(exchange.SetContractType("this_week"));
Log(exchange.GetContractType());
}Returns
| Type | Description |
string | The |
See Also
exchange.GetFundings
The exchange.GetFundings() function is used to get the funding rate data for the current period.
exchange.GetFundings()
exchange.GetFundings(symbol)Examples
Using a futures exchange object, call the exchange.GetFundings() function in the backtesting system. Before calling any market data function, GetFundings only returns Funding data for the current default trading pair; after calling market data functions, it returns Funding data for all requested symbols. Refer to the following test example:
javascript
/*backtest
start: 2024-10-01 00:00:00
end: 2024-10-23 00:05:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"SOL_USDC"}]
*/
function main() {
// LPT_USDT.swap 4-hour period
var symbols = ["SOL_USDT.swap", "ETH_USDT.swap", "LTC_USDT.swap", "SOL_USDC.swap", "ETH_USDC.swap", "BTC_USD.swap", "BTC_USDT.quarter", "LPT_USDT.swap"]
for (var symbol of symbols) {
exchange.GetTicker(symbol)
}
var arr = []
var arrParams = ["no param", "LTC_USDT.swap", "USDT.swap", "USD.swap", "USDC.swap", "USDT.futures", "BTC_USDT.quarter"]
for (p of arrParams) {
if (p == "no param") {
arr.push(exchange.GetFundings())
} else {
arr.push(exchange.GetFundings(p))
}
}
var tbls = []
var index = 0
for (var fundings of arr) {
var tbl = {
"type": "table",
"title": arrParams[index],
"cols": ["Symbol", "Interval", "Time", "Rate"],
"rows": [],
}
for (var f of fundings) {
tbl["rows"].push([f.Symbol, f.Interval / 3600000, _D(f.Time), f.Rate * 100 + " %"])
}
tbls.push(tbl)
index++
}
LogStatus(_D(), "\n Requested symbols:", symbols, "\n`" + JSON.stringify(tbls) + "`")
}
python
'''backtest
start: 2024-10-01 00:00:00
end: 2024-10-23 00:05:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"SOL_USDC"}]
'''
import json
def main():
# LPT_USDT.swap 4-hour period
symbols = ["SOL_USDT.swap", "ETH_USDT.swap", "LTC_USDT.swap", "SOL_USDC.swap", "ETH_USDC.swap", "BTC_USD.swap", "BTC_USDT.quarter", "LPT_USDT.swap"]
for symbol in symbols:
exchange.GetTicker(symbol)
arr = []
arrParams = ["no param", "LTC_USDT.swap", "USDT.swap", "USD.swap", "USDC.swap", "USDT.futures", "BTC_USDT.quarter"]
for p in arrParams:
if p == "no param":
arr.append(exchange.GetFundings())
else:
arr.append(exchange.GetFundings(p))
tbls = []
index = 0
for fundings in arr:
tbl = {
"type": "table",
"title": arrParams[index],
"cols": ["Symbol", "Interval", "Time", "Rate"],
"rows": [],
}
for f in fundings:
tbl["rows"].append([f["Symbol"], f["Interval"] / 3600000, _D(f["Time"]), str(f["Rate"] * 100) + " %"])
tbls.append(tbl)
index += 1
LogStatus(_D(), "\n Requested symbols:", symbols, "\n`" + json.dumps(tbls) + "`")
c++
/*backtest
start: 2024-10-01 00:00:00
end: 2024-10-23 00:05:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"SOL_USDC"}]
*/
void main() {
// LPT_USDT.swap 4-hour period
json arrSymbol = R"([])"_json;
std::string symbols[] = {"SOL_USDT.swap", "ETH_USDT.swap", "LTC_USDT.swap", "SOL_USDC.swap", "ETH_USDC.swap", "BTC_USD.swap", "BTC_USDT.quarter", "LPT_USDT.swap"};
for (const std::string& symbol : symbols) {
exchange.GetTicker(symbol);
arrSymbol.push_back(symbol);
}
std::vector<std::vector<Funding>> arr = {};
std::string arrParams[] = {"no param", "LTC_USDT.swap", "USDT.swap", "USD.swap", "USDC.swap", "USDT.futures", "BTC_USDT.quarter"};
for (const std::string& p : arrParams) {
if (p == "no param") {
arr.push_back(exchange.GetFundings());
} else {
arr.push_back(exchange.GetFundings(p));
}
}
json tbls = R"([])"_json;
int index = 0;
for (int i = 0; i < arr.size(); i++) {
auto fundings = arr[i];
json tbl = R"({
"type": "table",
"cols": ["Symbol", "Interval", "Time", "Rate"],
"rows": []
})"_json;
tbl["title"] = arrParams[index];
for (int j = 0; j < fundings.size(); j++) {
auto f = fundings[j];
// json arrJson = {f.Symbol, f.Interval / 3600000, _D(f.Time), string(f.Rate * 100) + " %"};
json arrJson = {f.Symbol, f.Interval / 3600000, _D(f.Time), f.Rate};
tbl["rows"].push_back(arrJson);
}
tbls.push_back(tbl);
index++;
}
LogStatus(_D(), "\n Requested symbols:", arrSymbol.dump(), "\n`" + tbls.dump() + "`");
}Returns
| Type | Description |
| The |
Arguments
| Name | Type | Required | Description |
symbol | string | No | The parameter |
See Also
Remarks
For futures exchanges that do not support batch querying of funding rate data, if the symbol parameter is specified as a query range (e.g., USDT.swap) or the symbol parameter is not passed, the interface will report an error. When calling the GetFundings() function using such futures exchange objects, the symbol parameter must be specified as a specific perpetual contract instrument to query the current funding rate data for that instrument.
The exchange.GetFundings() function supports both live trading and backtesting systems.
Exchanges that do not support batch retrieval of funding rate data: Futures_Bitget, Futures_OKX, Futures_MEXC, Futures_Deribit, Futures_Crypto. The symbol parameter must be passed to specify a specific instrument code, e.g., ETH_USDT.swap.
Exchanges that do not support the exchange.GetFundings() function:
| Function Name | Unsupported Spot Exchanges | Unsupported Futures Exchanges |
|---|---|---|
| GetFundings | -- | Futures_DigiFinex |