Strange iMAOnArray results between indicator and EA

 

I've noticed some strange behaviour with iMAOnArray() function.

Maybe it is the function, but it could also be in the way EAs differ from indicators. Anyway...

I was trying to incorporate the TDI indicator into an EA to give signals (rather than use iCustom and address the indicator directly, so perhaps that is my first mistake, but anyway....)

Here's the TDI code (note the debugging Print line:)

#property indicator_buffers 6
#property indicator_color1 Black
#property indicator_color2 MediumBlue
#property indicator_color3 Yellow
#property indicator_color4 MediumBlue
#property indicator_color5 Green
#property indicator_color6 Red
#property indicator_separate_window

extern int RSI_Period = 21;         //8-25
extern int RSI_Price = 0;           //0-6
extern int Volatility_Band = 34;    //20-40
extern int RSI_Price_Line = 2;      
extern int RSI_Price_Type = 0;      //0-3
extern int Trade_Signal_Line = 7;   
extern int Trade_Signal_Type = 0;   //0-3

double RSIBuf[],UpZone[],MdZone[],DnZone[],MaBuf[],MbBuf[];

int init()
  {
   IndicatorShortName("Traders Dynamic Index");
   SetIndexBuffer(0,RSIBuf);
   SetIndexBuffer(1,UpZone);
   SetIndexBuffer(2,MdZone);
   SetIndexBuffer(3,DnZone);
   SetIndexBuffer(4,MaBuf);
   SetIndexBuffer(5,MbBuf);
   
   SetIndexStyle(0,DRAW_NONE); 
   SetIndexStyle(1,DRAW_LINE); 
   SetIndexStyle(2,DRAW_LINE,0,2);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexStyle(4,DRAW_LINE,0,2);
   SetIndexStyle(5,DRAW_LINE,0,2);
   
   SetIndexLabel(0,NULL); 
   SetIndexLabel(1,"VB High"); 
   SetIndexLabel(2,"Market Base Line"); 
   SetIndexLabel(3,"VB Low"); 
   SetIndexLabel(4,"RSI Price Line");
   SetIndexLabel(5,"Trade Signal Line");
 
   SetLevelValue(0,50);
   SetLevelValue(1,68);
   SetLevelValue(2,32);
   SetLevelStyle(STYLE_DOT,1,DimGray);
   
   return(0);
  }

int start()
  {
   double MA,RSI[];
   ArrayResize(RSI,Volatility_Band);
   int counted_bars=IndicatorCounted();
   int limit = Bars-counted_bars-1;
   
   limit=40;
   
   for(int i=limit; i>=0; i--)
   {
      RSIBuf[i] = (iRSI(NULL,0,RSI_Period,RSI_Price,i)); 
      MA = 0;
      for(int x=i; x<i+Volatility_Band; x++) {
         RSI[x-i] = RSIBuf[x];
         MA += RSIBuf[x]/Volatility_Band;
      }
      UpZone[i] = (MA + (1.6185 * StDev(RSI,Volatility_Band)));
      DnZone[i] = (MA - (1.6185 * StDev(RSI,Volatility_Band)));  
      MdZone[i] = ((UpZone[i] + DnZone[i])/2);
      }
   for (i=limit-1;i>=0;i--)  
      {
       MaBuf[i] = (iMAOnArray(RSIBuf,0,RSI_Price_Line,0,RSI_Price_Type,i));
       Print("INDICATOR Index:" , i , " MaBuf:" , MaBuf[i], " RSIBuf index:" , RSIBuf[i]);
       MbBuf[i] = (iMAOnArray(RSIBuf,0,Trade_Signal_Line,0,Trade_Signal_Type,i));   
      } 
//----
   return(0);
  }
  
double StDev(double& Data[], int Per)
{return(MathSqrt(Variance(Data,Per)));
}
double Variance(double& Data[], int Per)
{double sum=0.0, ssum=0.0;
  for (int i=0; i<Per; i++)
  {sum += Data[i];
   ssum += MathPow(Data[i],2);
  }
  return((ssum*Per - sum*sum)/(Per*(Per-1)));
}

I run this on the weekend with no market activity. Here is the debugging results:

