本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © C0de_Master
//@version=6
// Correction Regime (TEMA/EMA) — publish build of Mode Logic, Correction mode only.
indicator("Correction Regime (TEMA/EMA) [C0de Master]", shorttitle="Correction Regime", overlay=true, behind_chart=false)
// ───────── Inputs ─────────
const string gS = "Signal"
temaLen = input.int(21, "TEMA length", minval=2, group=gS, tooltip="Tested value: 21. Other values were not tested.")
emaLen = input.int(55, "EMA length", minval=2, group=gS, tooltip="Tested value: 55. Other values were not tested.")
onClose = input.bool(true, "Confirm circles on bar close", group=gS, tooltip="On the live bar a circle can appear and disappear before the close. It is shown as a faint ✕ (possible) and is not counted anywhere.")
const string gD = "Display"
panelMode = input.string("Compact", "Panel", options=["Compact", "Full", "Off"], inline="pnl", group=gD)
panelPos = input.string("Bottom Right", "", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left"], inline="pnl", group=gD)
panelSize = input.string("Small", "", options=["Tiny", "Small", "Normal"], inline="pnl", group=gD, tooltip="Panel content, position and text size.")
showCtx = input.bool(true, "Plot the context EMA", group=gD)
showBg = input.bool(false, "Tint the correction regime", group=gD)
const string gR = "Record & hedge"
recBars = input.int(90, "Look-ahead window after a circle (bars)", minval=5, maxval=500, group=gR)
bigMove = input.float(20, "Large move = further drop / rise of at least (%)", minval=1, step=1, group=gR)
falseBars = input.int(20, "Blue circle is false if an orange one follows within (bars)", minval=1, maxval=500, group=gR, tooltip="Capped at the look-ahead window.")
ctxLen = input.int(200, "Context EMA (splits the record above/below it)", minval=10, group=gR)
warmup = input.int(200, "Start the hedge after (bars)", minval=60, group=gR)
costPct = input.float(0.1, "Cost per side (%)", minval=0, step=0.05, group=gR)
falseB = math.min(falseBars, recBars)
// ───────── Core (unchanged from the original Correction mode) ─────────
temaFunc(src, len) =>
e1 = ta.ema(src, len)
e2 = ta.ema(e1, len)
e3 = ta.ema(e2, len)
3 * e1 - 3 * e2 + e3
tema = temaFunc(close, temaLen)
ema = ta.ema(close, emaLen)
ctx = ta.ema(close, ctxLen)
rawDown = tema <= ema and tema[1] > ema[1]
rawUp = tema > ema and tema[1] <= ema[1]
conf = onClose ? barstate.isconfirmed : true
orange = rawDown and conf
blue = rawUp and conf
liveBar = onClose and barstate.islast and not barstate.isconfirmed
upConf = liveBar ? tema[1] > ema[1] : tema > ema // regime on the last closed bar
// ───────── Plots ─────────
plot(tema, "TEMA", color=tema > ema ? color.teal : color.orange, linewidth=2)
plot(ema, "EMA", color=color.gray, linewidth=1)
plot(showCtx ? ctx : na, "Context EMA", color=color.new(color.silver, 30), linewidth=1, linestyle=plot.linestyle_dotted)
bgcolor(showBg and not upConf ? color.new(color.orange, 92) : na, title="Correction regime tint")
plotshape(orange, "Correction start (orange circle)", shape.circle, location.abovebar, color.orange, size=size.tiny)
plotshape(blue, "Correction end (blue circle)", shape.circle, location.belowbar, color.blue, size=size.tiny)
plotshape(liveBar and rawDown, "Possible start (live bar)", shape.xcross, location.abovebar, color.new(color.orange, 50), size=size.tiny)
plotshape(liveBar and rawUp, "Possible end (live bar)", shape.xcross, location.belowbar, color.new(color.blue, 50), size=size.tiny)
// ───────── Hedge: exit at the next open after an orange circle, re-enter at the next open after a blue one ─────────
cost = costPct / 100
var bool inMkt = false
var int pend = 0 // 1 = exit next open, 2 = enter next open
var float eqH = 1.0
var float eqB = 1.0
var float pkH = 1.0
var float pkB = 1.0
var float ddH = 0.0
var float ddB = 0.0
var int outBar = na
var float outPx = na
var int nTr = 0
var int nIn = 0
var int nTot = 0
var int t0 = na
if bar_index == warmup
inMkt := tema > ema // start in the current regime
t0 := time
else if bar_index > warmup
if pend == 1
eqH *= open / close[1] * (1 - cost)
inMkt := false
outBar := bar_index
outPx := open
nTr += 1
else if pend == 2
eqH *= (1 - cost) * close / open
inMkt := true
nTr += 1
else if inMkt
eqH *= close / close[1]
eqB *= close / close[1]
pkH := math.max(pkH, eqH)
pkB := math.max(pkB, eqB)
ddH := math.min(ddH, eqH / pkH - 1)
ddB := math.min(ddB, eqB / pkB - 1)
nTot += 1
nIn += inMkt ? 1 : 0
pend := 0
if bar_index >= warmup and inMkt and orange
pend := 1
else if bar_index >= warmup and not inMkt and blue
pend := 2
// ───────── Record: further drop ≥ bigMove% within recBars after an orange circle, vs any bar ─────────
var array<int> evBar = array.new<int>()
var array<float> evPx = array.new<float>()
var array<bool> evAbv = array.new<bool>()
var int nA = 0
var int bA = 0
var int nU = 0
var int bU = 0
var int nAll = 0
var int bAll = 0
if orange
array.push(evBar, bar_index)
array.push(evPx, close)
array.push(evAbv, close > ctx)
lowW = ta.lowest(low, recBars)
if bar_index >= recBars
nAll += 1
if lowW / close[recBars] - 1 <= -bigMove / 100
bAll += 1
while array.size(evBar) > 0 and bar_index == array.get(evBar, 0) + recBars
hit = lowW / array.get(evPx, 0) - 1 <= -bigMove / 100
if array.get(evAbv, 0)
nA += 1
bA += hit ? 1 : 0
else
nU += 1
bU += hit ? 1 : 0
array.shift(evBar)
array.shift(evPx)
array.shift(evAbv)
// ───────── Record: rise ≥ bigMove% within recBars after a blue circle, and false blues ─────────
var array<int> bBar = array.new<int>()
var array<float> bPx = array.new<float>()
var array<bool> bAbv = array.new<bool>()
var array<int> bOr = array.new<int>() // bar of the first orange after this blue (na = none yet)
var int qA = 0
var int uA = 0
var int fA = 0
var int qU = 0
var int uU = 0
var int fU = 0
var int nAllU = 0
var int uAll = 0
if orange and array.size(bOr) > 0
for i = 0 to array.size(bOr) - 1
if na(array.get(bOr, i))
array.set(bOr, i, bar_index)
if blue
array.push(bBar, bar_index)
array.push(bPx, close)
array.push(bAbv, close > ctx)
array.push(bOr, na)
highW = ta.highest(high, recBars)
if bar_index >= recBars
nAllU += 1
if highW / close[recBars] - 1 >= bigMove / 100
uAll += 1
while array.size(bBar) > 0 and bar_index == array.get(bBar, 0) + recBars
rose = highW / array.get(bPx, 0) - 1 >= bigMove / 100
ob = array.get(bOr, 0)
isF = not na(ob) and ob - array.get(bBar, 0) <= falseB
if array.get(bAbv, 0)
qA += 1
uA += rose ? 1 : 0
fA += isF ? 1 : 0
else
qU += 1
uU += rose ? 1 : 0
fU += isF ? 1 : 0
array.shift(bBar)
array.shift(bPx)
array.shift(bAbv)
array.shift(bOr)
// ───────── Panel ─────────
pct(x) => na(x) ? "—" : (x >= 0 ? "+" : "") + str.tostring(x * 100, "0.0") + "%"
pr(b, n) => n > 0 ? str.tostring(b * 100.0 / n, "0") + "%" : "—"
pos = panelPos == "Top Right" ? position.top_right : panelPos == "Bottom Right" ? position.bottom_right : panelPos == "Top Left" ? position.top_left : position.bottom_left
tsz = panelSize == "Tiny" ? size.tiny : panelSize == "Normal" ? size.normal : size.small
var table dash = na
cell(table tb, c, r, txt, col, bold = false) =>
table.cell(tb, c, r, txt, text_color=col, text_size=tsz, text_halign=c == 0 ? text.align_left : text.align_center, bgcolor=color.new(#0d0d0d, 8), text_formatting=bold ? text.format_bold : text.format_none)
if barstate.islast and panelMode != "Off"
// rebuilt on every update of the last bar, so merged cells never stack up on realtime ticks
if not na(dash)
table.delete(dash)
dash := table.new(pos, 4, 12, border_width=1, frame_color=color.new(color.gray, 40), border_color=color.new(color.gray, 75), bgcolor=color.new(#0d0d0d, 15))
g = color.silver
w = color.white
regCol = upConf ? color.teal : color.orange
cell(dash, 0, 0, "Correction Regime", w, true)
cell(dash, 1, 0, upConf ? "Momentum" : "Correction", regCol, true)
table.merge_cells(dash, 1, 0, 3, 0)
live = liveBar and rawDown ? "possible orange ✕ (bar not closed)" : liveBar and rawUp ? "possible blue ✕ (bar not closed)" : ""
hedgeTxt = inMkt ? "hedge: in the market" : "hedge: out " + str.tostring(bar_index - outBar) + " bars, price " + pct(close / outPx - 1)
cell(dash, 0, 1, hedgeTxt + (live != "" ? " · " + live : ""), live != "" ? color.orange : (inMkt ? color.teal : color.orange))
table.merge_cells(dash, 0, 1, 3, 1)
mv = str.tostring(bigMove) + "%"
if panelMode == "Compact"
cell(dash, 0, 2, "Orange → drop ≥ " + mv + " in " + str.tostring(recBars), g)
cell(dash, 1, 2, "above EMA" + str.tostring(ctxLen) + " " + pr(bA, nA) + " · " + "below " + pr(bU, nU) + " · " + "any bar " + pr(bAll, nAll), w)
table.merge_cells(dash, 1, 2, 3, 2)
cell(dash, 0, 3, "Blue → false (orange ≤ " + str.tostring(falseB) + ")", g)
cell(dash, 1, 3, "above EMA" + str.tostring(ctxLen) + " " + pr(fA, qA) + " · " + "below " + pr(fU, qU), w)
table.merge_cells(dash, 1, 3, 3, 3)
else
// header
cell(dash, 0, 2, "After the circle", g, true)
cell(dash, 1, 2, "n", g, true)
cell(dash, 2, 2, "move ≥ " + mv + " in " + str.tostring(recBars), g, true)
cell(dash, 3, 2, "false (≤ " + str.tostring(falseB) + ")", g, true)
cell(dash, 0, 3, "orange, above EMA" + str.tostring(ctxLen), color.orange)
cell(dash, 1, 3, str.tostring(nA), w), cell(dash, 2, 3, pr(bA, nA) + " drop", w), cell(dash, 3, 3, "", w)
cell(dash, 0, 4, "orange, below EMA" + str.tostring(ctxLen), color.orange)
cell(dash, 1, 4, str.tostring(nU), w), cell(dash, 2, 4, pr(bU, nU) + " drop", w), cell(dash, 3, 4, "", w)
cell(dash, 0, 5, "any bar (baseline)", g)
cell(dash, 1, 5, str.tostring(nAll), g), cell(dash, 2, 5, pr(bAll, nAll) + " drop" + " · " + pr(uAll, nAllU) + " rise", g), cell(dash, 3, 5, "", g)
cell(dash, 0, 6, "blue, above EMA" + str.tostring(ctxLen), color.blue)
cell(dash, 1, 6, str.tostring(qA), w), cell(dash, 2, 6, pr(uA, qA) + " rise", w), cell(dash, 3, 6, pr(fA, qA), w)
cell(dash, 0, 7, "blue, below EMA" + str.tostring(ctxLen), color.blue)
cell(dash, 1, 7, str.tostring(qU), w), cell(dash, 2, 7, pr(uU, qU) + " rise", w), cell(dash, 3, 7, pr(fU, qU), w)
// hedge vs hold
since = na(t0) ? "—" : str.format_time(t0, "yyyy-MM-dd", syminfo.timezone)
cell(dash, 0, 8, "Since " + since, g, true)
cell(dash, 1, 8, "return", g, true), cell(dash, 2, 8, "max drawdown", g, true), cell(dash, 3, 8, "time in market", g, true)
cell(dash, 0, 9, "hedge (" + str.tostring(nTr) + " trades)", w)
cell(dash, 1, 9, pct(eqH - 1), w), cell(dash, 2, 9, pct(ddH), w), cell(dash, 3, 9, nTot > 0 ? str.tostring(nIn * 100.0 / nTot, "0") + "%" : "—", w)
cell(dash, 0, 10, "buy & hold", w)
cell(dash, 1, 10, pct(eqB - 1), w), cell(dash, 2, 10, pct(ddB), w), cell(dash, 3, 10, "100%", w)
cell(dash, 0, 11, "Past behavior on this chart, cost " + str.tostring(costPct) + "% per side. Not a forecast.", g)
table.merge_cells(dash, 0, 11, 3, 11)
// ───────── Alerts (confirmed on close) ─────────
alertcondition(orange, "Orange circle (confirmed)", "Correction Regime: orange circle confirmed on bar close (TEMA crossed below EMA)")
alertcondition(blue, "Blue circle (confirmed)", "Correction Regime: blue circle confirmed on bar close (TEMA crossed above EMA)")