How to Identify the Bearish Engulfing Pattern on MetaTrader with Python
Python Trading Bot
How to Identify the Bearish Engulfing Pattern on MetaTrader with Python
A common trading pattern used by traders to predict market movements is the Bearish Engulfing Pattern. This is a two-candle pattern that can identify the turning of a market from bullish (rising) to bearish (falling).
In this article, I’ll show you how to use Python code to detect this pattern with an added enhancement. This can then be used in your trading bot to (hopefully) enhance its effectiveness.
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 TradeOxy 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.
Building The Code
Assumptions
I’ll be assuming the following for this code
- You’re able to retrieve candlestick data from MetaTrader (or any other exchange which supports candlesticks) and turn it into a Pandas Dataframe. If you’d like to see how to do this with MetaTrader, check out this series.
- Your dataframe includes at least the fields
close
,open
,high
,low
,timeframe
Pseudo Code
In general, the bearish engulfing pattern must meet the following conditions:
- The previous candle must be Green (upwards)
- The current candle must be Red (downwards)
- The low of the Red candle must be lower than the low of the Green candle
- The body of the Red candle must be greater than the body of the Green candle
For our Enhanced Bearish Engulfing Pattern Detection, we add a 20-EMA rule which states that the Green open must be greater than the 20-EMA.
Visually, this can be seen as:
How to Code Using Python
Here’s the code to detect a Bearish Engulfing Pattern:
from indicators import ema_calculator
from strategies import engulfing_candle_strategy
# Function to calculate bearish engulfing pattern
def calc_bearish_engulfing(dataframe, exchange, project_settings):
"""
Function to detect a bearish engulfing pattern
: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 the second most recent candle is Green
if second_most_recent_candle['close'] > second_most_recent_candle['open']:
# Calculate if most recent candle is Red
if most_recent_candle['close'] < most_recent_candle['open']:
# Check the Red Body > Red Body
# Red Body
red_body = most_recent_candle['open'] - most_recent_candle['close']
# Green Body
green_body = second_most_recent_candle['close'] - second_most_recent_candle['open']
# Compare
if red_body > green_body:
# Compare Red low and Green low
if most_recent_candle['low'] < second_most_recent_candle['low']:
# 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 20-EMA and Green Open
if ema_second_most_recent['open'] > ema_second_most_recent['ema_20']:
# Use this function if you're planning on sending it to an alert generator
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="bearish_engulfing",
project_settings=project_settings
)
return True
return False
Note that the use of the 20-EMA function means that we must pass a dataframe at least 300 rows long.
Enjoy, and don’t forget to follow me on Medium, Twitter, and LinkedIn ❤
P.S. If you’d like to see an example of a Trading Bot incorporating this detection, follow my GitHub as I build the open-source Python Trading Bot.