How to get the open, close and profit price of an order from history section in MQL5

To add comments, please log in or register
Jhojan Alberto Tobon Monsalve
295
Jhojan Alberto Tobon Monsalve  

I'm trying to get the open, close prices and the profit of an ORDER in the history section, but I do not know how to achieve this. I'm trying with the following code:

    HistorySelect(0,TimeCurrent());
    int totalDeals = HistoryDealsTotal();
    int totalOrders = HistoryOrdersTotal();
    
    
    int i = 0;
    
    ulong dealTicket = HistoryDealGetTicket(i+1);
    ulong orderTicet = HistoryOrderGetTicket(i);
    double orderProfit = HistoryDealGetDouble(dealTicket,DEAL_PROFIT);
    double orderOpenPrice = HistoryOrderGetDouble(orderTicet,ORDER_PRICE_OPEN);
    double orderClosePrice = HistoryDealGetDouble(dealTicket,DEAL_PRICE);

But the tickets of DEALS and ORDERS are different and I do not know if there is a way to get the  open price of a DEAL or the close price and the profit of an ORDER.

I need to migrate an EA from Mql4 [ OrderProfit(),OrderClosePrice(),OrderOpenPrice()] to Mql5. 

Thanks in advance.

Giovanni Riccobene
11
Giovanni Riccobene  

look this https://www.mql5.com/en/code/19702

and this https://www.mql5.com/en/code/21434

VR Orders History MT5 Lite
VR Orders History MT5 Lite
  • www.mql5.com
VR Orders History MT5 Lite - a script for downloading trading history in the CSV format. The Lite version demonstrates a small part of the source code of the VR Orders History MT5 script. This universal script can run both on hedging and netting accounts. The script divides the report into two stages: The first stage includes existing current...
fxsaber
12466
fxsaber  
Jhojan Alberto Tobon Monsalve:

I need to migrate an EA from Mql4 [ OrderProfit(),OrderClosePrice(),OrderOpenPrice()] to Mql5.

Forum on trading, automated trading systems and testing trading strategies

How to figure out the close price when position ran into stop loss?

fxsaber, 2018.07.10 11:46

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

void OnStart()
{
  const int Total = OrdersHistoryTotal();
  
  for (int i = 0; i < Total; i++)
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      OrderPrint();  
}
Hosein Daneshnia
249
Hosein Daneshnia  
Jhojan Alberto Tobon Monsalve:

I'm trying to get the open, close prices and the profit of an ORDER in the history section, but I do not know how to achieve this. I'm trying with the following code:

But the tickets of DEALS and ORDERS are different and I do not know if there is a way to get the  open price of a DEAL or the close price and the profit of an ORDER.

I need to migrate an EA from Mql4 [ OrderProfit(),OrderClosePrice(),OrderOpenPrice()] to Mql5. 

Thanks in advance.

hi you can use this one : 


void history_checker()
{
int margines=0;
string ordertyp="NULL";
double prft=0;
for( int TradeCount=OrdersHistoryTotal()-1;TradeCount>=0;TradeCount--)
      {
        if ( OrderSelect(TradeCount,SELECT_BY_POS,MODE_HISTORY))
        {
          if(OrderType()==0)
          ordertyp="BUY";
          else if (OrderType()==1)
          ordertyp="SELL";
          else if (OrderType()>1)
          ordertyp="NULL";
          prft=(OrderSwap()+OrderProfit())-OrderCommission();
          
         //margines:=iBarShift(OrderSymbol(),PERIOD_W1,OrderOpenTime()); 
         //OrderType();    OrderProfit()  ; datetime OrderCloseTime() ;
         if(OrderType()<=1)
         { 
         Print("Order:"+ ordertyp +":"+"Order Profit:"+DoubleToStr(prft,2)+":"+"Order Open Time:"+TimeToStr(OrderOpenTime(),TIME_DATE|TIME_SECONDS)+":"+"Order Close Time:"+TimeToStr(OrderCloseTime(),TIME_DATE|TIME_SECONDS)         
          );
          
          }
      }
     }

Jevgeni
5
Jevgeni  

Jhojan, I am pretty sure that you already  found your way around the problem, as it is now more than 6 months since your initial post :)

Still, to keep other people aware of the solution (and the search engines, of course), I'd like to demonstrate, how I solved this problem.

First, you need to make sure, you understand a couple of concepts:

1. Order is something you send to the broker.

2. Deal is something that is executed in the market on your behalf by the broker.

3. Within one order there are opening deal, closing deal and some other kinds of deals.

4. Order tickets numbers and deal ticket numbers are different (as you pointed out correctly).

5. To close order, essentially an opposite deal is executed on the market.


What you have to do in MQL5 code:

1. First you need to select a history of all deals that are related to your order number. Order number is the number that you can find on the History tab in MT5 terminal, in the column called "Ticket".

HistorySelectByPosition(1234567890); // where 1234567890 is a ticket number of your ORDER

2. Iterate over all deals related to the order; find the one with entry type DEAL_ENTRY_OUT (meaning, this is the deal for position closing); get price of that deal.

I've created a function for convenience and sharing it here with you:

double MT5_getOrderClosePrice(void) {

    for (int i = 0; i < HistoryDealsTotal(); i++) {

        ulong deal_ticket = HistoryDealGetTicket(i);

        if (HistoryDealGetInteger(deal_ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT) {

            return HistoryDealGetDouble(deal_ticket, DEAL_PRICE);

        }

    }

    return 0.0;

}


To get answers to other questions, you can just follow same principle and consult MQL5 API documentation a bit. 

To add comments, please log in or register