A hammer is one of the first candlestick names many traders learn. The usual picture is easy to remember: a small body near the top and a long lower wick.
But a picture is not enough for Python. Python needs numbers.
So this lesson asks a more useful question: how can we turn the shape of a hammer into measurable conditions?
visual pattern
→ measure the candle
→ write explicit conditions
→ scan real market data
We are still not building a trading signal. We are learning how to turn a chart idea into code.
1. Start with the Shape, Not the Name
Forget the word hammer for a moment. Describe the shape instead.
small body near the top
long lower wick
small upper wick
Those three ideas can all be measured from OHLC data.
2. Reuse the Candle Measurements
body_top = max(open_price, close_price)
body_bottom = min(open_price, close_price)
body_size = abs(
close_price - open_price
)
full_range = (
high_price - low_price
)
upper_wick = (
high_price - body_top
)
lower_wick = (
body_bottom - low_price
)
Nothing here is a pattern yet. These are only measurements.
3. Compare the Lower Wick with the Body
lower_wick_to_body = (
lower_wick / body_size
)
If Python prints 2.50,
the lower wick is 2.5 times as long as the body.
Our first explicit rule is:
lower_wick_to_body >= 2.0
4. Keep the Upper Wick Small
upper_wick_to_body = (
upper_wick / body_size
)
In this lesson:
upper_wick_to_body <= 1.0
This is deliberately simple. Later, you can make the definition stricter or looser and test the effect.
5. Make Sure the Body Is Near the Top
body_bottom_position = (
(body_bottom - low_price)
/ full_range
)
Read the value like this:
0.0
→ body begins at the Low
0.5
→ body begins halfway up the range
1.0
→ body begins at the High
Our rule requires:
body_bottom_position >= 0.60
6. Put the Three Conditions Together
is_hammer = (
lower_wick_to_body >= 2.0
and upper_wick_to_body <= 1.0
and body_bottom_position >= 0.60
)
long lower wick
AND
small upper wick
AND
body near the top
→ hammer candidate
7. Why Call It a Candidate?
Candlestick definitions are not laws of physics. Different books, traders, and software packages may use different thresholds.
And a hammer-shaped candle does not guarantee that price will rise next.
Here, candidate only means:
this candle matches
our explicit geometric rule
8. Search Recent AAPL Data
today = date.today()
start_date = (
today - timedelta(days=220)
).strftime("%Y-%m-%d")
end_date = today.strftime("%Y-%m-%d")
df = fdr.DataReader(
symbol,
start_date,
end_date,
)
df = df.tail(120)
The exact dates change when you run the script. We are practicing on recent data instead of preserving one old example forever.
9. Scan Every Candle
for date_index, row in df.iterrows():
For every row:
read OHLC
→ measure body and wicks
→ calculate ratios
→ check the three conditions
When all conditions are true:
if is_hammer:
hammer_candidates.append(...)
10. Zero Matches Is a Valid Result
If no recent candle matches the exact rule, the correct result is zero.
Do not weaken the definition just to force a pattern to appear. Accepting the output of a rule is part of learning to research.
11. Highlight the Most Recent Candidate
When matches exist:
target = hammer_candidates[-1]
[-1] means the last item, so we select the most recent candidate.
The chart is still drawn from scratch with:
ax.vlines(...)
ax.bar(...)
We only add a marker and annotation to show which candle matched.
12. Keep the Chart Memory-Safe
figure_width = 11
figure_height = 6.5
figure_dpi = 100
We continue using fixed margins instead of tight_layout():
fig.subplots_adjust(
left=0.10,
right=0.97,
top=0.90,
bottom=0.23,
)
The PNG is saved at 120 DPI and the figure is released after the chart window closes:
plt.close(fig)
13. Complete Code
Use the included Python file:
260808_alphesta_phase2_05_hammer_candlestick_python.py
It prints the rule, the number of candidates, each matching date, and the exact PNG path.
14. Make the Rule Stricter Yourself
Change:
min_lower_wick_to_body = 2.0
to:
min_lower_wick_to_body = 3.0
Run the code again.
looser rule
→ more candidates
stricter rule
→ fewer candidates
This is an important bridge from chart reading to research: once a visual idea becomes a parameter, you can test different definitions.
What You Just Learned
OHLC
→ body and wick measurements
→ ratios
→ Boolean conditions
→ pattern candidates
The definition itself is now explicit. That means it can later be challenged, changed, and tested.
What We Have Not Tested Yet
We have not required a prior downtrend. We have not checked support, volume, next-candle confirmation, or future returns.
Those questions belong to later experiments.
For now, the goal is simpler: turn a visual candlestick pattern into code you understand.
Where Do We Go Next?
The next shape is almost a mirror image: a small body near the bottom with a long upper wick.
Next, we will define a shooting star with numbers and search recent market data the same way.