iMAOnArray returning wrong values when accesed from EA.

 

I wrote a custom indicator that uses iMAOnArray to average certain values. The indicator works great and calculates corectly. However when i try to acces the indicators values from an EA using iCustom the values retrieved are wrong. This values are used to calculate the SL and TP levels. The values are always corect to the pip on the first trade then they start to be wrong and get somewhat worse.

Here is a snipet of the code of the custom indicator:

extern int       Periods=5;




double BodyBuffer[];


double Body[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  

  
  
  
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,BodyBuffer);
   SetIndexLabel(2, "Body");

  
   ArraySetAsSeries(Body, true);


  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

    ArrayResize(Body, Bars);
  
  
   int    counted_bars=IndicatorCounted();
  
   int i,    limit=Bars-counted_bars;
  
  
  for( i=limit; i>=0; i--)
   {
  
  
  
if (Close[i]>Open[i])
   {
   Body[i]=Close[i]-Open[i];
   }
else
   {
   Body[i]=Open[i]-Close[i];
   }
  }
for(i=limit; i>=0; i--)
{
BodyBuffer[i]=iMAOnArray(Body,0,Periods,0,MODE_SMA,i);
}
//----
  
//----
   return(0);

To retrieve its values in the EA i use 





double BODY = iCustom(NULL,0,"BodyInd",Periods,2,1);

then i pas the value of body to my OrderSend function as the SL. As i said only the first trade has a corect value all the other trades are off by a few pips. Any sugestions on what i am doing wrong and how to fix this will be greatly apreciated.

 

Your indicator is wrong, because it uses only two indicator buffers but source contains such block:

   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,BodyBuffer);
   SetIndexLabel(2, "Body");

See also article Tester in the Terminal MetaTrader 4: It Should Be Known.

 
Rosh :

Your indicator is wrong, because it uses only two indicator buffers but source contains such block:

   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,BodyBuffer);
   SetIndexLabel(2, "Body");

See also article Tester in the Terminal MetaTrader 4: It Should Be Known.

my indicator is not wrong. The whole indicator has 3 buffers. This is only part of the indicator to give an idea about how the arrays have been populated. Lke i said the indicator works fine, its when i acces it from EA that i have problems.

 

I did some more testing trying to solve to problem with no succes. Atached is an EA that writes the values of the indicator to the log. Also i will atach the indicator and a script that i ran on the daily char to export the indicators values to a text file. Here is a snipet of the values side by side:

Values Returned By EA					Values Returned By Script			

Date/Time     U_Shadow:	Body:   L_Shadow:	Date/Time       U_Shadow Body: 	L_Shadow	

2007.05.31	0.0023	0.0022	0.0016		2007.05.31	0.0023	0.0022	0.0016
2007.06.01	0.0019	0.0020	0.0018		2007.06.01	0.0023	0.0020	0.0016
2007.06.04	0.0019	0.0023	0.0016		2007.06.04	0.0023	0.0023	0.0015
2007.06.05	0.0017	0.0024	0.0019		2007.06.05	0.0022	0.0021	0.0015
2007.06.06	0.0019	0.0019	0.0021		2007.06.06	0.0025	0.0020	0.0016
2007.06.07	0.0021	0.0019	0.0018		2007.06.07	0.0022	0.0018	0.0018
2007.06.08	0.0019	0.0019	0.0023		2007.06.08	0.0023	0.0017	0.0022
2007.06.11	0.0017	0.0023	0.0026		2007.06.11	0.0021	0.0020	0.0027
2007.06.12	0.0016	0.0025	0.0020		2007.06.12	0.0020	0.0022	0.0028
2007.06.13	0.0016	0.0019	0.0020		2007.06.13	0.0014	0.0019	0.0033
2007.06.14	0.0017	0.0021	0.0020		2007.06.14	0.0013	0.0021	0.0032
2007.06.15	0.0017	0.0024	0.0014		2007.06.15	0.0012	0.0023	0.0030
2007.06.18	0.0017	0.0020	0.0021		2007.06.18	0.0011	0.0018	0.0037
2007.06.19	0.0016	0.0018	0.0025		2007.06.19	0.0011	0.0018	0.0035
2007.06.20	0.0017	0.0021	0.0018		2007.06.20	0.0009	0.0020	0.0034
2007.06.21	0.0018	0.0021	0.0017		2007.06.21	0.0008	0.0019	0.0034
2007.06.22	0.0017	0.0019	0.0017		2007.06.22	0.0007	0.0021	0.0028
2007.06.25	0.0016	0.0019	0.0023		2007.06.25	0.0007	0.0016	0.0030
2007.06.26	0.0016	0.0020	0.0022		2007.06.26	0.0008	0.0016	0.0030
2007.06.27	0.0017	0.0022	0.0015		2007.06.27	0.0008	0.0019	0.0025
2007.06.28	0.0017	0.0024	0.0014		2007.06.28	0.0008	0.0019	0.0025
2007.06.29	0.0018	0.0023	0.0014		2007.06.29	0.0009	0.0018	0.0026
2007.07.02	0.0018	0.0021	0.0024		2007.07.02	0.0009	0.0019	0.0027
2007.07.03	0.0016	0.0020	0.0032		2007.07.03	0.0009	0.0021	0.0033
2007.07.04	0.0016	0.0021	0.0025		2007.07.04	0.0008	0.0020	0.0034
2007.07.05	0.0017	0.0020	0.0016		2007.07.05	0.0009	0.0020	0.0032

obviously i imported the log and the text file created by the script in an excel file which i will also atach. Both script and EA have used identical parameters for the iCustom function used to retrieve the indicator. I know the problem is with the iMaOnArray function because if choose the averaging period of 1 (meaning no averaging) then all the values are corect. Please advise what to do to fix the problem of if this is a bug.

Files:
 
here is the script
Files:
 
Indicator
Files:
 
and excel file
Files:
 
Any thoughts? Anyone?
 
ser01 :
Any thoughts? Anyone?

I dont know if this has anything to do regarding your problem, but it seems that the indicator is wrong here :

int i, limit=Bars-counted_bars;

for( i=limit; i>=0; i--)
{

if (Close[i]>Open[i])
{
etc..

when counted_bars == 0, your index i has Bars as value, but the maximum index possible is Bars-1 !

 
MichelB :
ser01 :
Any thoughts? Anyone?

I dont know if this has anything to do regarding your problem, but it seems that the indicator is wrong here :

int i, limit=Bars-counted_bars;

for( i=limit; i>=0; i--)
{

if (Close[i]>Open[i])
{
etc..

when counted_bars == 0, your index i has Bars as value, but the maximum index possible is Bars-1 !

I changed that line to int i, limit=Bars-counted_bars-1; but it still did not solve my problem

 
ser01 :

I changed that line to int i, limit=Bars-counted_bars-1; but it still did not solve my problem

I think you should try to not compute bar 0, like this :

int i, limit=Bars-IndicatorCounted()-1;
for( i=limit; i>0; i--) /// NOT ">="
{

I am not sure, but it seems that your indic access the 3 arrays as series arrays, you should also try to define them like that using ArraySetAsSerie(), otherwize, IMAOnArray take the elements in the wrong order

Reason: