Force Reverse Order Code?

 

Please Help !!! I need a small code which:

1) Checks History for the last (x) Magic#.

2) If that (Magic#) Order was a Buy.

3) The next Order needs to be Sell.

- Here is what i came up with but doe NOT work in forward test.

for(int i=0;i<OrdersHistoryTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
{
if(OrderMagicNumber()==6)
if(OrderType()==OP_BUY)
Rev=-1;
if(OrderType()==OP_SELL)
Rev=1;
}
}

 

My code seem to work in back-tests,

Could the History sort in the terminal effect this? 

 

You are looping thru all of the orderhistory from smallest index to highest index, overwriting the "Rev" value based on the OrderType() of older and older trades every time "i" is incremented because you don't exit your "for" loop until your incrementer "i" has been incremented to equal OrdersHistoryTotal().

Try:

for(int i=0;i<OrdersHistoryTotal();i++)
   {
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
      {
      if(OrderMagicNumber()==6)
         {
         if(OrderType()==OP_BUY)
            {
            Rev=-1;
            break;
            }
         if(OrderType()==OP_SELL)
            {
            Rev=1;
            break;
            }
         }
      }
   }
 
Oh - Thank you! Soooo Very Much.
Reason: