Download MetaTrader 5
To add comments, please log in or register
Have you found a good programmer? Make a targeted order!
brettosm8
241
brettosm8 2016.01.26 04:27 

Hi there,

My EA has bands that don't match the values of the bands in an indicator I'm using to test my EA.

The indicator displays the bands as below:

The upper band value of the previous bar to the current bar is:

 

It's code looks like this:

double upper[], middle[], lower[];
extern int period = 20;


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);
   
   IndicatorDigits(8 );

//---- 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) return(-1);
   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_EMA, PRICE_TYPICAL, x);
      avg  = findAvg(period, x);
      upper[x] = middle[x] + (iATR(NULL, 0, period, 0)*2.25);
      lower[x] = middle[x] - (iATR(NULL, 0, period, 0)*2.25);
   }
   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);
   }

 My EA prints out band values, as below.  Since Strategy Tester is set to open prices only, the most recent upper value should be the same as above.

 

 The code for this is:

//Declared as below
int      ChannelPeriod=20;
double   MiddleLine;
double   UpperLine;
double   LowerLine;   

//in OnTick() as below.
MiddleLine  = iMA(NULL, 0, ChannelPeriod, 0, MODE_EMA, PRICE_TYPICAL,1);
Print("Middle: ",MiddleLine);
UpperLine   = MiddleLine + (iATR(NULL, 0, ChannelPeriod, 1)*2.25);
Print("Upper: ",NormUpperLine);
LowerLine   = MiddleLine - (iATR(NULL, 0, ChannelPeriod, 1)*2.25);

 

p.s. I start strategy tester, apply the indicator and compare its values with those printed by my EA.

Can you please help to clarify why the values don't match?

Thankyou thankyou thankyou. 

brettosm8
241
brettosm8 2016.01.27 07:30  
Found I can get the result I want using iCustom.
Carl Schreiber
5927
Carl Schreiber 2016.01.27 08:20  

Why don't you directly subtract the indicator value from what you have calculated? Like:

MiddleLineDif  = iMA(NULL, 0, ChannelPeriod, 0, MODE_EMA, PRICE_TYPICAL,1) - iCustom(NULL,0,  "Keltner Channel",ChannelPeriod, MODE_EMA,PRICE_TYPICAL,   mid,1);
UpperLineDif   = MiddleLine + (iATR(NULL, 0, ChannelPeriod, 1)*2.25)  - iCustom(NULL,0,  "Keltner Channel",ChannelPeriod, MODE_EMA,PRICE_TYPICAL,   upp,1);
LowerLineDif   = MiddleLine - (iATR(NULL, 0, ChannelPeriod, 1)*2.25)  - iCustom(NULL,0,  "Keltner Channel",ChannelPeriod, MODE_EMA,PRICE_TYPICAL,   low,1);

But you have to check the sequence of the parameter for iCustom. Beside this compare the definition/code of Keltner channel and your calculation.

/
To add comments, please log in or register