One Time Framing Regime [OTF]

rocascemte · study · 162 行 · 点赞 9 · TradingView 原页

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

Pine Script

//@version=6
// =============================================================================
// One Time Framing (OTF) Regime  —  Auction Market Theory regime classifier
// -----------------------------------------------------------------------------
// Classifies each bar into one of three regimes:
//    UP      (+1) : One-Time-Framing Up   -> imbalance, buyers in control
//    DOWN    (-1) : One-Time-Framing Down -> imbalance, sellers in control
//    BALANCE  (0) : fair value / rangebound (the absence of one-time-framing)
//
// STRICT definition (per the video):
//    OTF-up  bar = higher high AND higher low AND close ABOVE prior bar's high
//    OTF-down bar = lower high  AND lower low  AND close BELOW prior bar's low
//
// CLASSIC definition (traditional Dalton):
//    OTF-up  bar = higher low   (+ close above prior high to confirm)
//    OTF-down bar = lower high  (+ close below prior low  to confirm)
//
// STATE MACHINE (the important part):
//    A single failed bar does NOT flip you to the opposite regime — it drops
//    you into BALANCE. You stay in balance until price CLOSES beyond the
//    balance range extreme: close > range-high => resume UP,
//    close < range-low => begin DOWN. While in balance the range expands to
//    the highest high / lowest low seen since balance began (the fair-value
//    bracket + your breakout levels).
// =============================================================================
 
indicator("One Time Framing Regime [OTF]", shorttitle="OTF Regime", overlay=true,
     max_lines_count=500, max_labels_count=500)
 
// ============================ INPUTS =========================================
grpDef = "Regime Definition"
mode = input.string("Strict (HH/HL + close beyond)", "Bar classification",
     options=["Strict (HH/HL + close beyond)", "Classic (higher low / lower high)"],
     group=grpDef,
     tooltip="Strict = the video's rule: to one-time-frame UP a bar must make a higher high, a higher low, AND close above the prior bar's high (mirror for down). Classic = traditional auction rule: higher lows for up / lower highs for down, confirmed by close beyond the prior extreme.")
 
