How to Identify the Bullish Engulfing Pattern on MetaTrader with Python
Python Trading Bot
There are a number of different patterns day traders use to identify market movements. Understanding them and then incorporating them into your trading bot can be a great way to help your algorithmic trading level up.
Even if you don’t automate the placing of trades, developing the ability to automatically detect these trading patterns can be a great way to make your trading more effective.
One common pattern traders use is the Bullish Engulfing Pattern.
What is the Bullish Engulfing Pattern?
The bullish engulfing pattern is a two-candle reversal pattern. Put simply, it is a pattern that many traders use to indicate a market reversal — in this case, indicating that the market is about to turn ‘bullish’.
Many traders use it exclusively on the daily candle, however, it can be used on any set of candlesticks. Note that results may vary. At Creative Appnologies for instance, we provide traders with Bullish Engulfing Pattern detection on the 15-minute, 20-minute, 30-minute, 1-hour, 6-hour, 12-hour, and 1-day timeframes across over 50 different symbol pairs on IC Markets, Binance, and Coinbase.
Visually, it looks like this:
Building the Code
Assumptions
I’ll be assuming the following for this article:
- You’re able to retrieve candlestick data from MetaTrader (or an exchange of your choice) and turn it into a Pandas dataframe
- Your dataframe includes at least the fields
close
,open
,high
,low
,timeframe
Pseudo Code
The classic bullish engulfing pattern must meet the following rules:
- The previous candle must be Red (downwards)
- The current candle must be Green (upwards)
- The high of the ‘Green’ candle must be higher than the high of the ‘Red’ candle
- The body of the Green candle must be greater than the body of the Red candle
To make our detection (hopefully) more effective, we then add a fifth rule which states that the Red close must be below the 20-EMA.
P.S. If you’re looking for an article on How to Build a 20-EMA, check this article out.
How to Code Using Python
Here’s the code to detect a bullish engulfing candle:
from indicators import ema_calculator
from strategies import engulfing_candle_strategy
# Function to calculate bullish engulfing pattern
def calc_bullish_engulfing(dataframe, exchange, project_settings):
"""
Function to calculate if a bullish engulfing candle has been detected
:param dataframe: Pandas dataframe of candle data
:param exchange: string
:param project_settings: JSON data object
:return: Bool
"""
# Extract the most recent candle
len_most_recent = len(dataframe) - 1
most_recent_candle = dataframe.loc[len_most_recent]
# Extract the second most recent candle
len_second_most_recent = len(dataframe) - 2
second_most_recent_candle = dataframe.loc[len_second_most_recent]
# Calculate if second most recent candle Red
if second_most_recent_candle['close'] < second_most_recent_candle['open']:
# Calculate if most recent candle green
if most_recent_candle['close'] > most_recent_candle['open']:
# Check the Green Body > Red Body
# Red Body
red_body = second_most_recent_candle['open'] - second_most_recent_candle['close']
# Green Body
green_body = most_recent_candle['close'] - second_most_recent_candle['open']
# Compare
if green_body > red_body:
# Compare Green High > Red High
if most_recent_candle['high'] > second_most_recent_candle['high']:
# Calculate the 20-EMA
ema_20 = ema_calculator.calc_ema(dataframe=dataframe, ema_size=20)
# Extract the second most recent candle from the new dataframe
ema_count = len(ema_20) - 2
ema_second_most_recent = ema_20.loc[ema_count]
# Compare the EMA and Red Low
if ema_second_most_recent['close'] < ema_second_most_recent['ema_20']:
# If plugging into a strategy such as the Engulfing Candle Strategy, send to alerting mechanism
strategy = engulfing_candle_strategy.engulfing_candle_strategy(
high=most_recent_candle['high'],
low=most_recent_candle['low'],
symbol=most_recent_candle['symbol'],
timeframe=most_recent_candle['timeframe'],
exchange=exchange,
alert_type="bullish_engulfing",
project_settings=project_settings
)
# Return true
return True
return False
Note the use of the 20-EMA function to increase the usefulness of the function.
Say Hi!
I love hearing from my readers, so feel free to reach out. It means a ton to me when you clap for my articles or drop a friendly comment — it helps me know that my content is helping.
❤