9:30 Sweep + Performance

krisskross18 · study · 277 行 · 点赞 23 · TradingView 原页

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

Pine Script

//@version=6
indicator("Miss YM | 9:30 Sweep + Performance", overlay=true, max_lines_count=500, max_labels_count=500)
 
// ─────────────────────────────
// SETTINGS
// ─────────────────────────────
string TZ = "America/New_York"
 
groupSession = "Session"
rangeSess = input.session("0812-0912", "Range (Eastern)", group=groupSession)
tradeSess = input.session("0930-1100", "Entry Window (Eastern)", group=groupSession)
exitSess = input.session("0930-1600", "Trade Management Window", group=groupSession)
 
groupSetup = "Setup"
maxConfirm = input.int(5, "Bars Allowed After Sweep", minval=1, maxval=30, group=groupSetup)
obLookback = input.int(5, "Order Block Lookback", minval=1, maxval=20, group=groupSetup)
stopBuffer = input.int(2, "Stop Buffer (Ticks)", minval=0, group=groupSetup)
maxRisk = input.float(0.0, "Maximum Stop Distance (Points, 0 = Off)", minval=0, group=groupSetup)
 
groupStats = "Performance"
lookbackDays = input.int(30, "Trading Sessions to Track", options=[20, 30, 60], group=groupStats)
ambiguousLoss = input.bool(true, "Count Same-Bar Stop/Target as Loss", group=groupStats)
 
groupVisual = "Appearance"
showLevels = input.bool(true, "Show Range Levels", group=groupVisual)
showSignals = input.bool(true, "Show Entry Signals", group=groupVisual)
showResults = input.bool(true, "Show Win/Loss Labels", group=groupVisual)
showDashboard = input.bool(true, "Show Performance Dashboard", group=groupVisual)
 
gold = color.rgb(203, 159, 61)
blue = color.rgb(85, 176, 204)
dark = color.rgb(35, 39, 45)
 
// ─────────────────────────────
// SESSION DETECTION
// ─────────────────────────────
inRange = not na(time(timeframe.period, rangeSess, TZ))
inEntry = not na(time(timeframe.period, tradeSess, TZ))
inManage = not na(time(timeframe.period, exitSess, TZ))
newRange = inRange and not inRange[1]
endRange = not inRange and inRange[1]
endManage = not inManage and inManage[1]
 
// ─────────────────────────────
// DAILY STATE
// ─────────────────────────────
var float rHigh = na
var float rLow = na
var bool ready = false
var bool traded = false
var int sweepDir = 0
var int sweepIndex = na
var float sweepExtreme = na
var float obHigh = na
var float obLow = na
var int tradeDir = 0
var float entryPrice = na
var float stopPrice = na
var float targetPrice = na
var int entryIndex = na
 
var array<int> outcomes = array.new_int()
var array<int> sessionIDs = array.new_int()
var int currentID = na
var int currentOutcome = 0
 
var line highLine = na
var line lowLine = na
 
// ─────────────────────────────
// RANGE BUILDING
// ─────────────────────────────
if newRange
    currentID := year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
    rHigh := high
    rLow := low
    ready := false
    traded := false
    sweepDir := 0
    sweepIndex := na
    sweepExtreme := na
    obHigh := na
    obLow := na
    tradeDir := 0
    currentOutcome := 0
    highLine := na
    lowLine := na
 
    array.push(sessionIDs, currentID)
    array.push(outcomes, 0)
    if array.size(outcomes) > lookbackDays
        array.shift(outcomes)
        array.shift(sessionIDs)
 
if inRange
    rHigh := math.max(nz(rHigh, high), high)
    rLow := math.min(nz(rLow, low), low)
 
if endRange
    ready := true
    if showLevels
        highLine := line.new(bar_index, rHigh, bar_index + 1, rHigh, color=gold, width=2)
        lowLine := line.new(bar_index, rLow, bar_index + 1, rLow, color=gold, width=2)
 
