Having Trouble Programming Custom Indicator; Keep Getting Value of 0

 

Hi,

I keep getting a value of 0 for my custom indicator.  Even if I set the value of the indicator buffer to 2 in a loop, I still get 0.  So, something is going wrong.


What's interesting is that I can copy my code (formulas, variables, etc) into another indicator that works but never call on my code to actually do anything.  That'll actually mess up the different indicator and make it return a value of 0.i


I think I'm making some basic programming mistake because I can rewrite the indicator from scratch and use different calculations for it; I still get the same value of 0 returned to the indicator.  Copying that code into a new indicator then gives me a value of 0 even if none of that code is used in the calculations involved in returning a value to the new indicator.

All I'm doing is declaring some variables in On_Calculate, doing some calculations with them, and then returning one of them as a double value to the indicator buffer in a for loop running from a set number of ticks forward all the way to the latest tick, rates_total.

Any ideas?

 

Could you provide code of your indicator?

MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 

Yes, here is my code:


Notice that the indicator buffer, TSFBuffer[], is set to equal 2.  I'm only making it that so that I can know whether it's working or not.  Instead of giving me 2 for every tick, it's giving me 0.000000000.

The calculations aren't being used so I'm not correctly calculating the indicator here.  I want to know why it's giving me 0.00000000 instead of 2.  Once I know that, I can fix everything else.


//+------------------------------------------------------------------+
//|                                                         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=25;
input int      backwards=250;

// global variables

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


//--- indicator buffers
double         TSFBuffer[];
double Numerator[];
double Xbar[];
double Ybar[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TSFBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,Numerator,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,Xbar,INDICATOR_CALCULATIONS); 
   SetIndexBuffer(3,Ybar,INDICATOR_CALCULATIONS);

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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
                )

  {
 
  Xbar[1] = 0;
  Ybar[1] = 0;
 
   for (int i =0; i<backwards; i++){ // for average totals
   Xbar[1] = Xbar[1] + i;
   Ybar[1] = Ybar[1] + price[rates_total-i];
   }
  
   // Get averages
   Xbar[1] = Xbar[1]/backwards;
   Ybar[1] = Xbar[1]/backwards;
 
 
   for (int i = backwards; i< rates_total; i++){
      Numerator[i] = 0; // initialize
  
      for (int y = i - backwards; y < i;y++){
        
         Numerator[i] = Numerator[i]  + (-Xbar[1])*(price[y]-Ybar[1]);
         // denom = denom + (i-xbar)*(i-xbar);
      }
   }
 
 
 
   for (int i = backwards; i < rates_total; i++){
   TSFBuffer[i] = 2;
   }
 
//---
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

/*
double TSFValue(){


 

  

  
  
  
   B = num / denom; // calculate slope
  
   TSF = (B*forwards) + price[rates_total];

}

*/


Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Insert code properly please.
MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
Reason: