My EA program loops prints same values! Why? - page 2

 
flwilliams87:

That makes sense but let me ask you this... I'm populating my arrays in the OnInit() function but then I'm reading from and testing those arrays in the OnTick() function, so during the the OnInit() doesn't the array just get populated once? Im thinking it must because I only size it for 9 elements and anything more than 9 would error out. So when we get to the OnTick() function the array only contains eight elements correct? Or am I not understanding the flow of my program?

Take this as you would from someone who has little MQL specific coding experience, but some other programming language experience.  Mostly because that is the case here.

I believe that the OnInit function is called each time that the EA (or other code that contains it) is initialized, as the name would suggest.  What all the things are that set off that event I do not know.  If it is not every time a new bar is generated, then you would need to do the update method if you wanted to use it as your source of accurate information.  But on that note, while doing research, there is an ArrayCopy function that you could possibly make use of in the situation I proposed above if you need to update the information.

Sorry if you already know the following, but I am not sure the knowledge level you have regarding arrays, and do not want to miss something small but vital.

You can populate an array at any time, as long as it is before you try to read information from it.   If you are saying you want it to hold 9 places, then it can be made with 9.  The index for the last place would be 8.  I think that as long as you populate the arrays in there with the latest information you should be alright, as long as every time a new bar comes in, it reinits the EA.  When I posted that, I had forgotten about the fact that EAs are not usually open as long as a regular program might be.  You would also have to adjust my example above to include one more element.

In an array, the computer usually sets it up by a numbering system (the index) so that it can easily access the info stored in each "box".  However, because it is a computer, it usually starts counting that index at zero, not like humans would and count the first box as box 1.  That is why you will see for loops, they are excellent for stepping through an array, among other things you need a counted process for.  https://www.mql5.com/en/articles/567 has some info on arrays in case I missed anything.  Trying to count past either end of the array's index will end up giving you an array index out of bounds error.


In theory you could have 300 places within an array.  Are you intentionally only giving it 9 spots or does it cause errors if you make it more than the 9?  If there is something else that is causing you errors if you try to make more than 9 spots, that is another issue.  But since you should try to keep your code (and memory usage) as minimal as you can to reduce operation time, you should only make the arrays and everything else as small as you can.

MQL5 Programming Basics: Arrays
MQL5 Programming Basics: Arrays
  • 2013.03.11
  • Dmitry Fedoseev
  • www.mql5.com
Arrays are an integral part of almost any programming language along with variables and functions. The article should be of interest primarily to novice MQL5 programmers, while experienced programmers will have a good opportunity to summarize and systematize their knowledge.
 
JD4:

Take this as you would from someone who has little MQL specific coding experience, but some other programming language experience.  Mostly because that is the case here.

I believe that the OnInit function is called each time that the EA (or other code that contains it) is initialized, as the name would suggest.  What all the things are that set off that event I do not know.  If it is not every time a new bar is generated, then you would need to do the update method if you wanted to use it as your source of accurate information.  But on that note, while doing research, there is an ArrayCopy function that you could possibly make use of in the situation I proposed above if you need to update the information.

Sorry if you already know the following, but I am not sure the knowledge level you have regarding arrays, and do not want to miss something small but vital.

You can populate an array at any time, as long as it is before you try to read information from it.   If you are saying you want it to hold 9 places, then it can be made with 9.  The index for the last place would be 8.  I think that as long as you populate the arrays in there with the latest information you should be alright, as long as every time a new bar comes in, it reinits the EA.  When I posted that, I had forgotten about the fact that EAs are not usually open as long as a regular program might be.  You would also have to adjust my example above to include one more element.

In an array, the computer usually sets it up by a numbering system (the index) so that it can easily access the info stored in each "box".  However, because it is a computer, it usually starts counting that index at zero, not like humans would and count the first box as box 1.  That is why you will see for loops, they are excellent for stepping through an array, among other things you need a counted process for.  https://www.mql5.com/en/articles/567 has some info on arrays in case I missed anything.  Trying to count past either end of the array's index will end up giving you an array index out of bounds error.


In theory you could have 300 places within an array.  Are you intentionally only giving it 9 spots or does it cause errors if you make it more than the 9?  If there is something else that is causing you errors if you try to make more than 9 spots, that is another issue.  But since you should try to keep your code (and memory usage) as minimal as you can to reduce operation time, you should only make the arrays and everything else as small as you can.

 
JD4:

Take this as you would from someone who has little MQL specific coding experience, but some other programming language experience.  Mostly because that is the case here.

I believe that the OnInit function is called each time that the EA (or other code that contains it) is initialized, as the name would suggest.  What all the things are that set off that event I do not know.  If it is not every time a new bar is generated, then you would need to do the update method if you wanted to use it as your source of accurate information.  But on that note, while doing research, there is an ArrayCopy function that you could possibly make use of in the situation I proposed above if you need to update the information.

Sorry if you already know the following, but I am not sure the knowledge level you have regarding arrays, and do not want to miss something small but vital.

You can populate an array at any time, as long as it is before you try to read information from it.   If you are saying you want it to hold 9 places, then it can be made with 9.  The index for the last place would be 8.  I think that as long as you populate the arrays in there with the latest information you should be alright, as long as every time a new bar comes in, it reinits the EA.  When I posted that, I had forgotten about the fact that EAs are not usually open as long as a regular program might be.  You would also have to adjust my example above to include one more element.

In an array, the computer usually sets it up by a numbering system (the index) so that it can easily access the info stored in each "box".  However, because it is a computer, it usually starts counting that index at zero, not like humans would and count the first box as box 1.  That is why you will see for loops, they are excellent for stepping through an array, among other things you need a counted process for.  https://www.mql5.com/en/articles/567 has some info on arrays in case I missed anything.  Trying to count past either end of the array's index will end up giving you an array index out of bounds error.


In theory you could have 300 places within an array.  Are you intentionally only giving it 9 spots or does it cause errors if you make it more than the 9?  If there is something else that is causing you errors if you try to make more than 9 spots, that is another issue.  But since you should try to keep your code (and memory usage) as minimal as you can to reduce operation time, you should only make the arrays and everything else as small as you can.

Thanks!  And I was originally wrong about my my program; I'm populating the array and comparing in the OnTick() function.  But after some more research it seem my for loop is not incrementing for whatever reason. It starts are zero and stays are zero.  The first for loop works but the second does not increment. Any thoughts as to why this might be?


  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++) 
     {
      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++)
  {
      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];
            }
          }
    }