Custom Indicator Values are not being plotted on chart

 

Hello everyone,

I hope this message finds you well. I'm new to programming and MQL5, and I've been trying to create a smoothed Heiken Ashi indicator. While I have successfully obtained all the required values, I'm encountering a challenge when it comes to plotting the indicator on the chart. Specifically, only the first candle is being plotted, and I'm unsure why this is happening. See code & SS below.


Chart USDJPY, H1, 2023.07.05 07:00 UTC, Raw Trading Ltd, MetaTrader 5, Demo
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  DodgerBlue,Red
#property indicator_label1  "Heiken Ashi Open;Heiken Ashi High;Heiken Ashi Low;Heiken Ashi Close"
//--- indicator buffers

//+------------------------------------------------------------------+
//| Custom indicator Inputs                                          |
//+------------------------------------------------------------------+
//Moving Average
input int                    Inp_MA_Period   = 20;         // Moving Average Period 
input ENUM_MA_METHOD         Inp_MA_Method   = MODE_EMA;   //Moving Average Method 

double Buffer_Main_Open[];
double Buffer_Main_High[];
double Buffer_Main_Low[];
double Buffer_Main_Close[];
double ExtColorBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator Handles                                         |
//+------------------------------------------------------------------+

//Smoothed HA Moving Average Open Handle
int Smoothed_HA_MA_Open_Handle;

//Previous Smoothed HA Moving Average Open Handle
int Previous_Smoothed_HA_MA_Open_Handle;

//Smoothed HA Moving Average High Handle
int Smoothed_HA_MA_High_Handle;

//Smoothed HA Moving Average Low Handle
int Smoothed_HA_MA_Low_Handle;

//Smoothed HA Moving Average Close Handle
int Smoothed_HA_MA_Close_Handle;

//Previous Smoothed HA Moving Average Close Handle
int Previous_Smoothed_HA_MA_Close_Handle;

//Smoothed HA Moving Average Open Buffer
double Smoothed_HA_MA_Open_Buffer[];

//Previous Smoothed HA Moving Average Open Buffer
double Prev_Smoothed_HA_MA_Open_Buffer[];

//Smoothed HA Moving Average High Buffer
double Smoothed_HA_MA_High_Buffer[];

//Smoothed HA Moving Average Low Buffer
double Smoothed_HA_MA_Low_Buffer[];

//Smoothed HA Moving Average Close Buffer
double Smoothed_HA_MA_Close_Buffer[];

//Previous Smoothed HA Moving Average Close Buffer
double Prev_Smoothed_HA_MA_Close_Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Buffer_Main_Open,INDICATOR_DATA);
   SetIndexBuffer(1,Buffer_Main_High,INDICATOR_DATA);
   SetIndexBuffer(2,Buffer_Main_Low,INDICATOR_DATA);
   SetIndexBuffer(3,Buffer_Main_Close,INDICATOR_DATA);
   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   IndicatorSetString(INDICATOR_SHORTNAME,"Heiken Ashi");
//--- sets drawing line empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//--- initialization done

   ArraySetAsSeries(Buffer_Main_Open,true);
   ArraySetAsSeries(Buffer_Main_High,true);
   ArraySetAsSeries(Buffer_Main_Low,true);
   ArraySetAsSeries(Buffer_Main_Close,true);
   ArraySetAsSeries(ExtColorBuffer,true);

   Smoothed_HA_MA_Open_Handle = iMA(_Symbol,_Period,Inp_MA_Period,0,Inp_MA_Method,PRICE_OPEN);
   Previous_Smoothed_HA_MA_Open_Handle = iMA(_Symbol,_Period,Inp_MA_Period,0,Inp_MA_Method,PRICE_OPEN);
   
   Smoothed_HA_MA_High_Handle = iMA(_Symbol,_Period,Inp_MA_Period,0,Inp_MA_Method,PRICE_HIGH);
   Smoothed_HA_MA_Low_Handle = iMA(_Symbol,_Period,Inp_MA_Period,0,Inp_MA_Method,PRICE_LOW);
   
   Smoothed_HA_MA_Close_Handle = iMA(_Symbol,_Period,Inp_MA_Period,0,Inp_MA_Method,PRICE_CLOSE);
   Previous_Smoothed_HA_MA_Close_Handle = iMA(_Symbol,_Period,Inp_MA_Period,0,Inp_MA_Method,PRICE_CLOSE);
  
   ArraySetAsSeries(Smoothed_HA_MA_Open_Buffer,true);
   ArraySetAsSeries(Prev_Smoothed_HA_MA_Open_Buffer,true);
   
   ArraySetAsSeries(Smoothed_HA_MA_High_Buffer,true);
   ArraySetAsSeries(Smoothed_HA_MA_Low_Buffer,true);
   
   ArraySetAsSeries(Smoothed_HA_MA_Close_Buffer,true);
   ArraySetAsSeries(Prev_Smoothed_HA_MA_Close_Buffer,true);

   if (Smoothed_HA_MA_Open_Handle == INVALID_HANDLE || Smoothed_HA_MA_High_Handle == INVALID_HANDLE || Smoothed_HA_MA_Low_Handle == INVALID_HANDLE || Smoothed_HA_MA_Close_Handle == INVALID_HANDLE)
      {
         Print( "Failed to create indicator handles");
      }
      return (INIT_SUCCEEDED);
  }
      