if ready and showLevels
    if not na(highLine)
        line.set_x2(highLine, bar_index)
    if not na(lowLine)
        line.set_x2(lowLine, bar_index)
 
// ─────────────────────────────
// SWEEP DETECTION
// ─────────────────────────────
canScan = ready and inEntry and not traded and tradeDir == 0
 
highSweep = canScan and sweepDir == 0 and high > rHigh
lowSweep = canScan and sweepDir == 0 and low < rLow
 
// Avoid ambiguous double-sided sweep candles.
if highSweep and not lowSweep
    sweepDir := -1
    sweepIndex := bar_index
    sweepExtreme := high
    obHigh := na
    obLow := na
    for i = 1 to obLookback
        if na(obHigh) and close[i] > open[i]
            obHigh := high[i]
            obLow := low[i]
    if na(obHigh)
        obHigh := high
        obLow := math.min(open, close)
 
if lowSweep and not highSweep
    sweepDir := 1
    sweepIndex := bar_index
    sweepExtreme := low
    obHigh := na
    obLow := na
    for i = 1 to obLookback
        if na(obLow) and close[i] < open[i]
            obHigh := high[i]
            obLow := low[i]
    if na(obLow)
        obHigh := math.max(open, close)
        obLow := low
 
// Update sweep extreme while waiting for confirmation.
if sweepDir == -1 and tradeDir == 0
    sweepExtreme := math.max(sweepExtreme, high)
if sweepDir == 1 and tradeDir == 0
    sweepExtreme := math.min(sweepExtreme, low)
 
// ─────────────────────────────
// ENTRY CONFIRMATION
// ─────────────────────────────
validWindow = not na(sweepIndex) and bar_index > sweepIndex and bar_index - sweepIndex <= maxConfirm
 
sell = barstate.isconfirmed and canScan and validWindow and sweepDir == -1 and close < obLow and close < rHigh and close < open
buy = barstate.isconfirmed and canScan and validWindow and sweepDir == 1 and close > obHigh and close > rLow and close > open
 
candidateStop = sell ? sweepExtreme + stopBuffer * syminfo.mintick : buy ? sweepExtreme - stopBuffer * syminfo.mintick : na
candidateTarget = sell ? rLow : buy ? rHigh : na
risk = sell ? candidateStop - close : buy ? close - candidateStop : na
reward = sell ? close - candidateTarget : buy ? candidateTarget - close : na
 
riskOK = (buy or sell) and risk > 0 and reward > 0 and (maxRisk == 0 or risk <= maxRisk)
enterBuy = buy and riskOK
enterSell = sell and riskOK
 
if enterBuy or enterSell
    tradeDir := enterBuy ? 1 : -1
    entryPrice := close
    stopPrice := candidateStop
    targetPrice := candidateTarget
    entryIndex := bar_index
    traded := true
    sweepDir := 0
    currentOutcome := 2
 
    if array.size(outcomes) > 0
        array.set(outcomes, array.size(outcomes) - 1, 2)
 
    if showLevels
        line.new(bar_index, stopPrice, bar_index + 30, stopPrice, color=color.red, style=line.style_dotted)
        line.new(bar_index, targetPrice, bar_index + 30, targetPrice, color=gold, style=line.style_dashed)
 
if not na(sweepIndex) and bar_index - sweepIndex > maxConfirm and tradeDir == 0
    sweepDir := 0
 
