Angle indicator keeps returning 0.000

 

Hi guys,

 Sorry to disturbed you guys again but I wrote this code and it keeps return 0.0000. All the values in CiMA signal are the same and I'm not sure why. I have a refresh function in the code I am hoping someone can help. Sorry to be annoying and thank you for help. This is one of my first every indicators and I'm trying to get the hang of it.

 

//+------------------------------------------------------------------+
//|                                                MA_angle_CiMA.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_minimum -90
#property indicator_maximum 90
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Angle
#property indicator_label1  "Angle"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#include <Indicators\Trend.mqh>
//--- input parameters
input int      MA_period = 100;
input ENUM_MA_METHOD     MA_type = MODE_SMA;
input int      AngleBars = 2;
//--- indicator buffers
double         AngleBuffer[];

CiMA *_ma;

double pi = 3.14159265358979323846;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

         SetIndexBuffer(0,AngleBuffer,INDICATOR_DATA);
         
   _ma = new CiMA;
   
     if (AngleBars < 2)
   {
      Print("Error anglebars less than 2, ending program");
      return(INIT_FAILED);
   }
  
   
   if (AngleBars > MA_period)
   {
      Print("Error anglebars must be less than moving average period");
      return(INIT_FAILED);
   }
   
   if (_ma == NULL)
      {
         Print("Indicator null value, exiting program");
         return(INIT_FAILED);
      }
      
    if (!_ma.Create(_Symbol,_Period, MA_period, 0, MA_type, PRICE_CLOSE))
      {
         Print("Error creating indicator, exiting program");
         return(INIT_FAILED);
      }
      


//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
{
   delete(_ma);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
      int i, start;
      
      if (rates_total < MA_period) return(0);
      
      _ma.Refresh(_Period);
      
      if (_ma.BarsCalculated() < rates_total) return(0);
      
       if (prev_calculated == 0)
      {
         for (i = 0; i < (AngleBars - 1); i++)
         {
            AngleBuffer[i] = 0.0;
         }
      }
      
      if (prev_calculated == 0)
      {
         start = AngleBars - 1;
      }
    else
      {
      //rates_total for the previous tick = prev_calculated
       start = prev_calculated - 1;
      }
      
       
    for (i = start; i < rates_total; i++)
      {
         
         double diff = _ma.Main(i) - _ma.Main(i - (AngleBars - 1));
         
         double roc = (diff * 10000)/AngleBars;
         
         AngleBuffer[i] = MathArctan(roc) * (180/pi);
         
      }
      
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

The code I am not sure where the problem is tbh. 

 

Need to be updated with the default value:

      if (rates_total < MA_period) return(0);
      _ma.Refresh();
      if (_ma.BarsCalculated() < rates_total) return(0);
 

sorry to be annoying what do you mean default value???

 I tried using your code and I still get 0.0000 returns. 

 
//+------------------------------------------------------------------+
//|                                                MA_angle_CiMA.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_minimum -90
#property indicator_maximum 90
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Angle
#property indicator_label1  "Angle"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#include <Indicators\Trend.mqh>
//--- input parameters
input int      MA_period=100;
input ENUM_MA_METHOD     MA_type=MODE_SMA;
input int      AngleBars=2;
//--- indicator buffers
double         AngleBuffer[];
CiMA *_ma;
double pi=3.14159265358979323846;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,AngleBuffer,INDICATOR_DATA);
   _ma=new CiMA;
   if(AngleBars<2)
     {
      Print("Error anglebars less than 2, ending program");
      return(INIT_FAILED);
     }
   if(AngleBars>MA_period)
     {
      Print("Error anglebars must be less than moving average period");
      return(INIT_FAILED);
     }
   if(_ma==NULL)
     {
      Print("Indicator null value, exiting program");
      return(INIT_FAILED);
     }
   if(!_ma.Create(_Symbol,_Period,MA_period,0,MA_type,PRICE_CLOSE))
     {
      Print("Error creating indicator, exiting program");
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   delete(_ma);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int i,start;
   if(rates_total<MA_period)
      return(0);
   _ma.Refresh();
   if(_ma.BarsCalculated()<rates_total)
      return(0);
//--- preliminary calculations
   if(prev_calculated==0)
     {
      for(i=0; i<(AngleBars-1); i++)
        {
         AngleBuffer[i]=0.0;
        }
      start=AngleBars-1;
     }
   else
     {
      //rates_total for the previous tick = prev_calculated
      start=prev_calculated-1;
     }
   for(i=start; i<rates_total; i++)
     {
      double diff=_ma.Main(i)-_ma.Main(i -(AngleBars-1));
      double roc=(diff*10000)/AngleBars;
      AngleBuffer[i]=MathArctan(roc) *(180/pi);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Your indicator:

1 

 
I continue to study the problem. I think that the indicator CiMA, in itself, has a displacement of buffers.
 

Rate indicator. There is a nuance: calculation goes only once.

 

//+------------------------------------------------------------------+
//|                                                MA_angle_CiMA.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window

#property indicator_buffers 1
#property indicator_plots   1
//--- plot Angle
#property indicator_label1  "Angle"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- input parameters
input int      MA_period=100;
input ENUM_MA_METHOD     MA_type=MODE_SMA;
//--- indicator buffers
double         AngleBuffer[];
double         TempBuffer[];
int            handle_MA;
double pi=3.14159265358979323846;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,AngleBuffer,INDICATOR_DATA);
   handle_MA=iMA(_Symbol,_Period,MA_period,0,MA_type,PRICE_CLOSE);
//--- if the handle is not created
   if(handle_MA==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  _Symbol,
                  EnumToString(_Period),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//Print(__FUNCTION__);
   int i,start;
   double diff,roc;
   if(rates_total<MA_period)
      return(0);
//--- preliminary calculations
   if(prev_calculated==0)
     {
      int to_copy=rates_total-prev_calculated;
      if(CopyBuffer(handle_MA,0,0,to_copy,TempBuffer)<=0)
        {
         Print("getting _ma is failed! Error",GetLastError());
         return(0);
        }
      for(i=1;i<rates_total;i++)
        {
         diff=TempBuffer[i]-TempBuffer[i-1];
         roc=(diff*10000)/MA_period;
         AngleBuffer[i]=MathArctan(roc) *(180/pi);
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

Picture:

1 

 

so CiMA class not working properly???? 

 P.S.

what do you mean "rate indicator" (sorry to be annoying). 

 
n22alpha:

so CiMA class not working properly???? 

 P.S.

what do you mean "rate indicator" (sorry to be annoying). 

I asked to estimate the indicator.
 

the indicator is great I am wondering great how about the CiMA class is it bugged when used in indicators???

 If it is I'll use the traditional coding method of handles. 

 
n22alpha:

the indicator is great I am wondering great how about the CiMA class is it bugged when used in indicators???

 If it is I'll use the traditional coding method of handles. 

This indicator is easier to read using the handle of.