single line graph with two colors

To add comments, please log in or register
Jayaram Naick Bukke
531
Jayaram Naick Bukke  

Hi Guys..

the indicator is drawing the single line w r to close price bars with two colors.


Blue color always up & red color always down. whenever the close of present bar=previous close bar then continue the previous color.

so here the specification is :

i)Connect Blue line from C[i] to C[i-1]: If present bar close[i-1]>previous bar close[i].

(ii)Connect Red line from C[i] to C[i-1]: if present bar close[i-1] <previous bar close[i].

(iii): if present bar close[i-1]=previous bar close[i], then repeat the previous color.

Code:

//+------------------------------------------------------------------+
//|                                                  News_trader.mq4 |
//|                                 Programmed by Noé Combes-Bardoll |
//|                             https://www.mql5.com/en/users/nono86 |
//+------------------------------------------------------------------+
#property copyright "Programmed by Noé Combes-Bardoll"
#property link      "https://www.mql5.com/en/users/nono86"
#property version   "1.1"
#property strict

#property indicator_chart_window
#property indicator_buffers 2       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line

double blue[],red[];
/*
 i)Connect Blue line from C[i] to C[i-1]: If present bar close[i-1]>previous bar close[i].
(ii)Connect Red line from C[i] to C[i-1]: if present bar close[i-1] <previous bar close[i].
(iii): if present bar close[i-1]=previous bar close[i], then repeat the previous color.
*/
int OnInit(){
   SetIndexBuffer(0,blue);         // Assigning an array to a buffer
   SetIndexStyle(0,DRAW_LINE,EMPTY,3,clrBlue);
   SetIndexBuffer(1,red);         // Assigning an array to a buffer
   SetIndexStyle(1,DRAW_LINE,EMPTY,3,clrRed);
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){

}

int start(){
   int i,Counted_bars;
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=2;           // Index of the first uncounted
   
   while(i<Bars){
      if(Close[i]<Close[i-1]){//conditons 1
         blue[i]=Close[i];
         blue[i-1]=Close[i-1];
         }
      else if(Close[i]>Close[i-1]){//conditions 2
         red[i]=Close[i]+5*_Point;
         red[i-1]=Close[i-1]+5*_Point;
         }
      
      else if(Close[i]==Close[i-1]&&Close[i]==red[i]){//conditions 3 if last color was red
         //red[i]=Close[i]+5*_Point;
         red[i-1]=Close[i-1]+5*_Point;
         }
      else if(Close[i]==Close[i-1]&&Close[i]==blue[i]){//conditions 3 if last color was blue
         blue[i-1]=Close[i-1];
         }
         
      i++;                          // Calculating index of the next bar
     }
     return(0);
   }


code is compiled correctly the problem is

additionally extending the blue line buffer to downside from bar to bar though red line exists already. Here Blue line(marked as 1 to 2 must be removed).

additionally extending the red line buffer to upside from bar to bar though blue line exists already. Here Red(marked as 3 to 4 must be removed).

So can some one point out the error in code  or if possible how it can be fixed so that 1 to 2 blue lines turned down & 3 to 4 red line turned up has to be removed or does not exists as per speficiation.

Thanks & Regards.

Jayaram

William Roeder
20210
William Roeder  
  1. Same as any colored line. Look in the codebase. If you assign to one color buffer, make the other color buffer empty.
              HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 and MetaTrader 4 - MQL4 programming forum

    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.)

    Two 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].

  2.       if(Close[i]<Close[i-1]){//conditons 1
    The previous bar is i+1.
Mladen Rakic
159843
Mladen Rakic  

You can not draw a non-repainting 2 color  line in metatrader 4 using only 2 buffers. You needs to use at least 3 buffers. Some examples can be found here : Make It No Repaint Please! but you can use any of the newer indicators as examples


PS: close[i-1] is not present bar in metatrader 4 but future bars close

Make It No Repaint Please!
Make It No Repaint Please!
  • 2015.04.07
  • www.mql5.com
I think time has come for this thread...Kindly place your repainters here...and...Voila...
Jayaram Naick Bukke
531
Jayaram Naick Bukke  
William Roeder:
  1. Same as any colored line. Look in the codebase. If you assign to one color buffer, make the other color buffer empty.
              HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 and MetaTrader 4 - MQL4 programming forum

    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.)

    Two 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].

  2. The previous bar is i+1.

