本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © blackcat1402
// V2 — Polished: edge-triggered CG signals, state machine, cooldown, ATR TP/SL reference, alerts, dashboard
//@version=6
indicator(title="[blackcat] L2 Ehlers Event Predictability", shorttitle="BCL5EEP", overlay=false, max_labels_count=500)
// ---- Input groups (const) ----
string GRP_EVENT = "Event Engine"
string GRP_SIG = "Signal Engine"
string GRP_RISK = "Risk Reference"
string GRP_VIS = "Visuals"
// ---- Inputs: Event Engine ----
StocLength = input.int(10, "Stochastic Length", minval=1, group=GRP_EVENT, tooltip="Lookback of the event stochastic. V1 author note: default should be 10.")
Price = input.source(hl2, "Price Source", group=GRP_EVENT)
EventLevel = input.float(0.2, "Event Trigger Level", minval=0.0, maxval=1.0, step=0.05, group=GRP_EVENT, tooltip="Stochastic must cross under this level to mark an Event.")
Lookback = input.int(9, "Event Lookback (bars back)", minval=1, group=GRP_EVENT, tooltip="Event and future-return reference window. V1 hardcoded 9 (= 10 bars back).")
ClampPct = input.float(10.0, "Future Return Clamp (+/- %)", minval=1.0, step=1.0, group=GRP_EVENT, tooltip="Future % return is clamped to +/- this value, then rescaled to 0-100 bins.")
// ---- Inputs: Signal Engine ----
useExclusivity = input.bool(true, "State Machine Gate (suppress duplicates)", group=GRP_SIG, tooltip="ON: CG-turn signals pass a position state machine so same-direction turns never repeat. OFF: raw turns (edge-triggered, still no per-bar spam).")
cooldownBars = input.int(3, "Cooldown Bars (0 = off)", minval=0, group=GRP_SIG)
// ---- Inputs: Risk Reference ----
useTPSL = input.bool(true, "Show TP/SL Reference (data window)", group=GRP_RISK)
atrLen = input.int(14, "ATR Length", minval=1, group=GRP_RISK)
slMult = input.float(2.0, "Stop Loss (xATR)", minval=0.5, step=0.25, group=GRP_RISK)
tpMult = input.float(3.0, "Take Profit (xATR)", minval=0.5, step=0.25, group=GRP_RISK)
// ---- Inputs: Visuals ----
showLabels = input.bool(true, "Show Signal Labels", group=GRP_VIS)
showBg = input.bool(true, "Show Regime Background", group=GRP_VIS)
showDash = input.bool(true, "Show Dashboard", group=GRP_VIS)
// ---- Color system ----
color C_LONG = color.new(color.green, 0)
color C_SHORT = color.new(color.red, 0)
color C_BGLONG = color.new(color.green, 94)
color C_BGSHORT = color.new(color.red, 94)
// ---- Vars ----
bool Event = false
float FuturePrice = 0.00
float CG = 0.00
float Denom = 0.00
float HiC = 0.00
float LoC = 0.00
float Stoc = 0.00
// Histogram of post-event future returns (100 bins, 0-100 scale)
var PredictBin = array.new_float(102)
// Signal state machine
var int posState = 0 // -1 short regime / 0 flat / +1 long regime
var int lastSignalBar = na
var int eventCount = 0
//>>>>>>>>>> Start Event Code (Ehlers)
HiC := ta.highest(Price, StocLength)
LoC := ta.lowest(Price, StocLength)
Stoc := HiC > LoC ? (Price - LoC) / (HiC - LoC) : na // division guard (HiC == LoC)
if ta.crossunder(nz(Stoc[Lookback]), EventLevel)
Event := true
else
Event := false
if Event
eventCount += 1
//<<<<<<<<<<<< End Event Code
if Event
FuturePrice := 100 * (Price - nz(Price[Lookback])) / nz(Price[Lookback])
// Future is referenced to Lookback+1 bars back
if FuturePrice < -ClampPct
FuturePrice := -ClampPct //Limits lower price to -Clamp%
if FuturePrice > ClampPct
FuturePrice := ClampPct //Limits higher price to +Clamp%
FuturePrice := (100 / (2 * ClampPct)) * (FuturePrice + ClampPct)
//scale -Clamp% to +Clamp% to be 0 - 100
//Place the FuturePrices into one of 100 bins
if FuturePrice != nz(FuturePrice[1])
for j = 1 to 100
if FuturePrice > j - 1 and FuturePrice <= j
array.set(PredictBin, j, nz(array.get(PredictBin, j)) + 1)
//Measure Center of Gravity as a quick estimate
CG := 0
Denom := 0
for j = 1 to 100
CG := CG + j * nz(array.get(PredictBin, j))
Denom := Denom + nz(array.get(PredictBin, j))
CG := Denom > 0 ? (CG / Denom - 50) / 5 : na // division guard; na until first sample
plot(CG, "Center of Gravity (CG)", color=CG > nz(CG[1]) ? color.new(color.lime, 0) : CG < nz(CG[1]) ? color.new(color.red, 0) : color.new(color.gray, 40), linewidth=2, style=plot.style_circles, join=true)
hline(0, "Zero Line", color=color.new(color.gray, 60), linestyle=hline.style_dotted)
// ---- Signal engine: edge-triggered CG turns + state machine (W4) ----
// V1 used level logic (CG rising = BUY every bar). V2 fires only on the turn:
bool cgRising = not na(CG) and CG > nz(CG[1])
bool cgFalling = not na(CG) and CG < nz(CG[1])
bool turnUp = cgRising and not na(CG[1]) and nz(CG[1]) <= nz(CG[2])
bool turnDn = cgFalling and not na(CG[1]) and nz(CG[1]) >= nz(CG[2])
bool cooldownOK = nz(lastSignalBar, -1) == -1 or bar_index - lastSignalBar > cooldownBars
bool longSig = useExclusivity ? turnUp and posState <= 0 and cooldownOK : turnUp and cooldownOK
bool shortSig = useExclusivity ? turnDn and posState >= 0 and cooldownOK : turnDn and cooldownOK
// State transition (at most one flip per bar); exits are reverse-signal flips (W2)
if longSig
posState := 1
lastSignalBar := bar_index
else if shortSig
posState := -1
lastSignalBar := bar_index
// plot(posState, "Position State", display=display.data_window)
// ---- ATR-based TP/SL reference for the latest signal (W3, indicator version) ----
float atrVal = ta.atr(atrLen)
var float entryPrice = na
var int entryDir = 0
if longSig
entryPrice := close
entryDir := 1
if shortSig
entryPrice := close
entryDir := -1
float slPrice = useTPSL and not na(entryPrice) and entryDir != 0 ? entryPrice - entryDir * slMult * atrVal : na
float tpPrice = useTPSL and not na(entryPrice) and entryDir != 0 ? entryPrice + entryDir * tpMult * atrVal : na
// plot(slPrice, "SL Reference", color=color.new(color.red, 0), style=plot.style_linebr, display=display.data_window)
// plot(tpPrice, "TP Reference", color=color.new(color.teal, 0), style=plot.style_linebr, display=display.data_window)
// ---- Signal labels (W5): only on confirmed turns, carry CG value ----
if showLabels and longSig
label.new(bar_index, nz(CG), "BUY " + str.tostring(nz(CG), "#.##"), color=C_LONG, textcolor=color.white, style=label.style_label_up, yloc=yloc.price, size=size.small)
if showLabels and shortSig
label.new(bar_index, nz(CG), "SELL " + str.tostring(nz(CG), "#.##"), color=C_SHORT, textcolor=color.white, style=label.style_label_down, yloc=yloc.price, size=size.small)
// ---- Regime background (W5) ----
color bgCol = showBg and posState == 1 ? C_BGLONG : showBg and posState == -1 ? C_BGSHORT : na
bgcolor(bgCol, title="Regime Background")
// ---- Dashboard (W5) ----
var table dash = table.new(position.top_right, 2, 5, bgcolor=color.new(color.black, 60), border_width=1)
if showDash and barstate.islast
string regimeTxt = posState == 1 ? "LONG" : posState == -1 ? "SHORT" : "FLAT"
color regimeCol = posState == 1 ? C_LONG : posState == -1 ? C_SHORT : color.gray
table.cell(dash, 0, 0, "Regime", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 0, regimeTxt, text_color=regimeCol, text_size=size.tiny)
table.cell(dash, 0, 1, "CG", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 1, str.tostring(nz(CG), "#.##") + (cgRising ? " rising" : cgFalling ? " falling" : " flat"), text_color=color.white, text_size=size.tiny)
table.cell(dash, 0, 2, "Events", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 2, str.tostring(eventCount), text_color=color.white, text_size=size.tiny)
table.cell(dash, 0, 3, "Samples", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 3, str.tostring(nz(Denom), "#"), text_color=color.white, text_size=size.tiny)
table.cell(dash, 0, 4, "SL / TP", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 4, na(entryPrice) ? "-" : str.tostring(slPrice, format.mintick) + " / " + str.tostring(tpPrice, format.mintick), text_color=color.white, text_size=size.tiny)
// ---- Alerts (W6): create alerts on BCL5EEP BUY / SELL / ANY / EVENT ----
alertcondition(longSig, "BCL5EEP BUY", "[BCL5EEP] BUY: CG turned up")
alertcondition(shortSig, "BCL5EEP SELL", "[BCL5EEP] SELL: CG turned down")
alertcondition(longSig or shortSig, "BCL5EEP ANY Signal", "[BCL5EEP] Regime flip signal")
alertcondition(Event, "BCL5EEP Event", "[BCL5EEP] Event trigger: stochastic crossed under event level")