Can I add to an array with a loop?

 

I'd like to add to the indexes of an array with a loop.  I'm new to coding (not just mql4) so I'm not sure this is the best way to do this at all.  What my goal is, is to use an array to look up a candle number.  I'm counting every time the MACD line crosses over and counting those "events".  I'm also noting the candle number for each of those events.  So, every time there is a crossover I'll send an alert that says on cross up 3 we are on candle 34 (just for example, it's something like that).  My current code is below.  For example, it's not in the code but I'd like to be able to say "myArray[3]" and it will equal 34 (as per the example I'm using).  I hope I'm making sense.

Any thoughts or suggestions?

void OnStart()
{
int period = 100;
int crossUpCount = 1;
int crossDownCount = 1;
  
   for(int i = 0; i<period; i++)
   {
    
   double MACD = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i);
   double signalLine = iMACD(NULL,0,12,26,9,PRICE_CLOSE,1,i);
   
   double MACDLast = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i+1);
   double signalLineLast = iMACD(NULL,0,12,26,9,PRICE_CLOSE,1,i+1);      
   
      //MACD line crosses UP over signal line
      if(MACD>signalLine && MACDLast<signalLineLast) 
      {
      ObjectCreate("Line" + Time[i],OBJ_VLINE,0,Time[i],0);    //(name, type, window, anchor point)
      ObjectSet("Line" + Time[i],OBJPROP_COLOR,clrBlue);
      ObjectSet("Line" + Time[i],OBJPROP_STYLE,STYLE_DOT);
      
      int crossUpCandle = i;
      Alert("Cross up " + crossUpCount + " is at candle " + crossUpCandle);   
            
      crossUpCount++;
              
      }
      
      //MACD line crosses DOWN under signal line
      if(MACD<signalLine && MACDLast>signalLineLast) 
      {
      ObjectCreate("Line" + Time[i],OBJ_VLINE,0,Time[i],0);    //(name, type, window, anchor point)
      ObjectSet("Line" + Time[i],OBJPROP_COLOR,clrRed);
      ObjectSet("Line" + Time[i],OBJPROP_STYLE,STYLE_DOT);
      
      int crossDownCandle = i;
      Alert("Cross down " + crossDownCount + " is at candle " + crossDownCandle);
      
      crossDownCount++;
        
      }
                  
   }
   
Alert("Total cross up count is at " + (crossUpCount - 1));   
Alert("Total cross down count is at " + (crossDownCount - 1)); 
}
 
dasilvja: I'd like to add to the indexes of an array with a loop.  I'm new to coding (not just mql4) so I'm not sure this is the best way to do this at all.  What my goal is, is to use an array to look up a candle number.  
  1. Do not store as-series indexes. When a new bar start they are now all wrong. You can store the candle time and find the candle. Or you can store a non-series index and convert back.
    #define  SERIES(I)   (Bars - 1 - I) // As-series to non-series or back.

  2. Help you with what? You haven't stated a problem, or a vague want. All you've done is stated a vague possible solution to an unstated problem.

    How To Ask Questions The Smart Way. 2004
              The XY Problem

  3. Your posted code doesn't have an array. What's the problem?

  4. Your “goal is to look up a candle number?” Index of what?
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

 
William Roeder:
  1. Do not store as-series indexes. When a new bar start they are now all wrong. You can store the candle time and find the candle. Or you can store a non-series index and convert back.
  2. Help you with what? You haven't stated a problem, or a vague want. All you've done is stated a vague possible solution to an unstated problem.

    How To Ask Questions The Smart Way. 2004
              The XY Problem

  3. Your posted code doesn't have an array. What's the problem?

  4. Your “goal is to look up a candle number?” Index of what?
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem


The problem is that I need a way to look up a specific candle that is discovered through the for loop. So I don't know which candle it is yet. It will be different on different charts. 

I don't know a lot about coding. I don't understand the logic very well yet. I don't know the things that can be done. My question to you, the experienced coder, is how would you go about doing this? You see where my code draws a vertical line? How do I "remember" those candles? How do I look up and use those points later on? 
Reason: