Momentum Range Break Alert - 1m Confirmed + Gaps

jacobamorris · study · 905 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator("Momentum Range Break Alert - 1m Confirmed + Gaps", shorttitle="MRB 1m x2 + Gaps", overlay=false)
 
// =====================================================
// MOMENTUM RANGE BREAK ALERT
// 1m CONFIRMED + GAPS
//
// ALERT / CHART CONTEXT:
// 10 MINUTES
//
// NORMAL BREAK CONFIRMATION:
// COMPLETED 1-MINUTE CANDLE
//
// OPENING GAP CONFIRMATION:
// 9:30 REGULAR-SESSION OPENING PRICE
//
// LEVELS:
//
// PMH = Premarket High
// PML = Premarket Low
// PDH = Previous Day High
// PDL = Previous Day Low
//
// HIGH LEVELS:
// Only valid moving BELOW -> ABOVE.
//
// LOW LEVELS:
// Only valid moving ABOVE -> BELOW.
//
// EACH LEVEL:
// Maximum TWO valid moves per trading day.
//
// OPENING GAPS:
// Immediate opening-price qualification.
// Counts as Move #1.
//
// A gap does NOT require a 1-minute candle close.
//
// Subsequent breaks still require completed
// 1-minute candle confirmation.
//
// RVOL:
// Context only. Never required for an alert.
// =====================================================
 
 
// =====================================================
// INPUTS
// =====================================================
 
rvolSessions = input.int(5, "RVOL Lookback Sessions", minval=1, maxval=5)
 
rvolMinSamples = input.int(3, "Minimum RVOL Samples", minval=1, maxval=5)
 
 
// =====================================================
// REQUIRE 10-MINUTE ALERT CONTEXT
// =====================================================
 
validTimeframe = timeframe.isminutes and timeframe.multiplier == 10
 
 
// =====================================================
// EXTENDED-HOURS SYMBOL
//
// Explicitly request extended-hours data so
// PMH/PML are available.
//
// Actual break signals remain regular-session only.
// =====================================================
 
extendedTicker = ticker.new(
     syminfo.prefix,
     syminfo.ticker,
     session.extended)
 
 
// =====================================================
// LEVEL ENGINE
//
// Tracks:
//
// PMH / PML
// PDH / PDL
// Previous regular-session close
// Current regular-session opening price
// RTH state
// =====================================================
 
f_levels() =>
 
    bool rth = session.ismarket
 
    bool newRth = rth and not rth[1]
 
    bool premarket = not na(time(timeframe.period, "0400-0930", "America/New_York")) and not rth
 
    bool newPremarket = premarket and not premarket[1]
 
 
    var float pmH = na
 
    var float pmL = na
 
 
    var float currentRthHigh = na
 
    var float currentRthLow = na
 
    var float currentRthClose = na
 
 
    var float pdH = na
 
    var float pdL = na
 
    var float pdC = na
 
 
    // ---------------------------------------------
    // PREMARKET RANGE
    // ---------------------------------------------
 
    if newPremarket
 
        pmH := high
 
        pmL := low
 
 
    else if premarket
 
        pmH := math.max(nz(pmH, high), high)
 
        pmL := math.min(nz(pmL, low), low)
 
 
    // ---------------------------------------------
    // REGULAR SESSION RANGE
    // ---------------------------------------------
 
    if newRth
 
        if not na(currentRthHigh)
 
            pdH := currentRthHigh
 
 
        if not na(currentRthLow)
 
            pdL := currentRthLow
 
 
        if not na(currentRthClose)
 
            pdC := currentRthClose
 
 
        currentRthHigh := high
 
        currentRthLow := low
 
        currentRthClose := close
 
 
    else if rth
 
        currentRthHigh := math.max(nz(currentRthHigh, high), high)
 
        currentRthLow := math.min(nz(currentRthLow, low), low)
 
        currentRthClose := close
 
 
    [pmH, pmL, pdH, pdL, pdC, open, rth, newRth]
 
 
// =====================================================
// REQUEST LEVELS
// =====================================================
 
