本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Winchester 10X1T Double Patterns Pro
// Double top / double bottom for Condition 3 of the 10X1T method, drawn on the candles.
// The pattern engine gives exactly the same patterns, pivots, neckline breaks, invalidations, counts and alerts
// as the classic 10X1T Double Patterns study (default settings: length 10, max risk per reward 30), so every
// student sees the same entries. Written from scratch with its own swing slots instead of zigzag arrays.
// 1. Swings: a bar that prints the highest high (or lowest low) of the last N bars is a swing candidate.
// The leg flips on the first candidate of the other side. Inside a leg only a more extreme candidate moves the swing.
// 2. Every swing is ranked against the swing before it on the same side: HH, LH, HL or LL.
// 3. Double Top = a higher high, a pullback low, then a lower high that stops close to the first top.
// "Close" is the risk share: gap between the two tops / (gap between the tops + depth of the pullback), below the max.
// Double Bottom is the mirror image.
// 4. Confirmation: price crosses the neckline (the pullback swing). Invalidation: price crosses the first top (bottom).
// The panel counts the first confirmation per neckline and the first invalidation per first top, like the classic study.
// 5. Drawing: M (double top) or W (double bottom) through the three swing points, dotted legs, solid line between the tops,
// label on the second top, HH LH HL LL on the latest swings, and for the latest pattern the neckline and the first top
// as dashed measuring lines on the exact price across the chart.
// The Data Window columns keep the names the 10X1T scanner reads ("DW | ...").
indicator("Winchester 10X1T Double Patterns Pro", shorttitle="10X1T DP Pro", overlay=true, max_lines_count=500, max_labels_count=500, max_bars_back=1000)
grpE = "Pattern engine"
swLen = input.int(10, "Length (bars)", minval=2, group=grpE, tooltip="A bar is a swing candidate when it prints the highest high or the lowest low of this many bars. 10 = classic.", display=display.none)
maxRR = input.int(30, "Max risk per reward %", minval=5, maxval=100, step=5, group=grpE, tooltip="How close the second top must stop to the first one. 30 = classic.", display=display.none)
grpV = "Display"
showPat = input.bool(true, "Pattern lines (M and W)", group=grpV, display=display.none)
showLbl = input.bool(true, "Pattern labels", group=grpV, display=display.none)
showRR = input.bool(true, "Risk per reward in the label", group=grpV, display=display.none)
showEv = input.bool(true, "Confirmation and invalidation lines", group=grpV, display=display.none)
showMeas = input.bool(true, "Measuring lines (neckline and first top)", group=grpV, display=display.none)
showMTag = input.bool(true, "Measuring prices on the price scale", group=grpV, display=display.none)
showSw = input.bool(true, "Swing labels HH LH HL LL", group=grpV, display=display.none)
keepTot = input.int(10, "Swing labels (latest)", minval=2, maxval=60, group=grpV, display=display.none)
swLblSz = input.string("Small", "Swing label size", options=["Tiny", "Small", "Normal"], group=grpV, display=display.none)
showZz = input.bool(false, "Zigzag", group=grpV, display=display.none)
showTab = input.bool(true, "Panel", group=grpV, display=display.none)
tabPos = input.string("Top right", "Panel corner", options=["Top right", "Top left", "Bottom right", "Bottom left"], group=grpV, display=display.none)
tabSize = input.string("Small", "Panel text size", options=["Tiny", "Small", "Normal"], group=grpV, display=display.none)
grpS = "Style"
colDT = input.color(color.rgb(53, 208, 197), "Double top", group=grpS, display=display.none)
colDB = input.color(color.rgb(148, 250, 226), "Double bottom", group=grpS, display=display.none)
colHi = input.color(color.rgb(255, 255, 255), "Swing high labels", group=grpS, display=display.none)
colLo = input.color(color.rgb(53, 208, 197), "Swing low labels", group=grpS, display=display.none)
colTxt = input.color(color.rgb(232, 240, 239), "Panel text", group=grpS, display=display.none)
colPnl = input.color(color.rgb(20, 20, 30), "Panel and label text", group=grpS, display=display.none)
colNeu = input.color(color.rgb(130, 144, 159), "Neutral", group=grpS, display=display.none)
// light chart: very light colours are drawn darker so they stay readable
f_lum(color c) => 0.299 * color.r(c) + 0.587 * color.g(c) + 0.114 * color.b(c)
f_dark(color c, float k) => color.rgb(color.r(c) * k, color.g(c) * k, color.b(c) * k)
bool lightBg = f_lum(chart.bg_color) > 160
color cDT = lightBg and f_lum(colDT) > 150 ? f_dark(colDT, 0.6) : colDT
color cDB = lightBg and f_lum(colDB) > 150 ? f_dark(colDB, 0.55) : colDB
color cNeu = lightBg and f_lum(colNeu) > 150 ? f_dark(colNeu, 0.7) : colNeu
color cHi = lightBg and f_lum(colHi) > 150 ? f_dark(colHi, 0.45) : colHi
color cLo = lightBg and f_lum(colLo) > 150 ? f_dark(colLo, 0.6) : colLo
f_lblTxt(color c) => f_lum(c) < 140 ? color.white : colPnl
string swSz = swLblSz == "Tiny" ? size.tiny : swLblSz == "Normal" ? size.normal : size.small
int keepSw = math.max(1, keepTot - 1) // finished swings; the forming swing gets its own label = keepTot in total
var array<label> swLbls = array.new<label>()
var array<line> swLines = array.new<line>()
f_swTxt(int code) =>
code == 2 ? "HH" : code == 1 ? "LH" : code == -1 ? "HL" : "LL"
f_swLabel(int b, float p, int code) =>
color sc = code > 0 ? cHi : cLo
array.push(swLbls, label.new(b, p, f_swTxt(code), yloc=yloc.price, style=code > 0 ? label.style_label_down : label.style_label_up, color=sc, textcolor=f_lblTxt(sc), size=swSz, text_font_family=font.family_monospace))
if array.size(swLbls) > keepSw
label.delete(array.shift(swLbls))
// ── Swing engine ──
// rank codes: +2 higher high, +1 lower (or equal) high, -1 higher (or equal) low, -2 lower low
float topRef = ta.highest(high, swLen)
float botRef = ta.lowest(low, swLen)
bool candHi = high >= topRef
bool candLo = low <= botRef
var int legDir = 0
int legPrev = legDir
if candHi and not candLo
legDir := 1
else if candLo and not candHi
legDir := -1
bool flipped = legDir != legPrev
// head = the swing that is still forming, s1..s3 = finished swings, newest first
var float hP = na
var int hB = na
var int hR = 0
var float aP = na
var int aB = na
var int aR = 0
var float bP = na
var int bB = na
var int bR = 0
var float cP = na
var int cB = na
var int cR = 0
var int nSw = 0 // swings stored (head included), capped at 4
f_code(int side, float price, float ref, bool hasRef) =>
int strength = hasRef and side * price > side * ref ? 2 : 1
side * strength
f_swText(int code) =>
code == 2 ? "HH" : code == 1 ? "LH" : code == -1 ? "HL" : "LL"
bool cand = candHi or candLo
float candPx = legDir == 1 ? high : low
bool swingMoved = false
bool swingAdded = false
if cand
if nSw == 0 or flipped
// the head is finished: it becomes s1, a new head starts
if nSw > 0 and showSw
f_swLabel(hB, hP, hR)
if nSw > 0 and showZz and not na(aB)
array.push(swLines, line.new(aB, aP, hB, hP, color=color.new(cNeu, 20), width=1))
if array.size(swLines) > keepSw
line.delete(array.shift(swLines))
int newCode = f_code(legDir, candPx, aP, nSw >= 2)
cP := bP, cB := bB, cR := bR
bP := aP, bB := aB, bR := aR
aP := hP, aB := hB, aR := hR
hP := candPx, hB := bar_index, hR := newCode
nSw := math.min(nSw + 1, 4)
swingAdded := true
else if (legDir == 1 and candPx > hP) or (legDir == -1 and candPx < hP)
hP := candPx, hB := bar_index, hR := f_code(legDir, candPx, bP, nSw >= 3)
swingMoved := true
// ── Pattern ──
float riskAbs = na
float rewardAbs = na
float rrNow = na
bool isDT = false
bool isDB = false
if nSw >= 4
riskAbs := math.abs(aP - cP)
rewardAbs := math.abs(aP - bP)
rrNow := riskAbs * 100 / (riskAbs + rewardAbs)
isDT := aR == 1 and cR == 2 and bR < 0 and rrNow < maxRR
isDB := aR == -1 and cR == -2 and bR > 0 and rrNow < maxRR
// stored pattern: p0 = second top/bottom, p1 = neckline swing, p2 = first top/bottom
var float p0 = na
var float p1 = na
var float p2 = na
var int i0 = na
var int i1 = na
var int i2 = na
var int lastType = 0 // 1 = last pattern was a Double Top, -1 = Double Bottom
var bool biasTop = false // v1.1: before the first pattern the bias reads -1, exactly like the classic version
bool newPat = (isDT or isDB) and (i0 != aB or i2 != cB or na(i0))
if isDT or isDB
p0 := aP, p1 := bP, p2 := cP
i0 := aB, i1 := bB, i2 := cB
lastType := isDT ? 1 : -1
biasTop := isDT
// break / invalidation of the latest pattern (series checked every bar)
bool dnNeck = ta.crossunder(low, p1)
bool upNeck = ta.crossover(high, p1)
bool upBase = ta.crossover(high, p2)
bool dnBase = ta.crossunder(low, p2)
int dtSig = lastType == 1 ? (dnNeck ? 1 : upBase ? -1 : 0) : 0
int dbSig = lastType == -1 ? (upNeck ? 1 : dnBase ? -1 : 0) : 0
// ── Pattern bookkeeping ──
// Count = one per pattern. Confirmation / invalidation are counted like the classic study: a neckline cross counts
// when its neckline differs from the one of the previous confirmation, a cross of the first top (bottom) counts when
// that level differs from the one of the previous invalidation. Each counted event gets its dashed line.
var int dtN = 0
var int dbN = 0
var int dtOk = 0
var int dtBad = 0
var int dbOk = 0
var int dbBad = 0
var int lastConfI = na
var int lastInvI = na
var int pState = 0 // latest pattern: 0 open, 1 confirmed, -1 invalidated (first outcome)
if newPat
if isDT
dtN += 1
else
dbN += 1
pState := 0
color pc = isDT ? cDT : cDB
if showPat
line.new(i0, p0, i2, p2, color=pc, width=2, style=line.style_solid)
line.new(i0, p0, i1, p1, color=pc, width=2, style=line.style_dotted)
line.new(i1, p1, i2, p2, color=pc, width=2, style=line.style_dotted)
if showLbl
string t = (isDT ? "Double Top" : "Double Bottom") + (showRR ? " RR " + str.tostring(rrNow, "0.00") : "")
label.new(i0, p0, t, yloc=isDT ? yloc.abovebar : yloc.belowbar, style=isDT ? label.style_label_down : label.style_label_up, color=pc, textcolor=f_lblTxt(pc), size=size.small, text_font_family=font.family_monospace)
bool confEv = dtSig > 0 or dbSig > 0
bool invEv = not confEv and (dtSig < 0 or dbSig < 0)
bool confNew = confEv and (na(lastConfI) or i1 != lastConfI)
bool invNew = invEv and (na(lastInvI) or i2 != lastInvI)
color cPat = lastType == 1 ? cDT : cDB
color cOpp = lastType == 1 ? cDB : cDT
if confEv
if confNew
dtOk += dtSig > 0 ? 1 : 0
dbOk += dbSig > 0 ? 1 : 0
if showEv
line.new(i1, p1, bar_index, p1, color=cPat, width=2, style=line.style_dashed)
lastConfI := i1
if invEv
if invNew
dtBad += dtSig < 0 ? 1 : 0
dbBad += dbSig < 0 ? 1 : 0
if showEv
line.new(i2, p2, bar_index, p2, color=cOpp, width=2, style=line.style_dashed)
lastInvI := i2
int outcome = lastType == 1 ? dtSig : lastType == -1 ? dbSig : 0
if pState == 0 and outcome != 0 and lastType != 0
pState := outcome
// forming swing label + head segment on the last bar
var label headLbl = na
var line headLn = na
if barstate.islast
label.delete(headLbl)
line.delete(headLn)
if showSw and nSw > 0
color hc = hR > 0 ? cHi : cLo
headLbl := label.new(hB, hP, f_swTxt(hR), yloc=yloc.price, style=hR > 0 ? label.style_label_down : label.style_label_up, color=hc, textcolor=f_lblTxt(hc), size=swSz, text_font_family=font.family_monospace)
if showZz and nSw > 1
headLn := line.new(aB, aP, hB, hP, color=color.new(cNeu, 20), width=1, style=line.style_dotted)
// ── Measuring lines for the latest pattern (exact prices, across the whole chart) ──
var line mNeck = na
var line mBase = na
bool hasL = lastType != 0
if barstate.islast
line.delete(mNeck)
line.delete(mBase)
if showMeas and hasL
mNeck := line.new(i1, p1, bar_index, p1, color=cPat, width=2, style=line.style_dashed, extend=extend.both)
mBase := line.new(i2, p2, bar_index, p2, color=cOpp, width=2, style=line.style_dashed, extend=extend.both)
plot(showMeas and showMTag and hasL ? p1 : na, title="DP Pro | Neckline price tag", color=cPat, display=display.price_scale)
plot(showMeas and showMTag and hasL ? p2 : na, title="DP Pro | First top or bottom price tag", color=cOpp, display=display.price_scale)
// ── Panel ──
f_pos(string p) => p == "Top left" ? position.top_left : p == "Bottom right" ? position.bottom_right : p == "Bottom left" ? position.bottom_left : position.top_right
var table tb = table.new(f_pos(tabPos), 7, 6, border_width=0, frame_width=0)
f_rate(int ok, int bad) => ok + bad == 0 ? "-" : str.tostring(ok * 100.0 / (ok + bad), "#") + "%"
string tSz = tabSize == "Tiny" ? size.tiny : tabSize == "Normal" ? size.normal : size.small
color bgBody = color.new(colPnl, 8)
bool tLeft = tabPos == "Top left" or tabPos == "Bottom left"
bool tTop = tabPos == "Top right" or tabPos == "Top left"
int c0 = tLeft ? 1 : 0
int rH = tTop ? 1 : 0
f_cell(int col, int row, string t, color c, string al) =>
table.cell(tb, c0 + col, rH + row, t, text_color=c, text_size=tSz, text_halign=al, bgcolor=bgBody, text_font_family=font.family_monospace)
if barstate.isfirst
table.merge_cells(tb, c0, rH, c0 + 4, rH)
if barstate.islast and showTab
color hd = color.new(colTxt, 45)
if tTop
table.cell(tb, c0, 0, "", height=5, text_size=size.tiny)
else
table.cell(tb, c0, 4, "", height=5, text_size=size.tiny)
table.cell(tb, tLeft ? 0 : 5, rH, "", width=2, text_size=size.tiny)
f_cell(0, 0, "DOUBLE PATTERNS PRO", color.new(colTxt, 15), text.align_left)
f_cell(0, 1, "", hd, text.align_left)
f_cell(1, 1, "Count", hd, text.align_right)
f_cell(2, 1, "Confirmation", hd, text.align_right)
f_cell(3, 1, "Invalidation", hd, text.align_right)
f_cell(4, 1, "Rate", hd, text.align_right)
f_cell(0, 2, "Double Top", colDT, text.align_left)
f_cell(1, 2, str.tostring(dtN), colTxt, text.align_right)
f_cell(2, 2, str.tostring(dtOk), colTxt, text.align_right)
f_cell(3, 2, str.tostring(dtBad), colTxt, text.align_right)
f_cell(4, 2, f_rate(dtOk, dtBad), colDT, text.align_right)
f_cell(0, 3, "Double Bottom", colDB, text.align_left)
f_cell(1, 3, str.tostring(dbN), colTxt, text.align_right)
f_cell(2, 3, str.tostring(dbOk), colTxt, text.align_right)
f_cell(3, 3, str.tostring(dbBad), colTxt, text.align_right)
f_cell(4, 3, f_rate(dbOk, dbBad), colDB, text.align_right)
// ── Alerts: same names and the same trigger bars as the classic study ──
alertcondition(isDB, "Double Bottom", "10X1T DP Pro: double bottom on {{ticker}} {{interval}}")
alertcondition(dbSig > 0, "Double Bottom Confirmation", "10X1T DP Pro: double bottom confirmation on {{ticker}} {{interval}}")
alertcondition(dbSig < 0, "Double Bottom Invalidation", "10X1T DP Pro: double bottom invalidation on {{ticker}} {{interval}}")
alertcondition(isDT, "Double Top", "10X1T DP Pro: double top on {{ticker}} {{interval}}")
alertcondition(dtSig > 0, "Double Top Confirmation", "10X1T DP Pro: double top confirmation on {{ticker}} {{interval}}")
alertcondition(dtSig < 0, "Double Top Invalidation", "10X1T DP Pro: double top invalidation on {{ticker}} {{interval}}")
// ── Data Window (scanner columns, invisible on the chart) ──
color dwc = color.new(color.gray, 100)
plot(isDT ? 1 : isDB ? -1 : 0, title="DW | PatternType (1=DT,-1=DB,0=none)", color=dwc, display=display.data_window)
plot(isDT ? 1 : 0, title="DW | DoubleTop NEW (bool)", color=dwc, display=display.data_window)
plot(isDB ? 1 : 0, title="DW | DoubleBottom NEW (bool)", color=dwc, display=display.data_window)
plot(p0, title="DW | P0 Price (confirm pivot)", color=dwc, display=display.data_window)
plot(i0, title="DW | P0 BarIndex", color=dwc, display=display.data_window)
plot(p1, title="DW | P1 Price (neckline)", color=dwc, display=display.data_window)
plot(i1, title="DW | P1 BarIndex", color=dwc, display=display.data_window)
plot(p2, title="DW | P2 Price (base)", color=dwc, display=display.data_window)
plot(i2, title="DW | P2 BarIndex", color=dwc, display=display.data_window)
float dwRisk = math.abs(p0 - p2)
float dwReward = math.abs(p0 - p1)
plot(dwRisk * 100 / (dwRisk + dwReward), title="DW | RiskPerReward %", color=dwc, display=display.data_window)
plot(maxRR, title="DW | MaxRiskPerReward %", color=dwc, display=display.data_window)
plot(dwRisk, title="DW | Risk (abs)", color=dwc, display=display.data_window)
plot(dwReward, title="DW | Reward (abs)", color=dwc, display=display.data_window)
plot(biasTop ? 1 : -1, title="DW | LastPattern Bias (1=DT,-1=DB)", color=dwc, display=display.data_window)
plot(dtSig, title="DW | DT Confirm (1=conf,-1=inval,0=neutral)", color=dwc, display=display.data_window)
plot(dbSig, title="DW | DB Confirm (1=conf,-1=inval,0=neutral)", color=dwc, display=display.data_window)
plot(dtN, title="DW | DT Count total", color=dwc, display=display.data_window)
plot(dbN, title="DW | DB Count total", color=dwc, display=display.data_window)
plot(dtOk, title="DW | DT Confirmation Count", color=dwc, display=display.data_window)
plot(dtBad, title="DW | DT Invalidation Count", color=dwc, display=display.data_window)
plot(dbOk, title="DW | DB Confirmation Count", color=dwc, display=display.data_window)
plot(dbBad, title="DW | DB Invalidation Count", color=dwc, display=display.data_window)
plot(dtOk + dtBad == 0 ? na : dtOk * 100.0 / (dtOk + dtBad), title="DW | DT WinRate %", color=dwc, display=display.data_window)
plot(dbOk + dbBad == 0 ? na : dbOk * 100.0 / (dbOk + dbBad), title="DW | DB WinRate %", color=dwc, display=display.data_window)
plot(close - p1, title="DW | Close - Neckline(P1)", color=dwc, display=display.data_window)
plot(close - p2, title="DW | Close - Base(P2)", color=dwc, display=display.data_window)
plot(close, title="DW | Close", color=dwc, display=display.data_window)
plot(nSw > 0 ? hP : na, title="DW | ZZ[0] Price (newest)", color=dwc, display=display.data_window)
plot(nSw > 0 ? hR : na, title="DW | ZZ[0] Dir (2=HH,1=LH,-1=HL,-2=LL)", color=dwc, display=display.data_window)
plot(nSw > 1 ? aP : na, title="DW | ZZ[1] Price", color=dwc, display=display.data_window)
plot(nSw > 1 ? aR : na, title="DW | ZZ[1] Dir", color=dwc, display=display.data_window)
plot(nSw > 2 ? bP : na, title="DW | ZZ[2] Price", color=dwc, display=display.data_window)
plot(nSw > 2 ? bR : na, title="DW | ZZ[2] Dir", color=dwc, display=display.data_window)
plot(nSw > 3 ? cP : na, title="DW | ZZ[3] Price", color=dwc, display=display.data_window)
plot(nSw > 3 ? cR : na, title="DW | ZZ[3] Dir", color=dwc, display=display.data_window)
// additional columns (new in Pro)
plot(newPat ? (isDT ? 1 : -1) : 0, title="DW | Pattern Event (1=DT,-1=DB, first bar only)", color=dwc, display=display.data_window)
plot(pState, title="DW | Pattern State (1=neckline broken,-1=invalidated,0=open)", color=dwc, display=display.data_window)
plot(i0 - i2, title="DW | Tops Distance (bars)", color=dwc, display=display.data_window)