How to Find the Hammer Candlestick in Your MetaTrader Candles with Python
Every time I hear about the Hammer Candlestick pattern, my mind instantly goes to the Hammer of Thor. Like a trading pattern crashing down in a giant explosion of lightning…
Anyways.
The Hammer Candlestick pattern bears very little resemblance to Mjolner, but it is a popular trading pattern and it can be incorporated into your trading bot.
In this article, I’ll show you how.
What is the Hammer Candlestick?
The hammer candlestick is thought to represent a market uptick. It typically occurs after a period of down-trending prices, and many traders use it to inform their trading.
It is a single-candle pattern.
How to Code
I’ll assume that you have the following when looking to code this:
- A connection to MetaTrader 5 (check out this article if you don’t)
- The ability to retrieve data from MetaTrader (check out this article if you don’t)
- The ability to transform your MetaTrader data into a dataframe (check out this series to learn how)
- You know how to trade and the risks associated with doing so
Pseudo-Code
Here are the rules on how we’ll calculate the pattern:
- Candle must be Green (i.e. trending up)
- The bottom wick (which I call the
wick_handle
) must be greater than 1.5 x the size of the wick body - The top wick (which I call the
wick_pommel
) must be less than 0.25 x the size of the wick body
Note. You can definitely modify these values if you feel they’re incorrect.
Code
Here’s the code:
# Function to calculate a green hammer
def check_green_hammer(dataframe):
"""
Function to calculate a green hammer
:param dataframe: Pandas Dataframe
:return: Boolean
"""
# Calculate bottom wick size
if dataframe.loc[0,'redorgreen'] == "Green":
bottom_wick = dataframe.loc[0,'open'] - dataframe.loc[0,'low']
top_wick = dataframe.loc[0,'high'] - dataframe.loc[0,'close']
body = dataframe.loc[0,'close'] - dataframe.loc[0, 'open']
wick_handle = bottom_wick/body
wick_pommel = top_wick/body
if wick_handle > 1.5 and wick_pommel < 0.25:
return True
return False
That’s it!
Taking It Further
You can definitely use this code to extend your Trading Bot. For instance, in my open-source Python Trading Bot, I pass these indicators into strategies, or pass them to our website.
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.
❤