I am not the Developer. Can you correct the code please. I am given the job to Developer https://www.mql5.com/en/users/nono86/portfolio and he is trying but not able to fix the error correctly. 

Thanks.

Keith Watford
Moderator
20371
Keith Watford  
Jayaram Naick Bukke:

Do not double post.

I have deleted your other topic.

Jayaram Naick Bukke
531
Jayaram Naick Bukke  
Keith Watford:

Do not double post.

I have deleted your other topic.

Ok. Anyway just before you delete the post. we got the Solution to this topic. Error or Bug Fixed by the Developer (https://www.mql5.com/en/users/tsengjoo)to the above code.

This is the correct drawing of single line with two colored: Blue line always w r to higher or equal close price of bars  & red line always w r to lower or equal close price of bars.

Thanks to the Developer who was actually coded the specification(https://www.mql5.com/en/users/nono86) & Thanks to the Developer who was later fixed the error or bug to this the existing code. Below is the Corrected coded as per the specification. along with sheet. If some one needs, finally it is available in this forum. 

//+------------------------------------------------------------------+
//|                                                  News_trader.mq4 |
//|                                 Programmed by Noé Combes-Bardoll |
//|                             https://www.mql5.com/en/users/nono86 |
//+------------------------------------------------------------------+
#property copyright "Programmed by Noé Combes-Bardoll"
#property link      "https://www.mql5.com/en/users/nono86"
#property version   "1.1"
#property strict

#property indicator_chart_window
#property indicator_buffers 8 

#property indicator_color1 clrBlue
#property indicator_color2 clrDodgerBlue
#property indicator_color3 clrRed
#property indicator_color4 clrGold
#property indicator_color5 clrCyan
#property indicator_color6 clrOrange
#property indicator_color7 clrGreen
#property indicator_color8 clrYellow


double blue[],blue2[],red[],red2[],blue3[],red3[],blue4[],red4[];
/*
 i)Connect Blue line from C[i+1] to C[i]: If present bar close[i]>previous bar close[i+1].
(ii)Connect Red line from C[i+1] to C[i]: if present bar close[i] <previous bar close[i+1].
(iii): if present bar close[i]=previous bar close[i+1], then repeat the previous color.
*/
int OnInit(){
IndicatorBuffers(8);
   SetIndexBuffer(0,blue);         // Assigning an array to a buffer
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3,clrBlue);
   SetIndexLabel(0,"Blue 1");
   SetIndexBuffer(1,blue2);         // Assigning an array to a buffer
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,3,clrBlue);
   SetIndexLabel(1,"Blue 2");
   
   SetIndexBuffer(2,red);         // Assigning an array to a buffer
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,3,clrRed);
   SetIndexLabel(2,"Red 1");
   SetIndexBuffer(3,red2);         // Assigning an array to a buffer
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,3,clrRed);
   SetIndexLabel(3,"Red 2");
   
   SetIndexBuffer(4,blue3);         // Assigning an array to a buffer
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,3,clrBlue);
   SetIndexLabel(4,"Blue 3");
   SetIndexBuffer(5,red3);         // Assigning an array to a buffer
   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,3,clrRed);
   SetIndexLabel(5,"Red 3");
   
   SetIndexBuffer(6,blue4);         // Assigning an array to a buffer
   SetIndexStyle(6,DRAW_LINE,STYLE_SOLID,3,clrBlue);
   SetIndexLabel(6,"Blue 4");
   SetIndexBuffer(7,red4);         // Assigning an array to a buffer
   SetIndexStyle(7,DRAW_LINE,STYLE_SOLID,3,clrRed);
   SetIndexLabel(7,"Red 4");
   
   SetIndexEmptyValue(0,0);
   SetIndexEmptyValue(1,0);
   SetIndexEmptyValue(2,0);
   SetIndexEmptyValue(3,0);
   SetIndexEmptyValue(4,0);
   SetIndexEmptyValue(5,0);
   SetIndexEmptyValue(6,0);
   SetIndexEmptyValue(7,0);
   
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){

}

