Plot Values Inconsistent

 

I am new to MT5 and having some trouble creating a simple moving average indicator. Sometimes the plots look correct, but sometimes the plot just flattens out at 0. Can anyone see what I am doing wrong here?


//+------------------------------------------------------------------+
//|                                                       MATest.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Plot
#property indicator_label1  "Plot"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      Period=10;
//--- indicator buffers
double         PlotBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,PlotBuffer,INDICATOR_DATA);
   
//---
   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 i=prev_calculated-1;
   int start=Period-1;
   if(start+1<prev_calculated)
      start=prev_calculated-2;
   
   for(int idx=start; idx<rates_total && !IsStopped(); idx++)
   {
      double sum = 0;
      for (int ii=idx; ii>idx-Period; ii--)
      {
         sum += iClose(Symbol(),PERIOD_CURRENT,ii);
      }
      double avg = sum / Period;
      
      PlotBuffer[idx] = avg;
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
Files:
MATest.mq5  3 kb
 

@Keith Watford


How to view an attached .mq5 online? or download a code  posted by code button as a .mq5? It is possible in codebase. Forum not support these features?

 
_MAHA_ #:

@Keith Watford

How to view an attached .mq5 online?  It is possible in codebase. Forum not support these features?

No, unfortunately and it would be a good option.

 
Anyone???
 
Greg Burgess: I am new to MT5 and having some trouble creating a simple moving average indicator. Sometimes the plots look correct, but sometimes the plot just flattens out at 0. Can anyone see what I am doing wrong here?

Don't use the iClose function. The event handler already provides the correct close prices in the proper sequence for the indicator.

sum += close[ii]; // sum += iClose(Symbol(),PERIOD_CURRENT,ii);

This is because iClose behaves as a series, but the OnCalculate event handler data in MT5 behaves as a non-series.