Download MetaTrader 5

Problems with a colored line chart

To add comments, please log in or register
mar
1148
mar  

Hey guys,

can somebody tell me where the error is?


Here is the code:

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red

double SMAup[], SMAdn[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
    SetIndexBuffer(0, SMAup);       
    SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
    SetIndexBuffer(1, SMAdn);      
    SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   int counted_bars=IndicatorCounted();
   if (counted_bars < 0) return (-1);
  
   if (counted_bars > 0) counted_bars--;
   int limit = Bars-counted_bars;
  
   for (int i=limit-1; i >= 0; i--)
   {  
      SMAdn[i] = EMPTY_VALUE;
      SMAup[i] = EMPTY_VALUE;
      if (Close[i] < Close[i+1]) SMAdn[i] = Close[i];
      if (Close[i] >= Close[i+1]) SMAup[i] = Close[i];
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
Tjipke de Vries
6392
Tjipke de Vries  
if (Close[i] < Close[i+1]) 
   {
   SMAdn[i+1] = Close[i+1];
   SMAdn[i] = Close[i];
   }
if (Close[i] >= Close[i+1]) 
   {
   SMAup[i+1] = Close[i+1];
   SMAup[i] = Close[i];
   }

DRAW_LINE,STYLE_SOLID can only appear between two bars if the two bars have a value

instead of draw_line you can do also draw_arrow and make width bigger

in that case you can the code be like you did

      if (Close[i] < Close[i+1]) SMAdn[i] = Close[i];
      if (Close[i] >= Close[i+1]) SMAup[i] = Close[i];
mar
1148
mar  
Great! Thank you!!
mar
1148
mar  

A little bit too early... I used your first code and the result is this:


Do you see the last up move? There is no green line although we had some up closes... Do you have an idea how to correct it?
mar
1148
mar  

It seems to me that the indicator causes problems when there are alternating up- and downcloses. It only works when there are at least two up- or two downcloses.

But I have no idea why.... ?!

mar
1148
mar  
Nobody has an idea? :-(
Keith Watford
Moderator
10614
Keith Watford  
mar:
Nobody has an idea? :-(
if (Close[i] < Close[i+1]) 
   {
   SMAdn[i+1] = Close[i+1];
   SMAdn[i] = Close[i];
   SMAup[i] = EMPTY_VALUE;
   }
if (Close[i] >= Close[i+1]) 
   {
   SMAup[i+1] = Close[i+1];
   SMAup[i] = Close[i];
   SMAdn[i] = EMPTY_VALUE;
   }

On a live chart the price may be above or below the previous close at any one time and so a value would be set for both buffers

Not sure if the above would be useful or not

Tjipke de Vries
6392
Tjipke de Vries  
mar:
Nobody has an idea? :-(


Why do you think i suggested to do it with DRAW_ARROW instead of using DRAW_LINE ??

See how your buffers are filled and then look to your line

A line needs two points to get from BarX to BarY

if you go up down up down up .......

then both buffers will have no empty value but you see the last buffer colored

mar
1148
mar  
I did not know that DRAW_LINE causes such problems. I thought that DRAW_ARROW is just another possibility. Then I will try it with DRAW_ARROW...
qjol
3243
qjol  
mar:
I did not know that DRAW_LINE causes such problems.
it's not a problem you need to handle all the possibilities
Ian Venner
2584
Ian Venner  

I think we can use DRAW_COLOR_LINE for this type of indicator. Having said that assigning the colors to it seems kinda complicated I havent tried it yet.

12
To add comments, please log in or register