How to call color of indicator in EA??

 

Dears,

Here I encounter a problem when I want to call the color out of my custom indicator.

For example, I have a trendline in changing color. Green means uptrend and Red meand downtrend. When the color changes from red to green, place buy and vice versa.

The question is how to call the color or determine which color it is in the current bar in an EA? Any one could share your idea?

Appreciate for you help!!

 

what do mean by green & red, you mean bull candle = green bear candle = red ?

 
qjol:

what do mean by green & red, you mean bull candle = green bear candle = red ?


When the color of this trendline changes from red to green, that means the market is to be on uptrend and the long order is supposed to be placed;

When the color of this trendline changes from green to red, that means the market is to be on downtrend and the short order is suppoed to be placed.

The question is how can I call the color of the trendline in an EA. I want to enter the market based on the color.

Thanks...

 

You need to track back the output drawings  drawing decisions of your custom indicator: 

 - Look for the part that determines an uptrend/downtrend drawings

- Put a flag there to determine the trade decision you need. Then, use that flag for your trade.  

It's maybe possible but very unreasonable to actually read what the latest trendline color is from your chart's pixel.  

 

nah.. you can access the color of the indicator trough the use of different buffers. each buffer is a different color..


//z

 
Hi zzuegg,

There are two possibilities of what the OP's referring to as 'trendline' :

    It can be something drawn by drawing buffers - as you pointed out, or Objects. But please re-read the OP's question:  
The question is how to call the color or determine which color it is in the current bar in an EA?

You'd still need to generate buy and sell flag from buffers values to make trade based on its 'color' state.

  - A two color display requires two drawing buffers, but the drawing will be broken without a third and a fourth buffer patch. It all depends on how your coloring algorithm works. 

- So you can't simply read the indicator buffer & tell what up/down state it is now. If an Upbuffer condition is true, usually the DownBuffer is set as EMPTY_VALUE. This is a form of flag, with which we can use for trade decisions.  

- If we're using objects, when Up condition is true, we set the objects color parameter accordingly. We can throw a flag here, or we can have the EA read the : ObjectGet("some_object"+i, OBJPROP_COLOR); but again, it's much simpler & elegant IMO to throw a flag when color decisions are made. Color decision = Trade decision

 

Hi Cameofx,

Thanks a lot for your patient tutorial. Unlucky, I am a newbie for coding and not clear about the useage of flag. Here I put a simple custom indicator for case study. Please help write an EA using flag to lock the buy & sell point based on the rule as below (check for open section is enough).

If the close price>=MA18, the line is green; If the close price<MA18, the line is red.

Buy: when the line becomes green from red.

Sell: when the line becomes red from green.

Thanks in advance!!

*************************

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
extern int maperiod=18;
double duo[];
double kong[];
int init()
{
SetIndexBuffer(0,duo);
SetIndexBuffer(1,kong);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexDrawBegin(0,maperiod);
SetIndexDrawBegin(1,maperiod);
IndicatorDigits(Digits);
return(0);
}
int start()
{
double temp0,temp1;
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int i=limit; i>=0; i--)
{
duo[i]=EMPTY_VALUE;
kong[i]=EMPTY_VALUE;
temp0=iMA(NULL,0,maperiod,0,MODE_SMA,PRICE_CLOSE,i);
temp1=iMA(NULL,0,maperiod,0,MODE_SMA,PRICE_CLOSE,i+1);
if(iClose(NULL,0,i)>=iMA(NULL,0,maperiod,0,MODE_SMA,PRICE_CLOSE,i))
{duo[i]=temp0; duo[i+1]=temp1;}
else {kong[i]=temp0; kong[i+1]=temp1;}
}
return(0);
}

 

I can only help you with some pointers, you have to write it yourself. 