void OnDeinit(const int reason) {
   IndicatorRelease (Smoothed_HA_MA_Open_Handle);
   IndicatorRelease (Previous_Smoothed_HA_MA_Open_Handle);
   
   IndicatorRelease (Smoothed_HA_MA_High_Handle);
   IndicatorRelease (Smoothed_HA_MA_Low_Handle);
   
   IndicatorRelease (Smoothed_HA_MA_Close_Handle);  
   IndicatorRelease (Previous_Smoothed_HA_MA_Close_Handle);  
}

//+------------------------------------------------------------------+
//| Heiken Ashi                                                      |
//+------------------------------------------------------------------+
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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
  // How many bars to calculate 
  int count = rates_total - prev_calculated; // no of bars - no of already calculated 
  if (prev_calculated > 0 ) count++; // force last calculated to be done again
  
  if (CopyBuffer(Smoothed_HA_MA_Open_Handle,0,0,count,Smoothed_HA_MA_Open_Buffer) <count) return(0);
   CopyBuffer(Previous_Smoothed_HA_MA_Open_Handle,0,1,count,Prev_Smoothed_HA_MA_Open_Buffer);// <count) return(0);
  
  if (CopyBuffer(Smoothed_HA_MA_High_Handle,0,0,count,Smoothed_HA_MA_High_Buffer) <count) return(0);
  if (CopyBuffer(Smoothed_HA_MA_Low_Handle,0,0,count,Smoothed_HA_MA_Low_Buffer) <count) return(0);
  
  if (CopyBuffer(Smoothed_HA_MA_Close_Handle,0,0,count,Smoothed_HA_MA_Close_Buffer) <count) return(0);
   CopyBuffer(Previous_Smoothed_HA_MA_Close_Handle,0,1,count,Prev_Smoothed_HA_MA_Close_Buffer);// <count) return(0);
 
  for(int i = count - 1; i >= 0; i--){
      
      double Open_MA = Smoothed_HA_MA_Open_Buffer[i];
      double Open_Prev_MA = Prev_Smoothed_HA_MA_Open_Buffer[i];
      double High_MA = Smoothed_HA_MA_High_Buffer[i];
      double Low_MA = Smoothed_HA_MA_Low_Buffer[i];
      double Close_MA = Smoothed_HA_MA_Close_Buffer[i];
      double Close_Prev_MA = Prev_Smoothed_HA_MA_Close_Buffer[i];

      Print("................................................");
      Print("Open MA = ",NormalizeDouble(Open_MA,_Digits));
      Print("Previous Open MA = ",NormalizeDouble(Open_Prev_MA,_Digits));
      Print("High MA = ",NormalizeDouble(High_MA,_Digits));
      Print("Low MA = ",NormalizeDouble(Low_MA,_Digits));
      Print("Close MA = ",NormalizeDouble(Close_MA,_Digits));
      Print("Previous Close MA = ",NormalizeDouble(Close_Prev_MA,_Digits)) ;
      
      double haOpen = (Open_Prev_MA+Close_Prev_MA)/2;
      double haClose = (Open_MA+High_MA+Low_MA+Close_MA)/4;
      double haHigh = MathMax(High_MA,MathMax(haOpen,haClose));
      double haLow = MathMin(Low_MA,MathMin(haOpen,haClose));
      
      Print("-------------------------------------------------");
      Print("HA Open: ",NormalizeDouble(haOpen,_Digits));
      Print("HA High: ",NormalizeDouble(haHigh,_Digits));
      Print("HA Low: ",NormalizeDouble(haLow,_Digits));
      Print("HA Close: ",NormalizeDouble(haClose,_Digits));
      
      Buffer_Main_Open[i] = haOpen;
      Buffer_Main_High[i] = haHigh;
      Buffer_Main_Low[i] = haLow;
      Buffer_Main_Close[i] = haClose;
 
      Print("-------------------------------------------------");
      Print("Buffer_Main_Open: ",NormalizeDouble(Buffer_Main_Open[i],_Digits));
      Print("Buffer_Main_High: ",NormalizeDouble(Buffer_Main_High[i],_Digits));
      Print("Buffer_Main_Low: ",NormalizeDouble(Buffer_Main_Low[i],_Digits));
      Print("Buffer_Main_Close: ",NormalizeDouble(Buffer_Main_Close[i],_Digits));
      
      if (haOpen > haClose) {
      ExtColorBuffer[i] = 1.0;
       }
      else {
      ExtColorBuffer[i] = 0.0;
      }
  }
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+

 
Your topic has been moved to the section: Technical Indicators — In the future, please consider which section is most appropriate for your query.