MT5 Python positions_get()
Returns the open positions on the account: all of them, those in one symbol, those whose symbol matches a filter, or the one with a given ticket.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
This is a Python function, not an HTTP endpoint. It has no method, no path and no address to send a request to: the package talks to a MetaTrader 5 terminal running on the same computer.
The page shows four forms of the call: with nothing, with symbol, with group and with ticket. Its opening line says three.
Signature
The 4 forms of the call on the vendor's page, with the vendor's comments taken out.
positions_get()
positions_get( symbol="SYMBOL" )
positions_get( group="GROUP" )
positions_get( ticket=TICKET )
Arguments
| Name | Type | Description |
|---|---|---|
| symbol | Not stated | A symbol name, to return only that symbol's positions. The page says that when a symbol is given the ticket argument is ignored. |
| group | Not stated | A filter on symbol names. The page describes it as comma separated conditions, where * is a wildcard and a leading ! excludes, applied in the order given. |
| ticket | Not stated | The ticket of one position, the value the page calls POSITION_TICKET. |
Return value
- Type
- tuple of namedtuples, or None
- What comes back
- One named tuple per position. On failure the page says it returns None, and that last_error() gives the reason.
Sample
Written by this site from the signature and arguments recorded on this page. It has not been run against a terminal, a server or an account, and the values in it are placeholders. No credential is typed into it: where one is needed it is read from an environment variable on your own machine. Nothing is entered on this site.
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(f"initialize() failed: {mt5.last_error()}")
positions = mt5.positions_get()
if positions is None:
print("positions_get() failed:", mt5.last_error())
else:
for p in positions:
print(p.ticket, p.symbol, p.type, p.volume, p.price_open, p.profit)
mt5.shutdown()