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);
}
//+------------------------------------------------------------------+