Moving average EA

 

I want to write an EA code to generate a 20 EMA and take another 3 SMA

am focusing on the first 5 candle to creater my Array but I keep on getting a array out of range please help.

thank you in advance.

void OnTick()
  {
 double ExtMapBuffer1[];
double ExtMapBuffer2[];
//int bar, limit;
//int counted_bars=IndicatorCounted();
//if(counted_bars>0) counted_bars--;
//limit=Bars-counted_bars;

int i;
for(i=1; i<5; i++)
{ExtMapBuffer1[i] = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,i);}

for(i=1; i<5; i++)
{ExtMapBuffer2[i]=iMAOnArray(ExtMapBuffer1,0,3,-4,MODE_SMA,i);}

      double MovingAverage = ExtMapBuffer1[1];
      double LastMovingAverage = ExtMapBuffer1[2];
     
      double MAMovingAverage= ExtMapBuffer2[1];
      double LastMAMovingAverage=ExtMapBuffer2[2];

 ///////////////////////////////////////////////////////////////////////////////////////////////////
  //Sell
      if((LastMovingAverage < LastMAMovingAverage)&&(MovingAverage > MAMovingAverage))
         {Comment ("SELL");}
        
        
 ///////////////////////////////////////////////////////////////////////////////////////////////////
   //Buy
       if((LastMovingAverage > LastMAMovingAverage)&&(MovingAverage < MAMovingAverage))
        { Comment ("Buy");}
        
  }

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Lebbs: I want to write an EA code to generate a 20 EMA and take another 3 SMA
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Don't try do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)
    Just get the value(s) of the indicator(s) into the EA (using iCustom) and do what you want with it.
    You should encapsulate your iCustom calls to make your code self-documenting.
              Detailed explanation of iCustom - MQL4 programming forum

  3. Lebbs:
    double ExtMapBuffer1[];
    double ExtMapBuffer2[];
    Your arrays have no size.

Reason: