OrdersHistoryTotal not reading history when starting EA

 

Hi,


I have a a couple of loops counting P/L trades and it works without issues in back-test but when I start the EA live, they fail to get the trades with matching magicnumber from Account History. 


Note that it still gets the live OrdersHistoryTotal number when starting EA live but can no longer find the profit and loss trades. There are plenty of trades with matching Magicnumber so this should not be an issue.


Any help is appriciated.  Thanks in advance


See code :


int history;
int profit = 0;
int loss = 0;
int oht = OrdersHistoryTotal()-1;
double partOHT = (OrdersHistoryTotal()*numOrders2);

for(int i = oht ; i >= partOHT ; i--)

  {
   
   OrderSelect(i, SELECT_BY_TICKET,MODE_HISTORY);
   if(OrderMagicNumber() == Magic && OrderSymbol() == symb) 

      {
      if(OrderType() == OP_BUY)
   
         {
         if(OrderClosePrice() > OrderOpenPrice())
            {
            profit++;
            }
         if(OrderClosePrice() < OrderOpenPrice())
            {
            loss++;
            }
         }
      }

      {
      if(OrderType() == OP_SELL)
         {
         if(OrderOpenPrice() > OrderClosePrice())
            {
            profit++;
            }
         if(OrderOpenPrice() < OrderClosePrice())
            {
            loss++;
            }
         }  
      }
    }
    
Print("PP " + profit);
Print("LL " + loss);
Print("OHT " + OrdersHistoryTotal());
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. int oht = OrdersHistoryTotal()-1;
    double partOHT = (OrdersHistoryTotal()*numOrders2);
    for(int i = oht ; i >= partOHT ; i--)

    OHT-1 will never be greater or equal than OHT * n.

  3. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum 2012.04.21
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 2020.06.08

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
William Roeder:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. OHT-1 will never be greater or equal than OHT * n.

  3. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum 2012.04.21
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 2020.06.08

Appologies, I have posted in the correct section now. It is my first post so unfortunatly I made a mistake and put it in the wrong area.

I have changed to this:


int oht = OrdersHistoryTotal()-1;
double partOHT = ((OrdersHistoryTotal()-1)*numOrders2);
for(int i = oht ; i >= partOHT ; i--)

However, issue is still the same. The code are counting profits and loss just fine in backtest but wont summarize profits and loss from Account history when EA is initialized. This confuse me greatly as I am quite new to coding, but have tried everything I can think of or find myself before asking for help.

 
martinRedg:

Appologies, I have posted in the correct section now. It is my first post so unfortunatly I made a mistake and put it in the wrong area.

Do not double post!!

Keith Watford:
I will move your topic to the MQL4 and Metatrader 4 section.

I had already told you that I moved the topic so there was no need for you to open a new one.

I have deleted your duplicate topic.

 
int oht = OrdersHistoryTotal()-1;
double partOHT = ((OrdersHistoryTotal()-1)*numOrders2);

for(int i = oht ; i >= partOHT ; i--)

As William has already pointed out, the for loop will never be executed. (unless numorders2 is less than 1)

 
Keith Watford:

As William has already pointed out, the for loop will never be executed. (unless numorders2 is less than 1)

Thank you for pointing this out Keith. However, even when numOrders2 is < 1 the issue remain. It wont read AccountHistory to give me the live trades in there when starting EA (but has no issues when backtesting, counting just fine then). Still trying every approach I can think of but nothing seems to mitigate the problem.
 
martinRedg:
Thank you for pointing this out Keith. However, even when numOrders2 is < 1 the issue remain. It wont read AccountHistory to give me the live trades in there when starting EA (but has no issues when backtesting, counting just fine then). Still trying every approach I can think of but nothing seems to mitigate the problem.

Why are you even using this  numOrders2?

Why not simply

int oht = OrdersHistoryTotal()-1;
for(int i = oht ; i >= 0 ; i--)
 
Keith Watford:

Why are you even using this  numOrders2?

Why not simply

This was as I only want to use x % of the latest history, so if total history is for example 300, I want to use the past say 100 of those trades. Point being to get recent history and not use too old trades done under different market conditions but rather have an ongoing update taking current conditions first and foremost into account

Btw have tried what you suggest before too as part of trying to find what the issue is, and it still wont print the profits and losses with magic number from account history. 

 
martinRedg:

This was as I only want to use x % of the latest history, so if total history is for example 300, I want to use the past say 100 of those trades.

Btw have tried what you suggest before too as part of trying to find what the issue is, and it still wont print the profits and losses with specified magic number from account history. 

I just noticed

OrderSelect(i, SELECT_BY_TICKET,MODE_HISTORY);

You should be selecting by position, not ticket.

 
martinRedg:

This was as I only want to use x % of the latest history, so if total history is for example 300, I want to use the past say 100 of those trades. Point being to get recent history and not use too old trades

OrdersHistory is not guaranteed to be in chronological order. If you want to filter by time, then filter by only checking trades that opened or closed after a particular time.

Reason: