Candlestick Anatomy and Single-Bar Patterns
Ask three traders what a hammer is and you will get three answers that agree on the picture and disagree on the numbers. That is not a failure of any of them. It is what happens when a pattern is defined by a drawing. By the end of this lesson you will be able to describe any candle as six numbers, write down a definition of Doji, Hammer and Shooting Star that two people would implement identically, and see on your own chart how many bars change name when you move a threshold by a single percentage point.
The six numbers in a candle
Section titled “The six numbers in a candle”Part 4 introduced the candlestick as a chart style: a body drawn between the open and the close, with thin lines reaching out to the high and the low. That is a rendering of four numbers you already have. Every candlestick pattern in existence is arithmetic on those four numbers, and it is easier to think about once you have named the pieces.
Given the open, high, low and close of one bar, the quantities that matter are:
| Name | Definition |
|---|---|
| Range | High - Low |
| Body top | the greater of the open and the close |
| Body bottom | the lesser of the open and the close |
| Body | Body top - Body bottom |
| Upper wick | High - Body top |
| Lower wick | Body bottom - Low |
The colour of the candle carries no extra information: it is just the sign of
Close - Open, which you can already read from which end of the body the close
sits at.
One more derived quantity does most of the work in practice. The close position says where in the session’s range the bar finished, on a scale where 0 means it closed at the very low and 1 means at the very high:
Fragment — not a complete formula
BarRange = High - Low;BodyTop = Max( Open, Close );BodyBottom = Min( Open, Close );Body = BodyTop - BodyBottom;UpperWick = High - BodyTop;LowerWick = BodyBottom - Low;
// The third argument is what SafeDivide returns when the range is zero, which// happens on halted, limit-locked or untraded bars.ClosePosition = SafeDivide( Close - Low, BarRange, 0.5 );Here is the arithmetic on four invented bars, so you can see the pieces separate:
Four candles, taken apart
| Bar | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
Open | 100.00 | 101.60 | 101.00 | 100.00 |
High | 102.00 | 101.80 | 104.00 | 100.60 |
Low | 99.50 | 98.00 | 100.80 | 99.40 |
Close | 101.50 | 101.40 | 101.10 | 100.05 |
Range | 2.50 | 3.80 | 3.20 | 1.20 |
Body | 1.50 | 0.20 | 0.10 | 0.05 |
UpperWick | 0.50 | 0.20 | 2.90 | 0.55 |
LowerWick | 0.50 | 3.40 | 0.20 | 0.60 |
Body % of range | 60 | 5 | 3 | 4 |
ClosePosition | 0.80 | 0.89 | 0.09 | 0.54 |
And here are those same four bars drawn, because the arithmetic above is what a candle is and the picture below is what you will actually be looking at. The numbers and the chart come from one set of values, so they cannot disagree.
The same four bars, drawn
- Close above open
- Close below open
- Lower wick 17x the body
- Upper wick 29x the body
- Body 4% of range
Show the numbers behind this chart
| Bar | Open | High | Low | Close |
|---|---|---|---|---|
| 1 | 100 | 102 | 100 | 102 |
| 2 | 102 | 102 | 98 | 101 |
| 3 | 101 | 104 | 101 | 101 |
| 4 | 100 | 101 | 99 | 100 |
Notice that the last two rows are expressed as ratios rather than in price units. That matters. A body of 0.20 means something completely different on an instrument trading at 5 than on one trading at 500, and something different again in a quiet week than in a volatile one. Every definition in this lesson is written as a ratio, either against the bar’s own range or against recent average true range, so that it means the same thing across instruments and across time.
What a candle encodes about the session, and what it cannot
Section titled “What a candle encodes about the session, and what it cannot”The story attached to candlestick patterns is a story about the session. A long lower wick is said to show that sellers pushed the price down and were overwhelmed; a tiny body is said to show indecision. It is a plausible story, and it is worth understanding because it is why the vocabulary exists.
It is also, strictly, unsupported by the bar itself. Part 2 made the point that a bar throws away the order in which prices occurred: the same open, high, low and close are consistent with a session that fell then recovered, and with one that rose, collapsed and recovered twice. The candle records the four extremes. It does not record the path, and the psychological story is a claim about the path.
Check your opens before you trust any of this
Section titled “Check your opens before you trust any of this”Every pattern in this part depends on the open. Of the four prices, it is the one most likely to be wrong or synthetic in a free data set, and a wrong open does not look wrong — it just quietly produces the wrong pattern count.
The Doji is defined as a bar that opened and closed at the same price. Taken literally, on real data with real tick sizes, this almost never happens, and on instruments with fine tick sizes it may never happen in your whole database.
So every practical definition replaces “the same” with “close enough”, and the threshold is a free parameter:
Fragment — not a complete formula
// Body no larger than DojiBodyPct percent of the bar's own range.BodyPct = 100 * SafeDivide( Body, BarRange, 0 );IsDoji = BodyPct <= DojiBodyPct;Set DojiBodyPct to 1 and you will find a handful of bars per decade. Set it to 10
and you will find hundreds. Nothing about the market changed between those two runs.
There is a second decision hiding in the Doji, and it is the more important one. A body of 5% of range on a bar whose range is one tick is not a Doji in any meaningful sense — it is a bar that barely traded. Guarding against that needs a second condition on the absolute size of the range, expressed against recent volatility rather than in price units:
Fragment — not a complete formula
// Ignore bars whose whole range is small next to recent average true range.Measurable = BarRange > 0.25 * ATR( 14 ) AND Volume > 0;IsDoji = Measurable AND BodyPct <= DojiBodyPct;Hammer
Section titled “Hammer”The Hammer is described as a small body near the top of the bar, a long lower wick, and little or no upper wick. Turning that into arithmetic needs three thresholds:
Fragment — not a complete formula
IsHammer = Measurable AND LowerWick >= WickRatio * Body // "long" lower wick AND UpperWickPct <= OppositeWickPct // "little or no" upper wick AND BodyPct <= MaxBodyPct; // "small" bodyWickRatio is usually quoted as 2 or 3. OppositeWickPct is rarely quoted at all —
most sources just say “little or no upper shadow” and leave you to pick a number.
MaxBodyPct is likewise implied rather than stated. Three unstated parameters in a
pattern that is presented as a single, well-known object.
And there is a fourth condition that most descriptions do state, in words: a hammer occurs after a decline. That is not a property of the bar at all. It is a property of the bars before it, and it needs its own definition — how far back, how much of a decline, measured how. The fourth lesson in this part is about what happens when that condition is added after the fact rather than before.
One shape, several names
Section titled “One shape, several names”Take the hammer’s shape and flip it vertically: small body near the bottom of the bar, long upper wick, little lower wick. That shape has two common names. After a rise it is called a Shooting Star. After a decline it is called an Inverted Hammer. Same geometry, opposite implied outcome.
The hammer shape itself has the same problem: appearing after a rise rather than a decline, it is usually called a Hanging Man, and the implied outcome flips.
This is worth sitting with, because it tells you something about how the vocabulary is built. The name bundles three separate things: a geometry, a prior-trend condition, and an expected outcome. Only the first is a property of the bar. The second is a definition you have to supply. The third is the claim being made — which means the name has the conclusion built into it. Once you notice that, you can no longer test “does the hammer work?” without first deciding, on your own, which of the three you are testing.
Why definitions vary between sources
Section titled “Why definitions vary between sources”Collect five descriptions of the hammer and you will find them differing on at least these points:
- whether the lower wick must be twice or three times the body
- whether an upper wick is allowed at all, and how much
- whether the body must be in the upper third, upper half, or merely “near the top”
- whether the body colour matters
- how far back the prior decline is measured, and how much of a decline counts
- whether the following bar must confirm, and how
Six binary or numeric choices give a large family of patterns wearing one name. That is why two people can run “the same” test on the same data and get different counts, and why a published result you cannot reproduce is more often a definition mismatch than a mistake.
The remedy is unglamorous and completely effective: write the definition down as arithmetic, with every number visible, before you look at any results.
Making a definition precise
Section titled “Making a definition precise”A definition is precise enough when someone else could implement it from your description alone and get the same count. In practice that means answering six questions in writing:
- What geometric conditions must hold? Stated as ratios, not adjectives.
- What counts as a measurable bar? Minimum range, minimum volume, minimum price.
- What prior context is required? Stated as a rule over previous bars, with its own numbers.
- What is the reference scale? The bar’s own range, recent ATR, or a fixed price amount.
- On which bar is the pattern true? The bar itself, or the bar that confirms it.
- What are the tie-breaking rules? Does “greater than” include equal?
Answer those and you have something testable. Leave any of them implicit and you have an argument waiting to happen.
Seeing it on your own chart
Section titled “Seeing it on your own chart”Build a chart that reports the six numbers for the bar under the cursor, marks the three single-bar shapes using thresholds you can move, and — crucially — counts how many bars each definition catches. The count is the point. It converts an argument about definitions into an experiment you can run in ten seconds.
Complete formula
Section titled “Complete formula”Complete runnable AFL
// candle-anatomy.afl// Part 7 - Candlestick Anatomy and Single-Bar Patterns//// Measures every candle in numbers instead of by eye, and marks the three// single-bar shapes the lesson defines: Doji, Hammer and Shooting Star.//// Assumptions you are agreeing to by running this:// - The Open in your database is a genuine opening print. On some data sets// it is not, and every shape below depends on it. The lesson shows how to// check before you trust it.// - Every threshold here is a choice, not a fact. Each one is a Param() so// you can watch how many bars are gained or lost when you move it.// - This formula labels shapes. It does not claim they lead anywhere.
_SECTION_BEGIN("Candle anatomy");
// Cum() no longer forces AmiBroker to process every bar (that changed in// version 5.30), so a running count would otherwise depend on how much history// the chart happened to compute. -2 means "all bars", which makes the counts// below reproducible.SetBarsRequired( -2, -2 );
// ---------------------------------------------------------------- thresholds// These four numbers ARE the pattern definitions. Anyone who quotes a different// number is describing a different pattern under the same name.DojiBodyPct = Param("Doji: body as % of range", 5, 1, 25, 1 );WickRatio = Param("Hammer: long wick / body, at least", 2, 1, 6, 0.5 );OppositeWickPct = Param("Hammer: opposite wick, max % of range", 15, 2, 50, 1 );MaxBodyPct = Param("Hammer: body, max % of range", 35, 10, 60, 5 );MinRangeATR = Param("Ignore bars with range below x ATR", 0.25, 0, 2, 0.05 );
// ------------------------------------------------------------- measurements// Six numbers describe any candle completely. Everything else in the// candlestick vocabulary is arithmetic on these.BarRange = High - Low;BodyTop = Max( Open, Close );BodyBottom = Min( Open, Close );Body = BodyTop - BodyBottom;UpperWick = High - BodyTop;LowerWick = BodyBottom - Low;
// Where the close sits inside the bar: 0 means it closed on the low of the// session, 1 means on the high. SafeDivide returns the third argument when the// range is zero, which happens on limit-locked, halted or untraded bars.ClosePosition = SafeDivide( Close - Low, BarRange, 0.5 );
// Expressing the parts as a percentage of the bar's own range keeps the// definitions meaningful when the price level or the instrument changes.BodyPct = 100 * SafeDivide( Body, BarRange, 0 );UpperWickPct = 100 * SafeDivide( UpperWick, BarRange, 0 );LowerWickPct = 100 * SafeDivide( LowerWick, BarRange, 0 );
// A bar whose entire range is tiny next to recent volatility makes all of the// ratios above unstable: a one-tick body inside a two-tick range is 50% body.// Excluding those bars is more honest than calling them patterns.Measurable = BarRange > MinRangeATR * ATR( 14 ) AND Volume > 0;
// ------------------------------------------------------------------- shapesIsDoji = Measurable AND BodyPct <= DojiBodyPct;
IsHammer = Measurable AND LowerWick >= WickRatio * Body AND UpperWickPct <= OppositeWickPct AND BodyPct <= MaxBodyPct;
IsShootingStar = Measurable AND UpperWick >= WickRatio * Body AND LowerWickPct <= OppositeWickPct AND BodyPct <= MaxBodyPct;
// ------------------------------------------------------------------ display// SetBarFillColor must be called BEFORE the Plot() it applies to.SetBarFillColor( IIf( Close > Open, colorPaleGreen, colorRose ) );Plot( Close, "Price", colorDefault, styleCandle );
PlotShapes( IIf( IsDoji, shapeSmallCircle, shapeNone ), colorBlue, 0, High, 16 );PlotShapes( IIf( IsHammer, shapeUpArrow, shapeNone ), colorGreen, 0, Low, -16 );PlotShapes( IIf( IsShootingStar, shapeDownArrow, shapeNone ), colorRed, 0, High, 16 );
// The running counts are the point of the exercise: move a threshold, re-apply,// and see how many bars change name.Title = Name() + " " + Date() + " " + "body " + NumToStr( BodyPct, 1.0 ) + "% " + "upper wick " + NumToStr( UpperWickPct, 1.0 ) + "% " + "lower wick " + NumToStr( LowerWickPct, 1.0 ) + "% " + "close at " + NumToStr( 100 * ClosePosition, 1.0 ) + "% of range" + "\nBars so far - Doji: " + NumToStr( Cum( IsDoji ), 1.0 ) + " Hammer: " + NumToStr( Cum( IsHammer ), 1.0 ) + " Shooting star: " + NumToStr( Cum( IsShootingStar ), 1.0 ) + " Measurable bars: " + NumToStr( Cum( Measurable ), 1.0 );
_SECTION_END();How it works
Section titled “How it works”The formula has four sections. The first declares every threshold as a Param(),
which puts them all in the chart’s Parameters dialog where you can drag them and
watch the chart respond. The second computes the six measurements plus the three
ratio forms. The third states the three shape definitions, each as a plain
conjunction of conditions. The fourth draws: candles with the body filled by
direction, a marker per shape, and a title carrying both the current bar’s numbers
and the running counts.
The Measurable condition deserves attention because it appears in all three
definitions. Without it, a stretch of thinly traded bars can generate dozens of
“patterns” that are really just rounding.
Key functions
Section titled “Key functions”Max()andMin()take two arrays and return the larger or smaller value bar by bar. That is how the body top and bottom are found without anifstatement.SafeDivide( x, y, valueifzerodiv )returns the third argument when the divisor is zero, which is exactly the behaviour needed on a bar where the high equals the low.ATR( period )gives recent average true range, used here only as a scale so that “small” means something comparable across instruments.SetBarFillColor()sets the interior colour of the candle bodies. The official reference is explicit that it must be called before thePlot()it applies to.PlotShapes()draws a marker per bar. The fourth argument anchors the marker to a price array, and the fifth shifts it in screen pixels: positive is up.Cum()accumulates a running total from the first computed bar, soCum( IsDoji )is the number of Doji bars so far.
Expected result
Section titled “Expected result”Test it
Section titled “Test it”Open the Parameters dialog and take the Doji threshold from 5% to 6%. The count should rise, and it should rise by more than you expect. Then take it to 4% and watch it fall. Record the three counts at 3%, 5% and 10% — you now have a small piece of evidence about how sensitive your conclusions are to a number nobody ever tells you.
For a correctness check rather than a sensitivity check, pick one marked bar, hover over it, and verify by hand from the title numbers that it satisfies the definition you set. If it does not, the definition in the formula is not the definition in your head.
Common errors
Section titled “Common errors”- Every bar is marked. The thresholds are too loose, or
MinRangeATRis at zero and thin bars are qualifying. Raise the range floor first. - No bars are marked on an intraday chart.
ATR( 14 )on 1-minute bars is a very different quantity fromATR( 14 )on daily bars; the range floor may be excluding everything. Adjust the multiplier, not the pattern. - The counts change when you scroll or zoom.
Cum()has not forced whole-history calculation since AmiBroker 5.30, which is why the formula callsSetBarsRequired( -2, -2 ). If you removed that line, the counts follow whatever AmiBroker decided to compute. - Bodies are not filled.
SetBarFillColor()was placed afterPlot().
Extension
Section titled “Extension”Add a fourth shape of your own — for instance a marubozu, a bar with almost no wick at either end — and give it its own threshold and its own counter. Then find the threshold at which its count equals the hammer count, and consider what it means that two supposedly different patterns can be made equally common by choosing a number.
What changed
Section titled “What changed”You started with three named pictures. You now have three definitions, each a conjunction of ratio tests with explicit thresholds, and a way to see how many bars each one selects. You also have the observation that the names in the candlestick vocabulary bundle geometry, context and conclusion together — which means a test of “the hammer” is not a single test at all until you separate them.
The next lesson chains bars together, and the number of patterns you can invent stops being a curiosity and becomes the central methodological problem.
Check your understanding
Sources for this lesson
9 verified · checked 2026-08-31
- 01AFL Function Reference — Plotamibroker.com/guide/afl/plot.html2026-08-31
- 02AFL Function Reference — SetBarFillColoramibroker.com/guide/afl/setbarfillcolor.html2026-08-31
- 03AFL Function Reference — PlotShapesamibroker.com/guide/afl/plotshapes.html2026-08-31
- 04AFL Function Reference — SafeDivideamibroker.com/guide/afl/safedivide.html2026-08-31
- 05AFL Function Reference — Maxamibroker.com/guide/afl/max.html2026-08-31
- 06AFL Function Reference — Minamibroker.com/guide/afl/min.html2026-08-31
- 07AFL Function Reference — ATRamibroker.com/guide/afl/atr.html2026-08-31
- 08AFL Function Reference — Cumamibroker.com/guide/afl/cum.html2026-08-31
- 09AFL Function Reference — WriteValamibroker.com/guide/afl/writeval.html2026-08-31
Every technical claim on this page was checked against the official AmiBroker documentation on the date shown. Where the course disagrees with folklore, the source is how you can tell which one to trust.