Libraries: MQL5 Wizard - Candlestick Patterns Class

 

MQL5 Wizard - Candlestick Patterns Class:

The MQL5 Wizard allows creating ready-made Expert Advisors based on the Standard library classes delivered together with the client terminal.  It allows to check your trade ideas quickly, all you need is to create your own trading signals class. The structure of this class and example can be found in the article MQL5 Wizard: How to Create a Module of Trading Signals.

The generic idea is the following: the class of trading signals is derived from CExpertSignal, the next, it's necessary to override the LongCondition() and ShortCondition() virtual methods with your own methods.

There is a book "Strategies of best traders" (in Russian), there are many trading strategies are considered there, we will focus on reversal candlestick patterns, confirmed by Stochastic, CCI, MFI and RSI oscillators.

The best way is to create the separate class, derived from CExpertSignal for checking of formation of candlestick patterns. For confirmation of trade signals, generated by candlestick patterns, it's sufficient to write the class, derived from CCandlePattern and add the necessary features (for example, confirmation by oscillators) there.

The class can be used for creation of trade signal classes with reversal candlestick patterns.

To reduce false signals, the reversal candlestick patterns must be confirmed by other indicators, such as oscillators.

The following patterns are considered in the "Strategies of best traders" book:

  • 3 Black Crows/3 White Soldiers
  • Dark Cloud Cover/Piercing Line
  • Morning Doji/Evening Doji
  • Bearish Engulfing/Bullish Engulfing
  • Evening Star/Morning Star
  • Hammer/Hanging Man
  • Bearish Harami/Bullish Harami
  • Bearish Meeting Lines/Bullish Meeting Lines

confirmed by Stochastic, CCI, MFI and RSI oscillators.

Author: MetaQuotes Software Corp.

 
Is it necessary to always use LongCondition() and ShortCondition()? What about if I would like to place a pending order?
 

Just some basic for traders -

Candlestick Basics

Candlestick charts are an effective way of visualizing price movements. There are two basic candlesticks:

  • Bullish Candle: When the close is higher than the open (usually green or white)
  • Bearish Candle: When the close is lower than the open (usually red or black)


Candlestick Parts

There are three main parts to a candlestick:

  • Upper Shadow: The vertical line between the high of the day and the close (bullish candle) or open (bearish candle)
  • Real Body: The difference between the open and close; colored portion of the candlestick
  • Lower Shadow: The vertical line between the low of the day and the open (bullish candle) or close (bearish candle)
Candlestick Patterns

The power of Candlestick Charts is with multiple candlesticks forming reversal and continuation patterns:

  • Bullish Engulfing Pattern
  • Bearish Engulfing Pattern
  • Dark Cloud Cover
  • Doji
  • Dragonfly Doji
  • Evening Star
  • Gravestone Doji
  • Hammer
  • Hanging Man
  • Harami
  • Inverted Hammer
  • Morning Star
  • Piercing Pattern
  • Shooting Star
  • Tweezer Tops & Bottoms
  • Windows



 

Real Body and Upper and Lower Shadows


The rectangular area between the opening and the close of a session of trading is called the real body. The thin lines that look like candle wicks above and below the real body are called shadows. The shadow above the real body is called the upper shadow, the top end of the upper shadow corresponding to the high of the session of trading, and the shadow below the real body is called the lower shadow, where the bottom end of the lower shadow corresponds to the low of the session of trading.

Bullish Candlestick


When discussing trading sessions based on a trading day (morning to afternoon), generally speaking the two most significant times of the trading day are the opening and the close. The opening and the close create the real body of the candlestick; hence, the most important part of a candlestick is the real body. By looking at a candlestick, a person can quickly tell whether traders were eagerly buying throughout the day (bulls were in charge for the trading day) - the candlestick is green, or whether traders were eagerly selling throughout the day (bears dominated the trading day) - the candlestick is red. By looking at the size of the real body of the candlestick, a trader can tell if the bulls were significantly in charge of the trading day (a tall green candlestick) or only moderately in charge of the trading day (a small green candlestick). Similarly, if a trader sees a large red candle, he or she can assume that the selling pressure of the bears overpowered the bulls for the day; however, if the candlestick is very small and red, then the trader can see that the bears were only slightly more powerful that day than the bulls. In summary, the real body of a candlestick can summarize the outcome of a period of trading in an easy to see way – green = bulls win the trading session, red = bears win the trading session; and the height of the candle equals the margin of victory for the bulls or the bears.


Steve Nison (1994) states that “for a [bullish] candle to have meaning, some Japanese candlestick traders believe that the real body should be at least three times as long as the previous day’s real body.” (p. 20). Roads (2008) suggests the following: “determine the area covered by the difference between the close and the open. If it’s at least 90 percent of the area covered by the difference between the high and low, you have a long white candle” (p. 76). An example of a computer charting package’s definition is: “Its Close price is higher than the Open price; Its body is longer than each shadow; Its body is longer than the average body size calculated for the specified number of preceding candles” (ThinkorSwim, 2011).


Bullish Marubozu

There are specific versions of the bullish candle. The first is a very bullish candlestick called the bullish marubozu. The rough translation of marubozu is “bald or little hair” (Rhoads, 2008, p. 74). A marubozu is bald or has little hair because a marubozu has no upper or lower shadow, or at least a very small upper and/or lower shadow. This is the most extreme form of the bullish candlestick because bulls were in charge from the opening to the close; bears were unable to push prices below the opening price and the trading session ended with bulls still buying pushing prices upward until the close.

 

Bullish Engulfing Pattern

The Bullish Engulfing Candlestick Pattern is a bullish reversal pattern, usually occuring at the bottom of a downtrend. The pattern consists of two Candlesticks:

  • Smaller Bearish Candle (Day 1)
  • Larger Bullish Candle (Day 2)

The bearish candle real body of Day 1 is usually contained within the real body of the bullish candle of Day 2.

On Day 2, the market gaps down; however, the bears do not get very far before bulls take over and push prices higher, filling in the gap down from the morning's open and pushing prices past the previous day's open.

The power of the Bullish Engulfing Pattern comes from the incredible change of sentiment from a bearish gap down in the morning, to a large bullish real body candle that closes at the highs of the day. Bears have overstayed their welcome and bulls have taken control of the market.


The chart below of the S&P 500 Depository Receipts Exchange Traded Fund (SPY) shows an example of a Bullish Engulfing Pattern occuring at the end of a downtrend:


Bullish Engulfing Buy Signal

There are three main times to buy using the Bullish Engulfing Pattern; the buy signals that are presented below are ordered from the most aggressive to most conservative:

  1. Buy at the close of Day 2 when prices rallied upwards from the gap down in the morning. A strong indication that the rally on Day 2 was significant and truly a reversal of market sentiment, is if there was a substantial increase in volume that accompanied the large move upward in price.
  2. Buy on the day after the Bullish Engulfing Pattern occurs; by waiting until the next day to buy, a trader is making sure that the bullish reversal and enthusiasm of the prior day is continuing and was not just a one day occurance like a short covering rally. In the chart above of the SPY's, a trader would likely not enter the market long on the day after the Bullish Engulfing Pattern because the market gapped down significantly and even made new lows. A trader using methodology #2, would likely wait for a more concrete buy signals such as the one presented in method #3 next.
  3. After a trader sees the Bullish Engulfing Pattern, the trader would wait for another signal, mainly a price break above the downward resistance line, before entering a buy order.

An example of what usually occurs intra-day during a Bullish Engulfing Pattern is presented next.

Intra-day Bullish Engulfing Pattern

The following 15-minute chart of the S&P 500 exchange traded fund (SPY) is of the 2-day period comprising the Bullish Engulfing Pattern example on the prior page:


  • Day 1: As is seen in the chart above, Day 1 was a down day, even closing the day at the low (bearish sentiment).
  • Day 2: The open was a gap down, very bearish sign; but the bulls appeared to have had enough because the price of the SPY's went up the rest of the day, closing near the day's highs (bullish sentiment) and higher than Day 1's high.
The Bullish Engulfing Pattern is one of the strongest candlestick reversal patterns. Its opposite is the Bearish Engulfing Pattern
 
Bearish Engulfing Pattern


The Bearish Engulfing Candlestick Pattern is a bearish reversal pattern, usually occuring at the top of an uptrend. The pattern consists of two Candlesticks:

  • Smaller Bullish Candle (Day 1)
  • Larger Bearish Candle (Day 2)


Generally, the bullish candle real body of Day 1 is contained within the real body of the bearish candle of Day 2.

The market gaps up (bullish sign) on Day 2; but, the bulls do not push very far higher before bears take over and push prices further down, not only filling in the gap down from the morning's open but also pushing prices below the previous day's open.

With the Bullish Engulfing Pattern, there is an incredible change of sentiment from the bullish gap up at the open, to the large bearish real body candle that closed at the lows of the day. Bears have successfully overtaken bulls for the day and possibly for the next few periods.

The chart below of Verizon (VZ) stock shows an example two Bearish Engulfing Patterns occuring at the end of uptrends:


Bearish Engulfing Sell Signal

Three methodologies for selling using the Bearish Engulfing Pattern are listed below in order of most aggressive to most conservative:

  1. Sell at the close of Day 2. An even stronger indication to sell is given when there is a substantial increase in volume that accompanies the large move downward in price.
  2. Sell on the day after the Bearish Engulfing Pattern occurs; by waiting until the next day to sell, a trader is making sure that the bearish reversal pattern is for real and was not just a one day occurance. In the chart above of Verizon, a trader would probably entered on the day after the Bearish Engulfing Pattern because the selling continued.
  3. Usually trader's wait for other signals, such as a price break below the upward support line, before entering a sell order. However, in the case of Verizon above, the Bearish Engulfing Pattern occured at the same time as the trendline break below support.

An example of what usually occurs intra-day during a Bearish Engulfing Pattern is presented next.

Intra-day Bearish Engulfing Pattern

The following 15-minute chart of Verizon (VZ) is of the 2-day period comprising the Bearish Engulfing Pattern example on the prior page:


  • Day 1: As is seen in the chart above, Day 1 was an up day, closing near the day's high (bullish sentiment).
  • Day 2: The open was a gap up, a very bullish sign; nevertheless, the bulls ran out of buying pressure and prices fell the rest of the day, closing near the day's lows (bearish sentiment) and lower than Day 1's lows.
The Bearish Engulfing Pattern is one of the strongest candlestick reversal patterns. Its opposite is the Bullish Engulfing Pattern
 

Dark Cloud Cover

Dark Cloud Cover is a bearish candlestick reversal pattern, similar to the Bearish Engulfing Pattern (see: Bearish Engulfing Pattern). There are two components of a Dark Cloud Cover formation:

  • Bullish Candle (Day 1)
  • Bearish Candle (Day 2)



A Dark Cloud Cover Pattern occurs when a bearish candle on Day 2 closes below the middle of Day 1's candle.

In addition, price gaps up on Day 2 only to fill the gap and close significantly into the gains made by Day 1's bullish candlestick.

The rejection of the gap up is a bearish sign in and of itself, but the retracement into the gains of the previous day's gains adds even more bearish sentiment. Bulls are unable to hold prices higher, demand is unable to keep up with the building supply.

Dark Cloud Cover Candlestick Chart Example

The chart below of Boeing (BA) stock illustrates an example of the Dark Cloud Cover Pattern:


Dark Cloud Cover Sell Signal

Traders usually suggest not selling exactly when one sees the Dark Cloud Cover Pattern (Day 1 & Day 2) until other confirming signals are given such as a break of an upward trendline or other technical indicators. One reason for waiting for confirmation is that the Dark Cloud Cover Pattern is a bearish pattern, but not as bearish as it could be: part of the gains from Day 1 have still been preserved.

A more bearish reversal pattern is the Bearish Engulfing Pattern that completely rejects the gains of Day 1 and usually closes below the lows of Day 1.

 

Dragonfly Doji

The Dragonfly Doji is a significant bullish reversal candlestick pattern that mainly occurs at the bottom of downtrends.


The Dragonfly Doji is created when the open, high, and close are the same or about the same price (Where the open, high, and close are exactly the same price is quite rare). The most important part of the Dragonfly Doji is the long lower shadow.

The long lower shadow implies that the market tested to find where demand was located and found it. Bears were able to press prices downward, but an area of support was found at the low of the day and buying pressure was able to push prices back up to the opening price. Thus, the bearish advance downward was entirely rejected by the bulls.

Dragonfly Doji Candlestick Chart Example

The chart below of the mini-Dow Futures contract illustrates a Dragonfly Doji occuring at the bottom of a downtrend:


In the chart above of the mini-Dow, the market began the day testing to find where demand would enter the market. The mini-Dow eventually found support at the low of the day, so much support and subsequent buying pressure, that prices were able to close the day approximately where they started the day.

The Dragonfly Doji is an extremely helpful Candlestick pattern to help traders visually see where support and demand is located. After a downtrend, the Dragonfly Doji can signal to traders that the downtrend could be over and that short positions should probably be covered. Other indicators should be used in conjunction with the Dragonfly Doji pattern to determine buy signals, for example, a break of a downward trendline.

The bearish version of the Dragonfly Doji is the Gravestone Doji
 

Evening Star

The Evening Star Pattern is a bearish reversal pattern, usually occuring at the top of an uptrend. The pattern consists of three candlesticks:

  • Large Bullish Candle (Day 1)
  • Small Bullish or Bearish Candle (Day 2)
  • Large Bearish Candle (Day 3)


The first part of an Evening Star reversal pattern is a large bullish green candle. On the first day, bulls are definitely in charge, usually new highs were made.

The second day begins with a bullish gap up. It is clear from the opening of Day 2 that bulls are in control. However, bulls do not push prices much higher. The candlestick on Day 2 is quite small and can be bullish, bearish, or neutral.

Generally speaking, a bearish candle on Day 2 is a stronger sign of an impending reversal. But it is Day 3 that is the most significant candlestick.

Day 3 begins with a gap down, (a bearish signal) and bears are able to press prices even further downward, often eliminating the gains seen on Day 1.

Evening Star Candlestick Chart Example

The chart below of Exxon-Mobil (XOM) stock shows an example a Evening Star bearish reversal pattern that occured at the end of an uptrend:


Day 1 of the Evening Star pattern for Exxon-Mobil (XOM) stock above was a strong bullish candle, in fact it was so strong that the close was the same as the high (very bullish sign). Day 2 continued Day 1's bullish sentiment by gapping up. However, Day 2 was a Doji, which is a candlestick signifying indecision. Bulls were unable to continue the large rally of the previous day; they were only able to close slightly higher than the open.

Day 3 began with a bearish gap down. In fact, bears took hold of Exxon-Mobil stock the entire day, the open was the same as the high and the close was the same as the low (a sign of very bearish sentiment). Also, Day 3 powerfully broke below the upward trendline that had served as support for XOM for the past week. Both the trendline break and the classic Evening Star pattern gave traders a signal to sell short Exxon-Mobil stock.

The Evening Star pattern is a very powerful three candlestick bearish reversal pattern. The bullish equivalent of the Evening Star is the Morning Star pattern
 

Gravestone Doji

The Gravestone Doji is a significant bearish reversal candlestick pattern that mainly occurs at the top of uptrends.


The Gravestone Doji is created when the open, low, and close are the same or about the same price (Where the open, low, and close are exactly the same price is quite rare). The most important part of the Graveston Doji is the long upper shadow.

The long upper shadow is generally interpreted by technicians as meaning that the market is testing to find where supply and potential resistance is located.

The construction of the Gravestone Doji pattern occurs when bulls are able to press prices upward.

However, an area of resistance is found at the high of the day and selling pressure is able to push prices back down to the opening price. Therefore, the bullish advance upward was entirely rejected by the bears.

Gravestone Doji Example

The chart below of Altria (MO) stock illustrates a Gravestone Doji that occured at the top of an uptrend:


In the chart above of Altria (MO) stock, the market began the day testing to find where support would enter the market. Altria eventually found resistance at the high of the day, and subsequently fell back to the opening's price.

The Gravestone Doji is an extremely helpful Candlestick reversal pattern to help traders visually see where resistance and supply is likely located. After an uptrend, the Gravestone Doji can signal to traders that the uptrend could be over and that long positions should probably be exited. But other indicators should be used in conjunction with the Gravestone Doji pattern to determine an actual sell signal. A potential trigger could be a break of the upward trendline support.

The reverse of the Gravestone Doji is the bullish Dragonfly Doji
 

Hammer

The Hammer candlestick formation is a significant bullish reversal candlestick pattern that mainly occurs at the bottom of downtrends.


The Hammer formation is created when the open, high, and close are roughly the same price. Also, there is a long lower shadow, twice the length as the real body.

When the high and the close are the same, a bullish Hammer candlestick is formed and it is considered a stronger formation because the bulls were able to reject the bears completely plus the bulls were able to push price even more past the opening price.

In contrast, when the open and high are the same, this Hammer formation is considered less bullish, but nevertheless bullish. The bulls were able to counteract the bears, but were not able to bring the price back to the price at the open.

The long lower shadow of the Hammer implies that the market tested to find where support and demand was located. When the market found the area of support, the lows of the day, bulls began to push prices higher, near the opening price. Thus, the bearish advance downward was rejected by the bulls.

Hammer Candlestick Chart Example

The chart below of American International Group (AIG) stock illustrates a Hammer reversal pattern after a downtrend:


In the chart above of AIG, the market began the day testing to find where demand would enter the market. AIG's stock price eventually found support at the low of the day. In fact, there was so much support and subsequent buying pressure, that prices were able to close the day even higher than the open, a very bullish sign.

The Hammer is an extremely helpful candlestick pattern to help traders visually see where support and demand is located. After a downtrend, the Hammer can signal to traders that the downtrend could be over and that short positions should probably be covered.

However, other indicators should be used in conjunction with the Hammer candlestick pattern to determine buy signals, for example, waiting a day to see if a rally off of the Hammer formation continues or other chart indications such as a break of a downward trendline. But other previous day's clues could enter into a traders analysis. An example of these clues, in the chart above of AIG, shows three prior day's Doji's (signs of indecision) that suggested that prices could be reversing trend; in that case and for an aggressive buyer, the Hammer formation could be the trigger to go long.

The bearish version of the Hammer is the Hanging Man formation

Another similar candlestick pattern to the Hammer is the Dragonfly Doji

Reason: