本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator('Odds-Implied Price', overlay = true)
kScale = input.float(10, 'Price scale k (displacement per 1σ of prob)', minval = 1.0)
smoothN = input.int(1, 'Smooth odds (SMA) — odds are discrete/steppy', minval = 1)
useSrc = input.bool(false, 'Use an odds source series on the chart')
oddsSrc = input.source(close, 'Odds source (0-1 or 0-100)')
// probit (inverse normal CDF) — Acklam approximation
probit(p) =>
pp = math.min(math.max(p, 0.0001), 0.9999)
a1 = -39.6968302866538
a2 = 220.946098424521
a3 = -275.928510446969
a4 = 138.357751867269
a5 = -30.6647980661472
a6 = 2.50662827745924
b1 = -54.4760987982241
b2 = 161.585836858041
b3 = -155.698979859887
b4 = 66.8013118877197
b5 = -13.2806815528857
c1 = -0.00778489400243029
c2 = -0.322396458041136
c3 = -2.40075827716184
c4 = -2.54973253934373
c5 = 4.37466414146497
c6 = 2.93816398269878
d1 = 0.00778469570904146
d2 = 0.32246712907004
d3 = 2.445134137143
d4 = 3.75440866190742
plow = 0.02425
phigh = 1 - 0.02425
float z = na
if pp < plow
q = math.sqrt(-2 * math.log(pp))
z := (((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1)
z
else if pp <= phigh
q = pp - 0.5
r = q * q
z := (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * q / (((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1)
z
else
q = math.sqrt(-2 * math.log(1 - pp))
z := -(((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1)
z
z
// Get the probability in 0-1. In live use, feed real UP odds via oddsSrc.
// For demonstration without an odds source, synthesize a probability from
// where price sits inside a recent range (so you can see the transform).
rawOdds = useSrc ? oddsSrc > 1.5 ? oddsSrc / 100.0 : oddsSrc : na
// Pine v5 has no math.tanh — define it via exp: tanh(x) = (e^2x-1)/(e^2x+1).
// Clamp the argument so exp() can't overflow on large inputs.
tanh(x) =>
xc = math.max(math.min(x, 20.0), -20.0)
e2 = math.exp(2.0 * xc)
(e2 - 1.0) / (e2 + 1.0)
demoProb = na(rawOdds) ? 0.5 + 0.5 * tanh((close - ta.sma(close, 20)) / (ta.atr(20) + 1e-9)) : rawOdds
prob = ta.sma(demoProb, smoothN)
// candle open = the "price to beat"; here use the 5m open as the anchor line.
openRef = request.security(syminfo.tickerid, '5', open, lookahead = barmerge.lookahead_on)
// For a clean OVERLAY on the price chart, express the odds displacement in real
// price units: scale by ATR so probit(prob) maps to a sensible number of price
// points. kScale is now "ATR multiples per 1σ of implied probability".
atrUnit = ta.atr(20)
synthetic = openRef + kScale * atrUnit * probit(prob) / 50.0
// ── derived signals ON the odds-price ──
rate = synthetic - synthetic[1] // odds-price velocity
firstMovePct = (synthetic - openRef) / openRef * 100
col = synthetic >= openRef ? color.teal : color.red
plot(synthetic, 'Odds-implied price', color = col, linewidth = 2)
plot(openRef, 'Open (price to beat)', color = color.new(color.gray, 40), style = plot.style_stepline)
// rate is kept as a plot you can add to a separate pane via "Move to" if wanted,
// but hidden by default so it doesn't distort the price overlay scale.
plot(rate, 'Odds-price rate', color = color.new(color.orange, 0), display = display.none)
// ── readout ──
var table t = table.new(position.top_right, 2, 4, border_width = 1)
if barstate.islast
table.cell(t, 0, 0, 'Odds-price', bgcolor = color.new(color.gray, 70), text_color = color.white, text_size = size.small)
table.cell(t, 1, 0, 'Value', bgcolor = color.new(color.gray, 70), text_color = color.white, text_size = size.small)
table.cell(t, 0, 1, 'Implied prob', text_size = size.small)
table.cell(t, 1, 1, str.tostring(prob * 100, '#.#') + '%', text_size = size.small)
table.cell(t, 0, 2, 'vs open', text_size = size.small)
table.cell(t, 1, 2, synthetic >= openRef ? 'ABOVE' : 'BELOW', text_size = size.small, text_color = col)
table.cell(t, 0, 3, 'move %', text_size = size.small)
table.cell(t, 1, 3, str.tostring(firstMovePct, '#.###') + '%', text_size = size.small, text_color = col)
alertcondition(ta.cross(synthetic, openRef), 'Odds-price crossed open', 'Odds-implied price crossed the open line')