Enter new order only when first is executed (filled)

 

Hey, I'm kinda of new in MQL5. I have a 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

 

Function

Action

OrderCalcMargin

Calculates the margin required for the specified order type, in the deposit currency

OrderCalcProfit

Calculates the profit based on the parameters passed, in the deposit currency

OrderCheck

Checks if there are enough funds to execute the required trade operation.

OrderSend

Sends trade requests to a server

OrderSendAsync

Asynchronously sends trade requests without waiting for the trade response of the trade server

PositionsTotal

Returns the number of open positions

PositionGetSymbol

Returns the symbol corresponding to the open position

PositionSelect

Chooses an open position for further working with it

PositionSelectByTicket

Selects a position to work with by the ticket number specified in it

PositionGetDouble

Returns the requested property of an open position (double)

PositionGetInteger

Returns the requested property of an open position (datetime or int)

PositionGetString

Returns the requested property of an open position (string)

PositionGetTicket

Returns the ticket of the position with the specified index in the list of open positions

OrdersTotal

Returns the number of orders

OrderGetTicket

Return the ticket of a corresponding order

OrderSelect

Selects a order for further working with it

OrderGetDouble

Returns the requested property of the order (double)

OrderGetInteger

Returns the requested property of the order (datetime or int)

OrderGetString

Returns the requested property of the order (string)

HistorySelect

Retrieves the history of transactions and orders for the specified period of the server time

HistorySelectByPosition

Requests the history of deals with a specified position identifier.

HistoryOrderSelect

Selects an order in the history for further working with it

HistoryOrdersTotal

Returns the number of orders in the history

HistoryOrderGetTicket

Return order ticket of a corresponding order in the history

HistoryOrderGetDouble

Returns the requested property of an order in the history (double)

HistoryOrderGetInteger

Returns the requested property of an order in the history (datetime or int)

HistoryOrderGetString

Returns the requested property of an order in the history (string)

HistoryDealSelect

Selects a deal in the history for further calling it through appropriate functions

HistoryDealsTotal

Returns the number of deals in the history

HistoryDealGetTicket

Returns a ticket of a corresponding deal in the history

HistoryDealGetDouble

Returns the requested property of a deal in the history (double)

HistoryDealGetInteger

Returns the requested property of a deal in the history (datetime or int)

HistoryDealGetString

Returns the requested property of a deal in the history (string)


For the function PositionGetInteger()

ENUM_POSITION_PROPERTY_INTEGER

Identifier

Description

Type

POSITION_TICKET

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.

long

POSITION_TIME

Position open time

datetime

POSITION_TIME_MSC

Position opening time in milliseconds since 01.01.1970

long

POSITION_TIME_UPDATE

Position changing time in seconds since 01.01.1970

long

POSITION_TIME_UPDATE_MSC

Position changing time in milliseconds since 01.01.1970

long

POSITION_TYPE

Position type

ENUM_POSITION_TYPE

POSITION_MAGIC

Position magic number (see ORDER_MAGIC)

long

POSITION_IDENTIFIER

Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.

 

Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.

 

When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.

long

POSITION_REASON

The reason for opening a position

ENUM_POSITION_REASON


Usually you can analyze the position open time to see whether a position was opened or not.

 
 
Alain Verleyen:
You need to use OnTradeTransaction().

Hey, thanks for your reply...

How do I use the OnTradeTransaction() to keep checking my order until it gets filled, so I can order the second one?

Sorry if I'm asking simple question, I'm kinda new to all this...(I read this article (https://www.mql5.com/en/articles/1111) but t didn't clarify a lot to me.

Thanks again

MQL5 Cookbook: Processing of the TradeTransaction Event
MQL5 Cookbook: Processing of the TradeTransaction Event
  • 2014.10.09
  • Dennis Kirichenko
  • www.mql5.com
This article considers capabilities of the MQL5 language from the point of view of the event-driven programming. The greatest advantage of this approach is that the program can receive information about phased implementation of a trade operation. The article also contains an example of receiving and processing information about ongoing trade operation using the TradeTransaction event handler. In my opinion, such an approach can be used for copying deals from one terminal to another.
 
Marco vd Heijden:

Function

Action

OrderCalcMargin

Calculates the margin required for the specified order type, in the deposit currency

OrderCalcProfit

Calculates the profit based on the parameters passed, in the deposit currency

OrderCheck

Checks if there are enough funds to execute the required trade operation.

OrderSend

Sends trade requests to a server

OrderSendAsync

Asynchronously sends trade requests without waiting for the trade response of the trade server

PositionsTotal

Returns the number of open positions

PositionGetSymbol

Returns the symbol corresponding to the open position

PositionSelect

Chooses an open position for further working with it

PositionSelectByTicket

Selects a position to work with by the ticket number specified in it

PositionGetDouble

Returns the requested property of an open position (double)

PositionGetInteger

Returns the requested property of an open position (datetime or int)

PositionGetString

Returns the requested property of an open position (string)

PositionGetTicket

Returns the ticket of the position with the specified index in the list of open positions

OrdersTotal

Returns the number of orders

OrderGetTicket

Return the ticket of a corresponding order

OrderSelect

Selects a order for further working with it

OrderGetDouble

Returns the requested property of the order (double)

OrderGetInteger

Returns the requested property of the order (datetime or int)

OrderGetString

Returns the requested property of the order (string)

HistorySelect

Retrieves the history of transactions and orders for the specified period of the server time

HistorySelectByPosition

Requests the history of deals with a specified position identifier.

HistoryOrderSelect

Selects an order in the history for further working with it

HistoryOrdersTotal

Returns the number of orders in the history

HistoryOrderGetTicket

Return order ticket of a corresponding order in the history

HistoryOrderGetDouble

Returns the requested property of an order in the history (double)

HistoryOrderGetInteger

Returns the requested property of an order in the history (datetime or int)

HistoryOrderGetString

Returns the requested property of an order in the history (string)

HistoryDealSelect

Selects a deal in the history for further calling it through appropriate functions

HistoryDealsTotal

Returns the number of deals in the history

HistoryDealGetTicket

Returns a ticket of a corresponding deal in the history

HistoryDealGetDouble

Returns the requested property of a deal in the history (double)

HistoryDealGetInteger

Returns the requested property of a deal in the history (datetime or int)

HistoryDealGetString

Returns the requested property of a deal in the history (string)


For the function PositionGetInteger()

ENUM_POSITION_PROPERTY_INTEGER

Identifier

Description

Type

POSITION_TICKET

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.

long

POSITION_TIME

Position open time

datetime

POSITION_TIME_MSC

Position opening time in milliseconds since 01.01.1970

long

POSITION_TIME_UPDATE

Position changing time in seconds since 01.01.1970

long

POSITION_TIME_UPDATE_MSC

Position changing time in milliseconds since 01.01.1970

long

POSITION_TYPE

Position type

ENUM_POSITION_TYPE

POSITION_MAGIC

Position magic number (see ORDER_MAGIC)

long

POSITION_IDENTIFIER

Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.

 

Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.

 

When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.

long

POSITION_REASON

The reason for opening a position

ENUM_POSITION_REASON


Usually you can analyze the position open time to see whether a position was opened or not.


Hey, thanks for your reply. I didn't understand how I can know if my position was filled by using the positon open time...

Thanks again

 
LucasHeise:

Hey, thanks for your reply. I didn't understand how I can know if my position was filled by using the positon open time...

Thanks again

What do you mean by "my position was filled" ? A position is open or close, but filled ??
 
Alain Verleyen:
What do you mean by "my position was filled" ? A position is open or close, but filled ??
He means the point where his order becomes a live position
 
Alain Verleyen:
What do you mean by "my position was filled" ? A position is open or close, but filled ??

I mean when my order becomes a trade or a deal (it actually executes and it gets filled)

 

I actually found a way of doing it by using the historySelect and HistoryDealsTotal functions...

Thanks for the replies

Reason: