Problems monitoring takeProfit

 
if (SellMarketCount(Symbol(),MagicNumber)== 0 && BuyMarketCount(Symbol(),MagicNumber)== 0)
   {
        int TP=MonitorTakeProfit(Symbol(), MagicNumber);
        
        if(TP==1)
        {
           Calcel_All();
           Reentry=false;
           EAActivated=false;
        }
   
   }

-----

int MonitorTakeProfit(string argSymbol, int argMagicNumber)
{

   int i,OrderCount,CurrDay, OrderDay,CurrMonth, OrderMoth, HistoryOrderType, Counter =OrdersHistoryTotal();

   for(i=Counter; Counter >= 0; i--)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      
      HistoryOrderType = OrderType();
      
      CurrDay=TimeDay(TimeCurrent());
      OrderDay=TimeDay(OrderCloseTime());
  
      
      if (CurrDay==OrderDay)
      {
       
              if (HistoryOrderType==OP_BUY)
            {
               
               if (OrderCloseTime()!=0 && OrderClosePrice()>OrderOpenPrice())
               {
               return(1);
               }
               
            }
      }
      else break;
   }
   
}


I am coding an Opening Range breakout strategy. The main rule is trading it in both directions the opening range during the whole day since a take a profit is reached.

This is the block of code I’m using to monitor when a take a profit is reached, but it is not working. The EA continues trading the range after the take a profit.

Can someone tell me where is the error?. Or is there a better one?

With this code I try to limit the loop only to the trades of the current session.








 
 

I take note of the SRC option,


thanks.

 
CJS:


I take note of the SRC option,


thanks.

Thank you.

From the code you have posted I can't see an error, maybe the problem lies elsewhere . . . if you don't want to disclose your code add in Print(); statements to output various variables to the Experts tab (journal tab in Strategy tester) and make sure the variables are what they should be . . . e.g.

  int TP=MonitorTakeProfit(Symbol(), MagicNumber);
 
       Print ("TP = ", TP);   // <---------------------------------------
       
        if(TP==1)
        {
           Calcel_All();
           Reentry=false;
           EAActivated=false;
 

Hi RaptorUK,

Thanks a lot for the quick response.

The problem was that I had to add "-1" to the Conter. Or, at least, seems to be working fine now.


Thanks again

 
  1. Counter =OrdersHistoryTotal();
    for(i=Counter; Counter >= 0; i--){
       OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
    i must start at counter-1. If orderSelect fails everything else is bogus, always test return codes
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
    

  2. CurrDay=TimeDay(TimeCurrent());
    OrderDay=TimeDay(OrderCloseTime());
    if (CurrDay==OrderDay)
    This was to test if the last order closed the same day. Instead it tests if today is Monday, did it close, this Monday, Last week Monday, .. last year Monday.
    datetime now = TimeCurrent(),
             bod = now - now % 86400, // Beginning of day
             oct = OrderCloseTime(),
             ocd = oct - ocd % 86400; // Order Close day.
          
          if (bod == ocd) // closed today.
 

That's exactly what I needed.

Really Nice help.

Reason: