Any Ideas on Why My Indicator Gives a Constant Value?

 

The idea of the indicator is to make a best fit line over a variable called backwards amount of ticks.  Then, the best fit line is projected forward a variable called forward number of ticks.  The value that is projected forward ticks ahead is then assigned as the current value of the indicator.

My problem is that I get a constant value of 6.324020 for the indicator's value and it looks like a horizontal line.

Does anyone know or have any ideas about my problem's solution?

Here's my code:

//+------------------------------------------------------------------+
//|                                                         TSF2.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot TSF
#property indicator_label1  "TSF"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      forwards=5;
input int      backwards=20;

// global variables

  // double xbar,ybar,B,TSF,num,denom;


//--- indicator buffers
double TSFBuffer[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TSFBuffer,INDICATOR_DATA);

  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(
                const int rates_total,    // number of available bars in history at the current tick
                const int prev_calculated,// number of bars, calculated at previous tick
                const int begin,          // index of the first bar
                const double &price[]     // price array for the calculation
                )

  {
  double xbar = 0;
  double ybar = 0;
  double numerator = 1;
  double denom = 1;
  double TSF = 0;
  
   if (prev_calculated>backwards){  
   
   for (int i = prev_calculated-backwards; i<prev_calculated; i++){ // for average totals
   xbar = xbar + 1;
   ybar = ybar + price[i];
   }
   
   // Get averages
   xbar = xbar/ backwards;
   ybar = ybar/ backwards;
   
  
   
      for (int y = prev_calculated-backwards; y < prev_calculated; y++){
         
         numerator = numerator  + (price[y]-ybar)*(1-xbar);
          denom = denom + (1-xbar)*(1-xbar);
      }
      
      }
   
   TSF = (numerator/denom)*forwards + price[rates_total-1];
   
 
 TSFBuffer[rates_total-1] = TSF;
 
   
  
//---
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Standard Constants, Enumerations and Structures / Objects Constants / Object Types - Documentation on MQL5