Is there a way to draw a dashed line on a MQL5 chart that contains two colors?
Is there a way to draw a dashed line on a MQL5 chart that contains two colors?
Loading saved threads...
Johnny Sandaire · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Johnny Sandaire
Original post: https://stackoverflow.com/questions/79669387
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I have an indicator that draws dashed lines in red or blue as needed for support and resistance levels. However, I have a difficult time seeing the lines on the black background charts, so I would like to change the lines to a dashed red-white or dashed blue-white line instead. I accomplished this by creating two separate indicators. One draws the support and resistance levels utilizing a white solid line. Then, I loaded the white line indicator onto the chart first; followed by loading the dashed red and blue line indicator. The results were much better visually. Therefore, instead of using two separate indicators, I would like to have one indicator that is able to generate the dashed multicolored lines.
I tried to add two blocks of a for loop to overlay the lines, but that does not work. MQL5 prioritizes the last block that ran, and I can only see one set of lines instead. Is it possible for me to render these lines on the charts with a single indicator?
This is my current for loop that is drawing the red, blue, and white lines.
int OnInit()
{
ArrayResize(sr_levels, 6); // Resize to 6 levels
ArrayResize(aas, 6); // Resize to 6 levels
ArrayInitialize(sr_levels, 0);
ArrayInitialize(aas, true);
// Initialize buffers
SetIndexBuffer(0, SupportBuffer1, INDICATOR_DATA);
SetIndexBuffer(1, SupportBuffer2, INDICATOR_DATA);
SetIndexBuffer(2, SupportBuffer3, INDICATOR_DATA);
SetIndexBuffer(3, ResistanceBuffer1, INDICATOR_DATA);
SetIndexBuffer(4, ResistanceBuffer2, INDICATOR_DATA);
SetIndexBuffer(5, ResistanceBuffer3, INDICATOR_DATA);
for (int i = 0; i < 6; i++)
{
PlotIndexSetString(i, PLOT_LABEL, (i < 3 ? "Support " : "Resistance ") + IntegerToString(i % 3 + 1));
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(i, PLOT_LINE_COLOR, clrWhite); // Support = Blue, Resistance = Red
PlotIndexSetInteger(i, PLOT_LINE_STYLE, STYLE_SOLID);
PlotIndexSetInteger(i, PLOT_LINE_WIDTH, 1);
}
// Set properties for each plot
for (int i = 0; i < 6; i++)
{
PlotIndexSetString(i, PLOT_LABEL, (i < 3 ? "Support " : "Resistance ") + IntegerToString(i % 3 + 1));
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(i, PLOT_LINE_COLOR, clrWhite); // Support = Blue, Resistance = Red
PlotIndexSetInteger(i, PLOT_LINE_COLOR, (i < 3) ? clrBlue : clrRed); // Support = Blue, Resistance = Red
PlotIndexSetInteger(i, PLOT_LINE_STYLE, STYLE_DASH);
PlotIndexSetInteger(i, PLOT_LINE_WIDTH, 1);
}
ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1); // Enable mouse move events
return(INIT_SUCCEEDED);
}
I would like to wind-up with the following:
Expected outcome
Quote
Report
Forex Coder · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Forex Coder
Original post: https://stackoverflow.com/a/79670369
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Hello you can use an array to specify the styles each plot acquires.
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots 6
double SupportBuffer1[];
double SupportBuffer2[];
double SupportBuffer3[];
double ResistanceBuffer1[];
double ResistanceBuffer2[];
double ResistanceBuffer3[];
/*
list of styles colors and widths
these will be accessed in the setup loops
the setup uses i%3 which is the remnant of the division i/3
so the first plot will go 0/3 -> remnant 0 it will read styles[0],colors[0],widths[0]
the 2nd plot will go 1/3 -> remnant 1 it will read styles[1]...
the 3rd plot will go 2/3 -> remnant 2 it will read styles[2]...
the 4th plot will go 3/3 -> remnant 0 it will read styles[0]...
*/
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DASHDOTDOT};
color colors[]={clrWhite,clrCrimson,clrRoyalBlue};
int widths[]={2,2,2};
int OnInit()
{
// Initialize buffers
SetIndexBuffer(0, SupportBuffer1, INDICATOR_DATA);
SetIndexBuffer(1, SupportBuffer2, INDICATOR_DATA);
SetIndexBuffer(2, SupportBuffer3, INDICATOR_DATA);
SetIndexBuffer(3, ResistanceBuffer1, INDICATOR_DATA);
SetIndexBuffer(4, ResistanceBuffer2, INDICATOR_DATA);
SetIndexBuffer(5, ResistanceBuffer3, INDICATOR_DATA);
for (int i = 0; i < 6; i++)
{
PlotIndexSetString(i, PLOT_LABEL, (i < 3 ? "Support " : "Resistance ") + IntegerToString(i % 3 + 1));
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(i, PLOT_LINE_COLOR, colors[i%3]); // Support = Blue, Resistance = Red
PlotIndexSetInteger(i, PLOT_LINE_STYLE, styles[i%3]);
PlotIndexSetInteger(i, PLOT_LINE_WIDTH, widths[i%3]);
}
return(INIT_SUCCEEDED);
}
int OnCalculate(const int32_t rates_total,
const int32_t 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 int32_t &spread[])
{
for(int i=prev_calculated;i<rates_total;i++){
int target=i-i%20;
SupportBuffer1[i]=high[target];
SupportBuffer2[i]=high[target];
SupportBuffer3[i]=high[target];
ResistanceBuffer1[i]=low[target];
ResistanceBuffer2[i]=low[target];
ResistanceBuffer3[i]=low[target];
}
return(rates_total);
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Johnny Sandaire Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/79669387 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I have an indicator that draws dashed lines in red or blue as needed for support and resistance levels. However, I have a difficult time seeing the lines on the black background charts, so I would like to change the lines to a dashed red-white or dashed blue-white line instead. I accomplished this by creating two separate indicators. One draws the support and resistance levels utilizing a white solid line. Then, I loaded the white line indicator onto the chart first; followed by loading the dashed red and blue line indicator. The results were much better visually. Therefore, instead of using two separate indicators, I would like to have one indicator that is able to generate the dashed multicolored lines. I tried to add two blocks of a for loop to overlay the lines, but that does not work. MQL5 prioritizes the last block that ran, and I can only see one set of lines instead. Is it possible for me to render these lines on the charts with a single indicator? This is my current for loop that is drawing the red, blue, and white lines. int OnInit() { ArrayResize(sr_levels, 6); // Resize to 6 levels ArrayResize(aas, 6); // Resize to 6 levels ArrayInitialize(sr_levels, 0); ArrayInitialize(aas, true); // Initialize buffers SetIndexBuffer(0, SupportBuffer1, INDICATOR_DATA); SetIndexBuffer(1, SupportBuffer2, INDICATOR_DATA); SetIndexBuffer(2, SupportBuffer3, INDICATOR_DATA); SetIndexBuffer(3, ResistanceBuffer1, INDICATOR_DATA); SetIndexBuffer(4, ResistanceBuffer2, INDICATOR_DATA); SetIndexBuffer(5, ResistanceBuffer3, INDICATOR_DATA); for (int i = 0; i < 6; i++) { PlotIndexSetString(i, PLOT_LABEL, (i < 3 ? "Support " : "Resistance ") + IntegerToString(i % 3 + 1)); PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE); PlotIndexSetInteger(i, PLOT_LINE_COLOR, clrWhite); // Support = Blue, Resistance = Red PlotIndexSetInteger(i, PLOT_LINE_STYLE, STYLE_SOLID); PlotIndexSetInteger(i, PLOT_LINE_WIDTH, 1); } // Set properties for each plot for (int i = 0; i < 6; i++) { PlotIndexSetString(i, PLOT_LABEL, (i < 3 ? "Support " : "Resistance ") + IntegerToString(i % 3 + 1)); PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE); PlotIndexSetInteger(i, PLOT_LINE_COLOR, clrWhite); // Support = Blue, Resistance = Red PlotIndexSetInteger(i, PLOT_LINE_COLOR, (i < 3) ? clrBlue : clrRed); // Support = Blue, Resistance = Red PlotIndexSetInteger(i, PLOT_LINE_STYLE, STYLE_DASH); PlotIndexSetInteger(i, PLOT_LINE_WIDTH, 1); } ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1); // Enable mouse move events return(INIT_SUCCEEDED); } I would like to wind-up with the following: Expected outcome
Checking account access…