Japanese Candlestick Patterns [Saikix]

Saikix · study · 534 行 · 点赞 111 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Japanese Candlestick Patterns [Saikix]
// © Saikix
 
//@version=6
indicator("Japanese Candlestick Patterns [Saikix]", shorttitle="Saikix-Candlestick", overlay=true, max_boxes_count=500, max_labels_count=500, max_bars_back=300, behind_chart=false)
 
G_BASIC = "Basic settings"
show1  = input.bool(true, "1-candle patterns", group=G_BASIC, display=display.none,
     tooltip="Patterns made of one candle, such as Hammer and Shooting Star.")
show2  = input.bool(true, "2-candle patterns", group=G_BASIC, display=display.none,
     tooltip="Patterns made of two candles, such as Engulfing and Piercing.")
show3  = input.bool(true, "3+ candle patterns", group=G_BASIC, display=display.none,
     tooltip="Patterns made of three or more candles, such as Morning Star and Three White Soldiers.")
useCtx = input.bool(true, "Only at swing highs and lows", group=G_BASIC, display=display.none,
     tooltip="If selected, a reversal pattern is shown only where it makes the lowest low (bullish) or the highest high (bearish) of the lookback, and a continuation pattern only in the direction of the trend (the close before the pattern above or below the simple moving average of the lookback). The same candle is named by where it appears: Hammer at a swing low, Hanging Man at a swing high.\nIf not selected, every pattern is shown and named by the trend.")
lookN  = input.int(20, "Lookback (bars)", minval=5, maxval=200, group=G_BASIC, display=display.none,
     tooltip="Bars used for swing highs and lows, for the trend, and for Three Gaps.")
 
G_DISP = "Chart display"
showBox    = input.bool(true, "Outline the candles", group=G_DISP, display=display.none,
     tooltip="A box around the candles that make the pattern, so you can see how many candles it uses. Filled for reversal patterns, dashed for continuation patterns, dotted for doji and spinning tops.")
showName   = input.bool(true, "Show pattern names", group=G_DISP, display=display.none)
showLatest = input.bool(true, "Show latest pattern", group=G_DISP, display=display.none,
     tooltip="The latest pattern on the chart, how many bars ago it formed, and what it is.")
showForm   = input.bool(true, "Show forming pattern", group=G_DISP, display=display.none,
     tooltip="The pattern taking shape on the current bar, drawn faintly with a question mark. It can change or disappear until the bar closes. Patterns on closed bars never change.")
fadeN      = input.int(50, "Fade after (bars)", minval=0, maxval=1000, group=G_DISP, display=display.none,
     tooltip="Patterns older than this many bars are drawn faintly, so the latest ones stand out. 0 = never fade.")
 
D_HAMMER   = "Small body at the top, lower shadow at least twice the body."
D_HANGING  = "Hammer shape after a rise: small body at the top, long lower shadow."
D_TAKURI   = "A hammer whose lower shadow is at least three times the body."
D_INVHAM   = "Small body at the bottom, upper shadow at least twice the body."
D_SHOOTING = "Small body at the bottom, long upper shadow, after a rise."
D_DRAGON   = "Doji with a long lower shadow and almost no upper shadow."
D_GRAVE    = "Doji with a long upper shadow and almost no lower shadow."
D_BELT_BU  = "Long white candle that opens at its low."
D_BELT_BE  = "Long black candle that opens at its high."
D_LONG_LO  = "Lower shadow is three quarters of the candle or more."
D_LONG_UP  = "Upper shadow is three quarters of the candle or more."
D_MARU_W   = "Long white candle with almost no shadows."
D_MARU_B   = "Long black candle with almost no shadows."
D_DOJI     = "Open and close are almost equal."
D_SPIN     = "Small body with upper and lower shadows longer than the body."
D_ENG_BU   = "A long white body covers the previous black body."
D_ENG_BE   = "A long black body covers the previous white body."
D_HAR_BU   = "A small body inside the previous long black body."
D_HAR_BE   = "A small body inside the previous long white body."
D_HCR_BU   = "A doji inside the previous long black body."
D_HCR_BE   = "A doji inside the previous long white body."
D_PIERCE   = "After a long black candle, a long white candle closes above its middle."
D_DARK     = "After a long white candle, a long black candle closes below its middle."
D_DST_BU   = "A doji at or below the close of a long black candle."
D_DST_BE   = "A doji at or above the close of a long white candle."
D_TWZ_BU   = "A long black candle and a white candle with the same low."
D_TWZ_BE   = "A long white candle and a black candle with the same high."
D_KICK_BU  = "A black marubozu, then a white marubozu that gaps up."
D_KICK_BE  = "A white marubozu, then a black marubozu that gaps down."
D_CTR_BU   = "After a long black candle, a long white candle opens lower and closes at the same close."
D_CTR_BE   = "After a long white candle, a long black candle opens higher and closes at the same close."
D_SEP_BU   = "After a long black candle, a long white candle opens at the same open."
D_SEP_BE   = "After a long white candle, a long black candle opens at the same open."
D_RWIN     = "A gap up: the low is above the previous high."
D_FWIN     = "A gap down: the high is below the previous low."
D_ONNECK   = "After a long black candle, a white candle opens lower and closes at its low."
D_INNECK   = "After a long black candle, a white candle opens lower and closes just above its close."
D_THRUST   = "After a long black candle, a white candle opens lower and closes inside its body, below the middle."
D_MSTAR    = "Long black candle, a small body at or below its close, then a white candle closing above its middle."
D_MDSTAR   = "A morning star whose middle candle is a doji."
D_ESTAR    = "Long white candle, a small body at or above its close, then a black candle closing below its middle."
D_EDSTAR   = "An evening star whose middle candle is a doji."
D_BABY_BU  = "A doji that gaps below the black candle before it and the white candle after it."
D_BABY_BE  = "A doji that gaps above the white candle before it and the black candle after it."
D_SOLDIERS = "Three long white candles, each opening inside the previous body and closing near its high."
D_CROWS    = "Three long black candles, each opening inside the previous body and closing near its low."
D_IDENT    = "Three long black candles, each opening at the previous close."
D_ADVANCE  = "Three rising white candles; the last body is smaller and the upper shadows are long."
D_DELIB    = "Two long white candles, then a small white candle opening at the second close."
D_TWOCROWS = "After a long white candle, two black candles above its close; the second covers the first."
D_RIVER    = "Long black candle, a black harami with a lower low, then a small white candle below the second close."
D_TRI_BU   = "Three doji, the middle one below the other two."
D_TRI_BE   = "Three doji, the middle one above the other two."
D_TSK_UP   = "Two white candles with a gap up, then a black candle closing inside the gap."
D_TSK_DN   = "Two black candles with a gap down, then a white candle closing inside the gap."
D_SIDE_BU  = "After a gap up, two white candles with a similar open and size."
D_SIDE_BE  = "After a gap down, two white candles with a similar open and size."
D_RISE3    = "Long white candle, three small candles inside its range, then a long white candle closing higher."
D_FALL3    = "Long black candle, three small candles inside its range, then a long black candle closing lower."
D_GAPS_UP  = "The third gap up in the lookback, with no gap down."
D_GAPS_DN  = "The third gap down in the lookback, with no gap up."
D_SWALLOW  = "Two black marubozu, a black candle that gaps down, then a black candle that covers it."
 
