My Indicator Drawing is Flatten on Certain Time Frame

 

Hi folks,

I need some help with my indicator, because the indicator drawing is flatten in weekly and monthly chart as shown below :

Flatten indicator

This is typically how it looks like in time frame H1 :

Normal indicator


And here is my code :

#include <MovingAverages.mqh>

#property indicator_separate_window 
#property indicator_buffers 5 
#property indicator_plots   2 

#property indicator_label1  "iATR" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1

#property indicator_label2  "TEMA-ATR" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrChocolate 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1

enum Creation 
  { 
   Call_iATR,                                              // Use iATR 
   Call_IndicatorCreate                                    // UseIndicatorCreate 
  }; 

input int       Inp_ATR_Period   = 7,                      // Period of ATR
                Inp_Period_TEMA  = 10;                     // Period of TEMA
//input Creation  Inp_CreationType = Call_IndicatorCreate;   // Type of the function

input bool      ShowSignalData   = false;                  // Show in data window

int    Han_ATR,
       bars_calculated = 0;

double iATRBuffer[], TemaBuffer[], Ema[], EmaOfEma[], EmaOfEmaOfEma[];

int OnInit() 
  {
   string IndicatorShortName = " ";
   
   SetIndexBuffer(0,iATRBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,TemaBuffer,INDICATOR_DATA); 
   SetIndexBuffer(2,Ema,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,EmaOfEma,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,EmaOfEmaOfEma,INDICATOR_CALCULATIONS);
   
   Han_ATR = iATR(_Symbol,PERIOD_CURRENT,Inp_ATR_Period); 
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,0);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,0);
 
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,ShowSignalData);
   PlotIndexSetInteger(1,PLOT_SHOW_DATA,ShowSignalData);
   
   if(ShowSignalData)
      {
         PlotIndexSetString(0,PLOT_LABEL,"ATR "+string(Inp_ATR_Period));
         PlotIndexSetString(1,PLOT_LABEL,"ATR-TEMA "+string(Inp_Period_TEMA));
         
         IndicatorShortName = "TEMA-ATR "+string(Inp_ATR_Period)+" "+string(Inp_Period_TEMA);
      }
   
   IndicatorSetString(INDICATOR_SHORTNAME,IndicatorShortName);
     
   if(Han_ATR==INVALID_HANDLE) 
     { 
      PrintFormat("Failed to create handle of the iATR indicator for the symbol %s/%s, error code %d", 
                  _Symbol, 
                  EnumToString(PERIOD_CURRENT), 
                  GetLastError()); 
      return(INIT_FAILED); 
     } 
   
   return(INIT_SUCCEEDED); 
  } 

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[]) 
  { 
//--- number of values copied from the iATR indicator 
   int values_to_copy; 
//--- determine the number of values calculated in the indicator 
   int calculated=BarsCalculated(Han_ATR); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- if it is the first start of calculation of the indicator or if the number of values in the iATR indicator changed 
//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- if the iATRBuffer array is greater than the number of values in the iATR indicator for symbol/period, then we don't copy everything  
      //--- otherwise, we copy less than the size of indicator buffers 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate() 
      //--- for calculation not more than one bar is added 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- fill the iATRBuffer array with values of the Average True Range indicator 
//--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation 
   if(!FillArrayFromBuffer(iATRBuffer,Han_ATR,values_to_copy)) return(0); 


      if(rates_total<3*Inp_Period_TEMA-3) return(0);
   //---
      int start;
      if(prev_calculated==0)
         start=0;
      else
         start=prev_calculated-1;
   //--- calculate EMA
      ExponentialMAOnBuffer(rates_total,prev_calculated,0,Inp_Period_TEMA,iATRBuffer,Ema);
   //--- calculate EMA on EMA array
      ExponentialMAOnBuffer(rates_total,prev_calculated,Inp_Period_TEMA-1,Inp_Period_TEMA,Ema,EmaOfEma);
   //--- calculate EMA on EMA array on EMA array
      ExponentialMAOnBuffer(rates_total,prev_calculated,2*Inp_Period_TEMA-2,Inp_Period_TEMA,EmaOfEma,EmaOfEmaOfEma);
   //--- calculate TEMA
      for(int i=start; i<rates_total && !IsStopped(); i++)
         TemaBuffer[i]=3*Ema[i]-3*EmaOfEma[i]+EmaOfEmaOfEma[i];


//--- memorize the number of values in the Average True Range indicator 
   bars_calculated=calculated; 
//--- return the prev_calculated value for the next call 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| Filling indicator buffers from the iATR indicator                | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // indicator buffer for ATR values 
                         int ind_handle,    // handle of the iATR indicator 
                         int amount         // number of copied values 
                         ) 
  { 
//--- reset error code 
   ResetLastError(); 
//--- fill a part of the iATRBuffer array with values from the indicator buffer that has 0 index 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- if the copying fails, tell the error code 
      PrintFormat("Failed to copy data from the iATR indicator, error code %d",GetLastError()); 
      //--- quit with zero result - it means that the indicator is considered as not calculated 
      return(false); 
     }
   return(true); 
  } 

void OnDeinit(const int reason) 
  { 
   if(Han_ATR!=INVALID_HANDLE) 
      IndicatorRelease(Han_ATR);
  }

I have completely no clue what is wrong with my code and i did not see any error messages after compiling. I have arround 600 bars in my monthly chart and 2000 bars in my weekly chart, so i think the data is more than enough to plot the indicator as it only uses 10 periods of the chart.

Please help folks, thank you.

Files:
ATR_TEMA.mq5  14 kb
 

Please initialize your buffers.

PlotIndexSetDouble(index, PLOT_EMPTY_VALUE, 0);
 
