Indicator can access buffers but EA cannot ?

 

Hello,

I have written a small piece of code (regarding MACD) and I can access global buffers from the indicator but I cannot do it with the same piece of code in EA. WHY ?

This is the piece of code from indicator init() fun that i don't use in EA:

int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   //DRAW_HISTOGRAM
   SetIndexDrawBegin(1,SignalSMA);
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   SetIndexBuffer(0,MacdBuffer);
   SetIndexBuffer(1,SignalBuffer);
   SetIndexBuffer(2,MainBuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");
   SetIndexLabel(2,"Main");
//---- initialization done
   return(0);
  }

//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double     MacdBuffer[];
double     SignalBuffer[];
double      MainBuffer[];

double TP = 10;
double SL = 10;
double Lots = 0.2;

int init()
  {
   return(0);
  }

int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
   for(int i=0; i<limit; i++)
      MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
   for(i=0; i<limit; i++)
      MainBuffer[i]=MacdBuffer[i]-SignalBuffer[i];
      
     Comment("MacdBuffer[1] "+ MacdBuffer[1]+ " MacdBuffer[limit-1] "+MacdBuffer[limit-1]);
      
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
 

Showing the macd indicator code isnt very helpful, If you show your piece of code you wrote that isnt getting the buffer values from the indicator, someone might be able to tell you what is wrong with it.

 

The problem is with the Comment function. In the indicator it shows proper values and in the EA it shows 0.

Talking about the code - I use the same in both indicator and EA excluding the init function.

EDIT:

Is it possible that something is wrong with my MetaTrader Terminal ? all of the EAs I download (I think) should work correctly but as I wrote already they cannot acess e.g. iMACD()

 
EA's do NOT have indicator buffers. You do NOT put indicator code in the EA. You call iMACD() to get the values.
 

Ok so now my EA code looks like this: ( but still the same effect - alerts and comments show 0)

#property copyright "Copyright © 2008, TradingSytemForex"
#property link "http://www.tradingsystemforex.com"

//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double     MacdBuffer[];
double     SignalBuffer[];
double      MainBuffer[];

double TP = 10;
double SL = 10;
double Lots = 0.2;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;

// I thought that maybe this line (with IndicatorCounted) caused the problem but no

  // int counted_bars=IndicatorCounted();
 // if(counted_bars>0) counted_bars--;
  //limit=Bars-counted_bars;
   limit=Bars;
//---- macd counted in the 1-st buffer
   for(int i=0; i<limit; i++){
      MacdBuffer[i]= iMACD(NULL,0,FastEMA,SlowEMA, SignalSMA, PRICE_CLOSE,MODE_MAIN,0);
      //MacdBuffer[i] = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

      //this alert still show 0
      if(i%3000 == 0) Alert(MacdBuffer[i]);
      }
//---- signal line counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
   for(i=0; i<limit; i++)
      MainBuffer[i]=MacdBuffer[i]-SignalBuffer[i];
      
      Comment("MacdBuffer[1] "+ MacdBuffer[1]+ " MacdBuffer[limit-1] "+MacdBuffer[limit-1]);
//---- done
   return(0);
  }
 

You have to call iMACD() using icustom()

 
WHRoeder:
EA's do NOT have indicator buffers. You do NOT put indicator code in the EA. You call iMACD() to get the values.
double     MacdBuffer[];
double     SignalBuffer[];
double      MainBuffer[];
 
Yeah I know but these are just arrays I want to fill with results from e.g. iMACD.
Reason: