Help with for loop

 

Hi, this for loop doesnt work - trades are still being taken even 4-7 candles after the MA cross

 if(MACDmain > MACDsignal && MACDmain1 < MACDsignal1) //MACD cross to the upside - the main line has crossed above the signal line
      {
      for(int i = 1; i <=3; i++)
         {
         double EMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,i);          //The for loop tests whether an MA crossover
         double EMA2 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,i+1);       //happened or not. A candle opening below 
         if(Close[i] > EMA && Open[i] <= EMA && Close[i+1] < EMA2 && Open[i+1] < EMA2)//the MA and closing above it signals a cross
            {                                                           //with the additional confirmation of the close of
            BuyOne = true;                                              //previous candle and open being below the MA
            break;
            }
            
         }
 

...and my sell orders aren't getting modified - getting error 4051 and 130


   for(int pos = 0; pos <= OrdersTotal(); pos++)
      {
      OrderSelect(OrderTicket(),SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol() != Symbol() && OrderMagicNumber() != MagicNumber) BandPierced = false;
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
         IsTrade++;
      
         if(OrderType() == OP_BUY) 
            {
            
            if(Close[1] > UpperBB || High[1] > UpperBB) BandPierced = true;
            if(BandPierced)
               {
                  if(Low[1] > OrderStopLoss())
                  {
                  ModStop = Low[1];
                  OrderModify(OrderTicket(),OrderOpenPrice(),ModStop,0,0,CLR_NONE);
                  }
                }
            
            
            
            if(Close[1] < TwentyEMA - Window * Point) 
                     {
                     Closed = OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE); 
                     }
                                                                                                                                               //order 
            }
   
         if (OrderType() == OP_SELL)
            {
            
            if(Close[1] < LowerBB || Low[1] < LowerBB) BandPierced = true;
            if(BandPierced)
               {
                  if(High[1] < OrderStopLoss()) 
                  {
                  ModStop = High[1];
                  OrderModify(OrderTicket(),OrderOpenPrice(),ModStop,0,0,CLR_NONE);
                  }
                }

            
            if(Close[1] > TwentyEMA + Window * Point) 
                     {
                     Closed = OrderClose(OrderTicket(),OrderLots(),Ask,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);
                     }
            }
         }
      }
        
 
Roll your sleeves up, add some (many) Print() statements and find what is going on.
 
Lol ok. Well said. Btw, I'm getting 20% profit over a year with 90% modeling quality. Almost there:)
 
gulzaar:
Lol ok. Well said. Btw, I'm getting 20% profit over a year with 90% modeling quality. Almost there:)

Have you try Optimization? You can get more.

 
diostar:

Have you try Optimization? You can get more.


There aren't too many optimizable parameters - but whenever I try to optimize(i select just one parameter, check optimize) but when I run the tester there is nothing there
 
gulzaar:

but when I run the tester there is nothing there
It doesn't do a visual test, if that is what you meant by nothing there ?
 
   for(int pos = 0; pos <= OrdersTotal(); pos++)
      {
      OrderSelect(OrderTicket(),SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol() != Symbol() && OrderMagicNumber() != MagicNumber) BandPierced = false;
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
You can NOT use OrderTicket() until you AFTER a OrderSelect() 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.
    ){
 
gulzaar:

There aren't too many optimizable parameters - but whenever I try to optimize(i select just one parameter, check optimize) but when I run the tester there is nothing there

I realise quite a lot tend to make this mistake. After checking optimize, and the parameters, you also MUST give values to step!=0, etc.

Then open your optimization graph to see which parameter value, in increment/decrement step value, gives the best balance, or any other optimization vrb you choose.

 
diostar:

I realise quite a lot tend to make this mistake. After checking optimize, and the parameters, you also MUST give values to step!=0, etc.

Then open your optimization graph to see which parameter value, in increment/decrement step value, gives the best balance, or any other optimization vrb you choose.



Yeah, even after I give values to step and stop(in the strategy tester menu right?), and when the test is done the balance graph is blank, and so is the optimization graph
 
RaptorUK:
Roll your sleeves up, add some (many) Print() statements and find what is going on.
I added Print(OrderTicket()) after the if(OrderType() == OP_SELL) and surprisingly it didnt return any values in the journal - which is odd because the orders close well enough(which code is under the same if bracket, and uses the same OrderType() statement)
Reason: