1 BBands + 3 TEMA + Candle color, not working

 

Hello, I am trying to learn MQL5, so i could follow some examples from the documentation. Now I am trying to create an indicator with a little bit of everything.

Well when I try putting together everything then something doesn't work, here I have 7 plots and the candle color is not working. So the indicator #7 should match the buffer index 6, right? I wanted to have only 8 buffers, since I need 1 buffer for each plot, plus 1 buffer for a RSI that I do not want to be plotted.


#1 So, why the candles aren't being painted?

#2 Why the compiler keeps saying I need more buffers? It is asking me to set buffers to 11, why?

#3 What is making everything derails when I try to assemble it all together in one indicator?

#4 what makes those crazy drawings? I attached an image showing how it's like with and without it.

Thank you, any help is appreciated.


#property indicator_chart_window
#property indicator_buffers 11
#property indicator_plots   7

#property indicator_label1  "Upper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrMediumSeaGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "Lower"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrMediumSeaGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "Middle"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrMediumSeaGreen
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

//--- plot TEMA1
#property indicator_label4  "TEMA1"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrMagenta
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot TEMA2
#property indicator_label5  "TEMA2"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrOrangeRed
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot TEMA3
#property indicator_label6  "TEMA3"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrAqua
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1

#property indicator_label7 "Open;High;Low;Close"
#property indicator_type7 DRAW_COLOR_CANDLES 
#property indicator_width7 3

//--- bollinger
double         buffer_boll_middle[], buffer_boll_upper[], buffer_boll_lower[];
int            handle_bands;

//--- indicator buffers
double         buffer_tema1[], buffer_tema2[], buffer_tema3[];
int            handle_tema1, handle_tema2, handle_tema3;

//--- buffer candle
double         buffer_color_line[];

//--- buffer rsi
double         buffer_RSI[];
int            handle_rsi;

//--- input parameters
input int      Periods1=15;
input int      Periods2=50;
input int      Periods3=150;


int OnInit()
  {
  
//--- bollinger
   SetIndexBuffer(0,buffer_boll_upper,INDICATOR_DATA);
   SetIndexBuffer(1,buffer_boll_lower,INDICATOR_DATA);
   SetIndexBuffer(2,buffer_boll_middle,INDICATOR_DATA); 
     
//--- temas
   SetIndexBuffer(3,buffer_tema1,INDICATOR_DATA);
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,Periods1);

   SetIndexBuffer(4,buffer_tema2,INDICATOR_DATA);
   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,Periods2);

   SetIndexBuffer(5,buffer_tema3,INDICATOR_DATA);
   PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,Periods3);
   
//--- candle colors
   SetIndexBuffer(6,buffer_color_line,INDICATOR_COLOR_INDEX);   
   PlotIndexSetInteger(6,PLOT_COLOR_INDEXES,10);
   PlotIndexSetInteger(6,PLOT_LINE_COLOR,0,clrWhite);
   PlotIndexSetInteger(6,PLOT_LINE_COLOR,1,clrForestGreen);
   PlotIndexSetInteger(6,PLOT_LINE_COLOR,2,clrCrimson);

//--- a RSI that should not be plotted
   SetIndexBuffer(7,buffer_RSI,INDICATOR_DATA);
 
//--- setting the handles
   handle_bands = iBands(_Symbol,_Period,20,0,2.0,PRICE_CLOSE);
   handle_tema1 = iTEMA(_Symbol, _Period, Periods1, 0, PRICE_CLOSE);
   handle_tema2 = iTEMA(_Symbol, _Period, Periods2, 0, PRICE_CLOSE);
   handle_tema3 = iTEMA(_Symbol, _Period, Periods3, 0, PRICE_CLOSE);
   handle_rsi   = iRSI(_Symbol,_Period, 5, PRICE_CLOSE);

//--- as series
   ArraySetAsSeries(buffer_boll_upper, true);
   ArraySetAsSeries(buffer_boll_lower, true);
   ArraySetAsSeries(buffer_boll_middle, true);   

   ArraySetAsSeries(buffer_tema1, true);
   ArraySetAsSeries(buffer_tema2, true);
   ArraySetAsSeries(buffer_tema3, true);

   ArraySetAsSeries(buffer_color_line, true);
   
   ArraySetAsSeries(buffer_RSI, true);

   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[])
{
   UpdateBuffer(buffer_boll_upper, handle_bands, rates_total, prev_calculated);
   UpdateBuffer(buffer_boll_lower, handle_bands, rates_total, prev_calculated);
   UpdateBuffer(buffer_boll_middle, handle_bands, rates_total, prev_calculated);

   UpdateBuffer(buffer_tema1, handle_tema1, rates_total, prev_calculated);
   UpdateBuffer(buffer_tema2, handle_tema2, rates_total, prev_calculated);
   UpdateBuffer(buffer_tema3, handle_tema3, rates_total, prev_calculated);

   UpdateBuffer(buffer_RSI, handle_rsi, rates_total, prev_calculated);

   for(int i=prev_calculated; i<rates_total; i++) {
      if(close[i] > buffer_tema2[i]) {
         buffer_color_line[i] = 1;
      }
      else {
         buffer_color_line[i] = 2;
      }
   }

   Comment("buffer_boll_middle: ", buffer_boll_middle[0], 
   "\nbuffer_boll_middle: ", buffer_tema2[0], 
   "\nbuffer_RSI: ", buffer_RSI[0],
   "\nbuffer_color_line: ", buffer_color_line[0]
   );

   return(rates_total);
}

void UpdateBuffer(double &_buffer[], int _handle, int _rates_total, int prev_calculated) {
   if(BarsCalculated(_handle) < _rates_total) return;

   int to_copy;
   if(prev_calculated > _rates_total || prev_calculated <= 0) {
      to_copy = _rates_total;
   }
   else {
      to_copy = _rates_total - prev_calculated;
      to_copy++;
   }

   if(CopyBuffer(_handle, 0, 0, to_copy, _buffer)<=0) {
      Print("error CopyBuffer:", _handle);
   }
}   
 
void OnDeinit(const int reason) {
   IndicatorRelease(handle_rsi);
   IndicatorRelease(handle_bands);
   IndicatorRelease(handle_tema1);
   IndicatorRelease(handle_tema2);
   IndicatorRelease(handle_tema3);
   Comment("");
 }
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Drawing Styles
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Drawing Styles
  • www.mql5.com
When creating a custom indicator, you can specify one of 18 types of graphical plotting (as displayed in the main chart window or a chart subwindow), whose values are specified in the ENUM_DRAW_TYPE enumeration. Depending on the drawing style, you may need one to four value buffers (marked as INDICATOR_DATA). If a style admits dynamic...
 
#property indicator_chart_window
#property indicator_buffers 12 //#property indicator_buffers 11
#property indicator_plots   7

#property indicator_label1  "Upper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrMediumSeaGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "Lower"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrMediumSeaGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "Middle"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrMediumSeaGreen
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

//--- plot TEMA1
#property indicator_label4  "TEMA1"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrMagenta
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot TEMA2
#property indicator_label5  "TEMA2"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrOrangeRed
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot TEMA3
#property indicator_label6  "TEMA3"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrAqua
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1

#property indicator_label7 "Open;High;Low;Close"
#property indicator_type7 DRAW_COLOR_CANDLES
#property indicator_color7  clrWhite, clrForestGreen, clrCrimson  //<---Add
#property indicator_style7  STYLE_SOLID                           //<---Add
#property indicator_width7 3
//--- bollinger
double         buffer_boll_middle[], buffer_boll_upper[], buffer_boll_lower[];
int            handle_bands;

//--- indicator buffers
double         buffer_tema1[], buffer_tema2[], buffer_tema3[];
int            handle_tema1, handle_tema2, handle_tema3;

//--- buffer candle
double         buffer_color_line[];
double         buffer_candle_open[];                   //<-----Add these buffers
double         buffer_candle_high[];
double         buffer_candle_low[];
double         buffer_candle_close[];
//--- buffer rsi
double         buffer_RSI[];
int            handle_rsi;

