Positions¶
Open position models and retrieval services.
Models¶
Position¶
Position
¶
Bases: BaseModel
An open trading position in MetaTrader 5.
Wraps the raw TradePosition struct returned by mt5.positions_get().
In addition to the raw fields, computed properties expose pip-based
profit metrics that are commonly needed for position management.
digits and point are not part of the MT5 position struct — they
are fetched from mt5.symbol_info() by PositionService and
attached here so that pip calculations are self-contained.
| ATTRIBUTE | DESCRIPTION |
|---|---|
ticket |
Unique position identifier.
TYPE:
|
time |
Position open time as a Unix timestamp (seconds).
TYPE:
|
time_msc |
Position open time in milliseconds.
TYPE:
|
time_update |
Last modification time as a Unix timestamp (seconds).
TYPE:
|
time_update_msc |
Last modification time in milliseconds.
TYPE:
|
type |
Direction of the position (BUY or SELL).
TYPE:
|
magic |
Expert Advisor identifier; 0 for manual trades.
TYPE:
|
identifier |
Position identifier matching the opening order ticket.
TYPE:
|
reason |
How the position was opened (terminal, EA, etc.).
TYPE:
|
volume |
Current position size in lots.
TYPE:
|
price_open |
Price at which the position was opened.
TYPE:
|
sl |
Stop loss price; 0.0 if not set.
TYPE:
|
tp |
Take profit price; 0.0 if not set.
TYPE:
|
price_current |
Current market price for the position's symbol.
TYPE:
|
swap |
Accumulated swap charges.
TYPE:
|
profit |
Floating profit/loss in account currency.
TYPE:
|
symbol |
Trading instrument name (e.g.
TYPE:
|
comment |
Arbitrary comment attached to the position.
TYPE:
|
external_id |
Position identifier in an external system.
TYPE:
|
digits |
Number of decimal places for the symbol's price.
TYPE:
|
point |
Smallest price increment for the symbol.
TYPE:
|
pip_size
property
¶
Pip size in price units for this symbol.
Delegates to calculate_pip_size which accounts for the
fractional-pip convention used by 3- and 5-digit symbols.
pips_profit
property
¶
Current floating profit expressed in pips.
Positive values indicate profit; negative values indicate loss.
Direction is taken from type so the sign is always correct
for both long and short positions.
| RETURNS | DESCRIPTION |
|---|---|
float
|
Profit in pips relative to the open price. |
pips_to_tp
property
¶
Distance from the current price to the take profit level in pips.
Returns 0.0 when no take profit is set (tp == 0). A positive
value means the TP has not yet been reached; a negative value means
the price has already passed the TP level.
| RETURNS | DESCRIPTION |
|---|---|
float
|
Distance to take profit in pips, or 0.0 if TP is not set. |
Service¶
PositionService¶
PositionService
¶
Retrieves open positions from the MT5 terminal.
Maintains an internal cache of (digits, point) pairs per symbol so
that symbol_info() is only called once per unique symbol across
repeated positions() calls within the same service instance.
positions
¶
Retrieve all open positions, optionally filtered by symbol.
Fetches (digits, point) from mt5.symbol_info() for each
unique symbol in the result set (using the cache to avoid redundant
calls) and attaches them to each Position for pip calculations.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
If provided, only positions for this symbol are returned.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Position]]
|
|
Result[list[Position]]
|
(may be empty), or a failure result if the MT5 call fails or the |
Result[list[Position]]
|
payload cannot be parsed. |