Dow Theory HH HL LH LL + CHoCH By SachinRkGandhi

buddygroup16 · study · 147 行 · 点赞 43 · TradingView 原页

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

Pine Script

//@version=6
indicator("Dow Theory HH HL LH LL + CHoCH bY SachinRkGandhi", overlay=true, max_labels_count=500, max_lines_count=500)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
pivotLen = input.int(5, "Swing Strength", minval=1, maxval=50, group="Structure")
showStructure = input.bool(true, "Show HH / HL / LH / LL", group="Structure")
showSwingLines = input.bool(true, "Show Recent Swing High / Low", group="Structure")
showBOS = input.bool(true, "Show BOS", group="Structure")
showCHoCH = input.bool(true, "Show CHoCH Trend Change", group="Structure")
 
bullColor = input.color(color.lime, "Bullish Structure Color", group="Colors")
bearColor = input.color(color.red, "Bearish Structure Color", group="Colors")
highLineColor = input.color(color.orange, "Swing High Line", group="Colors")
lowLineColor = input.color(color.aqua, "Swing Low Line", group="Colors")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CHoCH LINE CONTROLS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
chochLineStyleInput = input.string("Solid", "CHoCH Line Style", options=["Solid", "Dashed", "Dotted"], group="CHoCH Line")
chochLineWidth = input.int(3, "CHoCH Line Width", minval=1, maxval=5, group="CHoCH Line")
bullChochColor = input.color(color.lime, "Bullish CHoCH Color", group="CHoCH Line")
bearChochColor = input.color(color.red, "Bearish CHoCH Color", group="CHoCH Line")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BOS LINE CONTROLS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bosLineStyleInput = input.string("Dashed", "BOS Line Style", options=["Solid", "Dashed", "Dotted"], group="BOS Line")
bosLineWidth = input.int(2, "BOS Line Width", minval=1, maxval=5, group="BOS Line")
bullBosColor = input.color(color.green, "Bullish BOS Color", group="BOS Line")
bearBosColor = input.color(color.maroon, "Bearish BOS Color", group="BOS Line")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CONVERT LINE STYLE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
chochLineStyle = chochLineStyleInput == "Solid" ? line.style_solid : chochLineStyleInput == "Dashed" ? line.style_dashed : line.style_dotted
bosLineStyle = bosLineStyleInput == "Solid" ? line.style_solid : bosLineStyleInput == "Dashed" ? line.style_dashed : line.style_dotted
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CONFIRMED SWING POINTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low, pivotLen, pivotLen)
 
var float lastHigh = na
var float previousHigh = na
var float lastLow = na
var float previousLow = na
 
var int lastHighBar = na
var int lastLowBar = na
 
var bool highBroken = false
var bool lowBroken = false
 
// Trend: 1 = bullish, -1 = bearish, 0 = unknown
var int trend = 0
 
var line recentHighLine = na
var line recentLowLine = na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SWING HIGH: HH / LH
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if not na(ph)
    string highText = na(previousHigh) ? "SH" : ph > previousHigh ? "HH" : ph < previousHigh ? "LH" : "EQH"
 
    previousHigh := ph
    lastHigh := ph
    lastHighBar := bar_index - pivotLen
    highBroken := false
 
    if showStructure
        label.new(lastHighBar, ph, highText, style=label.style_label_down, color=highText == "HH" ? bullColor : highText == "LH" ? bearColor : highLineColor, textcolor=color.white, size=size.small)
 
    if not na(recentHighLine)
        line.delete(recentHighLine)
 
    recentHighLine := na
    if showSwingLines
        recentHighLine := line.new(lastHighBar, ph, bar_index, ph, extend=extend.right, color=highLineColor, width=2)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SWING LOW: HL / LL
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if not na(pl)
    string lowText = na(previousLow) ? "SL" : pl > previousLow ? "HL" : pl < previousLow ? "LL" : "EQL"
 
    previousLow := pl
    lastLow := pl
    lastLowBar := bar_index - pivotLen
    lowBroken := false
 
    if showStructure
        label.new(lastLowBar, pl, lowText, style=label.style_label_up, color=lowText == "HL" ? bullColor : lowText == "LL" ? bearColor : lowLineColor, textcolor=color.black, size=size.small)
 
    if not na(recentLowLine)
        line.delete(recentLowLine)
 
    recentLowLine := na
    if showSwingLines
        recentLowLine := line.new(lastLowBar, pl, bar_index, pl, extend=extend.right, color=lowLineColor, width=2)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BREAK OF STRUCTURE / CHoCH
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullBreak = not na(lastHigh) and not highBroken and not na(lastHighBar) and bar_index > lastHighBar and close > lastHigh and close[1] <= lastHigh
bearBreak = not na(lastLow) and not lowBroken and not na(lastLowBar) and bar_index > lastLowBar and close < lastLow and close[1] >= lastLow
 
if bullBreak
    highBroken := true
    bool isCHoCH = trend == -1
 
    if isCHoCH
        if showCHoCH
            line.new(lastHighBar, lastHigh, bar_index, lastHigh, color=bullChochColor, width=chochLineWidth, style=chochLineStyle)
            label.new(bar_index, lastHigh, "CHoCH", style=label.style_label_down, color=bullChochColor, textcolor=color.white, size=size.normal)
    else
        if showBOS
            line.new(lastHighBar, lastHigh, bar_index, lastHigh, color=bullBosColor, width=bosLineWidth, style=bosLineStyle)
            label.new(bar_index, lastHigh, "BOS", style=label.style_label_down, color=bullBosColor, textcolor=color.white, size=size.small)
 
    trend := 1
 
if bearBreak
    lowBroken := true
    bool isCHoCH = trend == 1
 
    if isCHoCH
        if showCHoCH
            line.new(lastLowBar, lastLow, bar_index, lastLow, color=bearChochColor, width=chochLineWidth, style=chochLineStyle)
            label.new(bar_index, lastLow, "CHoCH", style=label.style_label_up, color=bearChochColor, textcolor=color.white, size=size.normal)
    else
        if showBOS
            line.new(lastLowBar, lastLow, bar_index, lastLow, color=bearBosColor, width=bosLineWidth, style=bosLineStyle)
            label.new(bar_index, lastLow, "BOS", style=label.style_label_up, color=bearBosColor, textcolor=color.white, size=size.small)
 
    trend := -1
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(bullBreak and trend[1] == -1, "Bullish CHoCH", "Bullish Change of Character detected.")
alertcondition(bearBreak and trend[1] == 1, "Bearish CHoCH", "Bearish Change of Character detected.")
alertcondition(bullBreak, "Bullish Structure Break", "Price broke above the latest confirmed swing high.")
alertcondition(bearBreak, "Bearish Structure Break", "Price broke below the latest confirmed swing low.")

← 返回列表