Such indication ought be coded in a PositionSelect()
call:
First call
if ( PositionSelect( "PETR4" ) ) { ... /* further work will go here */}
This chooses an open position for further working with it. Returns True
if the function is successfully completed. Returns False
in case of failure.
If there is any active position reported for the given symbol, the further work will go ahead inside the {...}
-code-block
The hidden magic is in a fact, there is either a single-position for any symbol, or none at all. This makes the rest of the logic crystal-clear.
Execution of trade operations results in the opening of a position, changing of its volume and/or direction, or its disappearance. Trade operations are conducted based on orders, sent by the OrderSend()
function in the form of trade requests. For each financial security (symbol) only one open position is possible. A position has a set of properties available for reading by the PositionGet...()
functions.
...
POSITION_TICKET
Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the POSITION_IDENTIFIER
property.
POSITION_TICKET
value corresponds to MqlTradeRequest::position
.