Skip to content
Level 1 · Chart ReaderLessonPart 07 · page 1 of 524 min
24Minutes
9AFL functions
9Sources
StandardRequires
AFL functions taught here9

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.

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 2 has a long lower wick and closed near its high. Bar 3 is its mirror. Bar 4 has almost no body at all. Those three sentences are the whole of single-bar candlestick analysis.
Bar1234
Open100.00101.60101.00100.00
High102.00101.80104.00100.60
Low99.5098.00100.8099.40
Close101.50101.40101.10100.05
Range2.503.803.201.20
Body1.500.200.100.05
UpperWick0.500.202.900.55
LowerWick0.503.400.200.60
Body % of range60534
ClosePosition0.800.890.090.54
Bar 2 has a long lower wick and closed near its high. Bar 3 is its mirror. Bar 4 has almost no body at all. Those three sentences are the whole of single-bar candlestick analysis. Prices in this diagram are invented for the illustration. They are not market data and nothing should be inferred from them.

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

Lower wick 17x the bodyUpper wick 29x the bodyBody 4% of range
  • 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
BarOpenHighLowClose
1100102100102
210210298101
3101104101101
410010199100
Bar 1 is an ordinary up bar: the body is most of the range. Bars 2, 3 and 4 have bodies of 0.20, 0.10 and 0.05 against ranges of 3.80, 3.20 and 1.20 - which is why the wick, not the body, is what you notice. The data in this chart is invented for the illustration. It is not market data and nothing should be inferred from it.

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;

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" body

WickRatio 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.

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.

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.

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:

  1. What geometric conditions must hold? Stated as ratios, not adjectives.
  2. What counts as a measurable bar? Minimum range, minimum volume, minimum price.
  3. What prior context is required? Stated as a rule over previous bars, with its own numbers.
  4. What is the reference scale? The bar’s own range, recent ATR, or a fixed price amount.
  5. On which bar is the pattern true? The bar itself, or the bar that confirms it.
  6. 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.

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 runnable AFL

candle-anatomy.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;
// ------------------------------------------------------------------- shapes
IsDoji = 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();

Download candle-anatomy.afl93 lines

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.

  • Max() and Min() take two arrays and return the larger or smaller value bar by bar. That is how the body top and bottom are found without an if statement.
  • 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 the Plot() 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, so Cum( IsDoji ) is the number of Doji bars so far.

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.

  • Every bar is marked. The thresholds are too loose, or MinRangeATR is 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 from ATR( 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 calls SetBarsRequired( -2, -2 ). If you removed that line, the counts follow whatever AmiBroker decided to compute.
  • Bodies are not filled. SetBarFillColor() was placed after Plot().

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.

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

Question 1. A bar has Open 50.0, High 51.0, Low 45.0, Close 50.6. What is its lower wick?
Show the answer and why

Answer: 5.0

The lower wick runs from the bottom of the body to the low. The body bottom is the lesser of open and close, which is 50.0, so the lower wick is 50.0 − 45.0 = 5.0. The 5.6 answer measures from the close instead of from the body bottom, which is the most common slip.

Question 2. Why is the Doji almost always defined with a tolerance rather than as Open == Close?
Show the answer and why

Answer: Because an exact match is far rarer than the pattern the books describe, so the literal test almost never fires

On real prices an exact open-equals-close bar is rare, and rarer still on instruments with fine tick sizes. The tolerance is what makes the definition usable — and choosing it is a free parameter that changes the count substantially.

Question 3. Which of these are properties of the bar itself rather than of the surrounding chart? Select all that apply.
Show the answer and why

Answer: The ratio of lower wick to body, The close position within the range

Wick ratios and close position are computed from one bar. The prior decline is a rule over earlier bars, and the choice of name depends on that rule — which is why the name carries a conclusion the bar cannot support on its own.

Question 4. Why are the pattern conditions written as percentages of the bar range rather than in price units?
Show the answer and why

Answer: So the same definition means the same thing across instruments and volatility regimes

A body of 0.20 is enormous on a stock trading at 2 and negligible on one trading at 2,000. Ratios against the bar range, or against recent ATR, keep a definition portable — which is the only way a count from one instrument can be compared with a count from another.

Sources for this lesson

9 verified · checked 2026-08-31

  1. 01AFL Function Reference — Plotamibroker.com/guide/afl/plot.html2026-08-31
  2. 02AFL Function Reference — SetBarFillColoramibroker.com/guide/afl/setbarfillcolor.html2026-08-31
  3. 03AFL Function Reference — PlotShapesamibroker.com/guide/afl/plotshapes.html2026-08-31
  4. 04AFL Function Reference — SafeDivideamibroker.com/guide/afl/safedivide.html2026-08-31
  5. 05AFL Function Reference — Maxamibroker.com/guide/afl/max.html2026-08-31
  6. 06AFL Function Reference — Minamibroker.com/guide/afl/min.html2026-08-31
  7. 07AFL Function Reference — ATRamibroker.com/guide/afl/atr.html2026-08-31
  8. 08AFL Function Reference — Cumamibroker.com/guide/afl/cum.html2026-08-31
  9. 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.