Trend Continuations [AlgoAlpha]

AlgoAlpha · study · 117 行 · 点赞 392 · 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/
// © AlgoAlpha
 
//@version=6
indicator("Trend Continuations [AlgoAlpha]", shorttitle = "AlgoAlpha - Trend Cont", overlay = true, max_bars_back = 5000)
 
// Inputs
show_signals = input.bool(true, "Signals", inline = "display1", group = "Appearance")
show_zones = input.bool(true, "Shading", inline = "display1", group = "Appearance", tooltip = "Shows bounce signals and shaded areas around the active band. Signals confirm at candle close.")
show_pivots = input.bool(false, "Pivots", inline = "display2", group = "Appearance")
color_bars = input.bool(false, "Bar Colors", inline = "display2", group = "Appearance", tooltip = "Pivot marks appear on past bars only after confirmation. Bar colors follow the midline.")
 
pivot_length = input.int(40, "Pivot Length", minval = 2, maxval = 500, group = "Trend", tooltip = "Bars on each side of a pivot. A pivot is confirmed this many bars after it forms. The regression uses the bar distance from the latest confirmed pivot, up to 5000 bars.")
fast_length = input.int(50, "Fast Length", minval = 2, maxval = 500, group = "Trend", tooltip = "Length of the fast Hull average used for midline colors. The midline turns gray when its direction and the fast average disagree.")
volatility_length = input.int(14, "Volatility Length", minval = 1, maxval = 500, group = "Bands", tooltip = "Length of the average true range used to set band distance and bounce zone width.")
band_multiplier = input.float(2.0, "Band Multiplier", minval = 0.1, step = 0.1, group = "Bands", tooltip = "Sets band distance from the midline as ATR times this value. Both bands follow the midline on every bar. Higher values place them farther away.")
 
bullish_color = input.color(#00ffbb, "Bullish", inline = "colors", group = "Appearance")
bearish_color = input.color(#ff1100, "Bearish", inline = "colors", group = "Appearance", tooltip = "Colors for the midline, bands, and bounce signals. Gray shows a neutral midline.")
 
// Pivot regression
ph = ta.pivothigh(close, pivot_length, pivot_length)
pl = ta.pivotlow(close, pivot_length, pivot_length)
 
var last_pivot_bar = int(na)
 
if barstate.isconfirmed and (not na(ph) or not na(pl))
    last_pivot_bar := bar_index - pivot_length
 
lookback = math.min(5000, math.max(2, bar_index - nz(last_pivot_bar, bar_index - 2)))
regression = ta.linreg(close, lookback, 0)
midline = not na(last_pivot_bar) ? regression : na
fast = ta.hma(close, fast_length)
 
rising = midline > midline[1]
falling = midline < midline[1]
neutral = na(fast) or (rising and fast < midline) or (falling and fast > midline)
midline_color = neutral ? color.gray : falling ? bearish_color : rising ? bullish_color : color.gray
 
// Midline bands
volatility = ta.atr(volatility_length)
band_distance = volatility * band_multiplier
upper = midline + band_distance
lower = midline - band_distance
zone_width = math.min(volatility, band_distance * 0.5)
upper_inner = upper - zone_width
lower_inner = lower + zone_width
upper_outer = upper + zone_width
lower_outer = lower - zone_width
ready = not na(midline) and not na(volatility)
 
// Band visibility
var direction = 0
 
if ready
    if direction == 0
        direction := close >= midline ? 1 : -1
    else if direction == 1 and close < lower
        direction := -1
    else if direction == -1 and close > upper
        direction := 1
 
// Bounce zones
bullish_band = direction == 1 ? lower : na
bearish_band = direction == -1 ? upper : na
bullish_zone = direction == 1 ? lower_inner : na
bearish_zone = direction == -1 ? upper_inner : na
bullish_outer = direction == 1 ? lower_outer : na
bearish_outer = direction == -1 ? upper_outer : na
 
// Bounce signals
var bounce_ready = true
 
if direction != direction[1]
    bounce_ready := true
else if direction == 1 and low > bullish_zone[1]
    bounce_ready := true
else if direction == -1 and high < bearish_zone[1]
    bounce_ready := true
 
bullish_bounce = barstate.isconfirmed and bounce_ready and direction == 1 and direction[1] == 1 and low <= bullish_zone[1] and close > bullish_zone[1]
bearish_bounce = barstate.isconfirmed and bounce_ready and direction == -1 and direction[1] == -1 and high >= bearish_zone[1] and close < bearish_zone[1]
 
if bullish_bounce or bearish_bounce
    bounce_ready := false
 
bullish_trend = barstate.isconfirmed and direction == 1 and direction[1] == -1
bearish_trend = barstate.isconfirmed and direction == -1 and direction[1] == 1
 
// Plots
plot(midline, "Adaptive Midline", color = midline_color, linewidth = 3)
bullish_band_plot = plot(bullish_band, "Bullish Band", color = color.gray, linewidth = 1, style = plot.style_linebr)
bearish_band_plot = plot(bearish_band, "Bearish Band", color = color.gray, linewidth = 1, style = plot.style_linebr)
bullish_zone_plot = plot(bullish_zone, "Bullish Inner Edge", color = color.new(bullish_color, 90), linewidth = 1, style = plot.style_linebr)
bearish_zone_plot = plot(bearish_zone, "Bearish Inner Edge", color = color.new(bearish_color, 90), linewidth = 1, style = plot.style_linebr)
bullish_outer_plot = plot(bullish_outer, "Bullish Outer Edge", color = color.new(bullish_color, 50), linewidth = 1, style = plot.style_linebr)
bearish_outer_plot = plot(bearish_outer, "Bearish Outer Edge", color = color.new(bearish_color, 50), linewidth = 1, style = plot.style_linebr)
 
fill(bullish_outer_plot, bullish_band_plot, show_zones ? color.new(bullish_color, 50) : na, title = "Bullish Outer Shading", fillgaps = false)
fill(bullish_zone_plot, bullish_band_plot, show_zones ? color.new(bullish_color, 90) : na, title = "Bullish Inner Shading", fillgaps = false)
fill(bearish_outer_plot, bearish_band_plot, show_zones ? color.new(bearish_color, 50) : na, title = "Bearish Outer Shading", fillgaps = false)
fill(bearish_zone_plot, bearish_band_plot, show_zones ? color.new(bearish_color, 90) : na, title = "Bearish Inner Shading", fillgaps = false)
 
marker_gap = math.max(volatility * 0.25, syminfo.mintick * 2)
plotchar(show_signals and bullish_bounce ? bullish_outer - marker_gap : na, title = "Bullish Bounce", char = "▲", location = location.absolute, color = bullish_color, size = size.tiny)
plotchar(show_signals and bearish_bounce ? bearish_outer + marker_gap : na, title = "Bearish Bounce", char = "▼", location = location.absolute, color = bearish_color, size = size.tiny)
plotchar(show_pivots and barstate.isconfirmed and not na(ph), title = "Pivot High", char = "x", location = location.abovebar, color = bearish_color, offset = -pivot_length, size = size.tiny)
plotchar(show_pivots and barstate.isconfirmed and not na(pl), title = "Pivot Low", char = "x", location = location.belowbar, color = bullish_color, offset = -pivot_length, size = size.tiny)
 
barcolor(color_bars and not na(midline) ? midline_color : na)
 
// Alerts
alertcondition(bullish_bounce, title = "Bullish Bounce")
alertcondition(bearish_bounce, title = "Bearish Bounce")
alertcondition(bullish_bounce or bearish_bounce, title = "Any Bounce")
alertcondition(bullish_trend, title = "Bullish Trend")
alertcondition(bearish_trend, title = "Bearish Trend")

← 返回列表