G_ONE = "1-candle patterns"
tHammer = input.bool(true,  "Hammer / Hanging Man", group=G_ONE, display=display.none, active=show1)
tTakuri = input.bool(true,  "Takuri", group=G_ONE, display=display.none, active=show1, tooltip=D_TAKURI)
tInv    = input.bool(true,  "Inverted Hammer / Shooting Star", group=G_ONE, display=display.none, active=show1)
tDragon = input.bool(true,  "Dragonfly / Gravestone Doji", group=G_ONE, display=display.none, active=show1)
tBelt   = input.bool(true,  "Belt Hold", group=G_ONE, display=display.none, active=show1, tooltip="Long candle that opens at its low (bullish) or at its high (bearish).")
tLongSh = input.bool(false, "Long Lower / Upper Shadow", group=G_ONE, display=display.none, active=show1, tooltip="Off by default because it forms often.")
tMaru   = input.bool(false, "Marubozu", group=G_ONE, display=display.none, active=show1, tooltip="Off by default because it forms often.")
tDoji   = input.bool(false, "Doji", group=G_ONE, display=display.none, active=show1, tooltip="Off by default because it forms often.")
tSpin   = input.bool(false, "Spinning Top", group=G_ONE, display=display.none, active=show1, tooltip="Off by default because it forms often.")
 
G_TWO = "2-candle patterns"
tEngulf  = input.bool(false, "Engulfing", group=G_TWO, display=display.none, active=show2, tooltip="Off by default because it forms often.")
tPierce  = input.bool(false, "Piercing / Dark Cloud Cover", group=G_TWO, display=display.none, active=show2, tooltip="Off by default because it forms often.")
tDStar   = input.bool(false, "Doji Star", group=G_TWO, display=display.none, active=show2, tooltip="Off by default because it forms often.")
tTweezer = input.bool(true,  "Tweezer Bottom / Top", group=G_TWO, display=display.none, active=show2)
tHarami  = input.bool(false, "Harami", group=G_TWO, display=display.none, active=show2, tooltip="Off by default because it forms often.")
tHCross  = input.bool(false, "Harami Cross", group=G_TWO, display=display.none, active=show2, tooltip="Off by default because it forms often.")
tKick    = input.bool(true,  "Kicking", group=G_TWO, display=display.none, active=show2)
tCounter = input.bool(true,  "Counterattack Lines", group=G_TWO, display=display.none, active=show2, tooltip=D_CTR_BU)
tSep     = input.bool(true,  "Separating Lines", group=G_TWO, display=display.none, active=show2, tooltip=D_SEP_BU)
tWindow  = input.bool(true,  "Rising / Falling Window", group=G_TWO, display=display.none, active=show2)
tNeck    = input.bool(true,  "On Neck / In Neck / Thrusting", group=G_TWO, display=display.none, active=show2,
     tooltip="After a long black candle, a white candle opens lower and recovers only a little: to its low (On Neck), just above its close (In Neck), or below the middle of its body (Thrusting).")
 
G_THREE = "3+ candle patterns"
tStar     = input.bool(true, "Morning / Evening Star", group=G_THREE, display=display.none, active=show3)
tDStar3   = input.bool(true, "Morning / Evening Doji Star", group=G_THREE, display=display.none, active=show3)
tBaby     = input.bool(true, "Abandoned Baby", group=G_THREE, display=display.none, active=show3)
tSoldiers = input.bool(true, "Three White Soldiers / Three Black Crows", group=G_THREE, display=display.none, active=show3)
tIdent    = input.bool(true, "Identical Three Crows", group=G_THREE, display=display.none, active=show3, tooltip=D_IDENT)
tAdvance  = input.bool(true, "Advance Block", group=G_THREE, display=display.none, active=show3, tooltip=D_ADVANCE)
tDelib    = input.bool(true, "Deliberation", group=G_THREE, display=display.none, active=show3, tooltip=D_DELIB)
tTwoCrows = input.bool(true, "Upside Gap Two Crows", group=G_THREE, display=display.none, active=show3, tooltip=D_TWOCROWS)
tRiver    = input.bool(true, "Unique Three River Bottom", group=G_THREE, display=display.none, active=show3, tooltip=D_RIVER)
tTriStar  = input.bool(true, "Tri-Star", group=G_THREE, display=display.none, active=show3)
tTasuki   = input.bool(true, "Upside / Downside Tasuki Gap", group=G_THREE, display=display.none, active=show3)
tSide     = input.bool(true, "Side-by-Side White Lines", group=G_THREE, display=display.none, active=show3, tooltip="Two white candles with a similar open and size, after a gap up (bullish) or a gap down (bearish).")
tMethods  = input.bool(true, "Rising / Falling Three Methods", group=G_THREE, display=display.none, active=show3)
tGaps     = input.bool(true, "Three Gaps", group=G_THREE, display=display.none, active=show3, tooltip="The third gap in the same direction within the lookback. Three gaps up is bearish, three gaps down is bullish.")
tSwallow  = input.bool(true, "Concealing Baby Swallow", group=G_THREE, display=display.none, active=show3, tooltip=D_SWALLOW)
 
