I'm trying to draw an DRAW_COLOR_SECTION with sections missing or colorless.

 

Hello,

I'm trying to draw an DRAW_COLOR_SECTION with sections missing or colorless.

I tried this code but it's missed.


//+------------------------------------------------------------------+
//|                                        appDRAW_COLOR_SECTION.mq5 |
//|                                                   Pierre Rougier |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Pierre Rougier"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

#property description "This indicator is a demo of DRAW_COLOR_SECTION drawing style"
#property description "It plots color sections with specified number of bars"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot ColorSection
#property indicator_label1  "ColorSection"
#property indicator_type1   DRAW_COLOR_SECTION
//--- 8 colors
#property indicator_color1  clrRed,clrGold,clrMediumBlue,clrLime,clrMagenta,clrBrown,clrTan,clrMediumVioletRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//--- input parameters
input int      bars_in_section=25;        // Section width in bars
//--- additional variables
int            divider;
int            color_sections;
//--- plotting buffer
double         ColorSectionBuffer[];
//--- color buffer
double         ColorSectionColors[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ColorSectionBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ColorSectionColors,INDICATOR_COLOR_INDEX);
//--- set empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---- colors
   color_sections=8;
//--- check bars in section
   if(bars_in_section<=0)
     {
      PrintFormat("Invalid section width=%d",bars_in_section);
      return(INIT_PARAMETERS_INCORRECT);
     }
   else divider=color_sections*bars_in_section;
//---
   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[])
  {

//--- starting bar index
   int start=0;
//--- if not first call, set start to the previous bar
   if(prev_calculated>0) start=prev_calculated-1;
//--- calculations
   for(int i=start;i<rates_total;i++)
     {
      //--- if remainder=0, the end of the section
      if(i%bars_in_section==0)
        {
         //--- set High for the end of the section
         ColorSectionBuffer[i]=high[i];
         //--- the remainder = section_width*number_of_colors
         int rest=i%divider;
         // color index vary from 0 to number_of_colors-1
         int color_indext=rest/bars_in_section;
         if(color_indext==1)
           {
            ColorSectionColors[i]=NULL;
           }
         else
           {
            ColorSectionColors[i]=color_indext;
           }
        }
      //--- remainder<>0
      else
        {
         //--- skip bar (set empty value)
         ColorSectionBuffer[i]=0;
        }
     }
//--- return prev_calculated for the next call of the function
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Pierre Rougier:

Hello,

I'm trying to draw an DRAW_COLOR_SECTION with sections missing or colorless.

I tried this code but it's missed.


In this bit of code:

         if(color_indext==1)
           {
            ColorSectionColors[i]=NULL;
           }

you are basically saying:

"If the color index is 1 (clrGold), then make it NULL, which in this case becomes 0 (clrRed)."

You end up replacing one color for another color, which isn't what you want.

You could add clrBlack (or whatever your background color is) to your list of colors, then replace with that. This leaves artifacts. (See attached image.)

There is no "transparent color" that I know of, so I don't think you can do this with indexed colors.

Files:
 


One more time, thanks Anthony. ;-)

 
Anthony Garot:

In this bit of code:

you are basically saying:

"If the color index is 1 (clrGold), then make it NULL, which in this case becomes 0 (clrRed)."

You end up replacing one color for another color, which isn't what you want.

You could add clrBlack (or whatever your background color is) to your list of colors, then replace with that. This leaves artifacts. (See attached image.)

There is no "transparent color" that I know of, so I don't think you can do this with indexed colors.


You can use CLR_NONE or clrNONE

 
nicholishen:

You can use CLR_NONE or clrNONE

Ah yes!
 
nicholishen:

You can use CLR_NONE or clrNONE


Thanks nicholishen

 

OK CLR_NONE is great for DRAW_COLOR_SECTION, about the question was.

For DRAW_LINE I found something other.

I use  PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);


//+------------------------------------------------------------------+
//|                                            lines_in_sequence.mq5 |
//|                                                   Pierre Rougier |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Pierre Rougier"
#property link      "https://www.mql5.com/en/users/pierre8r"
#property version   "1.00"
#property indicator_chart_window


#property indicator_buffers 1
#property indicator_plots   1
//--- the line properties is defined using the compiler directives
#property indicator_label1  "Line"      // indicator label (in "Data Window")
#property indicator_type1   DRAW_LINE   // drawing style as a line
#property indicator_color1  clrMagenta      // line color
#property indicator_style1  STYLE_SOLID // line style
#property indicator_width1  3           // line width

//--- indicator buffer
double         LineBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- set index buffer
   SetIndexBuffer(0,LineBuffer,INDICATOR_DATA);
   PlotIndexSetDouble(0,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 iCtr=0;
//--- calculating indicator values
   for(int i=0;i<rates_total;i++)
     {
      iCtr++;
      if(iCtr<10)
        {
         LineBuffer[i]=close[i];
        }
      else
      if(iCtr<20)
        {
        }
      else
        {
         iCtr=0;
        }
     }

//--- return prev_calculated for the next call of the function
   return(rates_total);
  }
//+------------------------------------------------------------------+
Reason: