本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Armindo-Azevedo
// This source code is subject to the terms of the Mozilla Public License 2.0
// https://mozilla.org/MPL/2.0/
// Donchian Trend original © LonesomeTheBlue
//@version=6
indicator("Donchian Channels + Donchian Trend", shorttitle="DC + Trend",
overlay=true)
// ── Réglages
length = input.int(20, "Période commune", minval=10, group="Réglages")
lineWidth = input.int(3, "Épaisseur du Trend",
minval=1, maxval=4, group="Réglages")
offset = input.int(0, "Décalage du canal", group="Réglages")
// ── Affichage
showChannel = input.bool(true, "Afficher le canal", group="Affichage")
showBasis = input.bool(true, "Afficher la médiane", group="Affichage")
showFill = input.bool(true, "Afficher le fond", group="Affichage")
showTrend = input.bool(true, "Afficher le Trend", group="Affichage")
showArrows = input.bool(true, "Afficher les flèches", group="Affichage")
useGradient = input.bool(true, "Intensité du Trend selon sa force",
group="Affichage")
// ── Couleurs du canal
upperColor = input.color(#2962FF, "Borne haute", group="Couleurs du canal")
lowerColor = input.color(#2962FF, "Borne basse", group="Couleurs du canal")
basisColor = input.color(#FF6D00, "Médiane", group="Couleurs du canal")
fillColor = input.color(color.new(#2196F3, 95), "Fond du canal",
group="Couleurs du canal")
// ── Couleurs du Trend
bullColor = input.color(#00FF00, "Trend haussier",
group="Couleurs du Trend")
bearColor = input.color(#FF0000, "Trend baissier",
group="Couleurs du Trend")
upArrowColor = input.color(color.green, "Flèche haussière",
group="Couleurs du Trend")
downArrowColor = input.color(color.olive, "Flèche baissière",
group="Couleurs du Trend")
// ── Canal et tendance principale
donchian(len) =>
hh = ta.highest(high, len)
ll = ta.lowest(low, len)
var int direction = 0
direction := close > hh[1] ? 1 :
close < ll[1] ? -1 : nz(direction[1], 0)
[hh, ll, direction]
// ── Tendances secondaires
secondaryTrend(len) =>
hh = ta.highest(high, len)
ll = ta.lowest(low, len)
var int direction = 0
direction := close >= hh[1] ? 1 :
close <= ll[1] ? -1 : nz(direction[1], 0)
direction
[upper, lower, mainTrend] = donchian(length)
basis = math.avg(upper, lower)
t1 = secondaryTrend(length - 1)
t2 = secondaryTrend(length - 2)
t3 = secondaryTrend(length - 3)
t4 = secondaryTrend(length - 4)
t5 = secondaryTrend(length - 5)
t6 = secondaryTrend(length - 6)
t7 = secondaryTrend(length - 7)
t8 = secondaryTrend(length - 8)
t9 = secondaryTrend(length - 9)
directions = array.from(t1, t2, t3, t4, t5, t6, t7, t8, t9)
int score = mainTrend == 1 ? 10 : -10
for direction in directions
if math.sign(direction) != math.sign(mainTrend)
score := score - int(math.sign(mainTrend))
// ── Affichage du canal
upperPlot = plot(showChannel ? upper : na,
title="Borne haute", color=upperColor, offset=offset)
lowerPlot = plot(showChannel ? lower : na,
title="Borne basse", color=lowerColor, offset=offset)
plot(showChannel and showBasis ? basis : na,
title="Médiane", color=basisColor, offset=offset)
fill(upperPlot, lowerPlot,
color=showFill ? fillColor : na, title="Fond du canal")
// ── Couleur du Trend
strength = math.min(math.abs(score), 10)
baseColor = mainTrend == 1 ? bullColor : bearColor
// Préserve la transparence choisie et ajoute le dégradé de force.
baseTransparency = color.t(baseColor)
trendTransparency = useGradient ?
100 - (100 - baseTransparency) * strength / 10 :
baseTransparency
trendColor = color.new(baseColor, trendTransparency)
trendLine = mainTrend == 1 ? lower : upper
sameTrend = mainTrend != 0 and mainTrend == nz(mainTrend[1], 0)
plot(showTrend ? trendLine : na,
title="Donchian Trend",
color=sameTrend ? trendColor : na,
linewidth=lineWidth)
// ── Flèches
upArrow = mainTrend == 1 and score > 2 and
(score < 10 or low == lower)
downArrow = mainTrend == -1 and score < -2 and
(score > -10 or high == upper)
plotchar(showArrows and upArrow ? lower : na,
title="Flèche haussière", char="↑",
location=location.absolute,
color=upArrowColor, size=size.small)
plotchar(showArrows and downArrow ? upper : na,
title="Flèche baissière", char="↓",
location=location.absolute,
color=downArrowColor, size=size.small)