My for loop is not incrementing

 

I have multiple for loops in my EA program and while the first for loop works as designed the second for loop does not increment as stays stuck at zero. Both loops are in the OnTiick() function.  Any thoughts as to why this is?


  CopyBuffer(RSIHandle,0,0,9,RSI_ARRAY);
   ArraySetAsSeries(MACDMainArray,true);

   CopyBuffer(MACDHandle,MAIN_LINE,0,9,MACDMainArray);
   ArraySetAsSeries(MACDMainArray,true);

   CopyBuffer(MACDHandle,SIGNAL_LINE,0,9,MACDSignalArray);
   ArraySetAsSeries(MACDSignalArray,true);
   
   CopyBuffer(EMA5_Handle,0,0,17,EMA5_ARRAY);
   ArraySetAsSeries(EMA5_ARRAY,true);
   
   CopyBuffer(EMA10_Handle,0,0,17,EMA10_ARRAY);
   ArraySetAsSeries(EMA10_ARRAY,true);
   
   CopyBuffer(SMA50_Handle,MODE_SMA,0,17,SMA50_ARRAY);
   ArraySetAsSeries(SMA50_ARRAY,true);
   
   CopyBuffer(SMA100_Handle,MODE_SMA,0,17,SMA100_ARRAY);
   ArraySetAsSeries(SMA100_ARRAY,true);
 
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 0 =",NormalizeDouble(SMA50_ARRAY[0],_Digits),"--",NormalizeDouble(SMA100_ARRAY[0],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 1 =",NormalizeDouble(SMA50_ARRAY[1],_Digits),"--",NormalizeDouble(SMA100_ARRAY[1],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 2 =",NormalizeDouble(SMA50_ARRAY[2],_Digits),"--",NormalizeDouble(SMA100_ARRAY[2],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 3 =",NormalizeDouble(SMA50_ARRAY[3],_Digits),"--",NormalizeDouble(SMA100_ARRAY[3],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 4 =",NormalizeDouble(SMA50_ARRAY[4],_Digits),"--",NormalizeDouble(SMA100_ARRAY[4],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 5 =",NormalizeDouble(SMA50_ARRAY[5],_Digits),"--",NormalizeDouble(SMA100_ARRAY[5],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 6 =",NormalizeDouble(SMA50_ARRAY[6],_Digits),"--",NormalizeDouble(SMA100_ARRAY[6],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 7 =",NormalizeDouble(SMA50_ARRAY[7],_Digits),"--",NormalizeDouble(SMA100_ARRAY[7],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 8 =",NormalizeDouble(SMA50_ARRAY[8],_Digits),"--",NormalizeDouble(SMA100_ARRAY[8],_Digits) );
   FileWrite(file_handle,"Write SMA Values after CopyBuffer 9 =",NormalizeDouble(SMA50_ARRAY[9],_Digits),"--",NormalizeDouble(SMA100_ARRAY[9],_Digits) );
   FileWrite(file_handle,"--");

//-- Code to compare every element of an array to a static value

   for(int x=0; x<8 && PrintToJournalValues==true; x++) // The loop that works!
     {
      Print("MACD Main Array [ ",x," ] = ",NormalizeDouble(MACDMainArray[x],_Digits));
      Print("MACD Signal Array [ ",x," ] = ",NormalizeDouble(MACDSignalArray[x],_Digits));
      Print("EMA 5 Array [ ",x," ] = ",NormalizeDouble(EMA5_ARRAY[x],_Digits));
      Print("EMA 10 Array [ ",x," ] = ",NormalizeDouble(EMA10_ARRAY[x],_Digits));
      Print("SMA 50 Array [ ",x," ] = ",NormalizeDouble(SMA50_ARRAY[x],_Digits));
      Print("SMA 100 Array [ ",x," ] = ",NormalizeDouble(SMA100_ARRAY[x],_Digits));
      Print("RSI Array[ ",x," ] = ",NormalizeDouble(RSI_ARRAY[x],_Digits));
     }

//--- Calculate getting entry signals to buy and sale

   int EntrySignal;
   int SMASignal;
   int EMASignal;
   int RSISignal;
   int MACDSignal;
   double SMA50Value;
   double SMA100Value;
   double MACDSignalValue;
   double MACDMainValue;
   double EMA5Value;
   double EMA10Value;
   double RSIValue;
   //int y;
   //int x;
   int b;
   int s;
   b=0;  // Buy Counter
   s=0;  // Sale Counter
   EntrySignal=-1;     // Entry Set not to buy or sale
   SMASignal=-1;       // SMA  Signal to buy or sale 
   EMASignal=-1;       // EMA  Signal to buy or sale 
   RSISignal=-1;       // RSI  Signal to buy or sale
   MACDSignal=-1;      // MACD Signal to buy or sale 
   SMA50Value=0;
   SMA100Value=0;
   MACDSignalValue=0;
   MACDMainValue=0;
   EMA5Value=0;
   EMA10Value=0;
   RSIValue=0;
   
   
  
  for(int x=0; x<8; x++)  // The loop that does not work
  {
      FileWrite(file_handle,"For X Loop to Write SMA Values =",x,"--",NormalizeDouble(SMA50_ARRAY[x],_Digits),"--",NormalizeDouble(SMA100_ARRAY[x],_Digits) );
      if(SMA50_ARRAY[x] > SMA100_ARRAY[x])
      {
         SMASignal=0;  // SMA Buy Signal
         SMA50Value=SMA50_ARRAY[x];
         SMA100Value=SMA100_ARRAY[x];
         if(EMA5_ARRAY[x] < EMA10_ARRAY[x] )
         {
            for(int y=0; y<8; y++)
            { 
               if(EMA5_ARRAY[y] > EMA10_ARRAY[y])
                  {
                  EMASignal=0;  // EMA Buy SIgnal
                  EMA5Value=EMA5_ARRAY[y];
                  EMA10Value=EMA10_ARRAY[y];
                  }
            }
            if(MACDMainArray[x] < MACDSignalArray[x] && MACDMainArray[x] < 0 && MACDSignalArray[x] < 0)
            {
               for(int y=0; y<8; y++)
               { 
               if(MACDMainArray[y] > MACDSignalArray[y])
                  {
                  MACDSignal=0;  // MACD Buy Signal
                  MACDMainValue=MACDMainArray[y];
                  MACDSignalValue=MACDSignalArray[y];
                  }
               }
            }
          if(RSI_ARRAY[x] > 40)
            {
            RSISignal=0;  // RSI Buy Signal
            RSIValue=RSI_ARRAY[x];
            }
          }
    }
 

how about using the styler?

for(int x=0; x<8; x++) // The loop that does not work
  {
   FileWrite(file_handle,"For X Loop to Write SMA Values =",x,"--",NormalizeDouble(SMA50_ARRAY[x],_Digits),"--",NormalizeDouble(SMA100_ARRAY[x],_Digits));
   if(SMA50_ARRAY[x]>SMA100_ARRAY[x])
     {
      SMASignal=0;  // SMA Buy Signal
      SMA50Value=SMA50_ARRAY[x];
      SMA100Value=SMA100_ARRAY[x];
      if(EMA5_ARRAY[x]<EMA10_ARRAY[x])
        {
         for(int y=0; y<8; y++)
           {
            if(EMA5_ARRAY[y]>EMA10_ARRAY[y])
              {
               EMASignal=0;  // EMA Buy SIgnal
               EMA5Value=EMA5_ARRAY[y];
               EMA10Value=EMA10_ARRAY[y];
              }
           }
         if(MACDMainArray[x]<MACDSignalArray[x] && MACDMainArray[x]<0 && MACDSignalArray[x]<0)
           {
            for(int y=0; y<8; y++)
              {
               if(MACDMainArray[y]>MACDSignalArray[y])
                 {
                  MACDSignal=0;  // MACD Buy Signal
                  MACDMainValue=MACDMainArray[y];
                  MACDSignalValue=MACDSignalArray[y];
                 }
              }
           }
         if(RSI_ARRAY[x]>40)
           {
            RSISignal=0;  // RSI Buy Signal
            RSIValue=RSI_ARRAY[x];
           }
        }
     }
  }
//+------------------------------------------------------------------+
 

That is something that is easy to miss, which is why it is a good idea to make sure your indents are all spaced properly, it makes it a little easier to see if you are missing a closing parenthesis or brace.  Me personally, on the braces containing chunks of code, like within an if statement, I try to keep them aligned with the start of the statement they are with.  Like on your code section...

         if(MACDMainArray[x]<MACDSignalArray[x] && MACDMainArray[x]<0 && MACDSignalArray[x]<0)
           {
            for(int y=0; y<8; y++)
              {
               if(MACDMainArray[y]>MACDSignalArray[y])
                 {
                  MACDSignal=0;  // MACD Buy Signal
                  MACDMainValue=MACDMainArray[y];
                  MACDSignalValue=MACDSignalArray[y];  // yellow to show spacing
                 }
              }
           }

I would space it like so.

         if(MACDMainArray[x]<MACDSignalArray[x] && MACDMainArray[x]<0 && MACDSignalArray[x]<0)
         {
            for(int y=0; y<8; y++)
            {
               if(MACDMainArray[y]>MACDSignalArray[y])
               {
                  MACDSignal=0;  // MACD Buy Signal
                  MACDMainValue=MACDMainArray[y];
                  MACDSignalValue=MACDSignalArray[y]; // yellow to show spacing
               }
            }
         }

It might make it a little easier to see if you are missing the closer.  The spacing you use is a judgement call.  I have seen code indented 2 spaces, all the way up to 5.  Just try to keep it consistent within your code, that way if something is out of place, it is more likely to stand out. 

 
Use the styler. Ctrl+ ,
 
In the 3rd for loop, change the iterator from int y to another variable name (eg int z) 
 

Thanks all for the advice!  I used the styler (ctrl+,) and I changed the other names of the variables in my loops but still no luck.  The problem is probably staring at me but I'm too blind to see it!!


 for(int z=0; z<8; z++)
     {
      FileWrite(file_handle,"For X Loop to Write SMA Values ",z,"=",NormalizeDouble(SMA50_ARRAY[z],_Digits),"--",NormalizeDouble(SMA100_ARRAY[z],_Digits));
      if(SMA50_ARRAY[z]>SMA100_ARRAY[z])
        {
         SMASignal=0;  // SMA Buy Signal
         SMA50Value=SMA50_ARRAY[z];
         SMA100Value=SMA100_ARRAY[z];
         if(EMA5_ARRAY[z]<EMA10_ARRAY[z])
           {
            for(int y=0; y<8; y++)
              {
               if(EMA5_ARRAY[y]>EMA10_ARRAY[y])
                 {
                  EMASignal=0;  // EMA Buy SIgnal
                  EMA5Value=EMA5_ARRAY[y];
                  EMA10Value=EMA10_ARRAY[y];
                 }
              }
            if(MACDMainArray[z]<MACDSignalArray[z] && MACDMainArray[z]<0 && MACDSignalArray[z]<0)
              {
               for(int i=0; i<8; i++)
                 {
                  if(MACDMainArray[i]>MACDSignalArray[i])
                    {
                     MACDSignal=0;  // MACD Buy Signal
                     MACDMainValue=MACDMainArray[i];
                     MACDSignalValue=MACDSignalArray[i];
                    }
                 }
              }
            if(RSI_ARRAY[z]>40)
              {
               RSISignal=0;  // RSI Buy Signal
               RSIValue=RSI_ARRAY[z];
              }
           }
        }
      else
        {
         if(SMA50_ARRAY[z]<SMA100_ARRAY[z])
           {
            SMASignal=1;  // SMA Sell Signal
            SMA50Value=SMA50_ARRAY[z];
            SMA100Value=SMA100_ARRAY[z];
            if(EMA5_ARRAY[z]>EMA10_ARRAY[z])
              {
               for(int y=0; y<9; y++)
                 {
                  if(EMA5_ARRAY[y]<EMA10_ARRAY[y])
                    {
                     EMASignal=1;  // EMA Sell SIgnal
                     EMA5Value=EMA5_ARRAY[y];
                     EMA10Value=EMA10_ARRAY[y];
                    }
                 }
               if(MACDMainArray[z]>MACDSignalArray[z] && MACDMainArray[z]>0 && MACDSignalArray[z]>0)
                 {
                  for(int y=0; y<8; y++)
                    {
                     if(MACDMainArray[y]<MACDSignalArray[y])
                       {
                        MACDSignal=1;  // MACD Sell Signal
                        MACDMainValue=MACDMainArray[y];
                        MACDSignalValue=MACDSignalArray[y];
                       }
                    }
                 }
               if(RSI_ARRAY[z]<60)
                 {
                  RSISignal=1;  // RSI Sell Signal
                  RSIValue=RSI_ARRAY[z];
                 }
              }
           }
        }

      // Code here!!
      bool PosSelect;
      PosSelect=PositionSelect(_Symbol);

      if(SMASignal==0 && EMASignal==0 && MACDSignal==0 && RSISignal==0)
        {
         b++;
         EntrySignal=0;       // All signals indicate buy
                              //FileWrite(file_handle,"[",b,"] MACD   Main Value at time of buy =",NormalizeDouble(MACDMainValue,_Digits));
         //FileWrite(file_handle,"[",b,"] MACD   Signal Value to Time of buy =",NormalizeDouble(MACDSignalValue,_Digits));
         //FileWrite(file_handle,"[",b,"] EMA5   Value at time of buy =",NormalizeDouble(EMA5Value,_Digits));
         //FileWrite(file_handle,"[",b,"] EMA10  Value at time of buy =",NormalizeDouble(EMA10Value,_Digits));
         //FileWrite(file_handle,"[",b,"] SMA50  Value at time of buy =",NormalizeDouble(SMA50Value,_Digits));
         //FileWrite(file_handle,"[",b,"] SMA100 Value at time buy =",NormalizeDouble(SMA100Value,_Digits));
         //FileWrite(file_handle,"[",b,"] RSI    Value at time of buy =",NormalizeDouble(RSIValue,_Digits));
         //FileWrite(file_handle,"--"); 
         // Have to reset all signal after a buy or sale
         SMASignal=-1;       // SMA  Signal to buy or sale 
         EMASignal=-1;       // EMA  Signal to buy or sale 
         RSISignal=-1;       // RSI  Signal to buy or sale
         MACDSignal=-1;      // MACD Signal to buy or sale 
        }
      if(SMASignal==1 && EMASignal==1 && MACDSignal==1 && RSISignal==1)
        {
         s++;
         EntrySignal=1;  // All signals indicate sell
         FileWrite(file_handle,"[",s,"] MACD   Main Value at time of buy = ",NormalizeDouble(MACDMainValue,_Digits));
         FileWrite(file_handle,"[",s,"] MACD   Signal Value to Time of buy =",NormalizeDouble(MACDSignalValue,_Digits));
         FileWrite(file_handle,"[",s,"] EMA5   Value at time of buy =",NormalizeDouble(EMA5Value,_Digits));
         FileWrite(file_handle,"[",s,"] EMA10  Value at time of buy =",NormalizeDouble(EMA10Value,_Digits));
         FileWrite(file_handle,"[",s,"] SMA50  Value at time of buy =",NormalizeDouble(SMA50Value,_Digits));
         FileWrite(file_handle,"[",s,"] SMA100 Value at time buy =",NormalizeDouble(SMA100Value,_Digits));
         FileWrite(file_handle,"[",s,"] RSI    Value at time of buy =",NormalizeDouble(RSIValue,_Digits));
         FileWrite(file_handle,"--");
         // Have to reset all signal after a buy or sale
         SMASignal=-1;       // SMA  Signal to buy or sale 
         EMASignal=-1;       // EMA  Signal to buy or sale 
         RSISignal=-1;       // RSI  Signal to buy or sale
         MACDSignal=-1;      // MACD Signal to buy or sale
        }

      FileFlush(file_handle);
      FileClose(file_handle);

      if(EntrySignal==0 && PosSelect==false)
        {
         if(SendOrder(0)==false)
           {
            Print("Order send buy failed with error: ",GetLastError());
            ResetLastError();
           }
        }

      if(EntrySignal==1 && PosSelect==false)
        {
         if(SendOrder(1)==false)
           {
            Print("Order send sell failed with error: ",GetLastError());
            ResetLastError();
           }
        }

      PosSelect=PositionSelect(_Symbol);

      if(PosSelect==true)
        {
         SettingTPSL();
        }

     }
 
You're still declaring int y in two of the for loops. Not necessarily causing your problems but it could be throwing unexpected results and is bad coding practice....
 
Stuart Browne:
You're still declaring int y in two of the for loops. Not necessarily causing your problems but it could be throwing unexpected results and is bad coding practice....
You're right. I need to change that.  But any thoughts on why my loop is not incrementing?
 
if you look at my response you will see at the bottom i added a closing bracket to close the for loop.
 

Simple for loop doesn't work

I want to use for loops in my EA, but I can't get them to work.

I wrote a simple program to test it. I have both a for loop and a while loop. The while loops send the alert to the log, but i get nothing from the for loop.

Do I need to #include a file?

Thanks for your help,

Jim

Files:
LoopTest.mq5  2 kb
Journal_Data.txt  1344 kb
Reason: