iMA() in EA is not the same like in Custom indicator

 

Hello,

I programmed a simple EA and need the values out of the Custome Indicator. I dont know how to get those values from the Custome Indicator into the EA so I calculate them in EA programm too.

I started MT4 twice and compared the print() in both windows and they seemed not to be the same. The Indicator doesen`t refresh as much than the EA does. Also the iMA() in the EA goes up to 88 or drops to zero the other don`t (max was 82 while i observed it). The IndicatorCounted(); in EA is -1 and in Indicator about 1700.

I also figured out i have to write Print("middle2=",iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x)); and not Print("middle2=",middle2, x)); in the EA. Which means I couldnt refer to the definition of middle2 in the EA.

This is the Code of the EA:

double upper[], middle[], lower[], checkpointa[], checkpointb[];
extern int period = 100;
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

   int start() 
{  
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
      
   
   limit=Bars-counted_bars;
   
   double avg;
  
   for(int x=0; x<limit; x++) 
   {
      middle[x] = iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x);
      avg  = findAvg(period, x);
      upper[x] = middle[x] + avg;
      lower[x] = middle[x] - avg;
      checkpointa[x] = upper[x]+0.07;
      checkpointb[x] = lower[x]-0.07; 
     
    // Print("indicator=",counted_bars);
    Print("middle2=",iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x));
    //Print("checkpointa=",checkpointa[x]);
    //Print("upper=",upper[x]);
    //Print("avg=",avg);
    }         
   return(0);
  }
//+------------------------------------------------------------------+


   double findAvg(int period, int shift) {
      double sum=0;
      for (int x=shift;x<(shift+period);x++) {     
         sum += High[x]-Low[x];
      }
      sum = sum/period;
      return (sum);
   }

And this is the Custome Indicator:

#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2 White
#property indicator_color3 White
#property indicator_color4 Red
#property indicator_color5 Red

double upper[], middle[], lower[], checkpointa[], checkpointb[];
extern int period = 100;


int init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,0);
   SetIndexDrawBegin(0,0);
   SetIndexBuffer(0,upper);

   SetIndexStyle(1,DRAW_LINE);
   SetIndexShift(1,0);
   SetIndexDrawBegin(1,0);
   SetIndexBuffer(1,middle);

   SetIndexStyle(2,DRAW_LINE);
   SetIndexShift(2,0);
   SetIndexDrawBegin(2,0);
   SetIndexBuffer(2,lower);
    
   SetIndexStyle(3,DRAW_LINE);
   SetIndexShift(3,0);
   SetIndexDrawBegin(3,0);
   SetIndexBuffer(3,checkpointa);

   SetIndexStyle(4,DRAW_LINE);
   SetIndexShift(4,0);
   SetIndexDrawBegin(4,0);
   SetIndexBuffer(4,checkpointb);
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() 
{  
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
      
   
   limit=Bars-counted_bars;
   
   double avg;
  
   for(int x=0; x<limit; x++) 
   {
      middle[x] = iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x);
      avg  = findAvg(period, x);
      upper[x] = middle[x] + avg;
      lower[x] = middle[x] - avg;
      checkpointa[x] = upper[x]+0.07;
      checkpointb[x] = lower[x]-0.07; 
     
    // Print("indicator=",counted_bars);
    Print("middle2=",iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x));
    //Print("checkpointa=",checkpointa[x]);
    //Print("upper=",upper[x]);
    //Print("avg=",avg);
    }         
   return(0);
  }
//+------------------------------------------------------------------+


   double findAvg(int period, int shift) {
      double sum=0;
      for (int x=shift;x<(shift+period);x++) {     
         sum += High[x]-Low[x];
      }
      sum = sum/period;
      return (sum);
   }

Thanks for your Help

AlexDoe

 

try this code:

#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2 White
#property indicator_color3 White
#property indicator_color4 Red
#property indicator_color5 Red

double upper[], middle[], lower[], checkpointa[], checkpointb[];
extern int period = 100;


int init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,0);
   SetIndexDrawBegin(0,0);
   SetIndexBuffer(0,upper);

   SetIndexStyle(1,DRAW_LINE);
   SetIndexShift(1,0);
   SetIndexDrawBegin(1,0);
   SetIndexBuffer(1,middle);

   SetIndexStyle(2,DRAW_LINE);
   SetIndexShift(2,0);
   SetIndexDrawBegin(2,0);
   SetIndexBuffer(2,lower);
    
   SetIndexStyle(3,DRAW_LINE);
   SetIndexShift(3,0);
   SetIndexDrawBegin(3,0);
   SetIndexBuffer(3,checkpointa);

   SetIndexStyle(4,DRAW_LINE);
   SetIndexShift(4,0);
   SetIndexDrawBegin(4,0);
   SetIndexBuffer(4,checkpointb);
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() 
{  
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
      
   
   limit=Bars-counted_bars;
   
   double avg;
  
   for(int x=limit-1; x>=0 x--) 
   {
      middle[x] = iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x);
      avg  = findAvg(period, x);
      upper[x] = middle[x] + avg;
      lower[x] = middle[x] - avg;
      checkpointa[x] = upper[x]+0.07;
      checkpointb[x] = lower[x]-0.07; 
     
    // Print("indicator=",counted_bars);
    Print("middle2=",iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x));
    //Print("checkpointa=",checkpointa[x]);
    //Print("upper=",upper[x]);
    //Print("avg=",avg);
    }         
   return(0);
  }
//+------------------------------------------------------------------+


   double findAvg(int period, int shift) {
      double sum=0;
      for (int x=shift;x<(shift+period);x++) {     
         sum += High[x]-Low[x];
      }
      sum = sum/period;
      return (sum);
   }
 

Thank you, but it doesn`t changed. Would it work if I would use iCustom?

 

this code should work the same way in EA's a s in Indicators.

Since you compare the results trough the log, please notice that AFAIK the log-output is not guaranteed to be complete.

On thing. i dont know if IndicatorCounted() is allowed to be called inside an EA...

and your array in the EA have no Size..

//z

 

Even the log is not complete I dont understand why those values are this different. Like I told iMA() in the EA goes upt to 88. In custom Indicator only to 82. I thought about a array or buffer problem but have no idea how to handle it.

 

the arrays in you EA have no size, in indicators the buffers gets shifted and resized automatically, in EA's they don't

 

Do you have an idea how to solve this problem?

 

(not having read topic in detail but anyway ...)

One way (yes, there are other ways too) to solve problem is to build a custom indicator (with resized buffers etc), and an EA calling the indic via iCustom, and passing back up to 8 values via mode setting.

 

* note to admin *

How come my post is timestamped (2010.12.18 04:48) several hours BEFORE the previous post (2010.11.19 21:26) ? And no, that isn't my local time either.

Now, if I could work out how to do that with an EA, I'll have my fortune of millions made so easily .....

 
AlexDoe:

Thank you, but it doesn`t changed. Would it work if I would use iCustom?

If you're using a custom indicator you must use iCustom.

You can't put the code in the EA as there is no IndicatorCounted() but then you only need the current value not previous values.

If iCustom doesn't match iMA, your custom call is wrong.

upper[x] = middle[x] + avg;
Middle is a MA on price. Avg is an average price. Price+Price is meaning less.
 

double iCustom( string symbol, int timeframe, string name, ..., int mode, int shift)


double iCustom( your symbol, timeframe, "YOUR INDICATOR NAMNE", (Options for your indicator, in extern), Your buffer you want to check(0-7), which bar)
Reason: