Discussion of article "Cross-Platform Expert Advisor: Orders"

 

New article Cross-Platform Expert Advisor: Orders has been published:

MetaTrader 4 and MetaTrader 5 uses different conventions in processing trade requests. This article discusses the possibility of using a class object that can be used to represent the trades processed by the server, in order for a cross-platform expert advisor to further work on them, regardless of the version of the trading platform and mode being used.

The following shows a screen shot of the platform upon executing the EA. Note that since the EA calls the ExpertRemove function, it is automatically removed from the chart as soon as it executes its code (only single execution for the OnTick handler).




Author: Enrico Lambino

 

Enrico is it possible to check if the order (or position) was partially closed. 

In MT4 I have a new ticket in case of partial close. In fact, it is not a new entry but still previous entry. So I need separate such orders. The ones which indicate a true market entry and the ones which appear after partial close (not a new entry). Is it possible using your classes?

 
mbjen:

Enrico is it possible to check if the order (or position) was partially closed. 

In MT4 I have a new ticket in case of partial close. In fact, it is not a new entry but still previous entry. So I need separate such orders. The ones which indicate a true market entry and the ones which appear after partial close (not a new entry). Is it possible using your classes?


Or it would be one COrder object until the order closed fully? I mean after partial close no new COrder object?

If so how do I get the total order profit?

Also, how do I get initial order ticket or initial order type?

 

Suppose I have sell 5.00 #1 order at 1.09246. Next partial close buy 2.00 #2 at 1.08896. And final close remaining buy 3.00 #3 at 1.09161 (due to trailing stop actually).

This is my code:

   COrders *orders=order_manager.OrdersHistory();
   for(int i=order_manager.OrdersHistoryTotal()-1;i>=0;i--)
     {
      COrder *order=orders.At(i);
      if(!CheckPointer(order))
         continue;
      order.OnTick();
      if(!order.IsClosed())
         continue;
      if(order.OrderType()!=0 && order.OrderType()!=1)   
         continue;

To simplify let's call all these 3 orders as position. 

IsClosed() method returns true when the position closes (at last order #3). But how do I get the position type, position first order ticket, position open price. Position initial volume looks like can be calculated by using InitVolume() method. What about the rest?

 
mbjen:

Suppose I have sell 5.00 #1 order at 1.09246. Next partial close buy 2.00 #2 at 1.08896. And final close remaining buy 3.00 #3 at 1.09161 (due to trailing stop actually).

This is my code:

To simplify let's call all these 3 orders as position. 

IsClosed() method returns true when the position closes (at last order #3). But how do I get the position type, position first order ticket, position open price. Position initial volume looks like can be calculated by using InitVolume() method. What about the rest?

Not entirely sure at what you are trying to do, but to get the unrealized profit/loss in MQL5 on a COrder instance, one way is to get the remaining volume, and then multiply it with the difference between the current market price (bid or ask) and the COrder entry price, and then multiply it with the tick value. The trickier part is when the symbol's point is not equal to the tick size (you will need to divide the difference by the tick size). Gold used to be like this, but not anymore as far as I know. It is simply better to find a broker that offers greater liquidity. The same COrder instance is used until the end. In MQL4, you just need to select the order ticket and call the OrderProfit() function.

 
2017/10/30 09:04:38 Completed #169758 keltrem
 

When one sends a trade request, there are only two outcomes: processed or not processed. If the trade was not processed, it means that there was no deal, as the trade server was not able to process it for some reason (usually due to errors). Now, if the trade is processed, in MQL5, the client and the server have a deal. In this case, the order can be fully executed or partially executed.

That's not totally exact, you can also have a timeout. Which means you don't know if the order was processed or not. Of course in the end, an order is either processed or not, but it's important to know and process timeout on a live account.

MetaTrader 4 does not have this option, as the order is only executed entirely or not (fill or kill).

...

In MQL4, the calculation of volume is straightforward. Whenever an expert advisor sends a trade request, the volume of the request is also included, and it would be either denied or accepted. 

That's not exact. MT4 can also have partially filled orders. Of course it should be rare on Forex which is mainly traded by MT4, but it may happen technically.


In general I don't see the usefulness of the classes your provided in this article, as you still need specific MT4/MT5 in the main code, and compiler directives. (Maybe it's addressed on further articles, I didn't read them yet).