Apostle ITS MTF Institutional Zones Companion

hiddenriches · study · 121 行 · 点赞 19 · TradingView 原页

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

Pine Script

//@version=6
indicator("Apostle ITS — MTF Institutional Zones (Companion)", overlay=true, max_boxes_count=400, max_lines_count=300, max_labels_count=100)
// Standalone MTF companion for ITS. Place on the same chart as the main ITS indicator.
// TradingView does not allow independent indicators to pass their internal zone states to one another.
string mtfGroup = "MTF Institutional Zones"
bool mtfOn = input.bool(true, "Enable Higher-Timeframe Zones", group=mtfGroup)
bool mtfOBOn = input.bool(true, "Show HTF Order Blocks", group=mtfGroup)
string mtfOBTF = input.timeframe("240", "Order Block Timeframe", group=mtfGroup)
bool mtfSDOn = input.bool(true, "Show HTF Supply / Demand", group=mtfGroup)
string mtfSDTF = input.timeframe("60", "Supply / Demand Timeframe", group=mtfGroup)
bool mtfSROn = input.bool(true, "Show HTF Support / Resistance", group=mtfGroup)
string mtfSRTF = input.timeframe("240", "Support / Resistance Timeframe", group=mtfGroup)
bool mtfFVGOn = input.bool(true, "Show HTF Fair Value Gaps", group=mtfGroup)
string mtfFVGTF = input.timeframe("60", "FVG Timeframe", group=mtfGroup)
int mtfPivotLen = input.int(3, "Confirmed HTF Pivot Length", minval=2, maxval=20, group=mtfGroup)
float mtfImpulseATR = input.float(1.0, "Order Block Displacement (ATR)", minval=0.2, step=0.1, group=mtfGroup)
float mtfZoneATR = input.float(0.35, "Supply / Demand Zone Width (ATR)", minval=0.05, step=0.05, group=mtfGroup)
int mtfKeep = input.int(5, "Zones / Levels to Keep per Module", minval=1, maxval=12, group=mtfGroup)
bool mtfExtend = input.bool(true, "Extend Zones Right", group=mtfGroup)
bool mtfRemoveInvalid = input.bool(true, "Remove Invalidated Zones", group=mtfGroup)
string mtfInvalidation = input.string("Close", "Zone Invalidation", options=["Close", "Wick"], group=mtfGroup)
color mtfBullFill = input.color(color.new(color.teal, 85), "Bullish Zone Color", group=mtfGroup)
color mtfBearFill = input.color(color.new(color.red, 85), "Bearish Zone Color", group=mtfGroup)
color mtfSRColor = input.color(color.new(color.yellow, 25), "S/R Line Color", group=mtfGroup)
 
// All security expressions reference completed HTF bars only. Lookahead ON is safe
// here because each expression is offset by one confirmed HTF candle.
[obTime, obO, obH, obL, obC, obPrevO, obPrevH, obPrevL, obPrevC, obATR] = request.security(syminfo.tickerid, mtfOBTF, [time[1], open[1], high[1], low[1], close[1], open[2], high[2], low[2], close[2], ta.atr(14)[1]], lookahead=barmerge.lookahead_on)
[sdTime, sdHigh, sdLow, sdATR] = request.security(syminfo.tickerid, mtfSDTF, [time[1], ta.pivothigh(high, mtfPivotLen, mtfPivotLen)[1], ta.pivotlow(low, mtfPivotLen, mtfPivotLen)[1], ta.atr(14)[1]], lookahead=barmerge.lookahead_on)
[srTime, srHigh, srLow] = request.security(syminfo.tickerid, mtfSRTF, [time[1], ta.pivothigh(high, mtfPivotLen, mtfPivotLen)[1], ta.pivotlow(low, mtfPivotLen, mtfPivotLen)[1]], lookahead=barmerge.lookahead_on)
[fvgTime, fvgBullTop, fvgBullBottom, fvgBearTop, fvgBearBottom] = request.security(syminfo.tickerid, mtfFVGTF, [time[1], low[1] > high[3] ? low[1] : na, low[1] > high[3] ? high[3] : na, high[1] < low[3] ? low[3] : na, high[1] < low[3] ? high[1] : na], lookahead=barmerge.lookahead_on)
 
var array<box> mtfBoxes = array.new<box>()
var array<int> mtfBoxKinds = array.new<int>() // 0=OB, 1=S/D, 2=FVG
var array<bool> mtfBoxBulls = array.new<bool>()
var array<line> mtfSRLines = array.new<line>()
var array<float> mtfSRPrices = array.new<float>()
var array<bool> mtfSRBulls = array.new<bool>()
 