Please don't create topics randomly in any section. It has been moved to the section: Technical Indicators
MQL5 forum: Technical Indicators
MQL5 forum: Technical Indicators
  • www.mql5.com
Questions about the development of technical indicators in the MQL5 language
 
Yashar Seyyedin #:

Please initialize your buffers.

Thank you for your suggestion, I have initialized the buffers like you said, but the indicator still flatten.

int OnInit() 
  {
   string IndicatorShortName = " ";
   
   SetIndexBuffer(0,iATRBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,TemaBuffer,INDICATOR_DATA); 
   SetIndexBuffer(2,Ema,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,EmaOfEma,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,EmaOfEmaOfEma,INDICATOR_CALCULATIONS);
   
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
   
   Han_ATR = iATR(_Symbol,PERIOD_CURRENT,Inp_ATR_Period); 
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,0);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,0);
 
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,ShowSignalData);
   PlotIndexSetInteger(1,PLOT_SHOW_DATA,ShowSignalData);
   
   if(ShowSignalData)
      {
         PlotIndexSetString(0,PLOT_LABEL,"ATR "+string(Inp_ATR_Period));
         PlotIndexSetString(1,PLOT_LABEL,"ATR-TEMA "+string(Inp_Period_TEMA));
         
         IndicatorShortName = "TEMA-ATR "+string(Inp_ATR_Period)+" "+string(Inp_Period_TEMA);
      }
   
   IndicatorSetString(INDICATOR_SHORTNAME,IndicatorShortName);
     
   if(Han_ATR==INVALID_HANDLE) 
     { 
      PrintFormat("Failed to create handle of the iATR indicator for the symbol %s/%s, error code %d", 
                  _Symbol, 
                  EnumToString(PERIOD_CURRENT), 
                  GetLastError()); 
      return(INIT_FAILED); 
     } 
   
   return(INIT_SUCCEEDED); 
  } 

Any other suggestions?

 
Fernando Carreiro #:
Please don't create topics randomly in any section. It has been moved to the section: Technical Indicators

Thanks for the suggestion, this is my first time posting a question in this forum. I'll pay attention to it next time

 
Dedy Syahputralumbantobing #:

Thank you for your suggestion, I have initialized the buffers like you said, but the indicator still flatten.

Any other suggestions?

You have five buffers. Initialize all of them. I have not tested your code. This could help.
 
Yashar Seyyedin #:
You have five buffers. Initialize all of them. I have not tested your code. This could help

I have tried it, but the result remains the same Here is my new code according to your suggestion:

int OnInit() 
  {
   string IndicatorShortName = " ";
   
   SetIndexBuffer(0,iATRBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,TemaBuffer,INDICATOR_DATA); 
   SetIndexBuffer(2,Ema,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,EmaOfEma,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,EmaOfEmaOfEma,INDICATOR_CALCULATIONS);
   
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0.0);
   
   Han_ATR = iATR(_Symbol,PERIOD_CURRENT,Inp_ATR_Period); 

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,0);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,0);
   
 
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,ShowSignalData);
   PlotIndexSetInteger(1,PLOT_SHOW_DATA,ShowSignalData);
   
   if(ShowSignalData)
      {
         PlotIndexSetString(0,PLOT_LABEL,"ATR "+string(Inp_ATR_Period));
         PlotIndexSetString(1,PLOT_LABEL,"ATR-TEMA "+string(Inp_Period_TEMA));
         
         IndicatorShortName = "TEMA-ATR "+string(Inp_ATR_Period)+" "+string(Inp_Period_TEMA);
      }
   
   IndicatorSetString(INDICATOR_SHORTNAME,IndicatorShortName);
     
   if(Han_ATR==INVALID_HANDLE) 
     { 
      PrintFormat("Failed to create handle of the iATR indicator for the symbol %s/%s, error code %d", 
                  _Symbol, 
                  EnumToString(PERIOD_CURRENT), 
                  GetLastError()); 
      return(INIT_FAILED); 
     } 
   
   return(INIT_SUCCEEDED); 
  } 
And here is the result, there is something weird with the data: Updated

Any suggestion ?

 
  1.    PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
       PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
       PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);
       PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);
       PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0.0);
    

    That is not initializing buffers. It is just setting what value will be used for do not show.

  2. 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      iBar  = rates_total - MathMax(FastLength, prev_calculated);
       while(iBar > 0){  --iBar;
          iATRBuffer[iBar]   = TemaBuffer[iBar] = Ema[iBar] = EmaOfEma[iBar] = EmaOfEmaOfEma[iBar] = EMPTY_VALUE; // Initialize buffers
    
          // Compute buffers
       }  // iBar
       //----
       #define  REDRAW_BAR_LAST   (iBar == 0)             // Redraw bar last?
       return rates_total-iBar-REDRAW_BAR_LAST;
    }  // OnCalculate
    
    If you do not set all buffers per candle, initialize each candle.
 
William Roeder #:
  1. That is not initializing buffers. It is just setting what value will be used for do not show.

  2. If you do not set all buffers per candle, initialize each candle.

Hi, thank you for your reply. I finally figured out the problem based on your reply specifically this line :

int      iBar  = rates_total - MathMax(FastLength, prev_calculated);

So, I changed this line in my code : 

ExponentialMAOnBuffer(rates_total,prev_calculated,0,Inp_Period_TEMA,iATRBuffer,Ema);

To this :

ExponentialMAOnBuffer(rates_total,prev_calculated,Inp_Period_TEMA+1,Inp_Period_TEMA,iATRBuffer,Ema);

That solved the problem for me, and this case is officially closed. Thank you

Reason: