orderhistory

 

Good day I have been coding with mql4 and have recently had to change to a broker that uses mt5 only.

I have lines in mql4 that I dont know how to translate to mql5 language because my results are faulty.

Basically what this says is, if i have for example 5 closed positions in the history log arranged in order, the expert should select the latest one.

e.g (history log with positions)

875545 ---->SELECT THIS ORDER

875544

875543

875542

875541

Here are those lines please help

   int i,hstrTotal=OrdersHistoryTotal();

   for(i=hstrTotal-1;i<hstrTotal;i++)
{ 
    if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderType==OP_BUY)

    {
    Print("SELL");
    }   
}

Please help. Thank you

Files:
2019-08-04.png  28 kb
 
  1. Do not assume history has only closed orders.
    Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum

  2. Using OrdersTotal (or OrdersHistoryTotal) directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

 
William Roeder:
Do not assume history has only closed orders.
Do not assume history is ordered by date, it's not.
          Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum

I am not assuming anything,I do realise I need a code for sequence and also that orderhistory includes open orders, that is fine I have counter measures for that,all I need is to translate the lines to MQL5.

I have tried replacing "OrdersHistoryTotal" with "HistoryOrdersTotal" but it does not work...

 
Moses Mad: tried replacing "OrdersHistoryTotal" with "HistoryOrdersTotal" but it does not work...

Of course it doesn't. The first is a function. The second is BS. Learn to code.

 
William Roeder:

Of course it doesn't. The first is a function. The second is BS. Learn to code.

//--- request trade history 
   HistorySelect(from_date,to_date);
   uint total_deals=HistoryDealsTotal();
   ulong ticket_history_deal=0;
//--- for all deals 
   for(uint i=total_deals-1;i<total_deals;i++)
     {
     }
^This is all I needed, Thanks for nothing. 
 

Forum on trading, automated trading systems and testing trading strategies

OrderCloseTime Expert Advisor MQL5

fxsaber, 2018.07.06 00:49

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

void LastTimeMQL4( datetime &OpenTime, datetime &CloseTime )
{
  for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)  
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderType() <= OP_SELL))
    {
      OpenTime = OrderOpenTime();
      CloseTime = OrderCloseTime();
      
      break;
    }
}

void LastTimeMQL5( datetime &OpenTime, datetime &CloseTime )
{
  if (HistorySelect(0, INT_MAX))
  {
    for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
    {
      const ulong Ticket = HistoryDealGetTicket(i);
  
      if (HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
      {
        CloseTime = (datetime)HistoryDealGetInteger(Ticket, DEAL_TIME);

        if (HistorySelectByPosition(HistoryDealGetInteger(Ticket, DEAL_POSITION_ID)))
          OpenTime = (datetime)HistoryDealGetInteger(HistoryDealGetTicket(0), DEAL_TIME);
          
        break;
      }
    }
  }
}