Sunday, May 2, 2010

Meta-Trader - Engulfing Candles indicator

Welcome back Meta-Traders.

Indicators are an amazing thing in Meta-Trader.

Whatever conditions you dream up can be turned into an indicator that either calculates a value and paints a line or draws an arrow, or display some other type of object.

Drag the indicator from the navigator view onto a chart, and the indicator will be calculated on the chart. The indicator on the left draws a red arrow above the bar on a "bearish engulfing" candle, and a blue arrow below the bar on a "bullish engulfing" candle pattern.

Recall that bullish engulfing condition occurs when the real body of the candle exceeds or "engulfs" the prior candle.

Indicators are very easy to code. Start with somebody elses indicator file. Or you can use this indicator which has been uploaded to my Yahoo group under the name EngulfingPattern.mq4.

If you want access to my Yahoo group, drop me your e-mail via a private message via Twitter at http://twitter.com/tcxmon. Back to the coding.

First you declare an array of doubles for each indicator to be calculated. In our case, we need 2 arrays:

//---- buffers
double LowerLine[];
double UpperLine[];

Each array position simply holds the indicator's value for that bar. In the case of drawing arrows, the array position contains the approximate price value where the arrow should be drawn.

Next, in the Init() function, calls these functions to associate the array with the on-screen indicator. It also sets the style of the indicator (Line or Arrow):

//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 233);
SetIndexBuffer(0, LowerLine);

SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 234);
SetIndexBuffer(1, UpperLine);

Then inside the start function, use this code:

/* Calculate range for arrow drawing */
for(nCount=i; nCount <= i+9; nCount++) {
dAverageRange = dAverageRange + MathAbs(High[nCount]-Low[nCount]);
}
dAverageRange = dAverageRange/10;

/* Check for bearish engulfing */
if (Close[i] < Open[i] && Open[i] > MathMax(Open[i+1],Close[i+1]) && Close[i] < MathMin(Open[i+1],Close[i+1])) {

UpperLine[i] = High[i] + dAverageRange*0.3;
}

/* Check for bullish engulfing */
if (Close[i] > Open[i] && Open[i] < MathMin(Open[i+1],Close[i+1]) && Close[i] > MathMax(Open[i+1],Close[i+1])) {
LowerLine[i] = Low[i] - dAverageRange*0.3;
}

The code on the top simply calculates the 10-bar average of the range for the sole purpose of calculating how far the arrow should go above or below the price bar. The code below checks for the 3 conditions required for a bullish engulfing pattern:

Higher Close
Real Body top is above prior real body top
Real Body bottom is below prior real body bottom

I also coded my favorite EARangeBands indicator, but i'll save that for another post.

So if anybody out there is interested in have me code an indicator, just leave me a comment and i'll take a crack at it. So let me know some indicator ideas and if they have some promise, we can code them into an EA and see how it backtests.

3 comments:

  1. Hello Chris,

    Thank you for an interesting post :o) definitely posts like this are useful for people interested in details about indicator coding. However there are many websites showing how to code using mql4 and I think that providing such coding "instructions" will not be of interest to many people. (in my experience) Just some friendly constructive criticism to help you improve your site :o)

    I - in particular - would be much more interested in learning about any indicators you develop on your own and why you believe or how you think they would be helpful for traders. In particular indicators which derive important information about price like "acceleration", "speed", etc, may be very interesting.

    To sum it up, great work Chris, I hope you can keep your blog up a long time. I will come from time to time and give you a good share of comments :o)

    Best Regards,

    Daniel

    ReplyDelete
  2. Daniel-

    Thanks for the comments, I appreciate it.

    Understood the post is of limited value in making real money in the real world, but you have to start somewhere.

    Thanks again,

    Chris

    ReplyDelete