Problem with a simple indicator printing dots on candles

 

Hello guys,

while learning MQL5 I wanted to code an indicator which prints a dot in the middle of the candle body.

A green dot if it is a bullish candle and a red dot if it is a bearish candle.

Can somebody please tell me what's wrong with my code?

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2 
#property indicator_type1   DRAW_ARROW 
#property indicator_color1  clrGreen 
#property indicator_width1  1
#property indicator_type2   DRAW_ARROW 
#property indicator_color2  clrRed 
#property indicator_width2  1
double bullCandleBuffer[], bearCandleBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,bullCandleBuffer,INDICATOR_DATA); 
   PlotIndexSetInteger(0,PLOT_ARROW,159); 
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
   
   SetIndexBuffer(1,bearCandleBuffer,INDICATOR_DATA); 
   PlotIndexSetInteger(1,PLOT_ARROW,159); 
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0); 
//---
   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[])
  {
//---
   int start=1; 
   if(prev_calculated>0) start=prev_calculated-1; 
   for(int i=1;i<rates_total;i++) {
      if (iClose(_Symbol,PERIOD_CURRENT,i)>iOpen(_Symbol,PERIOD_CURRENT,i)) {
         bullCandleBuffer[i]=MathAbs(iClose(_Symbol,PERIOD_CURRENT,i)+iOpen(_Symbol,PERIOD_CURRENT,i))/2;
      }
      else if (iClose(_Symbol,PERIOD_CURRENT,i)<iOpen(_Symbol,PERIOD_CURRENT,i)) {
         bearCandleBuffer[i]=MathAbs(iClose(_Symbol,PERIOD_CURRENT,i)+iOpen(_Symbol,PERIOD_CURRENT,i))/2;
      }
   }
//--- return value of prev_calculated for next call
   return(rates_total);
}
 
Marbo :

Hello guys,

while learning MQL5 I wanted to code an indicator which prints a dot in the middle of the candle body.

A green dot if it is a bullish candle and a red dot if it is a bearish candle.

Can somebody please tell me what's wrong with my code?

What for? Why do you use iClose and iOpen functions ??? OnCalculate supplies all arrays at once:

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[])
 

Code:

//+------------------------------------------------------------------+
//|                                                Bull Bear Dot.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot Candle
#property indicator_label1  "Candle"
#property indicator_type1   DRAW_COLOR_ARROW
#property indicator_color1  clrDeepSkyBlue,clrTomato
#property indicator_style1  STYLE_SOLID
#property indicator_width1  5
//--- input parameters
input int      Input1=9;
//--- indicator buffers
double         CandleBuffer[];
double         CandleColors[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,CandleBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,CandleColors,INDICATOR_COLOR_INDEX);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
//---
   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[])
  {
//---
   int limit=prev_calculated-1;
   if(prev_calculated==0)
      limit=0;
   for(int i=limit; i<rates_total; i++)
     {
      CandleBuffer[i]=(open[i]+close[i])/2.0;
      if(open[i]<close[i])
         CandleColors[i]=0.0;
      else
         CandleColors[i]=1.0;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Result:

Bull Bear Dot

Files:
 
Vladimir Karputov:

Code:

Result:


Thank you very much! This is very helpful for me. I also like the idea of changing colors and using only one buffer.