Next time please use the 'SRC' button to insert codes on your post.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
extern int maperiod=18;
double duo[];
double kong[];
int init()
{
SetIndexBuffer(0,duo);
SetIndexBuffer(1,kong);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexDrawBegin(0,maperiod);
SetIndexDrawBegin(1,maperiod);
IndicatorDigits(Digits);
return(0);
}
int start()
{
  double temp0,temp1;
  int limit;
  int counted_bars=IndicatorCounted();
  if(counted_bars<0) return(-1);
  if(counted_bars>0) counted_bars--;
  limit=Bars-counted_bars;

  for(int i=limit; i>=0; i--)
  { 
    duo[i]=EMPTY_VALUE;         // each buffer is initialized with empty value
    kong[i]=EMPTY_VALUE;        // this will be used for trade decision

    temp0=iMA(NULL,0,maperiod,0,MODE_SMA,PRICE_CLOSE,i);
    temp1=iMA(NULL,0,maperiod,0,MODE_SMA,PRICE_CLOSE,i+1);

    if(iClose(NULL,0,i)>=iMA(NULL,0,maperiod,0,MODE_SMA,PRICE_CLOSE,i)) 
    {
      duo[i]=temp0; duo[i+1]=temp1;     // this is how a drawing buffer connects two dots/points i.e. plot or 'colorize' a line
    }                                   // check above what color buffer duo[] uses? we will use that buffer's index in the EA's iCustom call.
    else 
    {
      kong[i]=temp0; kong[i+1]=temp1;   // see above
    }
  } 
  return(0);
}

See iCustom, to properly call a particular buffer from your EA. 

Now, the trade decision part goes something like this :

duo1 = iCustom(........,0,1);  duo2 = iCustom(...........0,2);

kong1 = iCustom(........,1,1);  kong2 = iCustom(...........1,2);  

if ( duo1 != EMPTY_VALUE  &&  duo2 != EMPTY_VALUE  &&  kong1 == EMPTY_VALUE ) { Send a Buy } 

else if ( you get the picture ... ) { Send a Sell }. 

 

Or, alternatively as I mentioned you can use an additional buffer as your flag. It's not complicated at all.

Set for example, direction[] as buffer index 2 (third buffer). And (together inside the plotting part)  set it to 0.0 for buy & 1.0 for sell.

Then in the EA :  

dir = iCustom(........,2,1);; 

if ( dir== 0.0 ) { send buy }

else if ( dir==1.0 ) { send sell }. Much cleaner. 

 
cameofx:

Hi zzuegg,

There are two possibilities of what the OP's referring to as 'trendline' :

It can be something drawn by drawing buffers - as you pointed out, or Objects. But please re-read the OP's question:

You'd still need to generate buy and sell flag from buffers values to make trade based on its 'color' state.

- A two color display requires two drawing buffers, but the drawing will be broken without a third and a fourth buffer patch. It all depends on how your coloring algorithm works.

- So you can't simply read the indicator buffer & tell what up/down state it is now. If an Upbuffer condition is true, usually the DownBuffer is set as EMPTY_VALUE. This is a form of flag, with which we can use for trade decisions.

- If we're using objects, when Up condition is true, we set the objects color parameter accordingly. We can throw a flag here, or we can have the EA read the : ObjectGet("some_object"+i, OBJPROP_COLOR); but again, it's much simpler & elegant IMO to throw a flag when color decisions are made. Color decision = Trade decision.

at least i use for the colores ma, slopes ecc. arrows instead of lines. a two color ma can be drawn with two buffers and i have not to worry about the "back painting one bar" problem. At least i my opinion using Objects as signals is bad coding. i have recoded even my auto-trendlines and auto-SR-Lines indicator so that buffers are used and not object. I simply do not see any advantage of objects (well if you want to draw more than 8 lines or whatever you have to use objects, or 2 indicators).


to the topic:

i only want to inform you that your indicator will not behave equal for long and short trades. simply because if tmp0 equals tmp1 it will draw "duo". Also keep in mind that you "redraw the last bar" so this indicator will look better on visual backtesting then in real trading.


//z

 

Thanks Cameofx and zzuegg! I will try to code an EA per your instruction.

You are so nice ^_^

 
Hi, I need for a test a EA code.I have a point correct zig zag trend indicator(show DRAW_LINE Lime and OrangeRed) in a separate window in changing color Lime to upward and OrangeRed to downward. The EA only must open Buy order by colour Lime and close by colour OrangeRed. The same in reverse for open and close sell orders. Max order open 1 buy and 1 sell. Here are the settings I need: Lots = 0.1; MaximumRisk = 0.5; (in percent) MoneyManagement = false/true; Max. Lots = 100 AUTOLOTS or manualy 2/4 or 3/5 digits Broker takeProfit = true/false; StopLost = true/false; MAGICNUMBER=20070705;changeable I hope anybody can help me with the code? Regards Chris