f_mtf_add_box(float upper, float lower, bool bull, int kind, string tf, string name) =>
    if mtfOn and not na(upper) and not na(lower) and upper > lower
        color tint = bull ? mtfBullFill : mtfBearFill
        box b = box.new(bar_index, upper, bar_index + 1, lower, xloc=xloc.bar_index, extend=mtfExtend ? extend.right : extend.none, bgcolor=tint, border_color=color.new(tint, 20), text=tf + " " + name, text_color=color.new(color.white, 15), text_size=size.tiny, force_overlay=true)
        array.push(mtfBoxes, b)
        array.push(mtfBoxKinds, kind)
        array.push(mtfBoxBulls, bull)
        int kindCount = 0
        for i = array.size(mtfBoxes) - 1 to 0
            if array.get(mtfBoxKinds, i) == kind
                kindCount += 1
                if kindCount > mtfKeep * 2
                    box.delete(array.remove(mtfBoxes, i))
                    array.remove(mtfBoxKinds, i)
                    array.remove(mtfBoxBulls, i)
 
f_mtf_add_sr(float priceLevel, bool bull, string tf) =>
    if mtfOn and not na(priceLevel)
        line l = line.new(bar_index, priceLevel, bar_index + 1, priceLevel, xloc=xloc.bar_index, extend=mtfExtend ? extend.right : extend.none, color=mtfSRColor, style=line.style_dashed, width=2, force_overlay=true)
        array.push(mtfSRLines, l)
        array.push(mtfSRPrices, priceLevel)
        array.push(mtfSRBulls, bull)
        if array.size(mtfSRLines) > mtfKeep * 2
            line.delete(array.shift(mtfSRLines))
            array.shift(mtfSRPrices)
            array.shift(mtfSRBulls)
 
f_mtf_update_zones() =>
    // New HTF candle means the preceding candle has just been confirmed.
    if mtfOn and mtfOBOn and ta.change(obTime) != 0 and not na(obATR)
        bool bullOB = obC > obO and obPrevC < obPrevO and obC - obO >= obATR * mtfImpulseATR and obC > obPrevH
        bool bearOB = obC < obO and obPrevC > obPrevO and obO - obC >= obATR * mtfImpulseATR and obC < obPrevL
        if bullOB
            f_mtf_add_box(obPrevH, obPrevL, true, 0, mtfOBTF, "Bull OB")
        if bearOB
            f_mtf_add_box(obPrevH, obPrevL, false, 0, mtfOBTF, "Bear OB")
    if mtfOn and mtfSDOn and ta.change(sdTime) != 0
        if not na(sdHigh)
            f_mtf_add_box(sdHigh, sdHigh - sdATR * mtfZoneATR, false, 1, mtfSDTF, "Supply")
        if not na(sdLow)
            f_mtf_add_box(sdLow + sdATR * mtfZoneATR, sdLow, true, 1, mtfSDTF, "Demand")
    if mtfOn and mtfSROn and ta.change(srTime) != 0
        if not na(srHigh)
            f_mtf_add_sr(srHigh, false, mtfSRTF)
        if not na(srLow)
            f_mtf_add_sr(srLow, true, mtfSRTF)
    if mtfOn and mtfFVGOn and ta.change(fvgTime) != 0
        f_mtf_add_box(fvgBullTop, fvgBullBottom, true, 2, mtfFVGTF, "Bull FVG")
        f_mtf_add_box(fvgBearTop, fvgBearBottom, false, 2, mtfFVGTF, "Bear FVG")
 
    bool mtfLongZone = false
    bool mtfShortZone = false
    if mtfOn
        if array.size(mtfBoxes) > 0
            for i = array.size(mtfBoxes) - 1 to 0
                box b = array.get(mtfBoxes, i)
                bool bull = array.get(mtfBoxBulls, i)
                float upper = box.get_top(b)
                float lower = box.get_bottom(b)
                bool invalid = bull ? (mtfInvalidation == "Close" ? close < lower : low < lower) : (mtfInvalidation == "Close" ? close > upper : high > upper)
                if mtfRemoveInvalid and invalid
                    box.delete(array.remove(mtfBoxes, i))
                    array.remove(mtfBoxKinds, i)
                    array.remove(mtfBoxBulls, i)
                else
                    if not mtfExtend
                        box.set_right(b, bar_index + 1)
                    if close >= lower and close <= upper
                        mtfLongZone := mtfLongZone or bull
                        mtfShortZone := mtfShortZone or not bull
        if array.size(mtfSRLines) > 0
            for i = array.size(mtfSRLines) - 1 to 0
                line l = array.get(mtfSRLines, i)
                if not mtfExtend
                    line.set_x2(l, bar_index + 1)
                if mtfRemoveInvalid and (array.get(mtfSRBulls, i) ? close < array.get(mtfSRPrices, i) : close > array.get(mtfSRPrices, i))
                    line.delete(array.remove(mtfSRLines, i))
                    array.remove(mtfSRPrices, i)
                    array.remove(mtfSRBulls, i)
    [mtfLongZone, mtfShortZone]
 
[mtfLongZone, mtfShortZone] = f_mtf_update_zones()

← 返回列表