array out of range when coding MACD

 

I want to simultaneously use TEMA and MACD indicator to open/close position. But my EA always got "array out of range" error in my MACD code part.

//+------------------------------------------------------------------+
//|         TEMA+MACD                                     testEA.mq5 |
//+------------------------------------------------------------------+
#include  <Trade\Trade.mqh>

input int                InpFastEMA=2;               // MACD Fast EMA period
input int                InpSlowEMA=5;               // MACD Slow EMA period
input int                InpSignalSMA=2;              // MACD Signal SMA period
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // MACD Applied price

input    int      fastTEMA_Period=5;           
input    int      slowTEMA_Period=30;
input    int      EA_Magic=234508;
input    double   Lot=0.01;

int      MACD_Handle;
int      fastTEMAHandle;
int      slowTEMAHandle;

double   fastTEMAVal[];
double   



slowTEMAVal[];

CTrade trade;
//+------------------------------------------------------------------+
int OnInit()
  {
   ArraySetAsSeries(fastTEMAVal,true);
   ArraySetAsSeries(slowTEMAVal,true);

   MACD_Handle = iMACD(NULL,0,InpFastEMA,InpSlowEMA,InpSignalSMA,InpAppliedPrice);
   fastTEMAHandle=iTEMA(NULL,0,fastTEMA_Period,0,PRICE_CLOSE);
   slowTEMAHandle=iTEMA(NULL,0,slowTEMA_Period,0,PRICE_CLOSE);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(MACD_Handle);
   IndicatorRelease(fastTEMAHandle);
   IndicatorRelease(slowTEMAHandle);
  }
//+------------------------------------------------------------------+
void OnTick()
  {
   MqlTick           latest_tick;
   SymbolInfoTick(_Symbol,latest_tick);
   MqlTradeRequest   mrequest;
   MqlTradeResult    mresult;
   ZeroMemory(mrequest);

   double MACD[];
   double Signal[];

   int i=iBars(Symbol(),PERIOD_CURRENT);
   bool Open_Buy = (fastTEMAVal[0]>slowTEMAVal[0] && fastTEMAVal[1]<slowTEMAVal[1]);
   bool Open_Sell = (fastTEMAVal[0]<slowTEMAVal[0] && fastTEMAVal[1]>slowTEMAVal[1]);

   bool Close_Buy = (MACD[i]<MACD[i-1] && MACD[i-1]>Signal[i-1] && MACD[i]<Signal[i]) ;
   bool Close_Sell= (MACD[i]>MACD[i-1] && MACD[i-1]<Signal[i-1] && MACD[i]>Signal[i]);
   if(Open_Buy)
     {
      
     }
   if(Open_Sell)
     {
      
     }
   if(Close_Buy)
     {
     
     }
   if(Close_Sell)
     {
      
  }

 now, the problem is existing in 

   bool Close_Buy = (MACD[i]<MACD[i-1] && MACD[i-1]>Signal[i-1] && MACD[i]<Signal[i]) ;
   bool Close_Sell= (MACD[i]>MACD[i-1] && MACD[i-1]<Signal[i-1] && MACD[i]>Signal[i]);

MACD[i] is "array out of range". Why this happened?

 
coderelease:

I want to simultaneously use TEMA and MACD indicator to open/close position. But my EA always got "array out of range" error in my MACD code part.

 now, the problem is existing in 

MACD[i] is "array out of range". Why this happened?

int i=iBars(Symbol(),PERIOD_CURRENT); // this cannot be the index of a bar

int i=iBars(Symbol(),PERIOD_CURRENT)-1; // this can be!
 
coderelease: But my EA always got "array out of range" error in my MACD code part.
  1. Of course, it does, MACD, signal, fastTEMAVal and slow are arrays with zero size; you never put values into them.

    1. Arrays must be manually sized. They have no direction. You must move elements if you want a stack/as-series (set non-series, enlarge the array, set as-series).

    2. Buffers are automatically size, are as-series, and elements are moved for you, new elements are set to EMPTY_VALUE (or your designated. They can also draw on the chart automatically.

    3. In MT4, buffers and MT4 predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.

      To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.
                Event Handling Functions - Functions - Language Basics - MQL4 Reference

    4. In MT5, you must set the direction.

      To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] arrays, call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.
                Event Handling / OnCalculate - Reference on algorithmic/automated trading language for MetaTrader 5
 
Yashar Seyyedin #:

Really thanks for reply. I have fixed the problem.

Thank you!

 
William Roeder #:
  1. Of course, it does, MACD, signal, fastTEMAVal and slow are arrays with zero size; you never put values into them.

    1. Arrays must be manually sized. They have no direction. You must move elements if you want a stack/as-series (set non-series, enlarge the array, set as-series).

    2. Buffers are automatically size, are as-series, and elements are moved for you, new elements are set to EMPTY_VALUE (or your designated. They can also draw on the chart automatically.

    3. In MT4, buffers and MT4 predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.

    4. In MT5, you must set the direction.

Really thanks for your detailed reply.

I have now fixed the problem. Thank you very much!