2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:0 MaBuf:53.9677 RSIBuf index:52.1377
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:1 MaBuf:55.8963 RSIBuf index:55.7978
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:2 MaBuf:56.1844 RSIBuf index:55.9948
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:3 MaBuf:56.1967 RSIBuf index:56.374
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:4 MaBuf:54.2145 RSIBuf index:56.0194
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:5 MaBuf:52.2951 RSIBuf index:52.4096
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:6 MaBuf:52.2606 RSIBuf index:52.1807
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:7 MaBuf:52.6886 RSIBuf index:52.3405
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:8 MaBuf:52.7015 RSIBuf index:53.0367
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:9 MaBuf:53.4796 RSIBuf index:52.3662
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:10 MaBuf:54.2822 RSIBuf index:54.5929
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:11 MaBuf:54.3279 RSIBuf index:53.9716
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:12 MaBuf:52.4972 RSIBuf index:54.6842
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:13 MaBuf:51.2705 RSIBuf index:50.3103
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:14 MaBuf:53.7371 RSIBuf index:52.2308
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:15 MaBuf:53.4748 RSIBuf index:55.2434
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:16 MaBuf:51.7062 RSIBuf index:51.7062
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:17 MaBuf:52.3855 RSIBuf index:51.7062
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:18 MaBuf:52.6159 RSIBuf index:53.0647
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:19 MaBuf:53.5689 RSIBuf index:52.1671
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:20 MaBuf:54.3794 RSIBuf index:54.9707
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:21 MaBuf:52.9613 RSIBuf index:53.7881
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:22 MaBuf:52.8264 RSIBuf index:52.1346
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:23 MaBuf:55.5345 RSIBuf index:53.5181
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:24 MaBuf:60.1334 RSIBuf index:57.5509
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:25 MaBuf:63.6625 RSIBuf index:62.7159
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:26 MaBuf:64.3946 RSIBuf index:64.6092
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:27 MaBuf:62.3068 RSIBuf index:64.1801
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:28 MaBuf:60.8051 RSIBuf index:60.4335
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:29 MaBuf:63.1198 RSIBuf index:61.1766
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:30 MaBuf:64.3062 RSIBuf index:65.0629
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:31 MaBuf:58.6065 RSIBuf index:63.5495
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:32 MaBuf:51.4147 RSIBuf index:53.6636
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:33 MaBuf:48.5446 RSIBuf index:49.1658
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:34 MaBuf:47.578 RSIBuf index:47.9234
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:35 MaBuf:48.8251 RSIBuf index:47.2326
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:36 MaBuf:49.3473 RSIBuf index:50.4176
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:37 MaBuf:48.8731 RSIBuf index:48.2769
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:38 MaBuf:50.804 RSIBuf index:49.4693
2014.03.30 22:20:09.812 EA-TDI EURUSD,M15: INDICATOR Index:39 MaBuf:51.3135 RSIBuf index:52.1387

... so then I coded this function to run in an EA... which is more or less the same code except for the fact that I have to declare the arrays beforehand, and not use IndicatorCounted()

int tdi_signal(int TimeFrame, int RSI_Period, int RSI_Price, int Volatility_Band, int RSI_Price_Line, int RSI_Price_Type, int Trade_Signal_Line, int Trade_Signal_Type, int BarSignal) {
   double RSIBuf[],UpZone[],MdZone[],DnZone[],MaBuf[],MbBuf[],McBuf[];
   double MA,RSI[];
   int signal=0;
   
   //Can't use this in EA
   //int counted_bars=IndicatorCounted();
   //int limit = Bars-counted_bars-1;
   
   // forcibly set limit
   int limit=40;
   
   // have to resize in the EA
   ArrayResize(RSI,Volatility_Band);
   ArrayResize(RSIBuf,limit);
   ArrayResize(UpZone,limit);
   ArrayResize(MdZone,limit);
   ArrayResize(DnZone,limit);
   ArrayResize(MaBuf,limit);
   ArrayResize(MbBuf,limit);
   ArrayResize(McBuf,limit);

   for(int i=limit; i>=0; i--)
   {
      RSIBuf[i] = (iRSI(NULL,0,RSI_Period,RSI_Price,i)); 
      MA = 0;
      for(int x=i; x<i+Volatility_Band; x++) {
         RSI[x-i] = RSIBuf[x];
         MA += RSIBuf[x]/Volatility_Band;
      }
      UpZone[i] = (MA + (1.6185 * StDev(RSI,Volatility_Band)));
      DnZone[i] = (MA - (1.6185 * StDev(RSI,Volatility_Band)));  
      MdZone[i] = ((UpZone[i] + DnZone[i])/2);
      }
   for (i=limit-1;i>=0;i--)  
      {
       MaBuf[i] = (iMAOnArray(RSIBuf,0,RSI_Price_Line,0,RSI_Price_Type,i));
       Print("EA Index:" , i , " MaBuf:" , MaBuf[i], " RSIBuf index:" , RSIBuf[i]);
       MbBuf[i] = (iMAOnArray(RSIBuf,0,Trade_Signal_Line,0,Trade_Signal_Type,i));   
      } 

....

   return (signal);
}

However, the results are entirely different. Additionally, the first line in for processing is 0 where on the indicator, there is already a value:

2014.03.30 22:19:25.315 work EURUSD,M15: EA Index:0 MaBuf:50.804 RSIBuf index:52.1377
2014.03.30 22:19:25.315 work EURUSD,M15: EA Index:1 MaBuf:48.8731 RSIBuf index:55.7978
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:2 MaBuf:49.3473 RSIBuf index:55.9948
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:3 MaBuf:48.8251 RSIBuf index:56.374
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:4 MaBuf:47.578 RSIBuf index:56.0194
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:5 MaBuf:48.5446 RSIBuf index:52.4096
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:6 MaBuf:51.4147 RSIBuf index:52.1807
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:7 MaBuf:58.6065 RSIBuf index:52.3405
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:8 MaBuf:64.3062 RSIBuf index:53.0367
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:9 MaBuf:63.1198 RSIBuf index:52.3662
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:10 MaBuf:60.8051 RSIBuf index:54.5929
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:11 MaBuf:62.3068 RSIBuf index:53.9716
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:12 MaBuf:64.3946 RSIBuf index:54.6842
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:13 MaBuf:63.6625 RSIBuf index:50.3103
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:14 MaBuf:60.1334 RSIBuf index:52.2308
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:15 MaBuf:55.5345 RSIBuf index:55.2434
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:16 MaBuf:52.8264 RSIBuf index:51.7062
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:17 MaBuf:52.9613 RSIBuf index:51.7062
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:18 MaBuf:54.3794 RSIBuf index:53.0647
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:19 MaBuf:53.5689 RSIBuf index:52.1671
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:20 MaBuf:52.6159 RSIBuf index:54.9707
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:21 MaBuf:52.3855 RSIBuf index:53.7881
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:22 MaBuf:51.7062 RSIBuf index:52.1346
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:23 MaBuf:53.4748 RSIBuf index:53.5181
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:24 MaBuf:53.7371 RSIBuf index:57.5509
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:25 MaBuf:51.2705 RSIBuf index:62.7159
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:26 MaBuf:52.4972 RSIBuf index:64.6092
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:27 MaBuf:54.3279 RSIBuf index:64.1801
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:28 MaBuf:54.2822 RSIBuf index:60.4335
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:29 MaBuf:53.4796 RSIBuf index:61.1766
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:30 MaBuf:52.7015 RSIBuf index:65.0629
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:31 MaBuf:52.6886 RSIBuf index:63.5495
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:32 MaBuf:52.2606 RSIBuf index:53.6636
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:33 MaBuf:52.2951 RSIBuf index:49.1658
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:34 MaBuf:54.2145 RSIBuf index:47.9234
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:35 MaBuf:56.1967 RSIBuf index:47.2326
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:36 MaBuf:56.1844 RSIBuf index:50.4176
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:37 MaBuf:55.8963 RSIBuf index:48.2769
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:38 MaBuf:53.9677 RSIBuf index:49.4693
2014.03.30 22:19:25.314 work EURUSD,M15: EA Index:39 MaBuf:0 RSIBuf index:52.1387

Note that the RSIBuf is calculated correctly between both bits of code - which as far as I can tell are more or less the same - but the iMAOnArray results are different.

Can anyone explain to me what's going on here?

 
nayphee: but the iMAOnArray results are different. Can anyone explain to me what's going on here?
Indicator buffers are set as series. Your arrays are not, so OnArray is calculating backwards.
 
ah, ok cool.
 
make those arrays into calculating buffers
Reason: