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: [].