本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Donchian Trend Ribbon + Alerts", overlay=false)
basePeriod = input.int(20, "Base Donchian Period", minval=5)
showRibbon = input.bool(true, "Show Ribbon Columns")
donchianTrend(len) =>
highestHigh = ta.highest(high, len)[1]
lowestLow = ta.lowest(low, len)[1]
trend = 0
trend := close > highestHigh ? 1 : close < lowestLow ? -1 : nz(trend[1], 0)
trend
maintrend = donchianTrend(basePeriod)
t1 = donchianTrend(basePeriod)
t2 = donchianTrend(basePeriod - 1)
t3 = donchianTrend(basePeriod - 2)
t4 = donchianTrend(basePeriod - 3)
t5 = donchianTrend(basePeriod - 4)
t6 = donchianTrend(basePeriod - 5)
t7 = donchianTrend(basePeriod - 6)
t8 = donchianTrend(basePeriod - 7)
t9 = donchianTrend(basePeriod - 8)
t10 = donchianTrend(basePeriod - 9)
ribbonColor(trend) =>
trend == 1 ? color.new(#39FF14, 0) : trend == -1 ? color.new(#E53935, 0) : color.new(color.gray, 60)
plot(showRibbon ? 10 : na, color=ribbonColor(t1), style=plot.style_columns, linewidth=3, title="Ribbon 1")
plot(showRibbon ? 9 : na, color=ribbonColor(t2), style=plot.style_columns, linewidth=3, title="Ribbon 2")
plot(showRibbon ? 8 : na, color=ribbonColor(t3), style=plot.style_columns, linewidth=3, title="Ribbon 3")
plot(showRibbon ? 7 : na, color=ribbonColor(t4), style=plot.style_columns, linewidth=3, title="Ribbon 4")
plot(showRibbon ? 6 : na, color=ribbonColor(t5), style=plot.style_columns, linewidth=3, title="Ribbon 5")
plot(showRibbon ? 5 : na, color=ribbonColor(t6), style=plot.style_columns, linewidth=3, title="Ribbon 6")
plot(showRibbon ? 4 : na, color=ribbonColor(t7), style=plot.style_columns, linewidth=3, title="Ribbon 7")
plot(showRibbon ? 3 : na, color=ribbonColor(t8), style=plot.style_columns, linewidth=3, title="Ribbon 8")
plot(showRibbon ? 2 : na, color=ribbonColor(t9), style=plot.style_columns, linewidth=3, title="Ribbon 9")
plot(showRibbon ? 1 : na, color=ribbonColor(t10), style=plot.style_columns, linewidth=3, title="Ribbon 10")
bgcolor(maintrend == 1 ? color.new(#39FF14, 92) : maintrend == -1 ? color.new(#E53935, 92) : na)
// Alerts
bearishFlip = maintrend == -1 and maintrend[1] == 1
bullishFlip = maintrend == 1 and maintrend[1] == -1
alertcondition(bearishFlip, title="Ribbon Turns Red", message="Donchian Trend Ribbon turned RED on {{ticker}} {{interval}}")
alertcondition(bullishFlip, title="Ribbon Turns Green", message="Donchian Trend Ribbon turned GREEN on {{ticker}} {{interval}}")
if bearishFlip
alert("Donchian Trend Ribbon turned RED on " + syminfo.ticker + " " + timeframe.period, alert.freq_once_per_bar_close)
if bullishFlip
alert("Donchian Trend Ribbon turned GREEN on " + syminfo.ticker + " " + timeframe.period, alert.freq_once_per_bar_close)
// Daily alerts
dailyTrend = request.security(syminfo.tickerid, "D", maintrend, lookahead=barmerge.lookahead_off)
dailyBearishFlip = dailyTrend == -1 and dailyTrend[1] == 1
dailyBullishFlip = dailyTrend == 1 and dailyTrend[1] == -1
if dailyBearishFlip
alert("DAILY Donchian Trend Ribbon turned RED on " + syminfo.ticker, alert.freq_once_per_bar_close)
if dailyBullishFlip
alert("DAILY Donchian Trend Ribbon turned GREEN on " + syminfo.ticker, alert.freq_once_per_bar_close)