Appearance
Candlestick Patterns
Marks 19 candlestick patterns: hammers, engulfing, harami, stars, soldiers, tweezers, doji.
Kind indicator · Drawn on the price pane
Try it
On the chart, press / and choose Candlestick Patterns under Trend. To change it, copy the source below into the AlphaScript editor.
Inputs
| Input | Type | Default |
|---|---|---|
| Detect trend based on | select | "SMA50" |
| Show labels | bool | true |
| Bullish color | color | "#089981" |
| Bearish color | color | "#F23645" |
| Neutral color | color | "#787B86" |
| Hammer | bool | true |
| Inverted Hammer | bool | true |
| Bullish Engulfing | bool | true |
| Bullish Harami | bool | true |
| Piercing Line | bool | true |
| Morning Star | bool | true |
| Three White Soldiers | bool | true |
| Tweezer Bottom | bool | true |
| White Marubozu | bool | true |
| Hanging Man | bool | true |
| Shooting Star | bool | true |
| Bearish Engulfing | bool | true |
| Bearish Harami | bool | true |
| Dark Cloud Cover | bool | true |
| Evening Star | bool | true |
| Three Black Crows | bool | true |
| Tweezer Top | bool | true |
| Black Marubozu | bool | true |
| Doji | bool | true |
Source
From packages/alphascript/indicators/candlepatterns.as.js.
js
indicator({ name: "Candlestick Patterns", shortTitle: "Patterns", overlay: true });
const trendRule = input.select("Detect trend based on", "SMA50", ["SMA50", "No detection"]);
const showLabels = input.bool("Show labels", true);
const bullColor = input.color("Bullish color", "#089981", { group: "Style" }), bearColor = input.color("Bearish color", "#F23645", { group: "Style" });
const neutralColor = input.color("Neutral color", "#787B86", { group: "Style" });
const o = candle.open, h = candle.high, l = candle.low, c = candle.close;
const bodyHi = ta.calc([o, c], (a, b) => math.max(a, b)), bodyLo = ta.calc([o, c], (a, b) => math.min(a, b));
const body = ta.calc([bodyHi, bodyLo], (a, b) => a - b), bodyAvg = ta.ema(body, 14), sma50 = ta.sma(c, 50);
/** @param {number} k */
const bar = (k) => {
const O = o.at(k), H = h.at(k), L = l.at(k), C = c.at(k), Bh = bodyHi.at(k), Bl = bodyLo.at(k), B = body.at(k), avg = bodyAvg.at(k), ma = sma50.at(k);
const up = H - Bh, dn = Bl - L, range = H - L;
return {
O, H, L, C, Bh, Bl, B, avg, up, dn, range, mid: B / 2 + Bl,
white: O < C, black: O > C, small: B < avg, long: B > avg,
hasUp: up > 0.05 * B, hasDn: dn > 0.05 * B, hl2: (H + L) / 2,
upTrend: trendRule === "No detection" || C > ma, downTrend: trendRule === "No detection" || C < ma,
doji: range > 0 && B <= range * 0.05,
};
};
/** @param {(b0: ReturnType<typeof bar>, b1: ReturnType<typeof bar>, b2: ReturnType<typeof bar>) => boolean} test */
const pattern = (test) => ta.calc([c, bodyAvg], () => (test(bar(0), bar(1), bar(2)) ? 1 : 0));
const TESTS = {
"Hammer": pattern((b) => b.small && b.B > 0 && b.Bl > b.hl2 && b.dn >= 2 * b.B && !b.hasUp && b.downTrend),
"Inverted Hammer": pattern((b) => b.small && b.B > 0 && b.Bh < b.hl2 && b.up >= 2 * b.B && !b.hasDn && b.downTrend),
"Bullish Engulfing": pattern((b, p) => b.downTrend && b.white && b.long && p.black && p.small && b.C >= p.O && b.O <= p.C && (b.C > p.O || b.O < p.C)),
"Bullish Harami": pattern((b, p) => p.long && p.black && p.downTrend && b.white && b.small && b.H <= p.Bh && b.L >= p.Bl),
"Piercing Line": pattern((b, p) => p.downTrend && p.black && p.long && b.white && b.O <= p.L && b.C > p.mid && b.C < p.O),
"Morning Star": pattern((b, p, q) => q.long && p.small && b.long && b.downTrend && q.black && p.Bh < q.Bl && b.white && b.Bh >= q.mid && b.Bh < q.Bh && p.Bh < b.Bl),
"Three White Soldiers": pattern((b, p, q) => [b, p, q].every((x) => x.white && x.long && x.range * 0.05 > x.up) && b.C > p.C && p.C > q.C && b.O < p.C && b.O > p.O && p.O < q.C && p.O > q.O),
"Tweezer Bottom": pattern((b, p) => p.downTrend && (!b.doji || (b.hasUp && b.hasDn)) && math.abs(b.L - p.L) <= b.avg * 0.05 && p.black && b.white && p.long),
"White Marubozu": pattern((b) => b.white && b.long && b.up <= 0.05 * b.B && b.dn <= 0.05 * b.B),
"Hanging Man": pattern((b) => b.small && b.B > 0 && b.Bl > b.hl2 && b.dn >= 2 * b.B && !b.hasUp && b.upTrend),
"Shooting Star": pattern((b) => b.small && b.B > 0 && b.Bh < b.hl2 && b.up >= 2 * b.B && !b.hasDn && b.upTrend),
"Bearish Engulfing": pattern((b, p) => b.upTrend && b.black && b.long && p.white && p.small && b.C <= p.O && b.O >= p.C && (b.C < p.O || b.O > p.C)),
"Bearish Harami": pattern((b, p) => p.long && p.white && p.upTrend && b.black && b.small && b.H <= p.Bh && b.L >= p.Bl),
"Dark Cloud Cover": pattern((b, p) => p.upTrend && p.white && p.long && b.black && b.O >= p.H && b.C < p.mid && b.C > p.O),
"Evening Star": pattern((b, p, q) => q.long && p.small && b.long && b.upTrend && q.white && p.Bl > q.Bh && b.black && b.Bl <= q.mid && b.Bl > q.Bl && p.Bl > b.Bh),
"Three Black Crows": pattern((b, p, q) => [b, p, q].every((x) => x.black && x.long && x.range * 0.05 > x.dn) && b.C < p.C && p.C < q.C && b.O > p.C && b.O < p.O && p.O > q.C && p.O < q.O),
"Tweezer Top": pattern((b, p) => p.upTrend && (!b.doji || (b.hasUp && b.hasDn)) && math.abs(b.H - p.H) <= b.avg * 0.05 && p.white && b.black && p.long),
"Black Marubozu": pattern((b) => b.black && b.long && b.up <= 0.05 * b.B && b.dn <= 0.05 * b.B),
"Doji": pattern((b) => b.doji && (b.up === b.dn || (math.abs(b.up - b.dn) / b.dn < 1 && math.abs(b.dn - b.up) / b.up < 1))),
};
if (input.bool("Hammer", true, { group: "Patterns" })) plotShape(TESTS["Hammer"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "H" : undefined });
if (input.bool("Inverted Hammer", true, { group: "Patterns" })) plotShape(TESTS["Inverted Hammer"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "IH" : undefined });
if (input.bool("Bullish Engulfing", true, { group: "Patterns" })) plotShape(TESTS["Bullish Engulfing"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "BE" : undefined });
if (input.bool("Bullish Harami", true, { group: "Patterns" })) plotShape(TESTS["Bullish Harami"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "BH" : undefined });
if (input.bool("Piercing Line", true, { group: "Patterns" })) plotShape(TESTS["Piercing Line"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "P" : undefined });
if (input.bool("Morning Star", true, { group: "Patterns" })) plotShape(TESTS["Morning Star"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "MS" : undefined });
if (input.bool("Three White Soldiers", true, { group: "Patterns" })) plotShape(TESTS["Three White Soldiers"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "3WS" : undefined });
if (input.bool("Tweezer Bottom", true, { group: "Patterns" })) plotShape(TESTS["Tweezer Bottom"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "TB" : undefined });
if (input.bool("White Marubozu", true, { group: "Patterns" })) plotShape(TESTS["White Marubozu"], { shape: "arrowUp", location: "belowBar", color: bullColor, text: showLabels ? "MW" : undefined });
if (input.bool("Hanging Man", true, { group: "Patterns" })) plotShape(TESTS["Hanging Man"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "HM" : undefined });
if (input.bool("Shooting Star", true, { group: "Patterns" })) plotShape(TESTS["Shooting Star"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "SS" : undefined });
if (input.bool("Bearish Engulfing", true, { group: "Patterns" })) plotShape(TESTS["Bearish Engulfing"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "BE" : undefined });
if (input.bool("Bearish Harami", true, { group: "Patterns" })) plotShape(TESTS["Bearish Harami"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "BH" : undefined });
if (input.bool("Dark Cloud Cover", true, { group: "Patterns" })) plotShape(TESTS["Dark Cloud Cover"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "DCC" : undefined });
if (input.bool("Evening Star", true, { group: "Patterns" })) plotShape(TESTS["Evening Star"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "ES" : undefined });
if (input.bool("Three Black Crows", true, { group: "Patterns" })) plotShape(TESTS["Three Black Crows"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "3BC" : undefined });
if (input.bool("Tweezer Top", true, { group: "Patterns" })) plotShape(TESTS["Tweezer Top"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "TT" : undefined });
if (input.bool("Black Marubozu", true, { group: "Patterns" })) plotShape(TESTS["Black Marubozu"], { shape: "arrowDown", location: "aboveBar", color: bearColor, text: showLabels ? "MB" : undefined });
if (input.bool("Doji", true, { group: "Patterns" })) plotShape(TESTS["Doji"], { shape: "circle", location: "aboveBar", color: neutralColor, text: showLabels ? "D" : undefined });