Download MetaTrader 5

indicator color coding

To add comments, please log in or register
Having questions to the author of an article? Discuss them on the Forum!
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2015.01.26 08:17 

Hello,

I am newbie on MT4 coding. I am sure it is very simple to code what I want but for me is very complicated.

So I want to modify to the code of a custom indicator like this:

if condition==true

 indicator color=blue

else

 indicator color=red

endif

Please give me some instructions how to code this in MT4 language.

Thanks in advance ! 

honest_knave
Moderator
2152
honest_knave 2015.01.26 08:42  
Are you looking to change the color of the indicator's buffers?
If so, are you aware that a buffer can only have one color. So you can't have a buffer draw as red in bars 1, 2 and 3 while having bars 4, 5 and 6 draw in blue.
You need different buffers and then make each buffer only draw when your condition is true.
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2015.01.26 09:01  
honest_knave:
Are you looking to change the color of the indicator's buffers?
If so, are you aware that a buffer can only have one color. So you can't have a buffer draw as red in bars 1, 2 and 3 while having bars 4, 5 and 6 draw in blue.
You need different buffers and then make each buffer only draw when your condition is true.

As i said I am new to MT4 software. I dont know if  " I am looking to change the color of the indicator's buffers"

At this moment the color of the indicator is blue on chart.

As I said in my first post I want to change indicator color if certain condition is met. 

honest_knave
Moderator
2152
honest_knave 2015.01.26 10:03  

Perhaps if you posted some code, people would be better placed to help you.

"The color of the indicator"... what does the indicator do? Draw rectangles, plot arrows, mark text, draw a wiggly line, make you a cup of tea.

Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2015.01.26 10:21  
honest_knave:

Perhaps if you posted some code, people would be better placed to help you.

"The color of the indicator"... what does the indicator do? Draw rectangles, plot arrows, mark text, draw a wiggly line, make you a cup of tea.

Draw a line. It is similar with a moving average. I simply want to be blue on uptrend and red on downtrend but I do not think this is important. As I initial posted the general behavior has to be like:

if condition==true

 indicator color=blue

else

 indicator color=red

endif 

honest_knave
Moderator
2152
honest_knave 2015.01.26 10:29  

I can answer your general question with a general answer, but I don't think it will help you.

tenlau:

if condition==true

 indicator color=blue

else

 indicator color=red

endif

Please give me some instructions how to code this in MT4 language.

if(condition) {ind_color=clrBlue;}
else {ind_color=clrRed;} 
 

 But as I said, this most likely will not help you. That is not how most line-based indicators work.

A line indicator will generally be using at least one buffer to which you can assign a color. But the entirety of that line has to use the same color. If you want different colors, you need to use multiple buffers. Which then means your question is not how do I change the color, but how do I decide when to draw each buffer.

You need to get an understanding of how custom indicators work, either by reading the documentation or looking at other indicators and figuring out how they work.

Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2015.01.26 10:52  
honest_knave:

I can answer your general question with a general answer, but I don't think it will help you.

 But as I said, this most likely will not help you. That is not how most line-based indicators work.

A line indicator will generally be using at least one buffer to which you can assign a color. But the entirety of that line has to use the same color. If you want different colors, you need to use multiple buffers. Which then means your question is not how do I change the color, but how do I decide when to draw each buffer.

You need to get an understanding of how custom indicators work, either by reading the documentation or looking at other indicators and figuring out how they work.

 

Thanks for explanations. Do you know such an indicator (that draw a line of different color along the chart)? If yes, please give me a link to it's source code to inspire from it.
whroeder1
14457
whroeder1 2015.01.26 12:52  
tenlau:  Do you know such an indicator (that draw a line of different color along the chart)? If yes, please give me a link to it's source code to inspire from it.
Top of every page is the link - Code Base
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2015.01.26 14:06  

Ok I tried to code. It is almost good except it has gaps on transition from one color to the other. Cannot understand why.

Please help with this code:

#property indicator_buffers 2
#property indicator_color1 Orange
#property indicator_color2 DodgerBlue
extern int Lb=3;
double sslup[],ssldn[],Hld,Hlv;
int dir[2];

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,sslup);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,0);
   SetIndexBuffer(1,ssldn);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   for(int i=Bars-Lb;i>=0;i--)
     {
      dir[0]=dir[1];
      if(Close[i]>iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_HIGH,i+1))
        {dir[0]=1;}
      if(Close[i]<iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_LOW,i+1))
        {dir[0]=-1;}
      if(dir[0]==-1)
         sslup[i]=iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_HIGH,i+1);
      else
        {
         if(dir[0]==1)
            ssldn[i]=iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_LOW,i+1);
        }
      dir[1]=dir[0];
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
whroeder1
14457
whroeder1 2015.01.26 17:24  
tenlau: it has gaps on transition from one color to the other. Cannot understand why.
At a change in color, both buffers must have the same value.
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2015.01.26 18:43  
WHRoeder:
tenlau: it has gaps on transition from one color to the other. Cannot understand why.
At a change in color, both buffers must have the same value.

Sorry, don't know how to "translate" in code your advice: "At a change in color, both buffers must have the same value."

Please show in the code I've posted. 

12
To add comments, please log in or register