The Candlestick Pattern Scanner is a utility class that helps Expert Advisors and trading bots to efficiently detect reversal candle patterns. These special patterns can be used as potential indications of trend-reversals, especially in conjunction with other events, indicators and signals.
MotivationMany online traders preffer to automate their trading strategies, but they don't have good, reliable scripts, to help confidently detect candle-stick pattern like stars, engulfings and consolidation. I struggled with this alot and decided to make things easier for myself and other traders, by programming in and sharing this MQL5 Metatrader script.
ProblemOne of the biggest problems in trading with EAs (Expert Advisors/ trading bots) is finding reliable signals that can actually be used in code. There are many scripts in the marketplace that do visually idendify patterns on the chart, by drawing visual aids. But very few (if any) convert these candle-patterns into usable, code-ready objects, that can easily be incorporated as part of the internal making of an algorithm. Too many scripts only show patterns on the graphs, but that is useless for the trading bots, since they need objects from which to read data, to base decisions on.
SolutionMy script identifies reversal patterns, it converts them into objects stored in an array. There is an option to customize how rigid the pattern-detection should be, to mark out only the strongest reversal signals. Visual square markings can be customized as well (color, style and thickness), for bullish and bearish patterns. I even optimized memory usage, by making sure to delete older patterns, or broken ones. Reversal patterns have many attributes and my code can read, change, and distinguish reversals based on their properties - to decide which trading actions to undertake.
- Intro
- Table of Contents
- Project Description
- How to Install and Run the Project
- How to Tweak and Configure the Scanner Script Functionality
- How to Use the Project
- Credits
- License
What are bearish reversal patterns? [RED] Bearish consolidations & engulfings, along with evening stars are price bar signals, indicating that the price will be going downwards. It will continue dropping or reverse & fall, after an uptrend.
What are bullish reversal patterns? [GREEN] Bullish patterns (consolidations, engulfings and morning stars) are a signal of rising prices. The price will be moving upwards, or reverse from downtrend to uptrend.
Reversal | STARS & HAMMERS | ENGULFINGS | CONSOLIDATIONS |
---|---|---|---|
BEARISH | Evening Star |
Bearish Engulfing |
Bearish Consolidation |
BULLISH | Morning Star |
Bullish Engulfing |
Bullish Consolidation |
Based on priority, my scripts detects the following candle-patterns:
- Consolidations are the strongest reversal signals since they hold the highest amount of trading information, spaning across 3 to 6 bars.
- Engulfings represent highly trustworthy reversal patterns, particularly if their position & timing are right. They appear more frequently in mid-to-higher timeframes.
- Stars & Hammers, are pretty reliable signals in high timeframes, if used togheter with other measurements and trading signals.
Priority | BULLISH Candle Patterns | BEARISH Candle Patterns |
---|---|---|
1 | Bullish Consolidation | Bearish Consolidation |
2 | Bullish Engulfing | Bearish Engulfing |
3 | Morning Star / Hammer | Evening Star |
Bearish patterns are encircled in a magenta color, and they indicate a downward move in price. As shown in the graph above, this is actually true. Bullish patterns appear in gold-yellow; bullish means that price will move upwards, and this appears to be the case here as well. |
Similar example, but since color is a completely customizable feature, I chose an alternative color scheme this time. Deep pink means bearish signal. The price will drop, or reverse from uptrend to downtrend. Aqua blue indicates a bullish reversal. Rising prices are expected, as the two bullish engulfings prove in this graphic. |
Step 1. Include the class file in your trading robot. The file-path (inside the "#include" preprocessor directive) might change depending on your folder structure or where you place the file:
#include <__SimonG\MS\CandlestickPatternScanner\CandlestickPatternScanner.mqh>
Step 2. Declare two global or local variables of type "CandlestickPatternScanner" and "CandlestickPattern". Note that "CandlestickPatternScanner" detects, stores and retrieves objects of type "CandlestickPattern", which are captured and saved at pointer location, as defined below:
CandlestickPatternScanner * cps;
CandlestickPattern * patternDetected;
Step 3. Initialize the CandlestickPatternScanner object. The best place for this is inside the OnInit() function.
int OnInit(){
cps = new CandlestickPatternScanner (Symbol(), PERIOD_CURRENT, 15, true);
cps.adjust_Settings_Graphic_BearishReversals (clrDeepPink, STYLE_SOLID, 4);
cps.adjust_Settings_Graphic_BullishReversals (clrAqua, STYLE_SOLID, 4);
cps.adjust_Settings_Chart_Subwindow (0, 0);
cps.adjust_Settings_Engulfings (0.75, 5);
cps.adjust_Settings_Stars (0.25, 40, 0.35);
cps.setDoDrawCandlePatternRect (true);
}
Step 4. Activate the CandlestickPatternScanner (cps), and set it up so that it extracts the most recent price reversal pattern (captured at pointer variable CandlestickPattern (patternDetected)).
This function detects the most recent reversal and stores it inside an array of predefined-size (see previous step). Even though we write this piece of code inside OnTick(), because of the internal structure of CandlestickPatternScanner class, the function is only called every PERIOD_CURRENT minutes. (5min, 15min, ... 1 Day).
This periodicity can either be your current timeframe (PERIOD_CURRENT) or any other desired chart timeframe: PERIOD_M15, PERIOD_H4, PERIOD_D (check MQL5 specs).
void OnTick(){
patternDetected = cps.updateAndReturn_Cps_onNewBar();
}
Step 5. Everything is set up. Use both objects and the most recently detected reversal pattern (bullish / bearish) in your code logic for the algorithm, to determine trading outcome.
bool validateOpenBuy (){
if(patternDetected != NULL){
if(patternDetected.getPatternType() == BULLISH_ENGULFING)
return true;
else if(patternDetected.getPatternType() == BULLISH_MORNING_STAR)
return true;
}
return false;
}
bool validateOpenSell (){
if(patternDetected != NULL){
if(patternDetected.getPatternType() == BEARISH_ENGULFING)
return true;
else if(patternDetected.getPatternType() == BEARISH_EVENING_STAR)
return true;
}
return false;
}
Step 6. Memory clean-up, optimization, object deletion. Clean-up is by default automatically performed by the CandlestickPatternScanner in two ways:
Patterns in the internal storage-array are deleted once broken. Secondly, since the array has a LIFO structure (Last-In-First-Out), whenever a new object is detected and stored, the last one (oldest one) is popped out and deleted. Closing/stopping the EA-bot eliminates the CandlestickPatternScanner objects, which removes the array and the CandlestickPattern objects inside of it:
void OnDeinit (const int reason){
delete cps;
delete patternDetected;
}
This is how you can configure your scripts.
Now that we know how to install and run the project, let's see how we can use it. I shall provide a bunch of ideas for trading robots:
Pattern | Moving Average | Trading Outcome |
---|---|---|
Bullish Pattern | Price above MA50 | Open BUY trade. |
Bearish Pattern | Price below MA50 | Open SELL trade. |
Pattern | RSI | Trading Outcome |
---|---|---|
Bullish Pattern | Overbought (RSI<30) | Open BUY trade. |
Bearish Pattern | Oversold (RSI>30) | Open SELL trade. |
Pattern | Bollinger Bands | Trading Outcome |
---|---|---|
Bullish Pattern | Price above Upper Band | Open BUY trade. |
Bearish Pattern | Price below Lower Band | Open SELL trade. |
Pattern | Ichimoku Cloud | Trading Outcome |
---|---|---|
Bullish Pattern | Green Cloud (Leading A) | Open BUY trade. |
Bearish Pattern | Red Cloud (Leading B) | Open SELL trade. |
Pattern | Price | MACD | Trading Outcome |
---|---|---|---|
Bullish Engulfing on D/W/Mo | Reach engulf. conterminous line | Value above Signal on H1/H4. | Open BUY trade. |
Bearish Engulfing on D/W/Mo | Reach engulf. conterminous line | Signal above Value on H1/H4. | Open SELL trade. |
Pattern | Price | MACD | Trading Outcome |
---|---|---|---|
Bullish Engulfing on D/W/Mo | Reach engulf. conterminous line | Value is negative and below a threshold. | Open BUY trade. |
Bearish Engulfing on D/W/Mo | Reach engulf. conterminous line | Value is posative and above a threshold. | Open SELL trade. |
I can't credit anyone directly, but this section seems appropriate because I owe special thanks to so many course & content creators, chanels, youtubers.
1. MQL4 Programming. Visit https://www.youtube.com/channel/UCIuhfiM34b2P8qv_HX_uwug/featured
2. ForexBoat Team. Check out https://www.udemy.com/course/learn-mql4/
These guys create amazing content and I have learned so much from them!
Feel free to use this project for yourself. Or to edit it, use bits of it. Do not commercialize it! My Candlestick-Pattern-Scanner project is licensed under the GNU AGPLv3 license. Check out the licence link to better understand what you can and cannot do with this code.