This commit is contained in:
wvr committed 2026-11-22 02:27:30 -06:00
1 parent bb84929e73
commit 88ff433023
1 file changed
+38 -6
+38 -6
View File
@@ -48,9 +48,11 @@ nqSym = input.symbol("CME_MINI:NQ1!", "NQ", group = grpC, display = display.none
ngSym = input.symbol("NYMEX:NG1!", "Natural gas", group = grpC, display = display.none)
grpG = "Gold gap threshold"
thrInput = input.float(0.99, "Fallback threshold (%)", step = 0.01, group = grpG, tooltip = "Used until enough days are loaded for the live median. Update monthly.", display = display.none)
thrInput = input.float(0.99, "Threshold (%): 250-day median |gap|", step = 0.01, group = grpG, tooltip = "The backtested rule: median |silver-gold gap| over the last 250 trading days. 0.99% as of 11 Nov 2026. Update every month or two from fresh data.", display = display.none)
useAuto = input.bool(false, "Automatic threshold (5-min bars)", group = grpG, tooltip = "OFF by default. TradingView loads only about 18 (free) to 145 (Ultimate) days of 5-minute history, short of the 250-day rule. When ON, the median uses as many days as are loaded and is checked daily against the exact 1-minute gap.", display = display.none)
chkTol = input.float(0.05, "Check tolerance (% points)", step = 0.01, group = grpG, display = display.none)
lookback = input.int(250, "Median lookback (trading days)", minval = 20, group = grpG, display = display.none)
minDays = input.int(60, "Days before using the live median", minval = 10, group = grpG, display = display.none)
minDays = input.int(15, "Days before using the median", minval = 10, group = grpG, tooltip = "Tested: an 18-day median picks the same side as the 250-day one on 91-94% of days, with the same edge.", display = display.none)
useNews = input.bool(false, "NQ news-day rule (8:30 trades)", tooltip = "OFF (default): NQ always trades the normal-day rule at 9:31 / 9:33. The news-day rule failed the 2010-14 holdout test. ON only for testing.", display = display.none)
newsThr = input.float(4.4, "NQ news day: 8:30 volume vs 8:15-8:29 average (x)", step = 0.1, display = display.none)
@@ -241,6 +243,30 @@ usd(x) => na(x) ? "-" : "$" + str.tostring(x, "#,##0.00")
[nqAtr, nqPv, nqMt] = request.security(nqSym, "D", dailyInfo(), lookahead = barmerge.lookahead_on)
[ngAtr, ngPv, ngMt] = request.security(ngSym, "D", dailyInfo(), lookahead = barmerge.lookahead_on)
// ---------- automatic gold threshold ----------
// On standard 5-minute bars of the silver/gold ratio: the 6:00 pm bar's open is the reopen, and the 8:15-8:20 bar's close is the 8:19 close.
// The ratio's % change is (1+si)/(1+gc)-1, within ~0.01% of si%-gc% for normal moves. The median is of PRIOR days only
// (computed before today's value is added), over as many days as TradingView has loaded, up to the lookback.
autoThr() =>
var array<float> h = array.new<float>()
var float o18 = na
var float medPrior = na
var float lastGap = na
var int lastDay = na
if hour(time, tz) == 18 and minute(time, tz) == 0
o18 := open
if hour(time, tz) == 8 and minute(time, tz) == 15 and barstate.isconfirmed and not na(o18)
medPrior := h.size() >= minDays ? h.median() : na
g = (close / o18 - 1.0) * 100.0
lastGap := g
lastDay := dayofmonth(time_close, tz)
h.push(math.abs(g))
if h.size() > lookback
h.shift()
o18 := na
[medPrior, h.size(), lastGap, lastDay]
[aThr, aN, aGap, aDay] = request.security(siSym + "/" + gcSym, "5", autoThr())
// ---------- gold decision: once per trading day, as soon as both locked 8:19 moves exist ----------
gap = (sLive - gLive) * 100.0 // live, for the "now" display
gap819 = (s819 - g819) * 100.0 // final, from the closed 8:19 candles
@@ -249,7 +275,6 @@ var array<float> hist = array.new<float>()
var float thr = thrInput
var float goldGap = na
var int goldDir = 0
var bool liveMed = false
var bool goldDone = false
if timeframe.change("D")
@@ -257,9 +282,15 @@ if timeframe.change("D")
goldGap := na
goldDone := false
var string thrSrc = "input"
var bool autoBad = false
if not goldDone and not na(gap819)
liveMed := hist.size() >= minDays
thr := liveMed ? hist.median() : thrInput // prior days only: no look-ahead
sameDay = not na(aDay) and aDay == dayofmonth(time, tz)
if useAuto and sameDay and math.abs(aGap - gap819) > chkTol
autoBad := true // bars not aligned as expected: stop trusting the automatic value
useA = useAuto and not autoBad and not na(aThr)
thr := useA ? aThr : thrInput // the typed-in 250-day median unless the automatic value is switched on and passes its check
thrSrc := useA ? "auto, " + str.tostring(math.min(aN - 1, lookback)) + " days" : (autoBad ? "input; auto failed check" : "input") + "; auto history " + str.tostring(nz(aN)) + " days"
goldGap := gap819
goldDir := gap819 > thr ? 1 : -1
goldDone := true
@@ -321,7 +352,8 @@ if barstate.islast
table.cell(t, 0, 1, "Gold", text_color = color.white)
table.cell(t, 1, 1, "8:21", text_color = color.white)
table.cell(t, 2, 1, sideText(goldDir), text_color = sideColor(goldDir))
table.cell(t, 3, 1, "gap " + num(goldGap) + "% vs " + num(thr) + "%" + (liveMed ? " (live median)" : " (input)") + " | now " + num(gap) + "%", text_color = color.white)
table.cell(t, 3, 1, goldDone ? "locked at 8:19: gap " + num(goldGap) + "% vs " + num(thr) + "% (" + thrSrc + ")" :
"waiting for 8:19 | live gap " + num(gap) + "% vs " + num(useAuto and not na(aThr) and not autoBad ? aThr : thrInput) + "%", text_color = color.white)
table.cell(t, 4, 1, num(gcAtr), text_color = color.white)
table.cell(t, 5, 1, str.tostring(gcN) + (gcMicro ? " MGC" : ""), text_color = gcMicro ? color.yellow : color.white)
table.cell(t, 6, 1, usd(gcRiskUsd), text_color = color.red)