phy:
Reverse your counters...
for(i = MathMax(limit,0); i >= 0; i--)
... and make sure your limit is at least 0
I replaced with the following code in th EA. The SignalBuffer values are still different between the indicator and the EA. It's very obvious in high time frames like H4.
for(i = MathMax(limit,0); i >= 0; i--){MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);}
for(i = MathMax(limit,0); i >= 0; i--){SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalEMA,0,MODE_EMA,i);}
//+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int i, limit; ArrayResize(MacdBuffer, Bars); ArrayResize(SignalBuffer, Bars); ArraySetAsSeries(MacdBuffer,true); ArraySetAsSeries(SignalBuffer,true); int counted_bars=IndicatorCounted(); if(counted_bars>0) counted_bars--; limit=Bars-counted_bars; // limit=ArraySize(MacdBuffer); //makes no difference for(i = MathMax(limit,0); i >= 0; i--) {MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);} for(i = MathMax(limit,0); i >= 0; i--) {SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalEMA,0,MODE_EMA,i);} //different value here always positive Print("EA MACD=",MacdBuffer[0]," Sig=",SignalBuffer[0]); Comment("EA = ", SignalBuffer[0]); return(0); }
//INDICATOR CODE //---- indicator settings #property indicator_separate_window #property indicator_buffers 2 int FastEMA = 25; int SlowEMA = 170; int SignalEMA = 25; //---- indicator buffers double MacdBuffer[]; double SignalBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { IndicatorBuffers(2); SetIndexBuffer(0,MacdBuffer); SetIndexBuffer(1,SignalBuffer); return(0); } //+------------------------------------------------------------------+ //| Moving Average of Oscillator | //+------------------------------------------------------------------+ int start() { int i,limit; int counted_bars=IndicatorCounted(); if(counted_bars>0) counted_bars--; limit=Bars-counted_bars-1; //---- calculate MA1 & MA5 on MACD indicator for(i = MathMax(limit,0); i >= 0; i--) {MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);} for(i = MathMax(limit,0); i >= 0; i--) {SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalEMA,0,MODE_EMA,i);} Print("IN MACD=",MacdBuffer[0]," Sig=",SignalBuffer[0]); Comment("IN = ", SignalBuffer[0]); return(0); }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Below is the same basic code calculations. The first set of code is an indicator. The second set of code is an Expert Advisor.
When I print the values of MacdBuffer and SignalBuffer, the value of SignalBuffer is different in the EA and the Indicator.
I do not understand why it is different. I must be missing something in my code. The code below is complete.
Any help would be appreciated.
//INDICATOR CODE
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
int FastEMA = 25;
int SlowEMA = 170;
int SignalEMA = 25;
//---- indicator buffers
double MacdBuffer[];
double SignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(2);
SetIndexBuffer(0,MacdBuffer);
SetIndexBuffer(1,SignalBuffer);
return(0);
}
//+------------------------------------------------------------------+
//| Moving Average of Oscillator |
//+------------------------------------------------------------------+
int start()
{
int i,limit;
int counted_bars=IndicatorCounted();
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- calculate MA1 & MA5 on MACD indicator
for(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);}
for(i=0; i<limit; i++) {SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalEMA,0,MODE_EMA,i);}
Print("EA MACD=",MacdBuffer[0]," Sig=",SignalBuffer[0]);
return(0);
}
//EA CODE
//---- input parameters
int FastEMA = 25;
int SlowEMA = 170;
int SignalEMA = 25;
//---- calculation buffers
double MacdBuffer[5000];
double SignalBuffer[5000];
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
ArraySetAsSeries(MacdBuffer,true);
ArraySetAsSeries(SignalBuffer,true);
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int i, limit;
int counted_bars=IndicatorCounted();
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
// limit=ArraySize(MacdBuffer); //makes no difference
for(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);}
for(i=0; i<limit; i++) {SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalEMA,0,MODE_EMA,i);} //different value here always positive
Print("In MACD=",MacdBuffer[0]," Sig=",SignalBuffer[0]);
return(0);
}