HELP: Indicator that shows Commulative Sum of a Data

 

Hi! Im new in meta Editor, I tried to make a custom Indicator that shows the commulative sum, in my case the commulative sum of mcs[]; here is my code:


//+------------------------------------------------------------------+
//|                                  Volume Momentum Cummulative.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
double sv;
double bv;
double mcs[];
double bid;
double ask; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,mcs);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,clrGold);
   SetIndexDrawBegin(0,1);
   
   
//---
   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[])
     {
     int counted_bars = IndicatorCounted();
     if (counted_bars<0)return(-1);
     if(counted_bars>0)counted_bars--;
     int uncountedbars=Bars-counted_bars;
     for(int i=0;i<uncountedbars;i++)
     {
     if(close[i]>open[i])
       {
        bv = (close[i] - open[i])*tick_volume[i];
       }
       else
          {
           bv = (close[i] - low[i])*tick_volume[i];
          }
     if(close[i] < open[i])
       {
        sv = (open[i] - close[i])*tick_volume[i];
       }else
          {
           sv = (high[i] - close[i])*tick_volume[i];
          }
      mcs[i] = bv - sv + mcs[i+1];
      }
  
//---
   
//--- return value of prev_calculated for next call
   return(0);
  }
//+------------------------------------------------------------------+

the indicator doesnt show the commulative sum, what is wrong with my code? please I need some advice. thanks in advance!!

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 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
 

the for loop is

     for(int i=0;i<uncountedbars;i++)

so the index i increases, and a high i means a bar towards the right, and a low i means a bar near the left of the graph, and the formula for mcs is

      mcs[i] = bv - sv + mcs[i+1];

so i+1 is an index in the future of the bar i, so i guess you need

     for(int i=uncountedbars-1;i>=0;i--)
 
Enau #:

the for loop is

so the index i increases, and a high i means a bar towards the right, and a low i means a bar near the left of the graph, and the formula for mcs is

so i+1 is an index in the future of the bar i, so i guess you need

I see, correct me if Im wrong,so it means that the current Candle is actually the latest ith period while the history candles has lesser i values?

 

yes for MT4, the current candle on the right has index 0 for the arrays as ''timeseries''


watch those 2 videos

https://youtu.be/3xaGznDPLSc

https://youtu.be/tk0uyhPNFl8
 
Enau #:

the for loop is

so the index i increases, and a high i means a bar towards the right, and a low i means a bar near the left of the graph, and the formula for mcs is

so i+1 is an index in the future of the bar i, so i guess you need


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 BarLimit = rates_total - prev_calculated;
   if(!prev_calculated){
      BarLimit-=2;
   }
   for(int i = 0 ; i <= BarLimit ; i++)
   {

   }
   return(rates_total);
} 

You have an Array Out of Range problem and probably Calculation Problems.

Unfortunately cannot help with the Calculations of your indicator as I never used tick_volume[] and did not find the proper documentation for the array.