G_STYLE = "Colors and text"
colBull = input.color(#089981, "Colors", inline="col", group=G_STYLE, display=display.none, tooltip="Bullish, bearish, and indecision (doji and spinning tops).")
colBear = input.color(#F23645, "", inline="col", group=G_STYLE, display=display.none)
colNeu  = input.color(#787B86, "", inline="col", group=G_STYLE, display=display.none)
fillTr  = input.int(80, "Fill transparency", minval=0, maxval=100, group=G_STYLE, display=display.none,
     tooltip="Fill of the boxes around reversal patterns. 100 = no fill.")
txtSize = input.int(13, "Text size", minval=8, maxval=30, group=G_STYLE, display=display.none)
tblPos  = input.string("Top right", "Summary position", options=["Top right", "Top left", "Bottom right", "Bottom left"], group=G_STYLE, display=display.none)
 
G_ALERT = "Alerts"
alOn    = input.bool(true, "Alerts", group=G_ALERT, display=display.none,
     tooltip="Create the alert with \"Any alert() function call\". Sent once at bar close for each pattern shown on the chart.")
alWhich = input.string("Bullish and bearish", "Alert condition", options=["Bullish and bearish", "Bullish only", "Bearish only"], group=G_ALERT, display=display.none, active=alOn)
 
float bodyHi = math.max(open, close)
float bodyLo = math.min(open, close)
float body   = bodyHi - bodyLo
float rng    = high - low
float upSh   = high - bodyHi
float dnSh   = bodyLo - low
bool  white  = close > open
bool  black  = close < open
float sB     = ta.sma(body, 10)
float sR     = ta.sma(rng, 10)
float avgB   = nz(sB[1], body)
float avgR   = nz(sR[1], rng)
bool  isDoji = rng > 0 and body <= 0.1 * avgR and body <= 0.25 * rng
bool  isLong = not isDoji and body > avgB
bool  isSmall = not isDoji and body < avgB
float mid    = (open + close) / 2
float tol    = 0.05 * avgR
bool  maruW  = white and isLong and upSh <= 0.05 * body and dnSh <= 0.05 * body
bool  maruB  = black and isLong and upSh <= 0.05 * body and dnSh <= 0.05 * body
bool  rw     = low > high[1]
bool  fw     = high < low[1]
 
eq(float a, float b) => math.abs(a - b) <= tol
near(float a, float b) => math.abs(a - b) <= 0.2 * avgR
 
float lowN  = ta.lowest(low, lookN)
float highN = ta.highest(high, lookN)
float smaN  = ta.sma(close, lookN)
 
patLo(int k) =>
    float m = low
    for j = 1 to 4
        if j < k
            m := math.min(m, low[j])
    m
patHi(int k) =>
    float m = high
    for j = 1 to 4
        if j < k
            m := math.max(m, high[j])
    m
 
ctxOK(int d, int kind, int k) =>
    bool r = true
    if useCtx
        if kind == 0
            r := d == 1 ? patLo(k) <= lowN : patHi(k) >= highN
        else if kind == 1
            r := d == 1 ? close[k] > smaN[k] : close[k] < smaN[k]
        else if kind == 3
            r := low <= lowN or high >= highN
    r
 
bool lowSide  = useCtx ? low <= lowN : close < smaN
bool highSide = useCtx ? high >= highN : close > smaN
 
bool hamShape = not isDoji and body > 0 and dnSh >= 2 * body and upSh <= 0.1 * rng
bool invShape = not isDoji and body > 0 and upSh >= 2 * body and dnSh <= 0.1 * rng
bool dragon   = isDoji and upSh <= 0.1 * rng and dnSh >= 0.6 * rng
bool grave    = isDoji and dnSh <= 0.1 * rng and upSh >= 0.6 * rng
bool longLo   = rng > 0 and dnSh >= 0.75 * rng
bool longUp   = rng > 0 and upSh >= 0.75 * rng
bool beltBu   = white and isLong and dnSh <= 0.05 * body
bool beltBe   = black and isLong and upSh <= 0.05 * body
bool spin     = not isDoji and body < avgB and upSh >= 0.34 * rng and dnSh >= 0.34 * rng
 
bool engBu   = black[1] and white and isLong and open <= close[1] + tol and close > open[1]
bool engBe   = white[1] and black and isLong and open >= close[1] - tol and close < open[1]
bool harBu   = black[1] and isLong[1] and not isDoji and bodyHi <= open[1] and bodyLo >= close[1] - tol and body <= 0.5 * body[1]
bool harBe   = white[1] and isLong[1] and not isDoji and bodyLo >= open[1] and bodyHi <= close[1] + tol and body <= 0.5 * body[1]
bool hcrBu   = black[1] and isLong[1] and isDoji and bodyHi <= open[1] and bodyLo >= close[1] - tol
bool hcrBe   = white[1] and isLong[1] and isDoji and bodyLo >= open[1] and bodyHi <= close[1] + tol
bool pierce  = black[1] and isLong[1] and white and isLong and open <= close[1] + tol and close > mid[1] and close < open[1]
bool dark    = white[1] and isLong[1] and black and isLong and open >= close[1] - tol and close < mid[1] and close > open[1]
bool twzBu   = black[1] and isLong[1] and white and eq(low, low[1])
bool twzBe   = white[1] and isLong[1] and black and eq(high, high[1])
bool dstBu   = black[1] and isLong[1] and isDoji and bodyHi <= close[1] + tol
bool dstBe   = white[1] and isLong[1] and isDoji and bodyLo >= close[1] - tol
bool kickBu  = maruB[1] and maruW and low > high[1]
bool kickBe  = maruW[1] and maruB and high < low[1]
bool ctrBu   = black[1] and isLong[1] and white and isLong and open < close[1] - tol and eq(close, close[1])
bool ctrBe   = white[1] and isLong[1] and black and isLong and open > close[1] + tol and eq(close, close[1])
bool sepBu   = black[1] and isLong[1] and white and isLong and eq(open, open[1])
bool sepBe   = white[1] and isLong[1] and black and isLong and eq(open, open[1])
bool gapDn   = open < close[1] - tol
bool onNeck  = black[1] and isLong[1] and white and gapDn and eq(close, low[1])
bool inNeck  = black[1] and isLong[1] and white and gapDn and close >= close[1] and close <= close[1] + 0.1 * body[1]
bool thrust  = black[1] and isLong[1] and white and gapDn and close > close[1] + 0.1 * body[1] and close <= mid[1]
 
bool mStar   = black[2] and isLong[2] and isSmall[1] and body[1] <= 0.5 * body[2] and bodyHi[1] <= close[2] + tol and white and close > mid[2]
bool mDStar  = black[2] and isLong[2] and isDoji[1] and bodyHi[1] <= close[2] + tol and white and close > mid[2]
bool eStar   = white[2] and isLong[2] and isSmall[1] and body[1] <= 0.5 * body[2] and bodyLo[1] >= close[2] - tol and black and close < mid[2]
bool eDStar  = white[2] and isLong[2] and isDoji[1] and bodyLo[1] >= close[2] - tol and black and close < mid[2]
bool babyBu  = black[2] and isDoji[1] and high[1] < low[2] and white and low > high[1]
bool babyBe  = white[2] and isDoji[1] and low[1] > high[2] and black and high < low[1]
bool sold    = white[2] and white[1] and white and isLong[2] and isLong[1] and isLong and close > close[1] and close[1] > close[2] and open[1] > open[2] and open[1] <= close[2] + tol and open > open[1] and open <= close[1] + tol
bool soldiers = sold and upSh <= 0.3 * body and upSh[1] <= 0.3 * body[1] and upSh[2] <= 0.3 * body[2]
bool advBlock = sold and body < body[1] and (upSh > 0.5 * body or upSh[1] > 0.5 * body[1]) and not soldiers
bool crow    = black[2] and black[1] and black and isLong[2] and isLong[1] and isLong and close < close[1] and close[1] < close[2] and open[1] < open[2] and open[1] >= close[2] - tol and open < open[1] and open >= close[1] - tol and dnSh <= 0.3 * body and dnSh[1] <= 0.3 * body[1] and dnSh[2] <= 0.3 * body[2]
bool ident   = crow and eq(open[1], close[2]) and eq(open, close[1])
bool crows   = crow and not ident
bool delib   = white[2] and white[1] and isLong[2] and isLong[1] and close[1] > close[2] and open[1] > open[2] and open[1] <= close[2] + tol and white and isSmall and body <= 0.5 * body[1] and open >= close[1] - tol and close > close[1]
bool twoCrows = white[2] and isLong[2] and black[1] and bodyLo[1] > close[2] and black and open > open[1] and close < close[1] and close > close[2]
bool river   = black[2] and isLong[2] and black[1] and bodyHi[1] <= open[2] and bodyLo[1] >= close[2] - tol and low[1] < low[2] and white and isSmall and open > low[1] and close < close[1]
bool triBu   = isDoji[2] and isDoji[1] and isDoji and bodyHi[1] < bodyLo[2] and bodyHi[1] < bodyLo
bool triBe   = isDoji[2] and isDoji[1] and isDoji and bodyLo[1] > bodyHi[2] and bodyLo[1] > bodyHi
bool tskUp   = white[2] and white[1] and low[1] > high[2] and black and open < close[1] and open > open[1] and close < open[1] and close > high[2]
bool tskDn   = black[2] and black[1] and high[1] < low[2] and white and open > close[1] and open < open[1] and close > open[1] and close < low[2]
bool sideBu  = low[1] > high[2] and white[1] and white and near(open, open[1]) and near(body, body[1]) and low > high[2]
bool sideBe  = high[1] < low[2] and black[2] and white[1] and white and near(open, open[1]) and near(body, body[1]) and high < low[2]
inRange(int k) => not isLong[k] and bodyHi[k] <= high[4] and bodyLo[k] >= low[4]
bool ir1     = inRange(1)
bool ir2     = inRange(2)
bool ir3     = inRange(3)
bool rise3   = white[4] and isLong[4] and ir1 and ir2 and ir3 and white and isLong and close > close[4]
bool fall3   = black[4] and isLong[4] and ir1 and ir2 and ir3 and black and isLong and close < close[4]
float nRw    = math.sum(rw ? 1 : 0, lookN)
float nFw    = math.sum(fw ? 1 : 0, lookN)
bool gapsUp  = rw and nRw == 3 and nFw == 0
bool gapsDn  = fw and nFw == 3 and nRw == 0
bool swallow = maruB[3] and maruB[2] and black[1] and open[1] < close[2] and high[1] > close[2] and black and open > high[1] and close < low[1]
 
int gapSpan = 1
if gapsUp or gapsDn
    int cnt = 0
    for j = 0 to lookN - 1
        if gapsUp ? rw[j] : fw[j]
            cnt += 1
            if cnt == 3
                gapSpan := j + 2
                break
 
type Hit
    string name = na
    string lbl  = ""
    int    len  = 0
    int    kind = 0
    string def  = ""
    string why  = ""
    string also = ""
 
method add(Hit h, bool ok, string nm, string lb, int ln, int kd, string df, string why) =>
    if ok
        if na(h.name)
            h.name := nm
            h.lbl  := lb
            h.len  := ln
            h.kind := kd
            h.def  := df
            h.why  := why
        else
            h.also += (h.also == "" ? "" : ", ") + nm
    ok
 
string nTxt  = str.tostring(lookN)
string wLow  = useCtx ? "At the lowest low of the last " + nTxt + " bars." : ""
string wHigh = useCtx ? "At the highest high of the last " + nTxt + " bars." : ""
string wUp   = useCtx ? "In an uptrend." : ""
string wDn   = useCtx ? "In a downtrend." : ""
string wAny  = ""
 
bool cLo1 = ctxOK(1, 0, 1)
bool cLo2 = ctxOK(1, 0, 2)
bool cLo3 = ctxOK(1, 0, 3)
bool cLo4 = ctxOK(1, 0, 4)
bool cHi1 = ctxOK(-1, 0, 1)
bool cHi2 = ctxOK(-1, 0, 2)
bool cHi3 = ctxOK(-1, 0, 3)
bool cUp1 = ctxOK(1, 1, 1)
bool cUp2 = ctxOK(1, 1, 2)
bool cUp3 = ctxOK(1, 1, 3)
bool cUp5 = ctxOK(1, 1, 5)
bool cDn1 = ctxOK(-1, 1, 1)
bool cDn2 = ctxOK(-1, 1, 2)
bool cDn3 = ctxOK(-1, 1, 3)
bool cDn5 = ctxOK(-1, 1, 5)
bool cSw  = ctxOK(0, 3, 1)
 
Hit bu = Hit.new()
bu.add(show3 and tSwallow  and swallow and cLo4, "Concealing Baby Swallow", "Concealing\nBaby Swallow", 4, 0, D_SWALLOW, wLow)
bu.add(show3 and tGaps     and gapsDn  and cLo1, "Three Gaps Down", "Three Gaps Down", gapSpan, 0, D_GAPS_DN, wLow)
bu.add(show3 and tRiver    and river   and cLo3, "Unique Three River Bottom", "Unique Three\nRiver Bottom", 3, 0, D_RIVER, wLow)
bu.add(show3 and tBaby     and babyBu  and cLo3, "Bullish Abandoned Baby", "Abandoned Baby", 3, 0, D_BABY_BU, wLow)
bu.add(show3 and tTriStar  and triBu   and cLo3, "Bullish Tri-Star", "Tri-Star", 3, 0, D_TRI_BU, wLow)
bu.add(show3 and tDStar3   and mDStar  and cLo3, "Morning Doji Star", "Morning\nDoji Star", 3, 0, D_MDSTAR, wLow)
bu.add(show3 and tStar     and mStar   and cLo3, "Morning Star", "Morning Star", 3, 0, D_MSTAR, wLow)
bu.add(show3 and tSoldiers and soldiers and cLo3, "Three White Soldiers", "Three White\nSoldiers", 3, 0, D_SOLDIERS, wLow)
bu.add(show3 and tMethods  and rise3   and cUp5, "Rising Three Methods", "Rising Three\nMethods", 5, 1, D_RISE3, wUp)
bu.add(show3 and tTasuki   and tskUp   and cUp3, "Upside Tasuki Gap", "Upside\nTasuki Gap", 3, 1, D_TSK_UP, wUp)
bu.add(show3 and tSide     and sideBu  and cUp3, "Bullish Side-by-Side White Lines", "Side-by-Side\nWhite Lines", 3, 1, D_SIDE_BU, wUp)
bu.add(show2 and tKick     and kickBu, "Bullish Kicking", "Kicking", 2, 2, D_KICK_BU, wAny)
bu.add(show2 and tCounter  and ctrBu   and cLo2, "Bullish Counterattack", "Counterattack", 2, 0, D_CTR_BU, wLow)
bu.add(show2 and tEngulf   and engBu   and cLo2, "Bullish Engulfing", "Engulfing", 2, 0, D_ENG_BU, wLow)
bu.add(show2 and tPierce   and pierce  and cLo2, "Piercing", "Piercing", 2, 0, D_PIERCE, wLow)
bu.add(show2 and tHCross   and hcrBu   and cLo2, "Bullish Harami Cross", "Harami Cross", 2, 0, D_HCR_BU, wLow)
bu.add(show2 and tHarami   and harBu   and cLo2, "Bullish Harami", "Harami", 2, 0, D_HAR_BU, wLow)
bu.add(show2 and tDStar    and dstBu   and cLo2, "Bullish Doji Star", "Doji Star", 2, 0, D_DST_BU, wLow)
bu.add(show2 and tTweezer  and twzBu   and cLo2, "Tweezer Bottom", "Tweezer Bottom", 2, 0, D_TWZ_BU, wLow)
bu.add(show2 and tSep      and sepBu   and cUp2, "Bullish Separating Lines", "Separating Lines", 2, 1, D_SEP_BU, wUp)
bu.add(show2 and tWindow   and rw      and cUp2, "Rising Window", "Rising Window", 2, 1, D_RWIN, wUp)
bu.add(show1 and tBelt     and beltBu  and cLo1, "Bullish Belt Hold", "Belt Hold", 1, 0, D_BELT_BU, wLow)
bu.add(show1 and tTakuri   and hamShape and dnSh >= 3 * body and lowSide and cLo1, "Takuri", "Takuri", 1, 0, D_TAKURI, wLow)
bu.add(show1 and tHammer   and hamShape and lowSide and cLo1, "Hammer", "Hammer", 1, 0, D_HAMMER, wLow)
bu.add(show1 and tInv      and invShape and lowSide and cLo1, "Inverted Hammer", "Inverted Hammer", 1, 0, D_INVHAM, wLow)
bu.add(show1 and tDragon   and dragon  and cLo1, "Dragonfly Doji", "Dragonfly Doji", 1, 0, D_DRAGON, wLow)
bu.add(show1 and tLongSh   and longLo  and cLo1, "Long Lower Shadow", "Long Lower\nShadow", 1, 0, D_LONG_LO, wLow)
bu.add(show1 and tMaru     and maruW   and cUp1, "White Marubozu", "White Marubozu", 1, 1, D_MARU_W, wUp)
 
Hit be = Hit.new()
be.add(show3 and tGaps     and gapsUp  and cHi1, "Three Gaps Up", "Three Gaps Up", gapSpan, 0, D_GAPS_UP, wHigh)
be.add(show3 and tTwoCrows and twoCrows and cHi3, "Upside Gap Two Crows", "Upside Gap\nTwo Crows", 3, 0, D_TWOCROWS, wHigh)
be.add(show3 and tIdent    and ident   and cHi3, "Identical Three Crows", "Identical\nThree Crows", 3, 0, D_IDENT, wHigh)
be.add(show3 and tSoldiers and crows   and cHi3, "Three Black Crows", "Three Black\nCrows", 3, 0, D_CROWS, wHigh)
be.add(show3 and tAdvance  and advBlock and cHi3, "Advance Block", "Advance Block", 3, 0, D_ADVANCE, wHigh)
be.add(show3 and tDelib    and delib   and cHi3, "Deliberation", "Deliberation", 3, 0, D_DELIB, wHigh)
be.add(show3 and tBaby     and babyBe  and cHi3, "Bearish Abandoned Baby", "Abandoned Baby", 3, 0, D_BABY_BE, wHigh)
be.add(show3 and tTriStar  and triBe   and cHi3, "Bearish Tri-Star", "Tri-Star", 3, 0, D_TRI_BE, wHigh)
be.add(show3 and tDStar3   and eDStar  and cHi3, "Evening Doji Star", "Evening\nDoji Star", 3, 0, D_EDSTAR, wHigh)
be.add(show3 and tStar     and eStar   and cHi3, "Evening Star", "Evening Star", 3, 0, D_ESTAR, wHigh)
be.add(show3 and tMethods  and fall3   and cDn5, "Falling Three Methods", "Falling Three\nMethods", 5, 1, D_FALL3, wDn)
be.add(show3 and tTasuki   and tskDn   and cDn3, "Downside Tasuki Gap", "Downside\nTasuki Gap", 3, 1, D_TSK_DN, wDn)
be.add(show3 and tSide     and sideBe  and cDn3, "Bearish Side-by-Side White Lines", "Side-by-Side\nWhite Lines", 3, 1, D_SIDE_BE, wDn)
be.add(show2 and tKick     and kickBe, "Bearish Kicking", "Kicking", 2, 2, D_KICK_BE, wAny)
be.add(show2 and tCounter  and ctrBe   and cHi2, "Bearish Counterattack", "Counterattack", 2, 0, D_CTR_BE, wHigh)
be.add(show2 and tEngulf   and engBe   and cHi2, "Bearish Engulfing", "Engulfing", 2, 0, D_ENG_BE, wHigh)
be.add(show2 and tPierce   and dark    and cHi2, "Dark Cloud Cover", "Dark Cloud Cover", 2, 0, D_DARK, wHigh)
be.add(show2 and tHCross   and hcrBe   and cHi2, "Bearish Harami Cross", "Harami Cross", 2, 0, D_HCR_BE, wHigh)
be.add(show2 and tHarami   and harBe   and cHi2, "Bearish Harami", "Harami", 2, 0, D_HAR_BE, wHigh)
be.add(show2 and tDStar    and dstBe   and cHi2, "Bearish Doji Star", "Doji Star", 2, 0, D_DST_BE, wHigh)
be.add(show2 and tTweezer  and twzBe   and cHi2, "Tweezer Top", "Tweezer Top", 2, 0, D_TWZ_BE, wHigh)
be.add(show2 and tNeck     and onNeck  and cDn2, "On Neck", "On Neck", 2, 1, D_ONNECK, wDn)
be.add(show2 and tNeck     and inNeck  and cDn2, "In Neck", "In Neck", 2, 1, D_INNECK, wDn)
be.add(show2 and tNeck     and thrust  and cDn2, "Thrusting", "Thrusting", 2, 1, D_THRUST, wDn)
be.add(show2 and tSep      and sepBe   and cDn2, "Bearish Separating Lines", "Separating Lines", 2, 1, D_SEP_BE, wDn)
be.add(show2 and tWindow   and fw      and cDn2, "Falling Window", "Falling Window", 2, 1, D_FWIN, wDn)
be.add(show1 and tBelt     and beltBe  and cHi1, "Bearish Belt Hold", "Belt Hold", 1, 0, D_BELT_BE, wHigh)
be.add(show1 and tHammer   and hamShape and highSide and cHi1, "Hanging Man", "Hanging Man", 1, 0, D_HANGING, wHigh)
be.add(show1 and tInv      and invShape and highSide and cHi1, "Shooting Star", "Shooting Star", 1, 0, D_SHOOTING, wHigh)
be.add(show1 and tDragon   and grave   and cHi1, "Gravestone Doji", "Gravestone Doji", 1, 0, D_GRAVE, wHigh)
be.add(show1 and tLongSh   and longUp  and cHi1, "Long Upper Shadow", "Long Upper\nShadow", 1, 0, D_LONG_UP, wHigh)
be.add(show1 and tMaru     and maruB   and cDn1, "Black Marubozu", "Black Marubozu", 1, 1, D_MARU_B, wDn)
 
string wSwing = useCtx ? (low <= lowN ? wLow : wHigh) : ""
Hit ne = Hit.new()
ne.add(show1 and tDoji and isDoji and cSw, "Doji", "Doji", 1, 3, D_DOJI, wSwing)
ne.add(show1 and tSpin and spin   and cSw, "Spinning Top", "Spinning Top", 1, 3, D_SPIN, wSwing)
 
int HALF = int(timeframe.in_seconds() * 500)
 
fmtP(float p) => str.tostring(p, format.mintick)
tfLabel(string tf) =>
    int sec = timeframe.in_seconds(tf)
    sec < 3600 ? str.tostring(math.round(sec / 60.0), "#") + "m" : sec < 86400 ? str.tostring(math.round(sec / 3600.0), "#") + "h" : sec < 604800 ? "D" : sec < 2419200 ? "W" : "M"
candlesTxt(int k) => str.tostring(k) + (k == 1 ? " candle" : " candles")
 
var string lastName = na
var string lastDef  = ""
var int    lastLen  = 0
var int    lastBar  = na
var int    lastDir  = 0
 
LBL_GAP = 8
var int   luBar = na
var float luY   = na
var int   luN   = 1
var int   ldBar = na
var float ldY   = na
var int   ldN   = 1
 
MAX_KEEP = 450
var array<box>   gBox  = array.new<box>()
var array<label> gLbl  = array.new<label>()
var array<int>   gBar  = array.new<int>()
var array<int>   gKind = array.new<int>()
var array<color> gCol  = array.new<color>()
 
lineCount(string s) => str.contains(s, "\n") ? 2 : 1
 
paint(box b, label l, int kd, color c, bool fresh) =>
    bool rev = kd == 0 or kd == 2
    if not na(b)
        b.set_border_color(color.new(c, fresh ? 0 : 65))
        b.set_bgcolor(color.new(c, rev ? (fresh ? fillTr : math.min(100, fillTr + 12)) : 100))
    if not na(l)
        l.set_color(color.new(c, 100))
        l.set_textcolor(color.new(c, fresh ? 0 : 45))
 
bool forming = showForm and barstate.islast and not barstate.isconfirmed
bool drew = false
if barstate.isconfirmed or forming
    bool hasDir = not na(bu.name) or not na(be.name)
    array<Hit> hs = array.from(bu, be, ne)
    for [ix, h] in hs
        if not na(h.name) and (ix < 2 or not hasDir)
            int   k   = h.len
            int   kd  = h.kind
            float hi  = high
            float lo  = low
            if k > 1
                for j = 1 to k - 1
                    hi := math.max(hi, high[j])
                    lo := math.min(lo, low[j])
            float pad = math.max((hi - lo) * 0.1, syminfo.mintick * 2)
            int   x1  = time[k - 1] - HALF
            int   x2  = time + HALF
            color c   = ix == 0 ? colBull : ix == 1 ? colBear : colNeu
            box   b   = na
            label l   = na
            if showBox
                b := box.new(x1, hi + pad, x2, lo - pad, xloc=xloc.bar_time, border_color=c, border_width=1, border_style=forming or kd == 3 ? line.style_dotted : kd == 1 ? line.style_dashed : line.style_solid, bgcolor=color.new(c, 100))
            if showName
                string tip = (forming ? "Forming: " : "") + h.name + " (" + candlesTxt(k) + ")\n" + h.def + (h.why == "" ? "" : "\n" + h.why) + (h.also == "" ? "" : "\nAlso: " + h.also) + (forming ? "\nNot confirmed until the bar closes." : "")
                string txt = forming ? h.lbl + "?" : h.lbl
                int xm = (x1 + x2) / 2
                int nl = lineCount(h.lbl)
                if ix == 0
                    float step = avgR * (nl + luN) * 0.6
                    float y = lo - pad
                    if not na(luBar) and bar_index - luBar <= LBL_GAP and math.abs(y - luY) < step
                        y := luY - step
                    l := label.new(xm, y, txt, xloc=xloc.bar_time, style=label.style_label_up, color=color.new(c, 100), textcolor=c, size=txtSize, tooltip=tip)
                    luBar := bar_index
                    luY   := y
                    luN   := nl
                else
                    float step = avgR * (nl + ldN) * 0.6
                    float y = hi + pad
                    if not na(ldBar) and bar_index - ldBar <= LBL_GAP and math.abs(y - ldY) < step
                        y := ldY + step
                    l := label.new(xm, y, txt, xloc=xloc.bar_time, style=label.style_label_down, color=color.new(c, 100), textcolor=c, size=txtSize, tooltip=tip)
                    ldBar := bar_index
                    ldY   := y
                    ldN   := nl
            if forming
                if not na(b)
                    b.set_border_color(color.new(c, 45))
                if not na(l)
                    l.set_textcolor(color.new(c, 35))
                continue
            paint(b, l, kd, c, fadeN == 0 or last_bar_index - bar_index <= fadeN)
            gBox.push(b)
            gLbl.push(l)
            gBar.push(bar_index)
            gKind.push(kd)
            gCol.push(c)
            if gBar.size() > MAX_KEEP
                box   ob = gBox.shift()
                label ol = gLbl.shift()
                gBar.shift()
                gKind.shift()
                gCol.shift()
                if not na(ob)
                    ob.delete()
                if not na(ol)
                    ol.delete()
            drew     := true
            lastName := h.name
            lastDef  := h.def
            lastLen  := k
            lastBar  := bar_index
            lastDir  := ix == 0 ? 1 : ix == 1 ? -1 : 0
 
    if alOn and not forming
        string head = "[Saikix-Candlestick] " + syminfo.ticker + " " + tfLabel(timeframe.period) + " "
        if not na(bu.name) and alWhich != "Bearish only"
            alert(head + "Bullish: " + bu.name + " (" + candlesTxt(bu.len) + "). Close: " + fmtP(close), alert.freq_once_per_bar_close)
        if not na(be.name) and alWhich != "Bullish only"
            alert(head + "Bearish: " + be.name + " (" + candlesTxt(be.len) + "). Close: " + fmtP(close), alert.freq_once_per_bar_close)
 
if barstate.islast and barstate.isconfirmed and fadeN > 0 and gBar.size() > 0
    for i = 0 to gBar.size() - 1
        paint(gBox.get(i), gLbl.get(i), gKind.get(i), gCol.get(i), bar_index - gBar.get(i) <= fadeN)
 
var table tbl = table.new(tblPos == "Top left" ? position.top_left : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Bottom left" ? position.bottom_left : position.top_right, 2, 2, bgcolor=chart.bg_color, border_color=chart.bg_color, border_width=3)
if barstate.islast and showLatest
    table.clear(tbl, 0, 0, 1, 1)
    if na(lastName)
        table.cell(tbl, 0, 0, "No pattern yet", text_color=color.white, bgcolor=colNeu, text_size=txtSize)
    else
        color lc = lastDir == 1 ? colBull : lastDir == -1 ? colBear : colNeu
        int ago = bar_index - lastBar
        table.cell(tbl, 0, 0, lastName, text_color=color.white, bgcolor=lc, text_size=txtSize)
        table.cell(tbl, 1, 0, candlesTxt(lastLen) + " · " + (ago == 0 ? "last bar" : str.tostring(ago) + (ago == 1 ? " bar ago" : " bars ago")), text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
        table.cell(tbl, 0, 1, lastDef, text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
        table.merge_cells(tbl, 0, 1, 1, 1)

← 返回列表