One line in different colors

 

Hi guys,

i want to draw an indicator like SMA but the color should be variable. For example if the SMA is rising than it's color should be green. If the SMA is falling then it's color should be red.

I can't find a solution for that.

 
FamWue:

Hi guys,

i want to draw an indicator like SMA but the color should be variable. For example if the SMA is rising than it's color should be green. If the SMA is falling then it's color should be red.

I can't find a solution for that.

You have to use muliplee buffers,  if you want 3 colours you need 3 buffers.
 
FamWue: I can't find a solution for that.

Did you bother to look in the code base for a colored indicator?

Did you use this and you would have found these
 

Can anyone explain me, why there are empty spaces in the indicator line in the bottom window?



//+------------------------------------------------------------------+
//|                                                      Volumen.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 3      // Number of buffers
#property indicator_color1 Yellow     // Color of the 1st line
#property indicator_color2 Red     // Color of the 1st line
#property indicator_color3 Blue     // Color of the 1st line
//#property indicator_color4 Blue     // Color of the 1st line
extern int Period_MA = 20;          // Calculated MA period
//--- intern variables
double Volumen0[];                      // Wert Steigung
double Volumen1[];                      // Wert Steigung
//double Volumen2[];                      // Wert Steigung
double SMA[];                       // SMA
//--------------------------------------------------------------------
int init()                          // Special function init()
  {  
   string short_name = "Volumen";
   IndicatorBuffers(3);
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexBuffer(0,Volumen0);          // Assigning an array to a buffer
   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexBuffer(1,Volumen1);          // Assigning an array to a buffer
   SetIndexStyle (2,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexBuffer(2,SMA);           // Assigning an array to a buffer
   //SetIndexStyle (3,DRAW_LINE,STYLE_SOLID,2);// Line style
   //SetIndexBuffer(3,Volumen2);          // Assigning an array to a buffer
   SetIndexEmptyValue(0,0.0); 
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);
   //SetIndexEmptyValue(3,0);
   IndicatorShortName(short_name);  // Indicator name
   SetIndexLabel(0,short_name);     // Label im DataWindows
   return;                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int counted_bars;                // Number of counted bars 
   int i;                           // Bar index
   double Pip = 10*Point;
   //ArrayInitialize(StMA,0.0);   
//---- initial zero
   counted_bars=IndicatorCounted(); // Number of counted bars
   if(counted_bars<1)
      for(i=1;i<=Period_MA;i++) 
         {
         Volumen0[Bars-i]=0.0;
         Volumen1[Bars-i]=0.0;
         //Volumen2[Bars-i]=0.0;
         SMA[Bars-i]=0.0;
         }
   i=Bars-counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      Volumen0[i]=Volume[i]; // Value of 0 buffer on i bar
      i--;                          // Calculating index of the next bar
     }
   i=Bars-counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      SMA[i]=iMAOnArray(Volumen0,Bars,Period_MA,0,MODE_SMA,i); // Value of 0 buffer on i bar
      i--;                          // Calculating index of the next bar
     }                // Calculating index of the next bar
   
   i=Bars-counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
     if(Volumen0[i]>=SMA[i])
         {
         Volumen0[i]=Volumen0[i];
         Volumen1[i]=0.0;
         }
     else 
         {
         Volumen1[i]=Volumen0[i];
         Volumen0[i]=0.0;
         }
      i--;                          // Calculating index of the next bar
     }                // Calculating index of the next bar
  } 
//--------------------------------------------------------------------+
 
FamWue:

Can anyone explain me, why there are empty spaces in the indicator line in the bottom window?

Think about it . . .  at the bar where the line goes from red to yellow or from yellow to red, at that bar the buffer for red and yellow must have the same value,  otherwise you get gaps.
 

have u found solution to line gap problems as mention above by Raptor?

 
RaptorUK already told you the answer. When the line changes colors the two buffers "must have the same value, otherwise you get gaps."
 

Ok u r rite..but what shld b the code?..if now is red and previous is yellow,then yellow equals red??..whereas previous is a gap~..what will b the line color at the gaps? 

 

guyz..it is also noticed  in the above chart that there are gaps even when line doesnt changes color? ..what is happening here..

i m also trying to code threecolor indicator and found this post partially helpful bt i suspect somethin is still missing 

 
MirTD:

guyz..it is also noticed  in the above chart that there are gaps even when line doesnt changes color? 


No,  that might simply be a problem with the method used to compare doubles,  if you look carefully you will see that the gaps coincide with the red and blue lines touching or crossing in the upper chart.
 
MirTD:

Ok u r rite..but what shld b the code?..if now is red and previous is yellow,then yellow equals red??..whereas previous is a gap~..what will b the line color at the gaps? 

 

It's up to you to decide how you want the information displayed,  if the cross is between the bars when does the line change colour ?  before the cross or after it ?  what if there is no cross but the lines are equal ?  what then ?
Reason: