What's wrong with my simple iMA indicator ?? (MQL5)

 

Hi!

For some reason, it doesn't show anything and if I click on "Refresh", then I only see the moving average last part on the chart.

I must precise that I'm offline using custom symbols.

Please help!

Thanks

#property strict

#property indicator_buffers 1
#property indicator_plots   1
#property indicator_chart_window

#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3

input int periodMA= 10;

double MA[];
int    ma_handle;

//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,MA); //,INDICATOR_DATA);

   ArraySetAsSeries(MA,true);

   ResetLastError();
   ma_handle= iMA(NULL,0,periodMA,0,MODE_EMA,PRICE_CLOSE);
   if(ma_handle==INVALID_HANDLE)
     {
      Print("The creation of iMA has failed: Runtime error =",GetLastError());
      return(-1);
     }

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   if(BarsCalculated(ma_handle) < rates_total)
      return 0;

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rates_total-periodMA-1);

   if(CopyBuffer(ma_handle,0,0, rates_total-1-MathMax(periodMA,prev_calculated), MA) <= 0)
      return 0;

   return rates_total-1;
  }
 
ccou:

Hi!

For some reason, it doesn't show anything and if I click on "Refresh", then I only see the moving average last part on the chart.

I must precise that I'm offline using custom symbols.

Please help!

Thanks



int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   if(BarsCalculated(ma_handle) < rates_total)
      return 0;
   int to_copy; 
   if(prev_calculated>rates_total || prev_calculated<=0) 
        to_copy=rates_total; 
   else 
     { 
      to_copy=rates_total-prev_calculated; 
      to_copy++; 
     } 

   //PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rates_total-periodMA-1);

   if(CopyBuffer(ma_handle,0,0, to_copy, MA) <= 0)
      return 0;

   return rates_total-1;
  }
 

Thanks. Can you please explain ? :

1) When "prev_calculated" can be > "rates_total", in which cases ?

2) When "prev_calculated" can be < 0, in which cases ?  (I understand it can be == 0)

Thanks

 

"prev _ calculated > rates _ total" and "prev _ calculated < 0" are update errors, in which case they are restarted from the beginning.

Place the cursor over "CopyBuffer" and push F1 to access the reference. There is a sample, so please check it by yourself.

Reason: