Colouring DRAW_LINE / DRAW_COLOR_LINE segments

 

Hi,

I'm trying to get a draw line to change colours - green when the line has a positive slope, red when the line slope is negative. What I'm trying to achieve is essentially a multi coloured line drawn over the candles to indicate the change in a value over time. 

I've tried both the DRAW_LINE and the DRAW_COLOR_LINE functions, but I can't get them to change just the segment - any changes to the colour changes the entirety of the line.

Here's a modified version of the code I've created to test (produces the same result as my more complicated indicator). This is the first time that I've tried to create a custom indicator so I'm hoping that it's something dumb or simple that I'm missing. 

#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1

double closeBuffer[]; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  
    SetIndexBuffer(0,closeBuffer,INDICATOR_DATA);
    PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
    PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID);
    PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrRed,clrGreen);
    PlotIndexSetInteger(0,PLOT_LINE_WIDTH,3);
    PlotIndexSetString(0,PLOT_LABEL,"Close");
//--- indicator buffers mapping
   
//---
   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[])
  {
//---
   
   
   for (int i = prev_calculated; i < rates_total; i++){
            //Change colour of line
         Print("Current I Value: " + i);
         Print("Rates Total: " + rates_total);
         closeBuffer[i] = iClose(_Symbol,PERIOD_CURRENT,0);
         Print("Close Buffer = " + closeBuffer[i]);
 
         if (closeBuffer[rates_total - 1] < closeBuffer[rates_total - 2]){
            PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrGreen);     
         } else if (closeBuffer[rates_total - 1] > closeBuffer[rates_total - 2]){
            PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrRed);
         }
         
      }
     
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }



I've also included some screenshots to show how the whole line changes, rather than the line segment to make it a bit clearer. 

What I'm trying to achieve is something like:

https://www.mql5.com/en/docs/customind/indicators_examples/DRAW_COLOR_LINE#:~:text=The%20example%20shows%20the%20feature%20of%20the%20%22color%22,is%20set%20new%20colors%20in%20a%20special%20array.

Documentation on MQL5: Custom Indicators / Indicator Styles in Examples / DRAW_COLOR_LINE
Documentation on MQL5: Custom Indicators / Indicator Styles in Examples / DRAW_COLOR_LINE
  • www.mql5.com
The DRAW_COLOR_LINE value is a colored variant of the DRAW_LINE style; it also draws a line using the values of the indicator buffer. But this...
Files:
RedLine.png  44 kb
GreenLine.png  42 kb
 
Paul Boyd:

I'm trying to get a draw line to change colours - green when the line has a positive slope, red when the line slope is negative.

Not to thwart your learning by doing, but here's another coder's highly universal MA that changes colors.

Files:
 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Fernando Carreiro #:
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Thanks Fernando - I was worried that my request was too basic to be considered a "technical indicator" - appreciate the move
 
Ryan L Johnson #:

Not to thwart your learning by doing, but here's another coder's highly universal MA that changes colors.

No, not at all.... this indicator is fantastic, I'm just trying to do something that isn't covered in this one.

Saying that - I definitely figured out how to use the INDICATOR_COLOR_INDEX function so mission accomplished. 

Thanks for your input - greatly appreciated 

Cheers,
Paul
Files:
Working.png  34 kb