Tracking order by using OrderMagicNumber for an EA

 

I have a question regrading on the OrderMagicNumber.
I knew that we can set a manual magic number in the EA settings to set our strategies. But I counter a problem which is during backtesting which I track down previous order by using 

for(int i=OrdersHistoryTotal()-1; i < OrdersHistoryTotal(); i++)
         if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) && (OrderMagicNumber() == order_magic) && (OrderSymbol() == Symbol()))

to complete certain function.

Yes it successfully do what what I expecting during backtesting without any errors.

But I don't know why, when I run it in my real trading account it do not open any orders and didn't came out any warnings and errors.

Also I already confirm that it is the problem when I place this function( highlighted ) because after I delete the highlighted function the EA can run and open orders
Because I wanted to place the same EA with different strategies in the same currency by changing the order magic number.

for(int i=OrdersHistoryTotal()-1; i < OrdersHistoryTotal(); i++)
         if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) && (OrderMagicNumber() == order_magic) && (OrderSymbol() == Symbol()))

Am I doing some common mistake or something else?
Hope someone could kindly guide me on this.

Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC", "Every tick" and "Every tick based on real ticks" using actual historical data.
 
for(int i=OrdersHistoryTotal()-1; i < OrdersHistoryTotal(); i++)
         if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) && (OrderMagicNumber() == order_magic) && (OrderSymbol() == Symbol()))

What is the point of the loop?

There can only be one pass!

 
Keith Watford:

What is the point of the loop?

There can only be one pass!

Because the following functions are selecting previous closed order information that's why I place the for loop
I post the code down here

{
      /* entry */
         if(OrdersHistoryTotal() == 0)
         {
            int count_orders=0;
            if(entry>0)
            {
               if(entry==TRADE_SIGNAL_BUY)
               {
                  if(exit_opposite_signal)
                     exit_all_set(ORDER_SET_SELL,order_magic);
                  count_orders=count_orders(-1,order_magic);
                  if(maxtrades>count_orders)
                  {
                     if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                        enter_order(OP_BUY);
                  }
               }
               else if(entry==TRADE_SIGNAL_SELL)
               {
                  if(exit_opposite_signal)
                     exit_all_set(ORDER_SET_BUY,order_magic);
                  count_orders=count_orders(-1,order_magic);
                  if(maxtrades>count_orders)
                  {
                     if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                        enter_order(OP_SELL);
                  }
               }
            }
         }
         else if(OrdersHistoryTotal()>0)
      { static datetime time_open;
         for(int i=OrdersHistoryTotal()-1; i < OrdersHistoryTotal(); i++)
         if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) && (OrderMagicNumber() == order_magic) && (OrderSymbol() == Symbol()))
         {
            if((calculate_trigger()>=(OrderCloseTime()-OrderOpenTime())) && OrderProfit() < 0)
            {
               int count_orders=0;
               if(entry>0)
               {
                  if(entry==TRADE_SIGNAL_BUY)
                     {
                        if(exit_opposite_signal)
                           exit_all_set(ORDER_SET_SELL,order_magic);
                        count_orders=count_orders(-1,order_magic);
                        if(maxtrades>count_orders)
                        {
                           if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                             if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)&& (OrderMagicNumber() == order_magic) && (OrderSymbol() == Symbol()))
                              {
                                 time_open=OrderCloseTime()+(Bar_to_entry*Period()*60);
                                 if(time_open<Time[0])
                                 {
                                 enter_martingale_order(OP_BUY); 
                                 }
                              } 
                                                   
                        }
                     }
                  else if(entry == TRADE_SIGNAL_SELL)
                     {
                     if(exit_opposite_signal)
                     exit_all_set(ORDER_SET_BUY,order_magic);
                        count_orders=count_orders(-1,order_magic);
                        if(maxtrades>count_orders )
                        {
                           if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                             if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)&& (OrderMagicNumber() == order_magic) && (OrderSymbol() == Symbol()))
                              {
                                 time_open=OrderCloseTime()+(Bar_to_entry*Period()*60);
                                 if(time_open<Time[0])
                                 {
                                 enter_martingale_order(OP_SELL); 
                                 }
                              } 
                           
                        }
                     }
            
               }
            
            }
            else if((calculate_trigger()<=(OrderCloseTime()-OrderOpenTime())) && OrderProfit() < 0 && OrderClosePrice() >= OrderOpenPrice() + breakeven_plus)
            {
               int count_orders=0;
               if(entry>0)
               {
                  if(entry==TRADE_SIGNAL_BUY)
                     {
                        if(exit_opposite_signal)
                           exit_all_set(ORDER_SET_SELL,order_magic);
                        count_orders=count_orders(-1,order_magic);
                        if(maxtrades>count_orders)
                        {
                           if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                           enter_martingale_order(OP_BUY);                                                    
                        }
                     }
                  else if(entry == TRADE_SIGNAL_SELL)
                     {
                     if(exit_opposite_signal)
                     exit_all_set(ORDER_SET_BUY,order_magic);
                        count_orders=count_orders(-1,order_magic);
                        if(maxtrades>count_orders )
                        {
                           if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                           enter_martingale_order(OP_SELL); 
                        }
                     }
               }
            }
            else if(OrderProfit()>=0)
            {
               int count_orders=0;
               if(entry>0)
               {
                  if(entry==TRADE_SIGNAL_BUY)
                  {
                     if(exit_opposite_signal)
                        exit_all_set(ORDER_SET_SELL,order_magic);
                        count_orders=count_orders(-1,order_magic);
                     if(maxtrades>count_orders)
                     {
                        if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                        enter_order(OP_BUY);
                     }
                  }
                  else if(entry==TRADE_SIGNAL_SELL)
                     {
                        if(exit_opposite_signal)
                           exit_all_set(ORDER_SET_BUY,order_magic);
                           count_orders=count_orders(-1,order_magic);
                           if(maxtrades>count_orders)
                           {
                           if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                              enter_order(OP_SELL);
                           }
                     }
               }
            }
            
         }
      }
   }
 

I still fail to see the point of the loop

for(int i=OrdersHistoryTotal()-1; i < OrdersHistoryTotal(); i++)
         if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) && (OrderMagicNumber() == order_magic) && (OrderSymbol() == Symbol()))

Is the same as

int i=OrdersHistoryTotal()-1;
         if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) && (OrderMagicNumber() == order_magic) && (OrderSymbol() == Symbol()))
 
Keith Watford:

I still fail to see the point of the loop

Is the same as

You mean to track back the previous closed order no need use the for loop?
Because I saw most of the code to track back the previous closed order they using for loop

For Example:
https://www.mql5.com/en/forum/224183

Select the most recently closed order from History ?
Select the most recently closed order from History ?
  • 2018.01.08
  • www.mql5.com
Hi guys, After being filtered by Symbol, MagicN, Negative or positive Profit, is it possible to select the most recently closed order, I mean the l...
 
Woods Lim:

You mean to track back the previous closed order no need use the for loop?
Because I saw most of the code to track back the previous closed order they using for loop

For Example:
https://www.mql5.com/en/forum/224183

Yes, you would use a loop to find the last closed order, but you are not actually using a loop. You are just checking the single closed order that is last in the list.

Reason: