2

I'm kinda of new in MQL5.

I have an EA that buys one stock first (let's say PETR4) and then buys another one (let's say ABEV3).

The only problem is that I can only order the second one when the first one is executed (not only placed).

I used the MqlTradeResult.retcode to get the return number, but that only shows if the order has been placed and I need to know if the order has been filled in order to continue with the second operation.

Can anyone help me? Thanks

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Lucas
  • 41
  • 4

1 Answers1

1

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.

user3666197
  • 1
  • 6
  • 50
  • 92
  • Hey thanks for your reply. I tried this function but in my case, I don't need to check if the deal was closed. What I need is something like this: Order 100 PETR4 --- Wait for this order to turn into a deal (gets filled) --- Whe the first order gets filled Order 200 ABEV3. The method that you showed also gets the orders that are filled (deals) – Lucas Aug 15 '17 at 16:41
  • Seems you missed the point: `... OrderSend( reqStructPETR4, resStructPETR4 ); ... if ( PositionSelect( "PETR4" ) ) /* proves the fill-ed state */ { ... OrderSend( reqStructABEV3, resStructABEV3 ); ... }` is matching your intent. In a not mentioned use-case - If individual positions are allowed ( `ACCOUNT_MARGIN_MODE_RETAIL_HEDGING` ), multiple positions can be open for one symbol. In this case, `PositionSelect()` will select a position with the lowest ticket. This would still lead to a `PositionSelectByTicket()` introspection factory, if in a need to keep the logic of opening an `ABEV3` trade. – user3666197 Aug 15 '17 at 18:47