The extension provides a new building block condition that you can use both in Quantacula Studio, on the Quantacula.com web site. Here is how the condition appears in Q-Web. Note the full range of bearish, bullish, and neutral candlestick patterns available to select from. Integrating candlestick pattern detection into your model is now a few clicks away.
If you install the extension for Quantacula Studio, you will find a new tool under the Tools menu, the Candlestick Genetic Evolver. The extension distills candlestick patterns into a text code called CandleCode. A pattern on a given bar can be expressed by one or more CandleCodes. For example, the Bullish Long White Line is a one bar pattern that is composed of two CandleCodes:
BL is the code for Bar Length, and the L following the colon means a Long bar. The BC code stands for Bar Color, and the W indicates a White (or up) bar. If these two codes occur together on the same bar, we have a Bullish Long White Line. Many candlestick patterns span multiple bars, so they are expressed using a sequence of CandleCode sets.
The Candlestick Genetic Evolver works by creating 20 completely random CandleCode patterns, and evaluates their performance in the market using the historical universe that you specify. The patterns that did the best are retained, and variations are produced by mutating some of their CandleCode genes. The poorly performing patterns are discarded, and the process starts again. Each iteration of this process is called a generation. You can stop the evolution at any point, and then explore the generated patterns. If you discover valuable patterns, you can add them to your base pattern library. They then become available for selection in the building block condition.
Let's look at the interface for the Candlestick Genetic Evolver in more detail.
The Universe drop down is where you select the historical data you will mine for patterns. The Evaluate button performs a one-pass evaluation of the patterns, reporting the number of times they were detected in the universe, and the average percentage return after the number of trading days you specify in the N Bars Return field.
There are three tabs in the tool. The Pattern Library tab shows you the standard candlestick patterns that come with the extension, as well as any you may have added yourself. The Evolver tab contains the random patterns that we will discuss next. If you click the Randomize button, a new set of random seed patterns will be generated. The Chart tab lets you look at a selected pattern on the chart. Click any pattern in the Pattern Library or Evolver tab, then go to the Chart tab and enter some symbols on the selected universe. You will see any candlestick patterns circled and annotated on the chart.
To kick off the genetic algorithm press the Start button. During each generation, the Evolver will keep the best performing patterns, kill off the weakest patterns, and mutate the top patterns to produce random variations. This process continues generation after generation until you press the Stop button.
At this point you will see the results in the Evolver tab. The resulting patterns are sorted from best to worst. You can see how many times the pattern was detected in the source data, and how many generations it survived.
Evaluate the patterns by looking at the average return, compared to average return of the overall universe, which is reported below. Also, consider the number of observations detected. If a pattern has a very high average return, but a very low number of observations, its practical value is diminished. The Evolver discards patterns that have too few observations at the start of processing each generation.
Let's look at the pattern we highlighted above on a chart. It's a five bar pattern whose first CandleCode contains GAP:-, which translates to an open below the previous bar's low. Here's how the resulting pattern looks on the chart.
If you wanted to save this pattern, you would right click its entry in the Evolver list, and select the "Add to Library" popup menu item. This produces a dialog box where you assign the pattern a name. You should keep to the convention of beginning a pattern name with Bearish, Bullish or Neutral. The extension uses this naming convention to determine how to display the pattern on the chart.
You'll now see that the Bullish Five Bar pattern is available in the Candlestick building block condition in Quantacula Studio.
The extension provides a static class called CandleMethods which contains many useful methods you can leverage in your C# coded models in Quantacula Studio. To use these methods, add the following using statement in your model:
Many of the methods allow you to refer to price components as string codes. When you come across methods that take string code parameters (for example c1, c2), you can use the following codes:
public static bool AlmostEqual(BarHistory bars, int idx, string c1, string c2) public static bool AlmostEqual(BarHistory bars, int idx, double val1, double val2)
Compares the specified price component values for near-equality. You can control the threshold used for equality testing by changing the following static variable.
public static double EQUALITY_THRESHOLD = 0.0005;
public static bool Near(BarHistory bars, int idx, string c1, string c2) public static bool Near(BarHistory bars, int idx, double val1, double val2)
Determines if the specified price components are near each other. The threshold used to determine if two values are near is controlled by this static variable.
public static double NEAR_THRESHOLD = 0.005;
public static bool ShortBody(BarHistory bars, int idx) public static bool LongBody(BarHistory bars, int idx)
Detects whether the specified bar has a short or a long body. What determines a long or short body is controlled by the following static variable.
public static double SMALL_BODY_THRESHOLD = 0.005;
public static bool Doji(BarHistory bars, int idx)
Detects doji patterns, which are bars where the open and close are nearly equal.
public static double Top(BarHistory bars, int idx) public static double Bottom(BarHistory bars, int idx) public static double MidPoint(BarHistory bars, int idx)
Provides access to the top and bottom of a candle's body, excluding the shadows. Also provides access to the midpoint of the body.
public static bool WhiteBody(BarHistory bars, int idx) public static bool BlackBody(BarHistory bars, int idx)
Detects whether a bar is considered a white candle (close > open) or a black candle (close < open).
public static bool LongUpperShadow(BarHistory bars, int idx) public static bool LongLowerShadow(BarHistory bars, int idx)
Detects if the bar has a long upper or lower shadow. The threshold is determined by the SMALL_BODY_THRESHOLD variable.
public static bool ShortRange(BarHistory bars, int idx) public static bool LongRange(BarHistory bars, int idx)
Detects bars with short ranges and long ranges. The threshold for determining this is controlled by the SMALL_BODY_THRESHOLD variable.
public static bool Engulfs(BarHistory bars, int idx, int offset = 1)
Detects whether or not the bar completely engulfs a previous bar. Use the offset parameter to specify the number of bars to look back from the current bar (idx).
public static bool GapUpOpen(BarHistory bars, int idx) public static bool GapDownOpen(BarHistory bars, int idx)
Detects whether a bar has gapped down or up at the open. A gap up at open occurs when the market opens above the previous high, and a gap down at open when it opens below the previous low.
public static bool ContainedWithinBody(BarHistory bars, int idx, int compareIdx)
Detects whether a canxdle (at idx) is completely contained within the body of another candle (at compareIdx).
public static bool ContainedWithin(BarHistory bars, int idx, int compareIdx)
Detects whether a candle (at idx) is completely contained within the full range of another candle (at compareIdx).
If you're interested in delving more into the details of the CandleCode, refer to this article, which also describes how you can create your own extensions of the CandleCode to produce new genes for the Evolver to use.