Download MetaTrader 5

Angle indicator keeps returning 0.000

To add comments, please log in or register
Post web links to other trader's signals and make profit
n22alpha
63
n22alpha 2015.02.25 00:05 

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. 

Vladimir Karputov
Moderator
45586
Vladimir Karputov 2015.02.25 08:56  

Need to be updated with the default value:

      if (rates_total < MA_period) return(0);
      _ma.Refresh();
      if (_ma.BarsCalculated() < rates_total) return(0);
n22alpha
63
n22alpha 2015.02.25 11:59  

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

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

Vladimir Karputov
Moderator
45586
Vladimir Karputov 2015.02.25 12:18  
//+------------------------------------------------------------------+
//|                                                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 

Vladimir Karputov
Moderator
45586
Vladimir Karputov 2015.02.25 15:33  
I continue to study the problem. I think that the indicator CiMA, in itself, has a displacement of buffers.
Vladimir Karputov
Moderator
45586
Vladimir Karputov 2015.02.25 19:10  

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 

n22alpha
63
n22alpha 2015.02.25 23:11  

so CiMA class not working properly???? 

 P.S.

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

Vladimir Karputov
Moderator
45586
Vladimir Karputov 2015.02.26 03:59  
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.
n22alpha
63
n22alpha 2015.02.26 12:40  

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. 

Vladimir Karputov
Moderator
45586
Vladimir Karputov 2015.02.26 16:17  
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.
To add comments, please log in or register