Rounding Bottom & Top Detector [JPT]

Jos-ProTrader · study · 138 行 · 点赞 150 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jos-ProTrader
 
//@version=6
indicator("Rounding Bottom & Top Detector [JPT]", shorttitle="RBT Detector [JPT]", overlay=true, max_lines_count=200, max_labels_count=100, max_bars_back=1000)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
groupDetect = "Pattern Detection"
lookback    = input.int(50, "Pattern Lookback", minval=20, maxval=200, group=groupDetect)
edgePct     = input.float(0.20, "Edge Similarity (%)", minval=0.01, maxval=5.0, step=0.05, group=groupDetect)
breakoutPct = input.float(0.05, "Breakout Confirmation (%)", minval=0.0, maxval=2.0, step=0.05, group=groupDetect)
showBottom  = input.bool(true, "Show Rounding Bottom", group=groupDetect)
showTop     = input.bool(true, "Show Rounding Top", group=groupDetect)
 
groupVisual = "Visual Settings"
showCurves  = input.bool(true, "Show Curved Formation", group=groupVisual)
showLevels  = input.bool(true, "Show Support / Resistance", group=groupVisual)
showSignals = input.bool(false, "Show Breakout Signals", group=groupVisual)
curveSteps  = input.int(20, "Curve Smoothness", minval=8, maxval=40, group=groupVisual)
 
bottomColor = input.color(color.rgb(255, 65, 95), "Rounding Bottom Color", group=groupVisual)
topColor    = input.color(color.rgb(0, 180, 145), "Rounding Top Color", group=groupVisual)
levelColor  = input.color(color.rgb(145, 145, 155), "Support / Resistance Color", group=groupVisual)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PATTERN CALCULATIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
halfLen = int(math.floor(lookback / 2.0))
 
// Historical pattern boundaries
leftHigh  = ta.highest(high[halfLen], halfLen)
rightHigh = ta.highest(high[1], halfLen)
 
leftLow   = ta.lowest(low[halfLen], halfLen)
rightLow  = ta.lowest(low[1], halfLen)
 
// Pattern extremes
patternLow  = ta.lowest(low[1], lookback)
patternHigh = ta.highest(high[1], lookback)
 
// Extreme position within the formation
lowOffset  = math.abs(ta.lowestbars(low[1], lookback))
highOffset = math.abs(ta.highestbars(high[1], lookback))
 
// Relative difference between pattern boundaries
bottomEdgeDiff = math.abs(leftHigh - rightHigh) / math.max(math.abs(leftHigh), syminfo.mintick) * 100
topEdgeDiff    = math.abs(leftLow - rightLow) / math.max(math.abs(leftLow), syminfo.mintick) * 100
 
// Require the extreme to be reasonably near the middle
middleLow  = lowOffset >= int(lookback * 0.25) and lowOffset <= int(lookback * 0.75)
middleHigh = highOffset >= int(lookback * 0.25) and highOffset <= int(lookback * 0.75)
 
// Pattern boundaries
bottomResistance = math.max(leftHigh, rightHigh)
topSupport       = math.min(leftLow, rightLow)
 
// Curved pattern depth
bottomDepth = bottomResistance - patternLow
topHeight   = patternHigh - topSupport
 
validBottomShape = bottomEdgeDiff <= edgePct * 10 and bottomDepth > syminfo.mintick
validTopShape    = topEdgeDiff <= edgePct * 10 and topHeight > syminfo.mintick
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BREAKOUT CONFIRMATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
bottomBreakout = close > bottomResistance * (1 + breakoutPct / 100)
topBreakdown   = close < topSupport * (1 - breakoutPct / 100)
 
// Confirmed price movement
bottomSignal = showBottom and barstate.isconfirmed and bottomBreakout and middleLow and validBottomShape
topSignal    = showTop and barstate.isconfirmed and topBreakdown and middleHigh and validTopShape
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DRAW CURVED FORMATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
drawCurve(_startX, _endX, _edge, _extreme, _isBottom, _color) =>
    int totalBars = _endX - _startX
    if totalBars > 0
        for i = 0 to curveSteps - 1
            float t1 = i / float(curveSteps)
            float t2 = (i + 1) / float(curveSteps)
 
            float y1 = _isBottom ? _edge - (_edge - _extreme) * math.sin(math.pi * t1) : _edge + (_extreme - _edge) * math.sin(math.pi * t1)
            float y2 = _isBottom ? _edge - (_edge - _extreme) * math.sin(math.pi * t2) : _edge + (_extreme - _edge) * math.sin(math.pi * t2)
 
            int x1 = _startX + int(math.round(totalBars * t1))
            int x2 = _startX + int(math.round(totalBars * t2))
 
            line.new(x1, y1, x2, y2, xloc=xloc.bar_index, color=_color, width=2)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ROUNDING BOTTOM
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if bottomSignal
    if showCurves
        drawCurve(bar_index - lookback, bar_index - 1, bottomResistance, patternLow, true, bottomColor)
 
    if showLevels
        line.new(bar_index - lookback, bottomResistance, bar_index, bottomResistance, xloc=xloc.bar_index, color=levelColor, width=1, style=line.style_dashed)
 
    if showSignals
        label.new(bar_index, low, "Rounding Bottom\nBREAKOUT ↑", style=label.style_label_up, color=bottomColor, textcolor=color.white, size=size.small)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ROUNDING TOP
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if topSignal
    if showCurves
        drawCurve(bar_index - lookback, bar_index - 1, topSupport, patternHigh, false, topColor)
 
    if showLevels
        line.new(bar_index - lookback, topSupport, bar_index, topSupport, xloc=xloc.bar_index, color=levelColor, width=1, style=line.style_dashed)
 
    if showSignals
        label.new(bar_index, high, "Rounding Top\nBREAKDOWN ↓", style=label.style_label_down, color=topColor, textcolor=color.white, size=size.small)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
alertcondition(bottomSignal, title="Rounding Bottom Breakout", message="Potential Rounding Bottom breakout confirmed.")
alertcondition(topSignal, title="Rounding Top Breakdown", message="Potential Rounding Top breakdown confirmed.")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BREAKOUT MARKERS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plotshape(bottomSignal, title="Bullish Breakout", style=shape.triangleup, location=location.belowbar, color=bottomColor, size=size.small)
plotshape(topSignal, title="Bearish Breakdown", style=shape.triangledown, location=location.abovebar, color=topColor, size=size.small)

← 返回列表