//--- input parameters
input int      Periods1 = 15;
input int      Periods2 = 50;
input int      Periods3 = 150;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
{

//--- bollinger
   SetIndexBuffer(0, buffer_boll_upper, INDICATOR_DATA);
   SetIndexBuffer(1, buffer_boll_lower, INDICATOR_DATA);
   SetIndexBuffer(2, buffer_boll_middle, INDICATOR_DATA);

//--- temas
   SetIndexBuffer(3, buffer_tema1, INDICATOR_DATA);
   PlotIndexSetInteger(3, PLOT_DRAW_BEGIN, Periods1);

   SetIndexBuffer(4, buffer_tema2, INDICATOR_DATA);
   PlotIndexSetInteger(4, PLOT_DRAW_BEGIN, Periods2);

   SetIndexBuffer(5, buffer_tema3, INDICATOR_DATA);
   PlotIndexSetInteger(5, PLOT_DRAW_BEGIN, Periods3);

//--- candle colors
   SetIndexBuffer(6, buffer_candle_open, INDICATOR_DATA);
   SetIndexBuffer(7, buffer_candle_high, INDICATOR_DATA);
   SetIndexBuffer(8, buffer_candle_low,  INDICATOR_DATA);
   SetIndexBuffer(9, buffer_candle_close, INDICATOR_DATA);
   SetIndexBuffer(10, buffer_color_line, INDICATOR_COLOR_INDEX);
   
   /*SetIndexBuffer(6, buffer_color_line, INDICATOR_COLOR_INDEX);
   PlotIndexSetInteger(6, PLOT_COLOR_INDEXES, 10);
   PlotIndexSetInteger(6, PLOT_LINE_COLOR, 0, clrWhite);
   PlotIndexSetInteger(6, PLOT_LINE_COLOR, 1, clrForestGreen);
   PlotIndexSetInteger(6, PLOT_LINE_COLOR, 2, clrCrimson);*/

//--- a RSI that should not be plotted
   SetIndexBuffer(11, buffer_RSI, INDICATOR_CALCULATIONS); //SetIndexBuffer(7, buffer_RSI, INDICATOR_DATA);

//--- setting the handles
   handle_bands = iBands(_Symbol, _Period, 20, 0, 2.0, PRICE_CLOSE);
   handle_tema1 = iTEMA(_Symbol, _Period, Periods1, 0, PRICE_CLOSE);
   handle_tema2 = iTEMA(_Symbol, _Period, Periods2, 0, PRICE_CLOSE);
   handle_tema3 = iTEMA(_Symbol, _Period, Periods3, 0, PRICE_CLOSE);
   handle_rsi   = iRSI(_Symbol, _Period, 5, PRICE_CLOSE);

//--- as series
   /*ArraySetAsSeries(buffer_boll_upper, true);  //<--- No need
   ArraySetAsSeries(buffer_boll_lower, true);
   ArraySetAsSeries(buffer_boll_middle, true);

   ArraySetAsSeries(buffer_tema1, true);
   ArraySetAsSeries(buffer_tema2, true);
   ArraySetAsSeries(buffer_tema3, true);

   ArraySetAsSeries(buffer_color_line, true);

   ArraySetAsSeries(buffer_RSI, true);*/

   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[])
{
   /*UpdateBuffer(buffer_boll_upper, handle_bands, rates_total, prev_calculated);
   UpdateBuffer(buffer_boll_lower, handle_bands, rates_total, prev_calculated);
   UpdateBuffer(buffer_boll_middle, handle_bands, rates_total, prev_calculated);

   UpdateBuffer(buffer_tema1, handle_tema1, rates_total, prev_calculated);
   UpdateBuffer(buffer_tema2, handle_tema2, rates_total, prev_calculated);
   UpdateBuffer(buffer_tema3, handle_tema3, rates_total, prev_calculated);

   UpdateBuffer(buffer_RSI, handle_rsi, rates_total, prev_calculated);*/
   
   UpdateBuffer(buffer_boll_upper, handle_bands,1 , rates_total, prev_calculated);      //<---Add Indexes
   UpdateBuffer(buffer_boll_lower, handle_bands, 2, rates_total, prev_calculated);
   UpdateBuffer(buffer_boll_middle, handle_bands, 0, rates_total, prev_calculated);
   
   UpdateBuffer(buffer_tema1, handle_tema1, 0, rates_total, prev_calculated);
   UpdateBuffer(buffer_tema2, handle_tema2, 0, rates_total, prev_calculated);
   UpdateBuffer(buffer_tema3, handle_tema3, 0, rates_total, prev_calculated);
   
   UpdateBuffer(buffer_RSI, handle_rsi, 0, rates_total, prev_calculated);

   for(int i = prev_calculated; i < rates_total; i++)
   {
        buffer_candle_open[i]  = open[i];       //<----Add these lines
        buffer_candle_high[i]  = high[i];
        buffer_candle_low[i]   = low[i];
        buffer_candle_close[i] = close[i];
        
      if(close[i] > buffer_tema2[i])
      {
         buffer_color_line[i] = 1;
      }
      else
      {
         buffer_color_line[i] = 2;
      }
   }

   Comment("buffer_boll_middle: ", buffer_boll_middle[0],
           "\nbuffer_boll_middle: ", buffer_tema2[0],
           "\nbuffer_RSI: ", buffer_RSI[0],
           "\nbuffer_color_line: ", buffer_color_line[0]
          );

   return(rates_total);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//void UpdateBuffer(double &_buffer[], int _handle, int _rates_total, int prev_calculated)
void UpdateBuffer(double &_buffer[], int _handle, int index, int _rates_total, int prev_calculated) //<--- Add Index
{
   if(BarsCalculated(_handle) < _rates_total) return;

   int to_copy;
   if(prev_calculated > _rates_total || prev_calculated <= 0)
   {
      to_copy = _rates_total;
   }
   else
   {
      to_copy = _rates_total - prev_calculated;
      to_copy++;
   }

   if(CopyBuffer(_handle, index, 0, to_copy, _buffer) <= 0)  //if(CopyBuffer(_handle, 0, 0, to_copy, _buffer) <= 0)
   {
      Print("error CopyBuffer:", _handle);
   }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   IndicatorRelease(handle_rsi);
   IndicatorRelease(handle_bands);
   IndicatorRelease(handle_tema1);
   IndicatorRelease(handle_tema2);
   IndicatorRelease(handle_tema3);
   Comment("");
}
//+------------------------------------------------------------------+
 
Naguisa Unada:

Oh, I had many silly mistakes!

Works perfectly, thanks for the help!

Reason: