Discussão do artigo "Explorando as possibilidades de criar gráficos de velas multicoloridas"

 

Novo artigo Explorando as possibilidades de criar gráficos de velas multicoloridas foi publicado:

Neste artigo, veremos as possibilidades de criação de indicadores de velas personalizados, e falaremos sobre suas vantagens e desvantagens.

Neste artigo, veremos as possibilidades de criação de gráficos de velas personalizados, e falaremos sobre suas vantagens e desvantagens. Adicionalmente, consideraremos quatro alternativas para esse tipo de indicadores:

1. Plotagem de velas da mesma cor sem definir sua direção.

2. Plotagem de velas multicoloridas, coloridas de acordo com certas regras.

3. Plotagem de velas com e sem preenchimento usando várias cores para preencher as velas e seus contornos.

4. Plotagem de velas multicoloridas com e sem preenchimento.


Assim, alcançamos o objetivo deste artigo, que era o de demonstrar como criar regras de coloração com base em certas condições de negociação escolhidas para criar um gráfico de velas personalizado. O resultado do trabalho feito é mostrado na figura abaixo:




Autor: Samuel Manoel De Souza

 
//+------------------------------------------------------------------+
//|                                                Color Candles.mq5 |
//|                           Copyright 2020, Samuel Manoel de Souza |
//|                          https://www.mql5.com/pt/users/samuelmnl |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Samuel Manoel de Souza"
#property link      "https://www.mql5.com/pt/users/samuelmnl"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 12
#property indicator_plots   3
//--- plot bull
#property indicator_label1  "bull"
#property indicator_type1   DRAW_CANDLES
#property indicator_color1  clrGreen,clrBlack,clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot bear
#property indicator_label2  "bear"
#property indicator_type2   DRAW_CANDLES
#property indicator_color2  clrRed,clrBlack,clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot range
#property indicator_label3  "wait"
#property indicator_type3   DRAW_CANDLES
#property indicator_color3  clrGold,clrBlack,clrGold
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- indicator buffers
double         bullBuffer1[];
double         bullBuffer2[];
double         bullBuffer3[];
double         bullBuffer4[];
double         bearBuffer1[];
double         bearBuffer2[];
double         bearBuffer3[];
double         bearBuffer4[];
double         rangeBuffer1[];
double         rangeBuffer2[];
double         rangeBuffer3[];
double         rangeBuffer4[];

input int inpShift = 0; //Indicator Shift
input int inpBlue = 10;//Blue Shift
input int inpRed = 20; //Red Shift

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(inpBlue <= 0)
     {
      Alert("Blue Shift must be greater then 0!");
      return INIT_PARAMETERS_INCORRECT;
     }
   if(inpRed <= inpBlue)
     {
      Alert("Red Shift must be greater then Blue Shift!");
      return INIT_PARAMETERS_INCORRECT;
     }
//--- indicator buffers mapping
   SetIndexBuffer(0, bullBuffer1, INDICATOR_DATA);
   SetIndexBuffer(1, bullBuffer2, INDICATOR_DATA);
   SetIndexBuffer(2, bullBuffer3, INDICATOR_DATA);
   SetIndexBuffer(3, bullBuffer4, INDICATOR_DATA);
   SetIndexBuffer(4, bearBuffer1, INDICATOR_DATA);
   SetIndexBuffer(5, bearBuffer2, INDICATOR_DATA);
   SetIndexBuffer(6, bearBuffer3, INDICATOR_DATA);
   SetIndexBuffer(7, bearBuffer4, INDICATOR_DATA);
   SetIndexBuffer(8, rangeBuffer1, INDICATOR_DATA);
   SetIndexBuffer(9, rangeBuffer2, INDICATOR_DATA);
   SetIndexBuffer(10, rangeBuffer3, INDICATOR_DATA);
   SetIndexBuffer(11, rangeBuffer4, INDICATOR_DATA);
//---Setting which value make the bars invisible
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0);
//---Setting the shift values for each bar
   PlotIndexSetInteger(0, PLOT_SHIFT, inpShift);
   PlotIndexSetInteger(1, PLOT_SHIFT, inpShift);
   PlotIndexSetInteger(2, PLOT_SHIFT, inpShift);
//---Setting from where the bars must be drawed
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, inpRed - 1);
   PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, inpRed - 1);
   PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, inpRed - 1);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
//---If there is no sufficient bars, don't calculate
   if(rates_total < inpRed)
     {
      return rates_total;
     }
//---Defining from where we should start the calculation
   int limit;
   if(prev_calculated < inpRed)
      limit = inpRed;
   else
      limit = prev_calculated - 1;
//---Main calculation loop
   for(int i = limit; i < rates_total; i++)
     {
      int shift = BarIndex2shift(i);
      double open = iOpen(_Symbol, _Period, shift);
      double high = iHigh(_Symbol, _Period, shift);
      double low = iLow(_Symbol, _Period, shift);
      double close = iClose(_Symbol, _Period, shift);
      double red = price[i - (inpRed)]; //Getting the Red line value
      double blue = price[i - inpBlue]; //Getting the Blue line value
      double yellow = price[i];     //Getting the Yellow line value
      //There are three flags for this filter
      //0 = sell, 1= wait, 1 = buy
      // we can buy if 2 lines is above the red
      // we can sell if 2 lines is below the red
      int direction = 0;
      direction += (yellow > red) ? 1 : 0; // If Yellow is above Red, add 1
      red = blue;
      blue = yellow;
      direction += (blue > red) ? 1 : 0; // If Blue is above Red, add 1 .
      //For default bars isn't visible
      bullBuffer1[i] = 0;
      bullBuffer2[i] = 0;
      bullBuffer3[i] = 0;
      bullBuffer4[i] = 0;
      bearBuffer1[i] = 0;
      bearBuffer2[i] = 0;
      bearBuffer3[i] = 0;
      bearBuffer4[i] = 0;
      rangeBuffer1[i] = 0;
      rangeBuffer2[i] = 0;
      rangeBuffer3[i] = 0;
      rangeBuffer4[i] = 0;
      //We'll assign value to each bar according with the direction flag
      switch(direction)
        {
         case 0: // Is bear market
           {
            bearBuffer1[i] = open;
            bearBuffer2[i] = high;
            bearBuffer3[i] = low;
            bearBuffer4[i] = close;
            break;
           }
         case 1: // Is bull market
           {
            rangeBuffer1[i] = open;
            rangeBuffer2[i] = high;
            rangeBuffer3[i] = low;
            rangeBuffer4[i] = close;
            break;
           }
         case 2: // Is range market
           {
            bullBuffer1[i] = open;
            bullBuffer2[i] = high;
            bullBuffer3[i] = low;
            bullBuffer4[i] = close;
            break;
           }
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

int BarIndex2shift(int bar) {return Bars(_Symbol, _Period) - bar - 1;}

//+------------------------------------------------------------------+