2 min read

How to Find the Hammer Candlestick in Your MetaTrader Candles with Python

Learn how to detect the Hammer Candle with Python on your trading bot
Stylized man holding hammer
Hammer of Thor from Adobe Stock

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.

Hammer Candlestick Pattern

It is a single-candle pattern.

How to Code

I’ll assume that you have the following when looking to code this:

  1. A connection to MetaTrader 5 (check out this article if you don’t)
  2. The ability to retrieve data from MetaTrader (check out this article if you don’t)
  3. The ability to transform your MetaTrader data into a dataframe (check out this series to learn how)
  4. 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:

  1. Candle must be Green (i.e. trending up)
  2. The bottom wick (which I call the wick_handle ) must be greater than 1.5 x the size of the wick body
  3. 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.