Joshua S&D Zones Multi Time Frame

keybanger · study · 196 行 · 点赞 170 · TradingView 原页

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

Pine Script

//@version=6
indicator(title="Joshua S&D Zones Multi Time Frame", shorttitle="Josh S&D Zones Multi", overlay=true, max_boxes_count=500)
// ── Inputs ──────────────────────────────────────────────
showImbalance = input.bool(false, "Show Imbalance Candles (Chart TF)")
imbColor      = input.color(color.new(#000000, 0), "Imbalance Body Color")
minSize       = input.float(0.0, "Minimum Imbalance Size", minval=0.0, step=0.01)
maxConsec     = input.int(1, "Max Consecutive Imbalances", minval=1, maxval=20)
showRects     = input.bool(true, "Show Order Blocks", group="Order Blocks")
chartMode     = input.string("With Other TFs", "Chart Timeframe OBs", options=["Off", "With Other TFs", "Chart TF Only"], group="Order Blocks", tooltip="Off: only the 5 timeframes below. With Other TFs: chart timeframe plus the 5 below. Chart TF Only: ignore the 5 below.")
// Chart timeframe style controls
chartFill   = input.color(color.new(#153899, 75), "Chart Fill", group="Order Blocks")
chartBorder = input.color(color.new(#153899, 25), "Chart Border", group="Order Blocks")
chartText   = input.color(color.new(#9c27b0, 0), "Chart Text", group="Order Blocks")
chartWidth  = input.int(1, "Chart Border Width", minval=1, maxval=5, group="Order Blocks")
chartStyle  = input.string("Solid", "Chart Border Style", options=["Solid", "Dashed", "Dotted"], group="Order Blocks")
chartSize   = input.string("normal", "Chart Text Size", options=["tiny", "small", "normal", "large", "huge"], group="Order Blocks")
 
// Timeframe picker: "None" turns a slot off
tfOptions = array.from("None", "1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "1D", "1W", "1M")
tfVal(string s) =>
    switch s
        "1m"  => "1"
        "3m"  => "3"
        "5m"  => "5"
        "15m" => "15"
        "30m" => "30"
        "1h"  => "60"
        "2h"  => "120"
        "4h"  => "240"
        "1D"  => "1D"
        "1W"  => "1W"
        "1M"  => "1M"
        => ""
 
htf1 = tfVal(input.string("None", "Timeframe 1", options=["None", "1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "1D", "1W", "1M"], group="TF 1"))
c1Fill   = input.color(color.new(#000000, 94), "Fill", group="TF 1")
c1Border = input.color(color.new(#464646, 70), "Border", group="TF 1")
c1Text   = input.color(color.new(#636363, 0), "Text", group="TF 1")
c1Width  = input.int(1, "Border Width", minval=1, maxval=5, group="TF 1")
c1Style  = input.string("Solid", "Border Style", options=["Solid", "Dashed", "Dotted"], group="TF 1")
c1Size   = input.string("normal", "Text Size", options=["tiny", "small", "normal", "large", "huge"], group="TF 1")
htf2 = tfVal(input.string("None", "Timeframe 2", options=["None", "1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "1D", "1W", "1M"], group="TF 2"))
c2Fill   = input.color(color.new(#000000, 94), "Fill", group="TF 2")
c2Border = input.color(color.new(#1b4c75, 40), "Border", group="TF 2")
c2Text   = input.color(color.new(#1b4c75, 0), "Text", group="TF 2")
c2Width  = input.int(1, "Border Width", minval=1, maxval=5, group="TF 2")
c2Style  = input.string("Solid", "Border Style", options=["Solid", "Dashed", "Dotted"], group="TF 2")
c2Size   = input.string("normal", "Text Size", options=["tiny", "small", "normal", "large", "huge"], group="TF 2")
htf3 = tfVal(input.string("None", "Timeframe 3", options=["None", "1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "1D", "1W", "1M"], group="TF 3"))
c3Fill   = input.color(color.new(#000000, 94), "Fill", group="TF 3")
c3Border = input.color(color.new(#ffeb3b, 85), "Border", group="TF 3")
c3Text   = input.color(color.new(#ffeb3b, 80), "Text", group="TF 3")
c3Width  = input.int(1, "Border Width", minval=1, maxval=5, group="TF 3")
c3Style  = input.string("Solid", "Border Style", options=["Solid", "Dashed", "Dotted"], group="TF 3")
c3Size   = input.string("normal", "Text Size", options=["tiny", "small", "normal", "large", "huge"], group="TF 3")
htf4 = tfVal(input.string("None", "Timeframe 4", options=["None", "1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "1D", "1W", "1M"], group="TF 4"))
c4Fill   = input.color(color.new(#000000, 94), "Fill", group="TF 4")
c4Border = input.color(color.new(#311b92, 30), "Border", group="TF 4")
c4Text   = input.color(color.new(#311b92, 0), "Text", group="TF 4")
c4Width  = input.int(1, "Border Width", minval=1, maxval=5, group="TF 4")
c4Style  = input.string("Solid", "Border Style", options=["Solid", "Dashed", "Dotted"], group="TF 4")
c4Size   = input.string("normal", "Text Size", options=["tiny", "small", "normal", "large", "huge"], group="TF 4")
htf5 = tfVal(input.string("None", "Timeframe 5", options=["None", "1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "1D", "1W", "1M"], group="TF 5"))
c5Fill   = input.color(color.new(#000000, 94), "Fill", group="TF 5")
c5Border = input.color(color.new(#0e8841, 70), "Border", group="TF 5")
c5Text   = input.color(color.new(#0e8841, 40), "Text", group="TF 5")
c5Width  = input.int(1, "Border Width", minval=1, maxval=5, group="TF 5")
c5Style  = input.string("Solid", "Border Style", options=["Solid", "Dashed", "Dotted"], group="TF 5")
c5Size   = input.string("normal", "Text Size", options=["tiny", "small", "normal", "large", "huge"], group="TF 5")
 
// ── Chart-TF imbalances (visual only) ───────────────────
prevOpen    = open[1]
prevClose   = close[1]
prevBodyTop = math.max(prevOpen, prevClose)
prevBodyBot = math.min(prevOpen, prevClose)
isDown      = prevClose < prevOpen
leftHigh  = high[2]
leftLow   = low[2]
rightHigh = high
rightLow  = low
zoneBot = isDown ? rightHigh : leftHigh
zoneTop = isDown ? leftLow   : rightLow
hasGapZone = zoneTop > zoneBot
segTop = math.min(prevBodyTop, zoneTop)
segBot = math.max(prevBodyBot, zoneBot)
hasSegment = hasGapZone and segTop > segBot
segmentSize = hasSegment ? segTop - segBot : 0.0
leftCovers  = leftHigh >= segTop and leftLow <= segBot
rightCovers = rightHigh >= segTop and rightLow <= segBot
rawImbalance = hasSegment and not leftCovers and not rightCovers and segmentSize >= minSize
var int consecCount = 0
if rawImbalance
    consecCount += 1
else
    consecCount := 0
showThis = rawImbalance and consecCount <= maxConsec
barcolor(showImbalance and showThis ? imbColor : na, offset=-1)
 
// ── Shared box array for mitigation ─────────────────────
var box[] activeBoxes = array.new<box>()
 
// ── Shared HTF function (confirmed bars only) ───────────
htfData() =>
    float pO = open[2]
    float pC = close[2]
    float pBT = math.max(pO, pC)
    float pBB = math.min(pO, pC)
    bool  pDown = pC < pO
    float zBot = pDown ? high[1] : high[3]
    float zTop = pDown ? low[3] : low[1]
    bool  gap = zTop > zBot
    float sTop = math.min(pBT, zTop)
    float sBot = math.max(pBB, zBot)
    bool  hasS = gap and sTop > sBot
    float sSize = hasS ? sTop - sBot : 0
    bool  lCov = high[3] >= sTop and low[3] <= sBot
    bool  rCov = high[1] >= sTop and low[1] <= sBot
    bool  imb = hasS and not lCov and not rCov and sSize >= minSize
    float preO = open[3]
    float preC = close[3]
    float preH = high[3]
    float preL = low[3]
    int   preT = time[3]
    [imb, pDown, preO, preC, preH, preL, preT]
 
// ── Process one timeframe ───────────────────────────────
processTF(string tf, color fillCol, color borderCol, color textCol, int borderWidth, string borderStyleStr, string textSize) =>
    if tf != "" and showRects
        float chartSec = timeframe.in_seconds()
        float tfSec    = timeframe.in_seconds(tf)
        if tfSec >= chartSec
            [imb, isDn, pO, pC, pH, pL, pT] = request.security(syminfo.tickerid, tf, htfData(), lookahead=barmerge.lookahead_off)
            bool newBar = ta.change(time(tf)) != 0
            var int consec = 0
            if newBar
                consec := imb ? consec + 1 : 0
            bool doShow = imb and consec <= maxConsec and newBar
            if doShow
                float preBodyTop = math.max(pO, pC)
                float preBodyBot = math.min(pO, pC)
                float topPrice = isDn ? pH : preBodyTop
                float botPrice = isDn ? preBodyBot : pL
                int leftBar = bar_index
                bool sameTF = tfSec == chartSec
                if sameTF
                    leftBar := bar_index - 3
                else
                    int maxLook = timeframe.isintraday and timeframe.multiplier == 1 ? 2000 : 500
                    bool isDailyOrHigher = tf == "1D" or tf == "D" or tf == "1W" or tf == "W" or tf == "1M"
                    bool isLowChart = timeframe.isintraday and timeframe.multiplier <= 5
                    if isDailyOrHigher and isLowChart
                        maxLook := timeframe.multiplier == 1 ? 6000 : 4000
                    for i = 0 to maxLook
                        if time[i] <= pT
                            leftBar := bar_index - i
                            break
                string tfText = tf == "1D" or tf == "D" ? "1D" : tf == "1W" or tf == "W" ? "1W" : tf == "1M" ? "1M" : tf == "60" ? "1h" : tf == "120" ? "2h" : tf == "180" ? "3h" : tf == "240" ? "4h" : (not na(str.tonumber(tf)) and str.tonumber(tf) < 60 ? tf + "m" : tf)
                borderStyle = borderStyleStr == "Dashed" ? line.style_dashed : borderStyleStr == "Dotted" ? line.style_dotted : line.style_solid
                b = box.new(leftBar, topPrice, bar_index + 1, botPrice,
                     border_color=borderCol,
                     border_width=borderWidth,
                     border_style=borderStyle,
                     bgcolor=fillCol,
                     extend=extend.right,
                     text=tfText,
                     text_color=textCol,
                     text_size=textSize)
                array.push(activeBoxes, b)
 
// ── Run timeframes ──────────────────────────────────────
bool showChartTF = chartMode != "Off"
bool showOtherTFs = chartMode != "Chart TF Only"
 
// Skip a slot that matches the chart timeframe when the chart TF is already drawn
dedupe(string tf) =>
    showChartTF and tf != "" and timeframe.in_seconds(tf) == timeframe.in_seconds() ? "" : tf
 
if showChartTF
    processTF(timeframe.period, chartFill, chartBorder, chartText, chartWidth, chartStyle, chartSize)
if showOtherTFs
    processTF(dedupe(htf1), c1Fill, c1Border, c1Text, c1Width, c1Style, c1Size)
    processTF(dedupe(htf2), c2Fill, c2Border, c2Text, c2Width, c2Style, c2Size)
    processTF(dedupe(htf3), c3Fill, c3Border, c3Text, c3Width, c3Style, c3Size)
    processTF(dedupe(htf4), c4Fill, c4Border, c4Text, c4Width, c4Style, c4Size)
    processTF(dedupe(htf5), c5Fill, c5Border, c5Text, c5Width, c5Style, c5Size)
 
// ── Stop boxes when tagged ──────────────────────────────
if array.size(activeBoxes) > 0
    for i = array.size(activeBoxes) - 1 to 0
        bx = array.get(activeBoxes, i)
        float bTop = box.get_top(bx)
        float bBot = box.get_bottom(bx)
        if high >= bBot and low <= bTop
            box.set_right(bx, bar_index)
            box.set_extend(bx, extend.none)
            array.remove(activeBoxes, i)

← 返回列表