Draw Colour Lines on MT4

 
Hi everyone, am creating an indicator for MT4 and I need to be able to dynamically change the line colors when certain conditions are met.

Unfortunately, once I specify property indicator_type1 DRAW_COLORLINE the lines disappears entirely and are  no longer visible.

However if I change indicator_type1 to DRAW_LINE the lines appear but the colors do not change.

Is it that MT4 does not support drawing coloured lines or is there another problem elsewhere?

Am using the free mql5 indicator "Draw Colour lines"
To compile it in Mql4 I have to change DRAW_COLOR_LINE to DRAW_COLORLINE cause mql4 does not recognise the first
 
//+------------------------------------------------------------------+ 
//|                                              DRAW_COLOR_LINE.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                              https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
  
#property description "An indicator to demonstrate DRAW_COLOR_LINE" 
#property description "It draws a line on Close price in colored pieces of 20 bars each" 
#property description "The width, style and color of the line parts are changed randomly" 
#property description "every N ticks" 
  
#property indicator_chart_window 
#property indicator_buffers 2 
#property indicator_plots   1 
//--- plot ColorLine 
#property indicator_label1  "ColorLine" 
#property indicator_type1   DRAW_COLORLINE 
//--- Define 5 colors for coloring each bar (they are stored in the special array) 
#property indicator_color1  clrRed,clrBlue,clrGreen,clrOrange,clrDeepPink // (Up to 64 colors can be specified) 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  3 
//--- input parameters 
input int      N=5;           //Number of ticks to change  
input int      Length=20;     // The length of each color part in bars 
int            line_colors=5; // The number of set colors is 5 - see #property indicator_color1 
//--- A buffer for plotting 
double         ColorLineBuffer[]; 
//--- A buffer for storing the line color on each bar 
double         ColorLineColors[]; 
  
//--- The array for storing colors contains 7 elements 
color colors[]={clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod}; 
//--- An array to store the line styles 
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT}; 
//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- Binding an array and an indicator buffer 
   SetIndexBuffer(0,ColorLineBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,ColorLineColors,INDICATOR_COLOR_INDEX); 
//--- Initializing the generator of pseudo-random numbers 
   MathSrand(GetTickCount()); 
//--- 
   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[]) 
  { 
   static int ticks=0; 
//--- Calculate ticks to change the style, color and width of the line 
   ticks++; 
//--- If a critical number of ticks has been accumulated 
   if(ticks>=N) 
     { 
      //--- Change the line properties 
      ChangeLineAppearance(); 
      //--- Change the colors of line sections 
      ChangeColors(colors,5); 
      //--- Reset the counter of ticks to zero 
      ticks=0; 
     } 
  
//--- Block for calculating indicator values 
   for(int i=0;i<rates_total;i++) 
     { 
      //--- Write the indicator value into the buffer 
      ColorLineBuffer[i]=close[i]; 
      //--- Now, randomly set a color index for this bar 
      int color_index=i%(5*Length); 
      color_index=color_index/Length; 
      //--- For this bar, the line will have the color with the index color_index 
      ColorLineColors[i]=color_index; 
     } 
  
//--- Return the prev_calculated value for the next call of the function 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| Changes the color of line segments                               | 
//+------------------------------------------------------------------+ 
void  ChangeColors(color  &cols[],int plot_colors) 
  { 
//--- The number of colors 
   int size=ArraySize(cols); 
//---  
   string comm=ChartGetString(0,CHART_COMMENT)+"\r\n\r\n"; 
  
//--- For each color index define a new color randomly 
   for(int plot_color_ind=0;plot_color_ind<plot_colors;plot_color_ind++) 
     { 
      //--- Get a random value 
      int number=MathRand(); 
      //--- Get an index in the col[] array as a remainder of the integer devision 
      int i=number%size; 
      //--- Set the color for each index as the property PLOT_LINE_COLOR 
      PlotIndexSetInteger(0,                    //  The number of a graphical style 
                          PLOT_LINE_COLOR,      //  Property identifier 
                          plot_color_ind,       //  The index of the color, where we write the color 
                          cols[i]);             //  A new color 
      //--- Write the colors 
      comm=comm+StringFormat("LineColorIndex[%d]=%s \r\n",plot_color_ind,ColorToString(cols[i],true)); 
      ChartSetString(0,CHART_COMMENT,comm); 
     } 
//--- 
  } 
//+------------------------------------------------------------------+ 
//| Changes the appearance of a displayed line in the indicator      | 
//+------------------------------------------------------------------+ 
void ChangeLineAppearance() 
  { 
//--- A string for the formation of information about the line properties 
   string comm=""; 
//--- A block for changing the width of the line 
   int number=MathRand(); 
//--- Get the width of the remainder of integer division 
   int width=number%5; // The width is set from 0 to 4 
//--- Set the color as the PLOT_LINE_WIDTH property 
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width); 
//--- Write the line width 
   comm=comm+" Width="+IntegerToString(width); 
  
//--- A block for changing the style of the line 
   number=MathRand(); 
//--- The divisor is equal to the size of the styles array 
   int size=ArraySize(styles); 
//--- Get the index to select a new style as the remainder of integer division 
   int style_index=number%size; 
//--- Set the color as the PLOT_LINE_COLOR property 
   PlotIndexSetInteger(0,PLOT_LINE_STYLE,styles[style_index]); 
//--- Write the line style 
   comm=EnumToString(styles[style_index])+", "+comm; 
//--- Show the information on the chart using a comment 
   Comment(comm); 
  }

This is the code for Mql4, the only difference between this and mql5 version is that #property indicator_type1   DRAW_COLORLINE is #property indicator_type1   DRAW_COLOR_LINE in MQL5
 

 

 
holocast:

Unfortunately, once I specify property indicator_type1 DRAW_COLORLINE the lines disappears entirely and are  no longer visible.

          Drawing Styles - Indicator Constants - Constants, Enumerations and Structures - MQL4 Reference


However if I change indicator_type1 to DRAW_LINE the lines appear but the colors do not change.

Is it that MT4 does not support drawing coloured lines or is there another problem elsewhere?
  1. You already know where the problem is PICNIC.
  2. There is no DRAW_COLORLINE or DRAW_COLOR_LINE in MT4.
              Drawing Styles - Indicator Constants - Constants, Enumerations and Structures - MQL4 Reference
    There are only lines. You need n buffers for n colors. Look at any colored line indicator in the MT4 code base.
  3. For MT4 I use:

    One buffer has the value, color set to CLR_NONE so not shown on chart, (but in data window and pop up.)

    N buffers, one color each, with SetIndexLabel(i, NULL) so they don't show in data window.

    Then you need to connect the lines on color change. downBuffer[i]=value[i]; if(downBuffer[i+1]==EMPTY_VALUE) downBuffer[i+1]=value[i].

  4. HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 and MetaTrader 4 - MQL4 programming forum