Download MetaTrader 5

help with this simple indicator code

To add comments, please log in or register
eric_mql5
106
eric_mql5 2011.12.16 11:16 
//+------------------------------------------------------------------+
//|                                                          ROC.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2009, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description "Rate of Change"
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  LightSeaGreen
//--- input parameters
input int InpRocPeriod=12; // Period
input ENUM_TIMEFRAMES time_frame =PERIOD_M30;
//--- indicator buffers
double    ExtRocBuffer[];
//--- global variable
int       ExtRocPeriod;
//+------------------------------------------------------------------+
//| Rate of Change initialization function                           |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- check for input
   if(InpRocPeriod<1)
     {
      ExtRocPeriod=12;
      Print("Incorrect value for input variable InpRocPeriod =",InpRocPeriod,
            "Indicator will use value =",ExtRocPeriod,"for calculations.");
     }
   else ExtRocPeriod=InpRocPeriod;
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtRocBuffer,INDICATOR_DATA);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//--- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME,"ROC("+string(ExtRocPeriod)+")");
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtRocPeriod);
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Rate of Change                                                   |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,const int prev_calculated,const int begin,const double &price[])
  {
   
   for(int i=0;i<rates_total && !IsStopped();i++)
     {
      if(price[i]==0.0)
         ExtRocBuffer[i]=0.0;
      else{
         ExtRocBuffer[i]=myfunction(i,31,price);
         }
     }
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+

double myfunction(int shift,int period, const double &price[]){
   double ss =0.0;
   int j;
   for(j=0;j<period;j++)
      ss = ss + price[shift+j];
   return (10000.0*ss);
   }
   

I just copied the ROC code, and try to use a custum function that needs price[].

Also what is in price, close, open...?

thnks 

Slava
Moderator
6913
Slava 2011.12.16 12:06  

https://www.mql5.com/en/docs/basis/function/events#oncalculate

You can write second form of OnCalculate function and use predefined open[], high[], low[] or close[] array in accordance with your taste

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Language Basics / Functions / Event Handling Functions - Documentation on MQL5
eric_mql5
106
eric_mql5 2011.12.25 21:27  

I was busy I could replyt right away, thnk you.

but the problem I have with that indicator is that the end values are zero? I dont know what I am doing wrong. 

To add comments, please log in or register