What is wrong with this code?help!!

 

I'v created this function that will allow the EA to place another order when a current buy order exceeds 50 pips. The function works on testing but  problem is  it places the order too early for example when the first order has moved only 30 pips. What is the problem?


Thank

bool NTAB()
  {
  double stoplevel = MarketInfo(NULL,MODE_STOPLEVEL)*_Point;
double PBID= Bid-stoplevel;


   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()      != _Symbol)  continue;
      if(OrderMagicNumber() !=MagicNumber) continue;
      if(OrderType() !=OP_BUY) continue;
      if((OrderOpenPrice()-PBID)<500*_Point)    return(false);
     }
   return(true);
}

s.

 
prweza:

I'v created this function that will allow the EA to place another order when a current buy order exceeds 50 pips. The function works on testing but  problem is  it places the order too early for example when the first order has moved only 30 pips. What is the problem?


Thank

s.


You need to first make sure that you're working with the most recently opened order, and you can do that by looping through the orders and finding the one with the highest open time (aka most recently placed) and the selecting the order by its ticket number. Then the logic should be Bid - open price > 500*Point

bool NTAB(const int points)
{
   int      recent_ticket     = -1;
   datetime recent_open_time  = 0;
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(   OrderSelect(i,SELECT_BY_POS)
         && OrderSymbol()     == _Symbol
         && OrderMagicNumber()== MagicNumber
         && OrderType()       == OP_BUY
         && OrderOpenTime() > recent_open_time
         )
      {
         recent_open_time = OrderOpenTime();
         recent_ticket    = OrderTicket();      
      }
   }
   if(OrderSelect(recent_ticket,SELECT_BY_TICKET))
      return (Bid-OrderOpenPrice() >= (double)points*_Point);
   return false;
}
 
nicholishen:

You need to first make sure that you're working with the most recently opened order, and you can do that by looping through the orders and finding the one with the highest open time (aka most recently placed) and the selecting the order by its ticket number. Then the logic should be Bid - open price > 500*Point


Sorry, code giving errors, could you help refine it. Thanks.

 
prweza:

Sorry, code giving errors, could you help refine it. Thanks.


No errors on my end. 

 
nicholishen:

No errors on my end. 


thanks it works..

Reason: