Custom Indicator not drawing when timeframe changed

 

Hi All,


I've written a test custom indicator that uses 2 MA. It drew the indicator fine when I first attached it to a chart

however when I changed the timeframe. It somehow only draw the last bar and continue from the latest bar.

Please help.


PS. There's no error in the expert tab. 


Unfinished Indicator

Here's the code.


#property copyright "Copyright 2021, Teststyle"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot MADiff
#property indicator_label1  "MADiff"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      MAFast=10;
input int      MASlow=20;
//--- indicator buffers
double         MADiffBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  
  
//--- indicator buffers mapping
   SetIndexBuffer(0,MADiffBuffer,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[])
  {
//---
   
   
   double ma_fast[];
   double ma_slow[];
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(MADiffBuffer,true);
   ArraySetAsSeries(ma_fast,true);
   ArraySetAsSeries(ma_slow,true);
   
   int bars = rates_total -1;
   if (prev_calculated>0) bars = rates_total - prev_calculated;
   
//   
//   double l_close[];
//   
//   ArrayCopy(l_close,close,0,0,WHOLE_ARRAY);
   
   
   int h_maFast = iMA(_Symbol,_Period,MAFast,0,MODE_SMA,PRICE_CLOSE );
   int h_maSlow = iMA(_Symbol,_Period,MASlow,0,MODE_SMA,PRICE_CLOSE);
   
   CopyBuffer(h_maFast,0,0,bars+1,ma_fast);
   CopyBuffer(h_maSlow,0,0,bars+1,ma_slow);
   
  
   for (int i = bars; i >= 0; i--)
   {
      
      MADiffBuffer[i] = ma_slow[i] >= ma_fast[i] ? ma_slow[i] - ma_fast[i]: ma_fast[i] -ma_slow[i];
      
   }
   
  
   
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

You are making a gross mistake - you CREATE AN INDICATOR HANDLE AT EVERY TICK !!! This is a gross mistake! According to MQL5 style, HANDLE INDICATOR SHOULD BE CREATED ONLY ONCE and done in OnInit.

See the documentation for an example: iMA

I also recommend creating an indicator template using the 'MQL5 Wizard'.

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
iMA - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
@Vladimir Karputov  you are right. Moved it to the OnInit event and it worked. thanks. 
 
dhermanus # :
@Vladimir Karputov   you are right. Moved it to the OnInit event and it worked. thanks. 

Please attach your code (using the button Attach file) and a screenshot of the indicator's operation - I'm interested to see what happened.

 

Here it is. Just moved the handle declaration to the OnInit section. 

Files:
Reason: