Can anyone tell me why my indicator doesn't work properly

 

I'm trying to write an indicator. It kind of works but in backtesting the values returned don't match what's shown in the chart.

Where am I going wrong?

Here's the code:

//+------------------------------------------------------------------+
//|                                                          VMA.mq4 |
//|                                        https://www.metaquotes.net |
//+------------------------------------------------------------------+

#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue

//---- input parameters
extern int       period = 55;
extern int       volatilityPeriod = 60;

double VMA[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,VMA);
   SetIndexLabel(0, "VMA");
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
    int    counted_bars=IndicatorCounted();
    int    i, start;
    double k;
    
    //----
    i = Bars - counted_bars;

    while (i > 0) {
        // Smoothing constant
        double sc  = 2.0 /(period + 1);

        // Volatility index
        double vi = MathAbs(iCustom(NULL,0,"CMO",true,volatilityPeriod,0,0)) / 100;

        double vma = sc * vi * Close[i] + (1 - sc * vi) * VMA[i+1];
    
        VMA[i] = vma;
    
        i--;
    }
    //----
    return(0);
}
//+------------------------------------------------------------------+
 
We don't know what it's trying to do. Maybe explain what you're trying to achieve/expecting vs what you got.
 
ubzen:
We don't know what it's trying to do. Maybe explain what you're trying to achieve/expecting vs what you got.

It's a Variable Moving Average (AKA VIDYA) Indicator using a CMO Indicator I downloaded. The indicator works in charts when I select it, but when I try to use the indicator in backtesting the values that are returned at any point in time are not the same as what is shown in the charts. There seems to be some sort of 'shift lower' (e.g. all values are say 100 pips down) when I run it through the backtester and I can't figure out why.
 
double vi = MathAbs(iCustom(NULL,0,"CMO",true,volatilityPeriod,0,0)) / 100;
double vma = sc * vi * Close[i] + (1 - sc * vi) * VMA[i+1];
  1. vi should be changing, probably
    double vi = MathAbs(iCustom(NULL,0,"CMO",true,volatilityPeriod,0,i)) / 100;
    
  2. Don't calculate ema's like that, prone to round off errors increasing
    vma[i] = vma[i+1] + sc * (vi * Close[i] - vma[i+1]);

 

U da man!!

I've changed the code to:

    while (i > 0) {
        // Smoothing constant
        double sc  = 2.0 /(period + 1);

        // Volatility index
        double vi = MathAbs(iCustom(NULL,0,"CMO",true,volatilityPeriod,0,0)) / 100;
        
        VMA[i] = VMA[i+1] + sc * (vi * (Close[i] - VMA[i+1]));
    
        i--;
    }

And now it seems to be working as expected.

Thanks.

Reason: