Morning Range Filter (Trade / Skip)

lapaznyangel · study · 105 行 · 点赞 28 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lapaznyangel
 
//@version=6
indicator("Morning Range Filter (Trade / Skip)", shorttitle="MR Filter", overlay=true, max_labels_count=500)
 
// ---------------------------------------------------------------------------
// Decides at 10:15 ET whether the first 45 minutes of trading (9:30-10:14) were
// "bigger than usual". If so, it says SKIP (far-strike iron condors did poorly on
// those days in the backtest); otherwise TRADE.
//
//   morning range % = (high - low of 9:30-10:15 ET) / price
//   typical day %   = average of the previous N daily ranges (high-low)/close
//   ratio           = morning range % / typical day %
//   cutoff          = the chosen percentile of the ratio over the previous 60 days
//   SKIP if ratio > cutoff
//
// Use on a 1, 5 or 15 minute chart of SPY (regular trading hours).
// This does NOT see option quote sizes ("thin depth"); check that separately.
// Not financial advice.
// ---------------------------------------------------------------------------
 
pctl    = input.float(67, "Skip if ratio is above this percentile", minval=1, maxval=99, step=1)
histN   = input.int(60, "Days of history used for the percentile", minval=20, maxval=250)
adrN    = input.int(20, "Days used for the 'typical day' range", minval=5, maxval=60)
minHist = input.int(30, "Days of history needed before judging", minval=10, maxval=120)
showBg  = input.bool(true, "Tint the rest of the day")
showTbl = input.bool(true, "Show table")
 
if timeframe.in_seconds() > 900
    runtime.error("Use a chart of 15 minutes or shorter.")
 
tz = "America/New_York"
todEnd = hour(time_close, tz) * 60 + minute(time_close, tz)      // minutes after midnight at the END of this bar
dayKey = year(time, tz) * 10000 + month(time, tz) * 100 + dayofmonth(time, tz)
newDay = dayKey != nz(dayKey[1])
 
// Typical daily range: average of the previous adrN daily ranges, known before today opens.
adr = request.security(syminfo.tickerid, "D", ta.sma((high - low) / close * 100, adrN)[1], lookahead = barmerge.lookahead_on)
 
var float mHi = na
var float mLo = na
var float[] hist = array.new_float()
var int   dec = na          // 1 = skip today, 0 = trade today, na = not decided yet
var float lastMR = na
var float lastAdr = na
var float lastRatio = na
var float lastCut = na
var int   lastDec = na
var string lastDate = ""
 
if newDay
    mHi := na
    mLo := na
    dec := na
 
inMorning = todEnd > 570 and todEnd <= 615      // bars that end between 9:30 and 10:15 ET
if inMorning
    mHi := na(mHi) ? high : math.max(mHi, high)
    mLo := na(mLo) ? low  : math.min(mLo, low)
 
decideNow = todEnd == 615 and barstate.isconfirmed and not na(mHi) and not na(adr)
if decideNow
    mr = (mHi - mLo) / close * 100
    ratio = mr / adr
    cut = hist.size() >= minHist ? hist.percentile_linear_interpolation(pctl) : na
    dec := na(cut) ? na : (ratio > cut ? 1 : 0)
    lastMR := mr
    lastAdr := adr
    lastRatio := ratio
    lastCut := cut
    lastDec := dec
    lastDate := str.format_time(time, "yyyy-MM-dd", tz)
    hist.push(ratio)                 // today's ratio joins the history for FUTURE days
    if hist.size() > histN
        hist.shift()
    if not na(dec)
        label.new(bar_index, high, dec == 1 ? "SKIP" : "TRADE", yloc = yloc.price,
                  style = label.style_label_down,
                  color = dec == 1 ? color.new(color.red, 10) : color.new(color.green, 10),
                  textcolor = color.white, size = size.normal,
                  tooltip = "ratio " + str.tostring(ratio, "#.00") + " vs cutoff " + str.tostring(cut, "#.00"))
 
afterDecision = todEnd > 615 and todEnd <= 960
bgcolor(showBg and afterDecision and dec == 1 ? color.new(color.red, 92) : showBg and afterDecision and dec == 0 ? color.new(color.green, 94) : na)
 
alertcondition(decideNow and dec == 1, "Skip today", "Morning range is bigger than usual: SKIP today's far-strike condors")
alertcondition(decideNow and dec == 0, "Trade today", "Morning range is normal: TRADE today")
 
var table t = table.new(position.top_right, 2, 7, border_width = 1)
if showTbl and barstate.islast
    t.cell(0, 0, "Last decision", text_color = color.white, bgcolor = color.new(color.gray, 20))
    t.cell(1, 0, lastDate, text_color = color.white, bgcolor = color.new(color.gray, 20))
    t.cell(0, 1, "Morning range % (9:30-10:15)")
    t.cell(1, 1, na(lastMR) ? "-" : str.tostring(lastMR, "#.00") + "%")
    t.cell(0, 2, "Typical day range % (" + str.tostring(adrN) + "d)")
    t.cell(1, 2, na(lastAdr) ? "-" : str.tostring(lastAdr, "#.00") + "%")
    t.cell(0, 3, "Ratio")
    t.cell(1, 3, na(lastRatio) ? "-" : str.tostring(lastRatio, "#.00"))
    t.cell(0, 4, "Cutoff (p" + str.tostring(pctl, "#") + " of " + str.tostring(histN) + "d)")
    t.cell(1, 4, na(lastCut) ? "building history" : str.tostring(lastCut, "#.00"))
    t.cell(0, 5, "Days of history")
    t.cell(1, 5, str.tostring(hist.size()))
    t.cell(0, 6, "Decision", text_color = color.white, bgcolor = lastDec == 1 ? color.red : lastDec == 0 ? color.green : color.gray)
    t.cell(1, 6, na(lastDec) ? "n/a" : lastDec == 1 ? "SKIP" : "TRADE", text_color = color.white, bgcolor = lastDec == 1 ? color.red : lastDec == 0 ? color.green : color.gray)

← 返回列表