Hidden OB+

arleay · study · 309 行 · 点赞 862 · TradingView 原页

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

Pine Script

//@version=6
indicator("Hidden OB+", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
 
string patternTf = input.timeframe("240", "Pattern Timeframe", group = "Pattern Detection",
     tooltip = "Timeframe used to identify the two completed candles. The chart timeframe must be equal to or lower than this value.")
string lookbackChoice = input.string("1 Month", "Historical Lookback",
     options = ["1 Day", "1 Week", "1 Month", "Max"], group = "Pattern Detection")
int maxVisible = input.int(30, "Maximum Visible Zones", minval = 1, maxval = 200, group = "Pattern Detection",
     tooltip = "Display limit. When exceeded, the oldest zone and all its drawings are removed together.")
 
bool showBullish = input.bool(true, "Show Bullish Zones", group = "Bullish Zones")
color bullishColorInput = input.color(color.black, "Line and Label Color", group = "Bullish Zones")
string bullishStyleChoice = input.string("Dotted", "Boundary Line Style",
     options = ["Dotted", "Dashed", "Solid"], group = "Bullish Zones")
int bullishWidth = input.int(1, "Boundary Line Width", minval = 1, maxval = 4, group = "Bullish Zones")
bool showBullishTag = input.bool(true, "Show Zone Label", group = "Bullish Zones")
string bullishTagSize = input.string("Tiny", "Label Size",
     options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Bullish Zones")
 
bool showBearish = input.bool(true, "Show Bearish Zones", group = "Bearish Zones")
color bearishColorInput = input.color(color.black, "Line and Label Color", group = "Bearish Zones")
string bearishStyleChoice = input.string("Dotted", "Boundary Line Style",
     options = ["Dotted", "Dashed", "Solid"], group = "Bearish Zones")
int bearishWidth = input.int(1, "Boundary Line Width", minval = 1, maxval = 4, group = "Bearish Zones")
bool showBearishTag = input.bool(true, "Show Zone Label", group = "Bearish Zones")
string bearishTagSize = input.string("Tiny", "Label Size",
     options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Bearish Zones")
 
string displayMode = input.string("Boundary Lines", "Zone Display",
     options = ["Boundary Lines", "Filled Boxes"], group = "Display Settings",
     tooltip = "Boundary Lines uses the minimal line presentation. Filled Boxes uses native rectangles with separate box settings.")
string stylePreset = input.string("Monochrome", "Line Color Scheme",
     options = ["Monochrome", "Directional Colors"], group = "Display Settings",
     tooltip = "Monochrome uses black boundary lines and labels. Directional Colors applies separate bullish and bearish colors.")
 
color bullishBoxFill = input.color(color.new(color.teal, 88), "Background Color", group = "Bullish Box Style")
color bullishBoxBorder = input.color(color.teal, "Border Color", group = "Bullish Box Style")
string bullishBoxBorderStyle = input.string("Solid", "Border Style",
     options = ["Dotted", "Dashed", "Solid"], group = "Bullish Box Style")
int bullishBoxBorderWidth = input.int(1, "Border Width", minval = 1, maxval = 4, group = "Bullish Box Style")
string bullishBoxText = input.string("HOB+", "Box Label Text", group = "Bullish Box Style")
 
color bearishBoxFill = input.color(color.new(color.red, 88), "Background Color", group = "Bearish Box Style")
color bearishBoxBorder = input.color(color.red, "Border Color", group = "Bearish Box Style")
string bearishBoxBorderStyle = input.string("Solid", "Border Style",
     options = ["Dotted", "Dashed", "Solid"], group = "Bearish Box Style")
int bearishBoxBorderWidth = input.int(1, "Border Width", minval = 1, maxval = 4, group = "Bearish Box Style")
string bearishBoxText = input.string("HOB-", "Box Label Text", group = "Bearish Box Style")
 
string boxExtendChoice = input.string("None", "Horizontal Extension", options = ["None", "Left", "Right", "Both"], group = "Box Options")
bool showBoxText = input.bool(false, "Display Box Text", group = "Box Options")
color boxTextColor = input.color(color.black, "Text Color", group = "Box Options")
string boxTextSize = input.string("Tiny", "Text Size",
     options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Box Options")
string boxTextHAlign = input.string("Center", "Horizontal Text Alignment",
     options = ["Left", "Center", "Right"], group = "Box Options")
string boxTextVAlign = input.string("Center", "Vertical Text Alignment",
     options = ["Top", "Center", "Bottom"], group = "Box Options")
bool boxTextWrap = input.bool(true, "Wrap Text", group = "Box Options")
 
bool showCE = input.bool(true, "Show CE Line", group = "Consequent Encroachment",
     tooltip = "Draws the Consequent Encroachment at the midpoint of each zone in Filled Boxes mode.")
color ceColor = input.color(color.black, "CE Line Color", group = "Consequent Encroachment")
string ceStyleChoice = input.string("Dashed", "CE Line Style",
     options = ["Dotted", "Dashed", "Solid"], group = "Consequent Encroachment")
int ceWidth = input.int(1, "CE Line Width", minval = 1, maxval = 4, group = "Consequent Encroachment")
 
f_lineStyle(string choice) =>
    switch choice
        "Dashed" => line.style_dashed
        "Solid" => line.style_solid
        => line.style_dotted
 
f_labelSize(string choice) =>
    switch choice
        "Small" => size.small
        "Normal" => size.normal
        "Large" => size.large
        "Huge" => size.huge
        => size.tiny
 
f_boxExtend(string choice) =>
    switch choice
        "Left" => extend.left
        "Right" => extend.right
        "Both" => extend.both
        => extend.none
 
f_textHAlign(string choice) =>
    switch choice
        "Left" => text.align_left
        "Right" => text.align_right
        => text.align_center
 
f_textVAlign(string choice) =>
    switch choice
        "Top" => text.align_top
        "Bottom" => text.align_bottom
        => text.align_center
 
f_lookbackStart(string choice, int referenceTime) =>
    int refYear = year(referenceTime, "America/New_York")
    int refMonth = month(referenceTime, "America/New_York")
    int refDay = dayofmonth(referenceTime, "America/New_York")
    int refHour = hour(referenceTime, "America/New_York")
    int refMinute = minute(referenceTime, "America/New_York")
    int thisMonthStart = timestamp("America/New_York", refYear, refMonth, 1, refHour, refMinute)
    int previousMonthTime = thisMonthStart - 60000
    int previousMonthYear = year(previousMonthTime, "America/New_York")
    int previousMonth = month(previousMonthTime, "America/New_York")
    int previousMonthLastDay = dayofmonth(previousMonthTime, "America/New_York")
    int previousMonthDay = math.min(refDay, previousMonthLastDay)
    switch choice
        "1 Day" => timestamp("America/New_York", refYear, refMonth, refDay - 1, refHour, refMinute)
        "1 Week" => timestamp("America/New_York", refYear, refMonth, refDay - 7, refHour, refMinute)
        "1 Month" => timestamp("America/New_York", previousMonthYear, previousMonth, previousMonthDay, refHour, refMinute)
        => 0
 
f_zoneColor(int side) =>
    stylePreset == "Monochrome" ? color.black : side == 1 ? bullishColorInput : bearishColorInput
 
float chartSeconds = timeframe.in_seconds()
float patternSeconds = timeframe.in_seconds(patternTf)
if barstate.isfirst and not na(chartSeconds) and not na(patternSeconds) and chartSeconds > patternSeconds
    runtime.error("Pattern timeframe must be equal to or higher than the chart timeframe.")
 
int lookbackStart = f_lookbackStart(lookbackChoice, last_bar_time)
 
// Offset requests with lookahead_on expose only completed pattern-timeframe
// candles. The pair is checked at the next pattern-timeframe boundary.
[recentOpen, recentHigh, recentLow, recentClose, recentTime,
 olderOpen, olderHigh, olderLow, olderClose, olderTime] = request.security(
     syminfo.tickerid,
     patternTf,
     [open[1], high[1], low[1], close[1], time[1], open[2], high[2], low[2], close[2], time[2]],
     gaps = barmerge.gaps_off,
     lookahead = barmerge.lookahead_on)
 
bool newPatternPeriod = timeframe.change(patternTf)
bool havePair = not na(recentTime) and not na(olderTime)
bool withinLookback = lookbackChoice == "Max" or recentTime >= lookbackStart
 
// The candle shadows must intersect with positive width. The plotted range
// follows the tutorial's outer-wick boundaries, not just the intersection.
float bullishOverlapLow = math.max(olderClose, recentLow)
float bullishOverlapHigh = math.min(olderHigh, recentOpen)
float bearishOverlapLow = math.max(olderLow, recentOpen)
float bearishOverlapHigh = math.min(olderClose, recentHigh)
 
bool bullishWicksOverlap = havePair and
     olderClose > olderOpen and recentClose > recentOpen and
     olderHigh > olderClose and recentLow < recentOpen and
     bullishOverlapHigh > bullishOverlapLow
 
bool bearishWicksOverlap = havePair and
     olderClose < olderOpen and recentClose < recentOpen and
     olderLow < olderClose and recentHigh > recentOpen and
     bearishOverlapHigh > bearishOverlapLow
 
bool bullishSignal = newPatternPeriod and withinLookback and bullishWicksOverlap
bool bearishSignal = newPatternPeriod and withinLookback and bearishWicksOverlap
 
var array<line> upperLines = array.new<line>()
var array<line> lowerLines = array.new<line>()
var array<line> centerLines = array.new<line>()
var array<box> zoneBoxes = array.new<box>()
var array<label> zoneTags = array.new<label>()
var array<int> zoneSides = array.new<int>()
var array<float> zoneTops = array.new<float>()
var array<float> zoneBottoms = array.new<float>()
 
f_removeZone(int index) =>
    box zoneBox = array.get(zoneBoxes, index)
    line upperLine = array.get(upperLines, index)
    line lowerLine = array.get(lowerLines, index)
    line centerLine = array.get(centerLines, index)
    label zoneTag = array.get(zoneTags, index)
    if not na(zoneBox)
        box.delete(zoneBox)
    if not na(upperLine)
        line.delete(upperLine)
    if not na(lowerLine)
        line.delete(lowerLine)
    if not na(centerLine)
        line.delete(centerLine)
    if not na(zoneTag)
        label.delete(zoneTag)
    array.remove(zoneBoxes, index)
    array.remove(upperLines, index)
    array.remove(lowerLines, index)
    array.remove(centerLines, index)
    array.remove(zoneTags, index)
    array.remove(zoneSides, index)
    array.remove(zoneTops, index)
    array.remove(zoneBottoms, index)
 
f_addZone(int leftTime, int rightTime, float topPrice, float bottomPrice, int side) =>
    bool isBullish = side == 1
    string styleChoice = isBullish ? bullishStyleChoice : bearishStyleChoice
    int lineWidth = isBullish ? bullishWidth : bearishWidth
    string tagSizeChoice = isBullish ? bullishTagSize : bearishTagSize
    bool drawTag = isBullish ? showBullishTag : showBearishTag
    color drawColor = f_zoneColor(side)
    string tagText = isBullish ? "HOB+" : "HOB-"
    string tagTooltip = isBullish
         ? "Bullish higher-timeframe wick-overlap pattern. Wait for a return to the range and assess lower-timeframe CISD/MSS. This mark alone is not entry confirmation."
         : "Bearish higher-timeframe wick-overlap pattern. Wait for a return to the range and assess lower-timeframe CISD/MSS. This mark alone is not entry confirmation."
    box zoneBox = na
    line upperLine = na
    line lowerLine = na
    line centerLine = na
    label zoneTag = na
    if displayMode == "Filled Boxes"
        color boxFill = isBullish ? bullishBoxFill : bearishBoxFill
        color boxBorder = isBullish ? bullishBoxBorder : bearishBoxBorder
        string borderStyle = isBullish ? bullishBoxBorderStyle : bearishBoxBorderStyle
        int borderWidth = isBullish ? bullishBoxBorderWidth : bearishBoxBorderWidth
        string boxText = isBullish ? bullishBoxText : bearishBoxText
        zoneBox := box.new(
             left = leftTime, top = topPrice, right = rightTime, bottom = bottomPrice,
             border_color = boxBorder, border_width = borderWidth,
             border_style = f_lineStyle(borderStyle), extend = f_boxExtend(boxExtendChoice),
             xloc = xloc.bar_time, bgcolor = boxFill,
             text = showBoxText ? boxText : "", text_size = f_labelSize(boxTextSize),
             text_color = boxTextColor, text_halign = f_textHAlign(boxTextHAlign),
             text_valign = f_textVAlign(boxTextVAlign),
             text_wrap = boxTextWrap ? text.wrap_auto : text.wrap_none,
             text_font_family = font.family_monospace)
        if showCE
            float cePrice = (topPrice + bottomPrice) * 0.5
            centerLine := line.new(
                 x1 = leftTime, y1 = cePrice, x2 = rightTime, y2 = cePrice,
                 xloc = xloc.bar_time, extend = f_boxExtend(boxExtendChoice),
                 color = ceColor, style = f_lineStyle(ceStyleChoice), width = ceWidth)
    else
        upperLine := line.new(
             x1 = leftTime, y1 = topPrice, x2 = rightTime, y2 = topPrice,
             xloc = xloc.bar_time, extend = extend.none, color = drawColor,
             style = f_lineStyle(styleChoice), width = lineWidth)
        lowerLine := line.new(
             x1 = leftTime, y1 = bottomPrice, x2 = rightTime, y2 = bottomPrice,
             xloc = xloc.bar_time, extend = extend.none, color = drawColor,
             style = f_lineStyle(styleChoice), width = lineWidth)
        if drawTag
            int centerTime = leftTime + int(math.round((rightTime - leftTime) * 0.5))
            zoneTag := label.new(
                 x = centerTime, y = topPrice, text = tagText,
                 xloc = xloc.bar_time, yloc = yloc.price,
                 color = color.new(color.white, 100), style = label.style_none,
                 textcolor = drawColor, size = f_labelSize(tagSizeChoice),
                 textalign = text.align_center, tooltip = tagTooltip,
                 text_font_family = font.family_monospace)
    array.push(zoneBoxes, zoneBox)
    array.push(upperLines, upperLine)
    array.push(lowerLines, lowerLine)
    array.push(centerLines, centerLine)
    array.push(zoneTags, zoneTag)
    array.push(zoneSides, side)
    array.push(zoneTops, topPrice)
    array.push(zoneBottoms, bottomPrice)
    while array.size(upperLines) > maxVisible
        f_removeZone(0)
 
// Extend a setup only through a chart bar that touches its range. A trade
// through the distal edge kills the whole setup before any lower-timeframe
// confirmation is inferred.
if barstate.isconfirmed and array.size(upperLines) > 0
    int zoneIndex = array.size(upperLines) - 1
    while zoneIndex >= 0
        int side = array.get(zoneSides, zoneIndex)
        float zoneTop = array.get(zoneTops, zoneIndex)
        float zoneBottom = array.get(zoneBottoms, zoneIndex)
        bool tradedThrough = side == 1 ? low < zoneBottom : high > zoneTop
        bool touched = high >= zoneBottom and low <= zoneTop
        if tradedThrough
            f_removeZone(zoneIndex)
        else if touched
            int touchEndTime = na(time_close) ? time : time_close
            line upperLine = array.get(upperLines, zoneIndex)
            line lowerLine = array.get(lowerLines, zoneIndex)
            line centerLine = array.get(centerLines, zoneIndex)
            box zoneBox = array.get(zoneBoxes, zoneIndex)
            label zoneTag = array.get(zoneTags, zoneIndex)
            if not na(zoneBox)
                box.set_right(zoneBox, touchEndTime)
            if not na(upperLine)
                line.set_x2(upperLine, touchEndTime)
            if not na(lowerLine)
                line.set_x2(lowerLine, touchEndTime)
            if not na(centerLine)
                line.set_x2(centerLine, touchEndTime)
            if not na(zoneTag)
                int leftTime = line.get_x1(upperLine)
                int centerTime = leftTime + int(math.round((touchEndTime - leftTime) * 0.5))
                label.set_x(zoneTag, centerTime)
        zoneIndex -= 1
 
if bullishSignal and showBullish
    // The source range runs from the older candle's high to the newer low.
    f_addZone(olderTime, time, olderHigh, recentLow, 1)
 
if bearishSignal and showBearish
    // The source range runs from the older candle's low to the newer high.
    f_addZone(olderTime, time, recentHigh, olderLow, -1)
 
alertcondition(bullishSignal, title = "ICT Hidden OB+ — Long",
     message = "Bullish ICT hidden order block pattern formed. This is a reference range, not entry confirmation.")
alertcondition(bearishSignal, title = "ICT Hidden OB+ — Short",
     message = "Bearish ICT hidden order block pattern formed. This is a reference range, not entry confirmation.")

← 返回列表