grpViz = "Visuals"
showBG      = input.bool(true,  "Color background by regime",              group=grpViz)
colorBars   = input.bool(true,  "Color price bars by regime",              group=grpViz)
showRange   = input.bool(true,  "Show balance range (fair-value bracket)", group=grpViz)
showMarkers = input.bool(true,  "Show regime-change markers",              group=grpViz)
upCol   = input.color(#26a69a, "Up",      group=grpViz, inline="c1")
dnCol   = input.color(#ef5350, "Down",    group=grpViz, inline="c1")
balCol  = input.color(#787b86, "Balance", group=grpViz, inline="c1")
bgTrans = input.int(90, "Background transparency", minval=50, maxval=100, group=grpViz)
 
grpMTF = "Multi-Timeframe Table"
showTable = input.bool(true,  "Show MTF regime table", group=grpMTF)
tf1 = input.timeframe("D", "Timeframe 1", group=grpMTF)
tf2 = input.timeframe("W", "Timeframe 2", group=grpMTF)
tf3 = input.timeframe("M", "Timeframe 3", group=grpMTF)
tblPos  = input.string("Top Right", "Table position",
     options=["Top Right","Top Left","Bottom Right","Bottom Left"], group=grpMTF)
tblSize = input.string("Normal", "Table size",
     options=["Tiny","Small","Normal","Large"], group=grpMTF)
 
// ============================ CORE LOGIC =====================================
// Returns [regime, rangeHigh, rangeLow]. regime: 1=UP, -1=DOWN, 0=BALANCE.
f_regime() =>
    strict  = mode == "Strict (HH/HL + close beyond)"
    upBar   = strict ? (high > high[1] and low > low[1] and close > high[1])
                     : (low  > low[1]  and close > high[1])
    downBar = strict ? (high < high[1] and low < low[1] and close < low[1])
                     : (high < high[1] and close < low[1])
 
    var int   regime = 0
    var float rHigh  = na
    var float rLow   = na
 
    if barstate.isfirst
        regime := 0
        rHigh  := high
        rLow   := low
    else
        if regime == 1
            if upBar
                regime := 1
            else
                // up cycle broken -> enter balance, seed the bracket
                regime := 0
                rHigh  := math.max(high, high[1])
                rLow   := math.min(low,  low[1])
        else if regime == -1
            if downBar
                regime := -1
            else
                regime := 0
                rHigh  := math.max(high, high[1])
                rLow   := math.min(low,  low[1])
        else
            // BALANCE: only a close beyond the bracket flips the regime
            if close > rHigh
                regime := 1
            else if close < rLow
                regime := -1
            else
                rHigh := math.max(rHigh, high)
                rLow  := math.min(rLow,  low)
    [regime, rHigh, rLow]
 
// Current chart timeframe
[regime, rHigh, rLow] = f_regime()
 
// Higher-timeframe regimes for the table (developing HTF bar updates live)
[r1, rh1, rl1] = request.security(syminfo.tickerid, tf1, f_regime(), lookahead=barmerge.lookahead_off)
[r2, rh2, rl2] = request.security(syminfo.tickerid, tf2, f_regime(), lookahead=barmerge.lookahead_off)
[r3, rh3, rl3] = request.security(syminfo.tickerid, tf3, f_regime(), lookahead=barmerge.lookahead_off)
 
// ============================ VISUALS ========================================
regimeCol = regime == 1 ? upCol : regime == -1 ? dnCol : balCol
 
barcolor(colorBars ? regimeCol : na, title="Regime bar color")
bgcolor(showBG ? color.new(regimeCol, bgTrans) : na, title="Regime background")
 
// Balance bracket (drawn only while in balance) — your breakout / fade levels
plot(showRange and regime == 0 ? rHigh : na, "Balance High",
     color=balCol, style=plot.style_linebr, linewidth=2)
plot(showRange and regime == 0 ? rLow  : na, "Balance Low",
     color=balCol, style=plot.style_linebr, linewidth=2)
 
// Regime-change markers
upStart  = regime ==  1 and regime[1] !=  1
dnStart  = regime == -1 and regime[1] != -1
balStart = regime ==  0 and regime[1] !=  0
plotshape(showMarkers and upStart,  "OTF Up resumes", style=shape.triangleup,
     location=location.belowbar, color=upCol,  size=size.small)
plotshape(showMarkers and dnStart,  "OTF Down begins", style=shape.triangledown,
     location=location.abovebar, color=dnCol,  size=size.small)
plotshape(showMarkers and balStart, "Balance begins", style=shape.diamond,
     location=location.abovebar, color=balCol, size=size.tiny)
 
// ============================ MTF TABLE ======================================
f_txt(r)  => r == 1 ? "UP" : r == -1 ? "DOWN" : "BALANCE"
f_col(r)  => r == 1 ? upCol : r == -1 ? dnCol : balCol
 
var table t = na
if showTable
    pos  = tblPos == "Top Right"    ? position.top_right    :
           tblPos == "Top Left"     ? position.top_left     :
           tblPos == "Bottom Right" ? position.bottom_right : position.bottom_left
    sz   = tblSize == "Tiny"  ? size.tiny  :
           tblSize == "Small" ? size.small :
           tblSize == "Large" ? size.large : size.normal
    if na(t)
        t := table.new(pos, 2, 5, border_width=1)
    if barstate.islast
        table.clear(t, 0, 0, 1, 4)
        table.cell(t, 0, 0, "Timeframe", text_color=color.white, bgcolor=color.new(color.gray, 30), text_size=sz)
        table.cell(t, 1, 0, "Regime",    text_color=color.white, bgcolor=color.new(color.gray, 30), text_size=sz)
        table.cell(t, 0, 1, tf3, text_color=color.white, text_size=sz)
        table.cell(t, 1, 1, f_txt(r3), text_color=color.white, bgcolor=color.new(f_col(r3), 20), text_size=sz)
        table.cell(t, 0, 2, tf2, text_color=color.white, text_size=sz)
        table.cell(t, 1, 2, f_txt(r2), text_color=color.white, bgcolor=color.new(f_col(r2), 20), text_size=sz)
        table.cell(t, 0, 3, tf1, text_color=color.white, text_size=sz)
        table.cell(t, 1, 3, f_txt(r1), text_color=color.white, bgcolor=color.new(f_col(r1), 20), text_size=sz)
        table.cell(t, 0, 4, "Chart", text_color=color.white, text_size=sz)
        table.cell(t, 1, 4, f_txt(regime), text_color=color.white, bgcolor=color.new(regimeCol, 20), text_size=sz)
 
// ============================ ALERTS =========================================
alertcondition(upStart,  "OTF Up resumes",  "One-Time-Framing UP resumed")
alertcondition(dnStart,  "OTF Down begins", "One-Time-Framing DOWN began")
alertcondition(balStart, "Balance begins",  "Regime entered BALANCE")

← 返回列表