本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator('Pre-Close Hourly Signal', shorttitle = 'Dev1H-Z', overlay = false, max_labels_count = 500)
// This indicator works on the 5-MINUTE chart only.
if timeframe.in_seconds() != 300
runtime.error('Switch the chart to the 5-minute timeframe.')
// ================= INPUTS =================
lookback = input.int(864, 'Bars for sigma (5m bars, 864 = 3 days)', minval = 100)
zThr = input.float(2.0, 'Z threshold', step = 0.1)
minK = input.int(3, 'Min k (which 5m bar within the hour)', minval = 1, maxval = 11)
maxK = input.int(8, 'Max k', minval = 2, maxval = 11)
vpThr = input.float(1.5, 'Volume pace threshold (x)', step = 0.1)
clvThr = input.float(0.7, 'Partial CLV threshold', minval = 0.0, maxval = 1.0, step = 0.05)
confirmN = input.int(2, 'Consecutive confirmation (bars)', minval = 1, maxval = 6)
volAlpha = input.float(0.05, 'Volume profile EMA alpha', minval = 0.01, maxval = 0.5, step = 0.01)
showStats = input.bool(true, 'Show statistics table')
// ================= HELPERS =================
// Standard normal CDF (Abramowitz-Stegun approximation)
f_cdf(x) =>
t = 1.0 / (1.0 + 0.2316419 * math.abs(x))
d = 0.3989423 * math.exp(-x * x / 2.0)
p = d * t * (0.3193815 + t * (-0.3565638 + t * (1.781478 + t * (-1.821256 + t * 1.330274))))
x > 0 ? 1.0 - p : p
// ================= HOUR / k DETECTION =================
t60 = time('60')
newHour = na(t60[1]) or t60 != t60[1]
int k = math.round((time - t60) / 300000.0) + 1 // 1..12
// ================= STATE VARIABLES =================
var float hOpen = na
var float hHigh = na
var float hLow = na
var float hVol = 0.0
var bool firedThisHour = false
var int streak = 0
// Statistics arrays (index = k)
var nSig = array.new_int(13, 0)
var nGreen = array.new_int(13, 0)
var sumTheo = array.new_float(13, 0.0)
var sumRetC = array.new_float(13, 0.0) // log return from signal to hour close
var sumNext = array.new_float(13, 0.0) // log return of the following hour
var nNext = array.new_int(13, 0)
var avgCum = array.new_float(12, na) // average cumulative volume profile by k
var int pK = 0
var float pPrice = na
var float pTheo = na
var int aK = 0
var float aClose = na
// ================= HOUR ROLLOVER: CLOSE OUT THE LAST HOUR =================
if newHour
if aK > 0
array.set(sumNext, aK, array.get(sumNext, aK) + math.log(close[1] / aClose))
array.set(nNext, aK, array.get(nNext, aK) + 1)
aK := 0
aK
if pK > 0
array.set(nSig, pK, array.get(nSig, pK) + 1)
if close[1] > hOpen
array.set(nGreen, pK, array.get(nGreen, pK) + 1)
array.set(sumTheo, pK, array.get(sumTheo, pK) + pTheo)
array.set(sumRetC, pK, array.get(sumRetC, pK) + math.log(close[1] / pPrice))
aK := pK
aClose := close[1]
pK := 0
pK
hOpen := open
hHigh := high
hLow := low
hVol := volume
firedThisHour := false
firedThisHour
else
hHigh := math.max(hHigh, high)
hLow := math.min(hLow, low)
hVol := hVol + volume
hVol
// ================= FEATURES =================
r = math.log(close / close[1])
sig5 = math.sqrt(ta.sma(r * r, lookback)) // realized 5m volatility
z = math.log(close / hOpen) / (sig5 * math.sqrt(k)) // developing 1H candle z-score
zRoll = math.log(close / close[12]) / (sig5 * math.sqrt(12.0)) // rolling 60-minute z-score
clv = hHigh > hLow ? (close - hLow) / (hHigh - hLow) : 0.5
// Volume pace: hour volume so far / historical average at the same k
int ki = math.min(math.max(k, 1), 12) - 1
avgK = array.get(avgCum, ki)
vp = not na(avgK) and avgK > 0 ? hVol / avgK : na
if barstate.isconfirmed
array.set(avgCum, ki, na(avgK) ? hVol : avgK * (1.0 - volAlpha) + hVol * volAlpha)
probGreen = k < 12 ? f_cdf(z * math.sqrt(k / (12.0 - k))) : na
// ================= SIGNAL =================
cond = k >= minK and k <= maxK and z >= zThr and not na(vp) and vp >= vpThr and clv >= clvThr
streak := cond ? newHour ? 1 : streak + 1 : 0
sig = streak >= confirmN and not firedThisHour and barstate.isconfirmed
if sig
firedThisHour := true
pK := k
pPrice := close
pTheo := nz(probGreen, 0.5)
pTheo
// ================= VISUALS =================
plot(z, 'Z (developing 1H)', style = plot.style_columns, color = z >= zThr ? color.new(color.green, 0) : z > 0 ? color.new(color.green, 65) : color.new(color.red, 65))
plot(zRoll, 'Z (rolling 60m)', color = color.orange, linewidth = 2)
hline(zThr, 'Threshold', color = color.gray, linestyle = hline.style_dashed)
hline(0, 'Zero', color = color.gray)
bgcolor(sig ? color.new(color.lime, 70) : na)
plotshape(sig, 'Signal', shape.triangleup, location.bottom, color.lime, size = size.small)
plot(k, 'k', display = display.data_window)
plot(vp, 'Volume pace (x)', display = display.data_window)
plot(clv, 'Partial CLV', display = display.data_window)
plot(probGreen * 100, 'Theoretical green close %', display = display.data_window)
// ================= STATISTICS TABLE =================
var table tb = table.new(position.top_right, 6, 11, border_width = 1)
if showStats and barstate.islast
hdr = array.from('k', 'N', 'Actual green %', 'Theoretical %', 'To close %', 'Next 1H %')
for c = 0 to 5 by 1
table.cell(tb, c, 0, array.get(hdr, c), text_color = color.white, bgcolor = color.gray, text_size = size.small)
for rr = 1 to 10 by 1
kk = minK + rr - 1
if kk <= maxK
n = array.get(nSig, kk)
nn = array.get(nNext, kk)
gTxt = n > 0 ? str.tostring(100.0 * array.get(nGreen, kk) / n, '#.#') : '-'
tTxt = n > 0 ? str.tostring(100.0 * array.get(sumTheo, kk) / n, '#.#') : '-'
cTxt = n > 0 ? str.tostring(100.0 * array.get(sumRetC, kk) / n, '#.###') : '-'
xTxt = nn > 0 ? str.tostring(100.0 * array.get(sumNext, kk) / nn, '#.###') : '-'
table.cell(tb, 0, rr, str.tostring(kk), text_color = color.white, bgcolor = color.new(color.blue, 40), text_size = size.small)
table.cell(tb, 1, rr, str.tostring(n), text_color = color.white, bgcolor = color.new(color.blue, 40), text_size = size.small)
table.cell(tb, 2, rr, gTxt, text_color = color.white, bgcolor = color.new(color.blue, 40), text_size = size.small)
table.cell(tb, 3, rr, tTxt, text_color = color.white, bgcolor = color.new(color.blue, 40), text_size = size.small)
table.cell(tb, 4, rr, cTxt, text_color = color.white, bgcolor = color.new(color.blue, 40), text_size = size.small)
table.cell(tb, 5, rr, xTxt, text_color = color.white, bgcolor = color.new(color.blue, 40), text_size = size.small)
else
for c = 0 to 5 by 1
table.cell(tb, c, rr, '', bgcolor = na)
alertcondition(sig, 'Developing 1H candle bullish signal', 'Early uptrend (intra-hour candle): {{ticker}}')