sum results of a formula - MQL5

 

Hello folks,


I think it is very easy, but I'm a begginer in MQL5 programming.

I would like to return the sum of periods of the following formula.


n = Number of periods

TimeFrame = 15 minutes


ich = (Candle_High - Candle_Low) * Candle_Volume


Final results should be the sum of -> ich(n) + ich(n-1) + ich(n-2) ... ich (0).

 
Guilherme Mendonca :

Hello folks,


I think it is very easy, but I'm a begginer in MQL5 programming .

I would like to return the sum of periods of the following formula.


n = Number of periods

TimeFrame = 15 minutes


ich = (Candle_High - Candle_Low) * Candle_Volume


Final results should be the sum of -> ich(n) + ich(n-1) + ich(n-2) ... ich (0).

You need to write an indicator. The indicator will have one input parameter: "Number of bars - N".

 

Here is the indicator code (the code uses a constant passage through the "j" cycle - this is not an optimal and not very beautiful method, but it is accessible and understandable)

//+------------------------------------------------------------------+
//|                                              High Low Volume.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Force
#property indicator_label1  "Force"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      Inp_Number_Bars   = 3;  // Number of bars
//--- indicator buffers
double         ForceBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ForceBuffer,INDICATOR_DATA);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,Digits());
//--- name for DataWindow
   IndicatorSetString(INDICATOR_SHORTNAME,"HLW"+"("+IntegerToString(Inp_Number_Bars)+")");
//---- sets drawing line empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//--- check for bars count
   if(rates_total<Inp_Number_Bars-1)
      return(0);// not enough bars for calculation
//--- main loop
   int i=0,limit=0;
//--- first calculation or number of bars was changed
   if(prev_calculated==0)// first calculation
     {
      ArrayInitialize(ForceBuffer,0.0);
      limit=Inp_Number_Bars;
      //---
      double firstValue=0.0;
      for(i=0; i<limit; i++)
         ForceBuffer[i]=0.0;
     }
   else
      limit=prev_calculated-1;
//--- main loop
   for(i=limit; i<rates_total && !IsStopped(); i++)
     {
      double firstValue=0.0;
      for(int j=i-Inp_Number_Bars+1; j<=i; j++)
         firstValue+=(high[j]-low[j])*tick_volume[j];

      ForceBuffer[i]=firstValue;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Result:

High Low Volume

Files:
 
Guilherme Mendonca:

Hello folks,


I think it is very easy, but I'm a begginer in MQL5 programming.

I would like to return the sum of periods of the following formula.


n = Number of periods

TimeFrame = 15 minutes


ich = (Candle_High - Candle_Low) * Candle_Volume


Final results should be the sum of -> ich(n) + ich(n-1) + ich(n-2) ... ich (0).

ich[i] += (Candle_High[j] - Candle_Low[j]) * Candle_Volume[j];

I let you think about the appropriated loop to do it, but here you go

 
Icham Aidibe :

I let you think about the appropriated loop to do it, but here you go

I did not understand anything.

 
Vladimir Karputov:

I did not understand anything.

?????

full code ?????

 
Icham Aidibe :

?????

full code ?????

You write clearly. I can not understand you..

 
Vladimir Karputov:

You write clearly. I can not understand you..

Sorry, english isn't my first language so writing & talking using it restricts the quality & the understanding of my expression 

Reason: