Created
May 25, 2025 23:51
-
-
Save valmont/41960dfbe982dd2fdc3f431238186dd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@version=6 | |
indicator("TTR + Supertrend + NY Clarity Filter [Indicator]", overlay=true) | |
// --- Supertrend Parameters --- | |
atrPeriod = input.int(10, "ATR Length", minval=1) | |
factor = input.float(3.0, "Factor", minval=0.01, step=0.01) | |
// --- TTR Inputs --- | |
ttrLongCondition = ta.crossover(ta.sma(close, 5), ta.sma(close, 20)) | |
ttrShortCondition = ta.crossunder(ta.sma(close, 5), ta.sma(close, 20)) | |
// --- Supertrend Calculation --- | |
[supertrend, direction] = ta.supertrend(factor, atrPeriod) | |
// --- NY Clarity Filter Inputs --- | |
nyOpenHour = input.int(9, "NY Open Hour (ET)", minval=0, maxval=23) | |
nyOpenMin = input.int(30, "NY Open Minute (ET)", minval=0, maxval=59) | |
premarketStartHour = input.int(7, "Premarket Start Hour (ET)", minval=0, maxval=23) | |
premarketStartMin = input.int(0, "Premarket Start Minute (ET)", minval=0, maxval=59) | |
premarketVolThreshold = input.float(80, "Premarket Volume % Threshold", minval=0, maxval=200) | |
overnightRangeCompressionPct = input.float(50, "Max Overnight Range Compression %", minval=0, maxval=100) | |
openingVolumeThrustMultiplier = input.float(2.0, "Opening Minute Volume Thrust Multiplier", minval=0.1, maxval=10) | |
// --- Helper Function --- | |
f_isInSession(_startH, _startM, _endH, _endM) => | |
(hour > _startH or (hour == _startH and minute >= _startM)) and (hour < _endH or (hour == _endH and minute <= _endM)) | |
premarketSession = f_isInSession(premarketStartHour, premarketStartMin, nyOpenHour, nyOpenMin) | |
nySession = f_isInSession(nyOpenHour, nyOpenMin, 16, 0) | |
// --- NY Clarity Filter Data Storage --- | |
var float[] premarketVols = array.new_float() | |
var float premarketVolToday = 0.0 | |
var int premarketDay_ = na | |
if dayofmonth != premarketDay_ | |
premarketDay_ := dayofmonth | |
premarketVolToday := 0.0 | |
if premarketSession | |
premarketVolToday += volume | |
if (hour == nyOpenHour and minute == nyOpenMin and barstate.islast) | |
array.unshift(premarketVols, premarketVolToday) | |
if array.size(premarketVols) > 10 | |
array.pop(premarketVols) | |
premarketVolAvg = array.size(premarketVols) > 0 ? array.avg(premarketVols) : 0.0 | |
premarketVolPct = premarketVolAvg > 0 ? (premarketVolToday / premarketVolAvg) * 100 : 100 | |
filter1 = premarketVolPct >= premarketVolThreshold | |
prevClose = request.security(syminfo.tickerid, "D", close[1]) | |
todayOpen = open | |
overnightRange = math.abs(todayOpen - prevClose) | |
var float[] overnightRanges = array.new_float() | |
if (hour == nyOpenHour and minute == nyOpenMin and barstate.islast) | |
array.unshift(overnightRanges, overnightRange) | |
if array.size(overnightRanges) > 10 | |
array.pop(overnightRanges) | |
overnightRangeAvg = array.size(overnightRanges) > 0 ? array.avg(overnightRanges) : 0.0 | |
compressionPct = overnightRangeAvg > 0 ? (overnightRange / overnightRangeAvg) * 100 : 100 | |
filter2 = compressionPct <= overnightRangeCompressionPct | |
prevHigh = request.security(syminfo.tickerid, "D", high[1]) | |
prevLow = request.security(syminfo.tickerid, "D", low[1]) | |
sweptPDH = high > prevHigh and close < prevHigh | |
sweptPDL = low < prevLow and close > prevLow | |
filter3 = sweptPDH or sweptPDL | |
var float openingMinVolume = na | |
if (hour == nyOpenHour and minute == nyOpenMin) | |
openingMinVolume := volume | |
var float[] preNyVolumes = array.new_float() | |
if not nySession and not premarketSession and barstate.islast | |
array.unshift(preNyVolumes, volume) | |
if array.size(preNyVolumes) > 10 | |
array.pop(preNyVolumes) | |
preNyVolAvg = array.size(preNyVolumes) > 0 ? array.avg(preNyVolumes) : 0.0 | |
filter4 = openingMinVolume >= preNyVolAvg * openingVolumeThrustMultiplier | |
filterScore = (filter1 ? 1 : 0) + (filter2 ? 1 : 0) + (filter3 ? 1 : 0) + (filter4 ? 1 : 0) | |
sessionLikelyClean = filterScore >= 3 | |
longSupertrend = direction < 0 | |
shortSupertrend = direction > 0 | |
ttrLongSignal = ttrLongCondition and longSupertrend | |
ttrShortSignal = ttrShortCondition and shortSupertrend | |
plotshape(ttrLongSignal and sessionLikelyClean, title="Long Entry", location=location.belowbar, color=color.green, style=shape.labelup, text="Long") | |
plotshape(ttrShortSignal and sessionLikelyClean, title="Short Entry", location=location.abovebar, color=color.red, style=shape.labeldown, text="Short") | |
plotshape(ttrLongSignal and not sessionLikelyClean, title="Long Blocked", location=location.belowbar, color=color.gray, style=shape.xcross, text="Blocked") | |
plotshape(ttrShortSignal and not sessionLikelyClean, title="Short Blocked", location=location.abovebar, color=color.gray, style=shape.xcross, text="Blocked") | |
bgcolor(sessionLikelyClean ? color.new(color.green, 90) : color.new(color.red, 90)) | |
var label dash = na | |
if na(dash) | |
dash := label.new(bar_index, high, "", xloc.bar_index, yloc.price, style=label.style_label_left, color=color.new(color.black, 80), textcolor=color.white, size=size.small) | |
// Construct dashboard string separately to avoid multi-line assignment errors | |
dashboardText = "NY Clarity Filter\n" | |
dashboardText += "Premkt Vol %: " + str.tostring(premarketVolPct, "#.0") + "% (" + (filter1 ? "✔" : "✘") + ")\n" | |
dashboardText += "Overnight Range Compression: " + str.tostring(compressionPct, "#.0") + "% (" + (filter2 ? "✔" : "✘") + ")\n" | |
dashboardText += "PDH/PDL Sweep+Reject: " + (filter3 ? "✔" : "✘") + "\n" | |
dashboardText += "Opening Vol Thrust: " + (filter4 ? "✔" : "✘") + "\n" | |
dashboardText += "Score: " + str.tostring(filterScore) + "/4\n" | |
dashboardText += "Session Clean: " + (sessionLikelyClean ? "✔" : "✘") | |
label.set_xy(dash, bar_index, high) | |
label.set_text(dash, dashboardText) | |
alertcondition(not sessionLikelyClean and (ttrLongSignal or ttrShortSignal), title="Blocked Trade Due to NY Clarity Filter", message="Trade blocked because NY session likely not clean.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment