The exchange.GetPositions()
function is used to get the position information; The GetPositions()
function is a member function of the exchange object {@var/EXCHANGE exchange}. The GetPositions()
function gets the position information of the exchange account bound to the exchange object exchange
. The purpose of the member functions (methods) of the exchange
object is only related to exchange
and will not be repeated here.
The exchange.GetPositions()
function returns an array of {@struct/Position Position} structures if the request for data succeeds, and it returns null value if the request for data fails.
{@struct/Position Position} arrays, null values
exchange.GetPositions() exchange.GetPositions(symbol)
The parameter symbol
is used to set the trading symbol or trading symbol range to be queried.
If the symbol
parameter is not passed, the default is to request the position data of all symbols in the dimension range of the current trading pair and contract code.
symbol false string
/*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 out the information once and then return to prevent the order from being executed during the subsequent backtest and affecting data observation
return
}
'''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
/*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;
}
Use futures exchange objects to place market orders for multiple different trading pairs and contract codes. Query positions in multiple ways.
Cryptocurrency futures contracts are different from cryptocurrency spot, which has only the logical concept of a position. In the system of FMZ Quant Trading platform, the specific types of cryptocurrency futures contracts are identified by trading pairs, contract code together. Please refer to {@fun/Account/exchange.SetCurrency exchange.SetCurrency}, {@fun/Futures/exchange.SetContractType exchange.SetContractType} functions.
In the GetPositions
function, the usage scenarios of the symbol parameter are summarized as follows:
Exchange Object Classification | symbol Parameters | Query Scope | Remark |
---|---|---|---|
Futures | Do not pass symbol parameter | Query all trading products within the current trading pair and contract code dimension range | If the current trading pair is BTC_USDT and the contract code is swap, all USDT-based perpetual contracts will be queried. This is equivalent to calling GetPositions("USDT.swap") |
Futures | Specify the trading product, the symbol parameter is: “BTC_USDT.swap” | Query the USDT-based perpetual contract of a specified BTC | For futures exchange objects, the format of parameter symbol is: a combination of trading pair and contract code defined by the FMZ platform, separated by the characters ". . |
Futures | Specify the range of trading products, the symbol parameter is: “USDT.swap” | Query all USDT-based perpetual contracts | - |
Futures exchanges that support options | Do not pass symbol parameter | Query all option contracts within the current trading pair dimension range | If the current trading pair is BTC_USDT, the contract is set to an option contract, for example, Binance option contract: BTC-240108-40000-C |
Futures exchanges that support options | Specify specific trading product | Query the specified option contract | For example, for Binance Futures Exchange, the symbol parameter is: BTC_USDT.BTC-240108-40000-C |
Futures exchanges that support options | Specify the range of trading products, the symbol parameter is: “USDT.option” | Query all USDT-based options contracts | - |
In the GetPositions
function, the futures exchange object
query dimension range is summarized as follows:
symbol Parameters | Request Scope Definition | Remark |
---|---|---|
USDT.swap | USDT-based perpetual contract range. | For |
dimensions that are not supported by the exchange API interface, an error will be reported and a null value will be returned when calling. |
| USDT.futures | USDT-based delivery contract range. | - |
| USD.swap | Scope of currency-based perpetual contracts. | - |
| USD.futures | Scope of currency-based delivery contracts. | - |
| USDT.option | USDT-based options contract range. | - |
| USD.option | Currency-based options contract range. |
| USDT.futures_combo | Range of CFD combinations. | Futures_Deribit Exchange |
| USD.futures_ff | Scope of mixed margin delivery contracts. | Futures_Kraken Exchange |
| USD.swap_pf | Mixed margin perpetual contract range. | Futures_Kraken Exchange |
Compatible with exchange.GetPosition()
call, GetPosition
is exactly the same as GetPositions
.
When the account represented by the exchange object exchange
has no positions in the query range or specified trading instruments, the exchange.GetPositions()
function returns an empty array, for example: []
.
{@struct/Position Position}, {@fun/Account/exchange.SetCurrency exchange.SetCurrency}, {@fun/Futures/exchange.SetContractType exchange.SetContractType}
Account exchange.SetMarginLevel