positions_get
positions_get
Get open positions with the ability to filter by symbol or ticket. There are three call options.
Call without parameters. Return open positions for all symbols.
positions_get()Call specifying a symbol open positions should be received for.
positions_get(
symbol="SYMBOL" // symbol name
)Call specifying a group of symbols open positions should be received for.
positions_get(
group="GROUP" // filter for selecting positions by symbols
)Call specifying a position ticket.
positions_get(
ticket=TICKET // ticket
)Parameters
- symbol=“SYMBOL”
[in] Symbol name. Optional named parameter. If a symbol is specified, the ticket parameter is ignored.
- group=“GROUP”
[in] The filter for arranging a group of necessary symbols. Optional named parameter. If the group is specified, the function returns only positions meeting a specified criteria for a symbol name.
- ticket=TICKET
[in] Position ticket (POSITION_TICKET). Optional named parameter.
Return Value
Return info in the form of a named tuple structure (namedtuple). Return None in case of an error. The info on the error can be obtained using last_error().
Note
The function allows receiving all open positions within one call similar to the PositionsTotal and PositionSelect tandem.
The group parameter may contain several comma separated conditions. A condition can be set as a mask using ‘’. The logical negation symbol ‘!’ can be used for an exclusion. All conditions are applied sequentially, which means conditions of including to a group should be specified first followed by an exclusion condition. For example, group=", !EUR" means that positions for all symbols should be selected first and the ones containing “EUR” in symbol names should be excluded afterwards.
Example:
import MetaTrader5 as mt5
import pandas as pd
pd.set_option('display.max_columns', 500) # number of columns to be displayed
pd.set_option('display.width', 1500) # max table width to display
# display data on the MetaTrader 5 package
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
print()
# establish connection to the MetaTrader 5 terminal
if not mt5.initialize():
print("initialize() failed, error code =",mt5.last_error())
quit()
# get open positions on USDCHF
positions=mt5.positions_get(symbol="USDCHF")
if positions==None:
print("No positions on USDCHF, error code={}".format(mt5.last_error()))
elif len(positions)>0:
print("Total positions on USDCHF =",len(positions))
# display all open positions
for position in positions:
print(position)
# get the list of positions on symbols whose names contain "*USD*"
usd_positions=mt5.positions_get(group="*USD*")
if usd_positions==None:
print("No positions with group=\"*USD*\", error code={}".format(mt5.last_error()))
elif len(usd_positions)>0:
print("positions_get(group=\"*USD*\")={}".format(len(usd_positions)))
# display these positions as a table using pandas.DataFrame
df=pd.DataFrame(list(usd_positions),columns=usd_positions[0]._asdict().keys())
df['time'] = pd.to_datetime(df['time'], unit='s')
df.drop(['time_update', 'time_msc', 'time_update_msc', 'external_id'], axis=1, inplace=True)
print(df)
# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()
Result:
MetaTrader5 package author: MetaQuotes Software Corp.
MetaTrader5 package version: 5.0.29
positions_get(group="*USD*")=5
ticket time type magic identifier reason volume price_open sl tp price_current swap profit symbol comment
0 548297723 2020-03-18 15:00:55 1 0 548297723 3 0.01 1.09301 1.11490 1.06236 1.10104 -0.10 -8.03 EURUSD
1 548655158 2020-03-18 20:31:26 0 0 548655158 3 0.01 1.08676 1.06107 1.12446 1.10099 -0.08 14.23 EURUSD
2 548663803 2020-03-18 20:40:04 0 0 548663803 3 0.01 1.08640 1.06351 1.11833 1.10099 -0.08 14.59 EURUSD
3 548847168 2020-03-19 01:10:05 0 0 548847168 3 0.01 1.09545 1.05524 1.15122 1.10099 -0.06 5.54 EURUSD
4 548847194 2020-03-19 01:10:07 0 0 548847194 3 0.02 1.09536 1.04478 1.16587 1.10099 -0.08 11.26 EURUSD