exchange.GetPositions
Gets position information. GetPositions() is a member function of the exchange object exchange and acts only on the account bound to that exchange object (with several accounts, use exchanges[i].GetPositions() to pick one).
exchange.GetPositions()
exchange.GetPositions(symbol)Examples
-
Query positions (default scope when no argument is given) and print each one.
javascriptfunction main() { var positions = exchange.GetPositions() if (!positions) return // null on failure, [] when there are no positions for (var pos of positions) { Log(pos.Symbol, "Amount", pos.Amount, "Price", pos.Price, "Profit", pos.Profit) } // Output: one line per position: symbol, amount, average price, unrealized profit }pythondef main(): positions = exchange.GetPositions() if positions is None: return for pos in positions: Log(pos["Symbol"], "Amount", pos["Amount"], "Price", pos["Price"], "Profit", pos["Profit"]) # Output: one line per position: symbol, amount, average price, unrealized profitrustfn main() { let Ok(positions) = exchange.GetPositions(None) else { return }; for pos in &positions { Log!(pos.Symbol, "Amount", pos.Amount, "Price", pos.Price, "Profit", pos.Profit); } // Output: one line per position: symbol, amount, average price, unrealized profit } -
Using the futures exchange object, place market orders on multiple symbols with different trading pairs and contract codes, and query position information through various methods.
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) + "`") // After printing the information once, return to prevent subsequent order fills during // backtesting from affecting 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) + "`") returnrust/*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"}] */ fn main() { let 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); } let defaultPositions = exchange.GetPositions(None).unwrap(); let swapPositions = exchange.GetPositions("USDT.swap").unwrap(); let futuresPositions = exchange.GetPositions("USDT.futures").unwrap(); let btcUsdtSwapPositions = exchange.GetPositions("BTC_USDT.swap").unwrap(); // The Rust SDK has no JSON serialization; use format! to concatenate the table's JSON text let mut tbls: Vec<String> = Vec::new(); let arr = [defaultPositions, swapPositions, futuresPositions, btcUsdtSwapPositions]; let tblDesc = ["defaultPositions", "swapPositions", "futuresPositions", "btcUsdtSwapPositions"]; for (index, positions) in arr.iter().enumerate() { let mut rows: Vec<String> = Vec::new(); for pos in positions { rows.push(format!(r#"["{}", {}, {}, {}, {}, {}, {}, "{}", {}]"#, pos.Symbol, pos.MarginLevel, pos.Amount, pos.FrozenAmount, pos.Price, pos.Profit, pos.Type, pos.ContractType, pos.Margin)); } let tbl = format!(r#"{{"type": "table", "title": "{}", "cols": ["Symbol", "MarginLevel", "Amount", "FrozenAmount", "Price", "Profit", "Type", "ContractType", "Margin"], "rows": [{}]}}"#, tblDesc[index], rows.join(",")); tbls.push(tbl); } LogStatus!(format!("`[{}]`", tbls.join(","))); // After printing the information once, return to prevent subsequent order fills during // backtesting from affecting data observation return; }
Returns
| Type | Description |
|
|
Arguments
| Name | Type | Required | Description |
symbol | string | No | The instrument (for example |
See Also
Remarks
Cryptocurrency futures contracts are different from cryptocurrency spot; spot only has a logical concept of position. In the FMZ Quant Trading Platform system, the specific instrument of a cryptocurrency futures contract is jointly identified by the **trading pair** and the **contract code**. Refer to the exchange.SetCurrency and exchange.SetContractType functions.
Range notation (USDT.swap, USD.futures, USDT.option, etc.) and the default range when symbol is omitted are described in Symbols; a range the exchange API does not support is reported as an error and returns a null value.
Compatible with the exchange.GetPosition() call; GetPosition and GetPositions are used in exactly the same way.
When the account represented by the exchange object exchange has no positions within the query scope or on the specified trading instrument, the exchange.GetPositions() function returns an empty array, for example: [].