int start()
{
   int i,Counted_bars;
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars

   ArrayInitialize(red,0);
   ArrayInitialize(red2,0);
   ArrayInitialize(red3,0);
   ArrayInitialize(red4,0);
   ArrayInitialize(blue,0);
   ArrayInitialize(blue2,0);
   ArrayInitialize(blue3,0);
   ArrayInitialize(blue4,0);

   i = Bars - 2;
   
   while(i>0)
   {
      if(Close[i+1]>Close[i])
         SetBuffer(i,clrRed);
      else
      if(Close[i+1]<Close[i])
         SetBuffer(i,clrBlue);
      else
      if (Close[i+1]==Close[i])
         SetBuffer(i);
      i--;
   }

   return(0);
}
   
void SetBuffer(int Bar, int Color = -1)
{
   static int LastColor = clrBlack;
   if (Color==-1)
      Color = LastColor;
   int BufIdx = Bar%4;
   switch (BufIdx)
   {
      case 0:
      {
         if (Color==clrBlue)
         {
            blue[Bar] = Close[Bar];
            blue[Bar+1] = Close[Bar+1];
         }
         else
         if (Color==clrRed)
         {
            red[Bar] = Close[Bar];
            red[Bar+1] = Close[Bar+1];
         }
         break;
      }
      case 1:
      {
         if (Color==clrBlue)
         {
            blue2[Bar] = Close[Bar];
            blue2[Bar+1] = Close[Bar+1];
         }
         else
         if (Color==clrRed)
         {
            red2[Bar] = Close[Bar];
            red2[Bar+1] = Close[Bar+1];
         }
         break;
      }
      case 2:
      {
         if (Color==clrBlue)
         {
            blue3[Bar] = Close[Bar];
            blue3[Bar+1] = Close[Bar+1];
         }
         else
         if (Color==clrRed)
         {
            red3[Bar] = Close[Bar];
            red3[Bar+1] = Close[Bar+1];
         }
         break;
      }
      case 3:
      {
         if (Color==clrBlue)
         {
            blue4[Bar] = Close[Bar];
            blue4[Bar+1] = Close[Bar+1];
         }
         else
         if (Color==clrRed)
         {
            red4[Bar] = Close[Bar];
            red4[Bar+1] = Close[Bar+1];
         }
         break;
      }
   }

   LastColor = Color;
}
paul selvan
629
paul selvan  

also with graphical objets 

#property strict
#property indicator_chart_window
//--------------------------------------------------------------------
int OnInit(){return(INIT_SUCCEEDED);}
//--------------------------------------------------------------------
int start(){
   double dClose;
   string objName;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars-1;
   
   for(int i=0; i<limit; i++){
      dClose = Close[i]-Close[i+1];
      objName = TimeToStr(Time[i]);
      ObjectDelete(objName);
      ObjectCreate(0,objName,OBJ_TREND,0,Time[i+1],Close[i+1],Time[i],Close[i]);
      ObjectSetInteger(0,objName,OBJPROP_RAY_RIGHT,false);
      ObjectSetInteger(0,objName,OBJPROP_SELECTABLE,false);
      ObjectSetInteger(0,objName,OBJPROP_WIDTH,3);
      
      if(dClose>0) ObjectSetInteger(0,objName,OBJPROP_COLOR,clrBlue);
      
      if(dClose<0) ObjectSetInteger(0,objName,OBJPROP_COLOR,clrRed);
      
      
      if(dClose==0) {
         if (Close[i+1]<Close[i+2])   ObjectSetInteger(0,objName,OBJPROP_COLOR,clrRed);
         else                         ObjectSetInteger(0,objName,OBJPROP_COLOR,clrBlue);
     }
   }
   return(0);
}
To add comments, please log in or register