[pmHigh, pmLow, prevDayHigh, prevDayLow, prevDayClose, rthOpen, inRTH, newRTH] = request.security(extendedTicker, timeframe.period, f_levels(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
 
// =====================================================
// DAILY STATE
//
// Each level gets TWO available moves.
// =====================================================
 
varip int pmhMoves = 0
 
varip int pdhMoves = 0
 
varip int pmlMoves = 0
 
varip int pdlMoves = 0
 
 
// =====================================================
// GAP FLAGS
//
// Prevent opening gaps from being counted again
// as normal 1m Break #2 on the first 1m close.
// =====================================================
 
varip bool pmhGapToday = false
 
varip bool pdhGapToday = false
 
varip bool pmlGapToday = false
 
varip bool pdlGapToday = false
 
 
// =====================================================
// DAILY RESET
// =====================================================
 
dayKey = time_tradingday
 
varip int lastResetDayKey = na
 
 
if inRTH and (na(lastResetDayKey) or dayKey != lastResetDayKey)
 
    pmhMoves := 0
 
    pdhMoves := 0
 
    pmlMoves := 0
 
    pdlMoves := 0
 
 
    pmhGapToday := false
 
    pdhGapToday := false
 
    pmlGapToday := false
 
    pdlGapToday := false
 
 
    lastResetDayKey := dayKey
 
 
// =====================================================
// OUTPUT EVENT VARIABLES
// =====================================================
 
float breakTrigger = 0.0
 
int rangeBreak = 0
 
int breakType = 0
 
int moveNumber = 0
 
int gapEvent = 0
 
float breakLevel = na
 
float breakClose = na
 
 
// =====================================================
// OPENING GAP ENGINE
//
// DOES NOT WAIT FOR A 1-MINUTE CLOSE.
//
// At the 9:30 regular-session open:
//
// Open > PMH = PMH Gap Up #1
// Open > PDH = PDH Gap Up #1
//
// Open < PML = PML Gap Down #1
// Open < PDL = PDL Gap Down #1
//
// Each qualifying gap consumes Move #1.
// =====================================================
 
varip int gapProcessedDayKey = na
 
 
if validTimeframe and inRTH and newRTH and (na(gapProcessedDayKey) or dayKey != gapProcessedDayKey)
 
    bool pmhGap = not na(pmHigh) and rthOpen > pmHigh
 
    bool pdhGap = not na(prevDayHigh) and rthOpen > prevDayHigh
 
    bool pmlGap = not na(pmLow) and rthOpen < pmLow
 
    bool pdlGap = not na(prevDayLow) and rthOpen < prevDayLow
 
 
    // ---------------------------------------------
    // HIGH GAPS
    // ---------------------------------------------
 
    if pmhGap
 
        pmhMoves += 1
 
        pmhGapToday := true
 
 
    if pdhGap
 
        pdhMoves += 1
 
        pdhGapToday := true
 
 
    int highGapCode = (pmhGap ? 1 : 0) + (pdhGap ? 2 : 0)
 
 
    if highGapCode > 0
 
        breakTrigger := 1.0
 
        rangeBreak := 1
 
        breakType := highGapCode
 
        moveNumber := 1
 
        gapEvent := 1
 
        breakClose := rthOpen
 
 
        string highGapMessage = ""
 
 
        if highGapCode == 3
 
            breakLevel := math.max(pmHigh, prevDayHigh)
 
            highGapMessage := syminfo.ticker + " - PMH + PDH Gap Up #1"
 
 
        else if highGapCode == 2
 
            breakLevel := prevDayHigh
 
            highGapMessage := syminfo.ticker + " - PDH Gap Up #1"
 
 
        else
 
            breakLevel := pmHigh
 
            highGapMessage := syminfo.ticker + " - PMH Gap Up #1"
 
 
        if barstate.isrealtime
 
            alert(highGapMessage, alert.freq_all)
 
 
    // ---------------------------------------------
    // LOW GAPS
    // ---------------------------------------------
 
    if pmlGap
 
        pmlMoves += 1
 
        pmlGapToday := true
 
 
    if pdlGap
 
        pdlMoves += 1
 
        pdlGapToday := true
 
 
    int lowGapCode = (pmlGap ? 1 : 0) + (pdlGap ? 2 : 0)
 
 
    if lowGapCode > 0
 
        breakTrigger := 1.0
 
        rangeBreak := -1
 
        breakType := -lowGapCode
 
        moveNumber := 1
 
        gapEvent := 1
 
        breakClose := rthOpen
 
 
        string lowGapMessage = ""
 
 
        if lowGapCode == 3
 
            breakLevel := math.min(pmLow, prevDayLow)
 
            lowGapMessage := syminfo.ticker + " - PML + PDL Gap Down #1"
 
 
        else if lowGapCode == 2
 
            breakLevel := prevDayLow
 
            lowGapMessage := syminfo.ticker + " - PDL Gap Down #1"
 
 
        else
 
            breakLevel := pmLow
 
            lowGapMessage := syminfo.ticker + " - PML Gap Down #1"
 
 
        if barstate.isrealtime
 
            alert(lowGapMessage, alert.freq_all)
 
 
    gapProcessedDayKey := dayKey
 
 
// =====================================================
// REQUEST 1-MINUTE INTRABARS
//
// Used for NORMAL range breaks and re-breaks.
//
// Opening gaps do not depend on this request.
// =====================================================
 
[oneOpens, oneCloses, onePrevCloses, oneTimes, oneCloseTimes] = request.security_lower_tf(extendedTicker, "1", [open, close, close[1], time, time_close])
 
 
// =====================================================
// REALTIME PROCESSING STATE
// =====================================================
 
varip int lastProcessedMinuteTime = na
 
 
// =====================================================
// PROCESS COMPLETED 1-MINUTE CANDLES
// =====================================================
 
intrabarCount = array.size(oneCloses)
 
 
if validTimeframe and inRTH and intrabarCount > 0
 
    for i = 0 to intrabarCount - 1
 
        float oneClose = array.get(oneCloses, i)
 
        float onePrevClose = array.get(onePrevCloses, i)
 
        int oneTime = array.get(oneTimes, i)
 
        int oneCloseTime = array.get(oneCloseTimes, i)
 
 
        bool minuteClosed = not barstate.isrealtime or oneCloseTime <= timenow
 
        bool newMinute = na(lastProcessedMinuteTime) or oneCloseTime > lastProcessedMinuteTime
 
 
        if minuteClosed and newMinute
 
            int oneHour = hour(oneTime, "America/New_York")
 
            int oneMinute = minute(oneTime, "America/New_York")
 
            bool openingMinute = oneHour == 9 and oneMinute == 30
 
 
            bool pmhEvent = false
 
            bool pdhEvent = false
 
            bool pmlEvent = false
 
            bool pdlEvent = false
 
 
            // =========================================
            // PMH
            //
            // HIGH LEVEL:
            // Previous 1m close <= PMH
            // Current 1m close > PMH
            // =========================================
 
            if pmhMoves < 2 and not na(pmHigh) and not na(onePrevClose)
 
                bool pmhCross = onePrevClose <= pmHigh and oneClose > pmHigh
 
 
                if openingMinute and pmhGapToday
 
                    pmhCross := false
 
 
                if pmhCross
 
                    pmhMoves += 1
 
                    pmhEvent := true
 
 
            // =========================================
            // PDH
            //
            // HIGH LEVEL:
            // Previous 1m close <= PDH
            // Current 1m close > PDH
            // =========================================
 
            if pdhMoves < 2 and not na(prevDayHigh) and not na(onePrevClose)
 
                bool pdhCross = onePrevClose <= prevDayHigh and oneClose > prevDayHigh
 
 
                if openingMinute and pdhGapToday
 
                    pdhCross := false
 
 
                if pdhCross
 
                    pdhMoves += 1
 
                    pdhEvent := true
 
 
            // =========================================
            // PML
            //
            // LOW LEVEL:
            // Previous 1m close >= PML
            // Current 1m close < PML
            // =========================================
 
            if pmlMoves < 2 and not na(pmLow) and not na(onePrevClose)
 
                bool pmlCross = onePrevClose >= pmLow and oneClose < pmLow
 
 
                if openingMinute and pmlGapToday
 
                    pmlCross := false
 
 
                if pmlCross
 
                    pmlMoves += 1
 
                    pmlEvent := true
 
 
            // =========================================
            // PDL
            //
            // LOW LEVEL:
            // Previous 1m close >= PDL
            // Current 1m close < PDL
            // =========================================
 
            if pdlMoves < 2 and not na(prevDayLow) and not na(onePrevClose)
 
                bool pdlCross = onePrevClose >= prevDayLow and oneClose < prevDayLow
 
 
                if openingMinute and pdlGapToday
 
                    pdlCross := false
 
 
                if pdlCross
 
                    pdlMoves += 1
 
                    pdlEvent := true
 
 
            // =========================================
            // EVENT CODES
            //
            //  1 = PMH
            //  2 = PDH
            //  3 = PMH + PDH
            //
            // -1 = PML
            // -2 = PDL
            // -3 = PML + PDL
            // =========================================
 
            int highCode = (pmhEvent ? 1 : 0) + (pdhEvent ? 2 : 0)
 
            int lowCode = (pmlEvent ? 1 : 0) + (pdlEvent ? 2 : 0)
 
 
            // =========================================
            // BULLISH BREAK
            // =========================================
 
            if highCode > 0
 
                breakTrigger := 1.0
 
                rangeBreak := 1
 
                breakType := highCode
 
                gapEvent := 0
 
                breakClose := oneClose
 
 
                string highMessage = ""
 
 
                if highCode == 3
 
                    breakLevel := math.max(pmHigh, prevDayHigh)
 
                    moveNumber := math.max(pmhMoves, pdhMoves)
 
                    highMessage := syminfo.ticker + " - PMH Break #" + str.tostring(pmhMoves) + " + PDH Break #" + str.tostring(pdhMoves) + "\n1m close confirmed above PMH + PDH"
 
 
                else if highCode == 2
 
                    breakLevel := prevDayHigh
 
                    moveNumber := pdhMoves
 
                    highMessage := syminfo.ticker + " - PDH Break #" + str.tostring(pdhMoves) + "\n1m close confirmed above PDH"
 
 
                else
 
                    breakLevel := pmHigh
 
                    moveNumber := pmhMoves
 
                    highMessage := syminfo.ticker + " - PMH Break #" + str.tostring(pmhMoves) + "\n1m close confirmed above PMH"
 
 
                if barstate.isrealtime
 
                    alert(highMessage, alert.freq_all)
 
 
            // =========================================
            // BEARISH BREAK
            // =========================================
 
            else if lowCode > 0
 
                breakTrigger := 1.0
 
                rangeBreak := -1
 
                breakType := -lowCode
 
                gapEvent := 0
 
                breakClose := oneClose
 
 
                string lowMessage = ""
 
 
                if lowCode == 3
 
                    breakLevel := math.min(pmLow, prevDayLow)
 
                    moveNumber := math.max(pmlMoves, pdlMoves)
 
                    lowMessage := syminfo.ticker + " - PML Break #" + str.tostring(pmlMoves) + " + PDL Break #" + str.tostring(pdlMoves) + "\n1m close confirmed below PML + PDL"
 
 
                else if lowCode == 2
 
                    breakLevel := prevDayLow
 
                    moveNumber := pdlMoves
 
                    lowMessage := syminfo.ticker + " - PDL Break #" + str.tostring(pdlMoves) + "\n1m close confirmed below PDL"
 
 
                else
 
                    breakLevel := pmLow
 
                    moveNumber := pmlMoves
 
                    lowMessage := syminfo.ticker + " - PML Break #" + str.tostring(pmlMoves) + "\n1m close confirmed below PML"
 
 
                if barstate.isrealtime
 
                    alert(lowMessage, alert.freq_all)
 
 
            lastProcessedMinuteTime := oneCloseTime
 
 
// =====================================================
// BREAK DISTANCE
//
// Retained as scanner/debug context.
//
// Not included in the alert message.
// =====================================================
 
float breakDistancePct = na
 
 
if not na(breakClose) and not na(breakLevel) and breakLevel != 0
 
    breakDistancePct := ((breakClose / breakLevel) - 1.0) * 100.0
 
 
// =====================================================
// 10-MINUTE SAME-TIME RVOL
//
// CONTEXT ONLY.
// =====================================================
 
slotsPerDay = 39
 
ringSessions = 6
 
 
var rvolHistory = array.new_float(
     slotsPerDay * ringSessions,
     na)
 
 
var int currentSessionSlot = -1
 
 
if newRTH
 
    currentSessionSlot := (currentSessionSlot + 1) % ringSessions
 
 
rthMinutesFromOpen =
     hour(time, "America/New_York") * 60 +
     minute(time, "America/New_York") -
     570
 
 
rthSlot =
     int(
         math.floor(
             rthMinutesFromOpen /
             10.0))
 
 
float rvolValue = na
 
int rvolSamples = 0
 
 
if inRTH and currentSessionSlot >= 0 and rthSlot >= 0 and rthSlot < slotsPerDay
 
    float volumeSum = 0.0
 
 
    for lookback = 1 to rvolSessions
 
        int priorSession = (currentSessionSlot - lookback + ringSessions) % ringSessions
 
        int historyIndex = priorSession * slotsPerDay + rthSlot
 
        float historyVolume = array.get(rvolHistory, historyIndex)
 
 
        if not na(historyVolume)
 
            volumeSum += historyVolume
 
            rvolSamples += 1
 
 
    if rvolSamples >= rvolMinSamples
 
        float averageVolume = volumeSum / rvolSamples
 
 
        if averageVolume > 0
 
            rvolValue := volume / averageVolume
 
 
// =====================================================
// STORE COMPLETED 10m VOLUME
// =====================================================
 
if inRTH and barstate.isconfirmed and currentSessionSlot >= 0 and rthSlot >= 0 and rthSlot < slotsPerDay
 
    int currentHistoryIndex = currentSessionSlot * slotsPerDay + rthSlot
 
    array.set(
         rvolHistory,
         currentHistoryIndex,
         volume)
 
 
// =====================================================
// DAY %
// =====================================================
 
float dayPct = na
 
 
if not na(prevDayClose) and prevDayClose != 0
 
    dayPct := ((close / prevDayClose) - 1.0) * 100.0
 
 
// =====================================================
// OUTPUTS
//
// IMPORTANT:
//
// The first plot has a unique name so you can verify
// that TradingView is using THIS version of the script.
// =====================================================
 
 
// 1 = new qualifying event
// 0 = nothing new
 
plot(
     validTimeframe ? breakTrigger : na,
     "MRB Trigger - 2 Moves + Gaps")
 
 
// +1 = bullish
// -1 = bearish
 
plot(
     validTimeframe ? rangeBreak : na,
     "Range Break")
 
 
//  1 = PMH
//  2 = PDH
//  3 = PMH + PDH
//
// -1 = PML
// -2 = PDL
// -3 = PML + PDL
 
plot(
     validTimeframe ? breakType : na,
     "Break Type")
 
 
// 1 = first move
// 2 = second move
 
plot(
     validTimeframe ? moveNumber : na,
     "Move Number")
 
 
// 1 = opening gap
// 0 = normal break
 
plot(
     validTimeframe ? gapEvent : na,
     "Opening Gap")
 
 
// Diagnostic only
 
plot(
     validTimeframe ? breakClose : na,
     "Break Price")
 
plot(
     validTimeframe ? breakLevel : na,
     "Break Level")
 
plot(
     validTimeframe ? breakDistancePct : na,
     "Break Distance %")
 
plot(
     validTimeframe ? rvolValue : na,
     "10m RVOL Live")
 
plot(
     validTimeframe ? dayPct : na,
     "Day %")

← 返回列表