// ─────────────────────────────
// TRADE OUTCOME
// ─────────────────────────────
// Entries are modeled at the signal candle close.
// Exit evaluation begins on the following candle.
if tradeDir != 0 and bar_index > entryIndex
    stopHit = tradeDir == 1 ? low <= stopPrice : high >= stopPrice
    targetHit = tradeDir == 1 ? high >= targetPrice : low <= targetPrice
 
    int result = 0
    if stopHit and targetHit
        result := ambiguousLoss ? -1 : 1
    else if stopHit
        result := -1
    else if targetHit
        result := 1
 
    if result != 0
        currentOutcome := result
        if array.size(outcomes) > 0
            array.set(outcomes, array.size(outcomes) - 1, result)
        if showResults
            label.new(bar_index, close, result == 1 ? "WIN" : "LOSS", style=label.style_label_left, color=result == 1 ? blue : color.red, textcolor=color.white, size=size.tiny)
        tradeDir := 0
 
// Unresolved positions at the end of the management window
// remain recorded as open/unresolved (2).
if endManage and tradeDir != 0
    tradeDir := 0
 
// ─────────────────────────────
// VISUAL SIGNALS
// ─────────────────────────────
plotshape(showSignals and enterBuy, title="BUY", text="BUY", style=shape.labelup, location=location.belowbar, color=blue, textcolor=color.white, size=size.tiny)
plotshape(showSignals and enterSell, title="SELL", text="SELL", style=shape.labeldown, location=location.abovebar, color=dark, textcolor=color.white, size=size.tiny)
 
plotshape(showSignals and highSweep and not lowSweep, title="High Sweep", style=shape.xcross, location=location.abovebar, color=gold, size=size.tiny)
plotshape(showSignals and lowSweep and not highSweep, title="Low Sweep", style=shape.xcross, location=location.belowbar, color=gold, size=size.tiny)
 
// ─────────────────────────────
// PERFORMANCE DASHBOARD
// ─────────────────────────────
var table dash = table.new(position.top_right, 2, 8, bgcolor=color.new(color.white, 5), border_width=1)
 
if barstate.islast and showDashboard
    int wins = 0
    int losses = 0
    int unresolved = 0
    int noTrade = 0
 
    if array.size(outcomes) > 0
        for i = 0 to array.size(outcomes) - 1
            result = array.get(outcomes, i)
            if result == 1
                wins += 1
            else if result == -1
                losses += 1
            else if result == 2
                unresolved += 1
            else
                noTrade += 1
 
    totalTrades = wins + losses
    winRate = totalTrades > 0 ? 100.0 * wins / totalTrades : na
 
    table.cell(dash, 0, 0, "MISS YM", bgcolor=dark, text_color=color.white)
    table.cell(dash, 1, 0, "9:30 SWEEP", bgcolor=dark, text_color=gold)
    table.cell(dash, 0, 1, "Sessions", text_color=color.black)
    table.cell(dash, 1, 1, str.tostring(array.size(outcomes)), text_color=color.black)
    table.cell(dash, 0, 2, "Wins", text_color=color.black)
    table.cell(dash, 1, 2, str.tostring(wins), text_color=color.teal)
    table.cell(dash, 0, 3, "Losses", text_color=color.black)
    table.cell(dash, 1, 3, str.tostring(losses), text_color=color.red)
    table.cell(dash, 0, 4, "Win Rate", text_color=color.black)
    table.cell(dash, 1, 4, na(winRate) ? "N/A" : str.tostring(winRate, "#.0") + "%", text_color=color.black)
    table.cell(dash, 0, 5, "No Trade", text_color=color.black)
    table.cell(dash, 1, 5, str.tostring(noTrade), text_color=color.black)
    table.cell(dash, 0, 6, "Unresolved", text_color=color.black)
    table.cell(dash, 1, 6, str.tostring(unresolved), text_color=color.black)
    table.cell(dash, 0, 7, "Closed Trades", text_color=color.black)
    table.cell(dash, 1, 7, str.tostring(totalTrades), text_color=color.black)
 
// ─────────────────────────────
// ALERTS
// ─────────────────────────────
alertcondition(enterBuy, "Miss YM BUY", "Miss YM bullish sweep confirmation")
alertcondition(enterSell, "Miss YM SELL", "Miss YM bearish sweep confirmation")

← 返回列表