本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator("Donchian", overlay=true, precision=3)
//------------------------------------------------------------------------ // ***** Colors ***** \\ ------------------------------------------------------------------------\\
Black = #000000, Charcoal= #363A45, Gray = #787B86, Silver = #C0C0C0, White = #FFFFFF
Brown = #A52A2A, Red = #FF5350, Pink = #FF69B4, Gold = #D4AF37, Orange = #FF6A00, Yellow = #FFFF00
Green = #00AF00, Teal = #008080, Lime = #00FF0B // Green = #008000
Navy = #311B92, Blue = #0094FF, Aqua = #00BCD4 // #2962FF #00FFFF
Purple = #800080, Fucshia = #E040FB //#FF00FF
None = color.new(Black, 100)
//------------------------------------------------------------------------ // ***** Inputs ***** \\ ------------------------------------------------------------------------\\
// Trend EMA
trendLength = input.int(title="Trend", defval = 200, inline="trend")
trendRes = input.timeframe(title="", defval="", inline = "trend")
currentEMA = ta.ema(close, trendLength) // used to get multi-timeframe ema
mtfEMA = request.security(syminfo.tickerid, trendRes, currentEMA, gaps=barmerge.gaps_off)
TrendEMA = mtfEMA // ta.ema(close, 200)
// Indicator Settings
indicator_length = input.int(20, minval=1, title="Indicator Length", inline = "01")
tf_indicator = input.timeframe(defval ="", title="", inline = "01")
// Donchian Settings
DonchianSelection = input.bool(defval = true, title="On/Off", inline = "02") // input.string(defval="Off", title="Donchian", options=["Off", "On"], inline = "05")
bDonchian = DonchianSelection // != "Off" ? true : false
bClose = input.bool(defval = true, inline="02", tooltip="Donchian trend change based on close being above or below prevous high/low channel rather than just a high/low push above previos channel")
Style = "Trend" // input.string(defval="Trend Adv", title="Donchian Style", options=["Simple", "Trend", "Trend Adv"], inline = "03")
bBars = input.bool(defval = true, title="Bars", inline = "03")
bDoncLabel = input.bool(defval = false, title="Labels", inline = "03")
bDoncOutline = input.bool(defval = false, title="Lines", inline = "03")
bDoncFill = input.bool(defval = true, title="Fill", inline = "03")
//------------------------------------------------------------------------ // ***** Donchian Channel ***** \\ ------------------------------------------------------------------------\\
// ----------------- Donchian Inputs -------------- //
//length = input(20, minval=1) // Use length from the beginning
DoncUpper = ta.highest(indicator_length) // bDonchClose ? highest(source,length)
DoncLower = ta.lowest(indicator_length) // bDonchClose ? lowest(source,length)
DoncMiddle = math.avg(DoncUpper, DoncLower) // (uppper + lower) / 2
upper = request.security(syminfo.tickerid, tf_indicator, DoncUpper, gaps=barmerge.gaps_off)
lower = request.security(syminfo.tickerid, tf_indicator, DoncLower, gaps=barmerge.gaps_off)
middle = request.security(syminfo.tickerid, tf_indicator, DoncMiddle, gaps=barmerge.gaps_off)
// This is used to figure out if it has made new higher highs or lower lows even though the past high and low values have changed due to surpassing the length.
// i.e. During an uptrend of HH and HL, it stops making HHs and now makes a LH. When the top of the channel comes down after *Length* bars, it is in an uptrend until a new LL is made.
int trend = 0
trend := not bClose ? (high) > upper[1] ? 1 : (low) < lower[1] ? -1 : nz(trend[1]) : (close) > upper[1] ? 1 : (close) < lower[1] ? -1 : nz(trend[1])
HHSignal = (hl2 > TrendEMA and close > TrendEMA and trend == 1 and trend[1] != 1) ? true : false
LLSignal = (hl2 < TrendEMA and close < TrendEMA and trend == -1 and trend[1] != -1) ? true : false
// ----------------- Colors -------------- //
//ChannelColor = trend == 1 ? color.new(Blue, 20) : color.new(Orange, 20)
ChannelColor =
bDoncOutline == true and Style == "Simple" ? color.new(Blue, 20) :
bDoncOutline == true and trend == 1 and Style == "Trend" ? color.new(Blue, 20) :
bDoncOutline == true and trend == -1 and Style == "Trend" ? color.new(Red, 20) :
bDoncOutline == true and trend == 1 and Style == "Trend Adv" and high > TrendEMA and low > TrendEMA ? color.new(Blue, 20) :
bDoncOutline == true and trend == 1 and Style == "Trend Adv" ? color.new(Aqua, 20) :
bDoncOutline == true and trend == -1 and Style == "Trend Adv" and high < TrendEMA and low < TrendEMA ? color.new(Red, 20) :
bDoncOutline == true and trend == -1 and Style == "Trend Adv" ? color.new(Orange, 20) :
bDoncOutline == false and Style == "Simple" ? color.new(Blue, 100) :
bDoncOutline == false and trend == 1 and Style == "Trend" ? color.new(Blue, 100) :
bDoncOutline == false and trend == -1 and Style == "Trend" ? color.new(Red, 100) :
bDoncOutline == false and trend == 1 and Style == "Trend Adv" and high > TrendEMA and low > TrendEMA ? color.new(Blue, 100) :
bDoncOutline == false and trend == 1 and Style == "Trend Adv" ? color.new(Aqua, 100) :
bDoncOutline == false and trend == -1 and Style == "Trend Adv" and high < TrendEMA and low < TrendEMA ? color.new(Red, 100) :
bDoncOutline == false and trend == -1 and Style == "Trend Adv" ? color.new(Orange, 100) :
bDoncOutline == true ? color.new(Silver, 50) :
color.new(Silver, 100)
// high > EMAt and low > EMAt open > EMAt and close > EMAt
// high < EMAt and low < EMAt open < EMAt and close < EMAt
MiddleColor = bDoncOutline == true ? color.new(Gray, 25) : color.new(Gray, 75) //color.new(Orange, 75)
fillColor =
bDoncFill == false ? None :
Style == "Simple" ? color.new(Blue, 80) :
trend == 1 and Style == "Trend" ? color.new(Blue, 80) :
trend == -1 and Style == "Trend" ? color.new(Red, 80) :
trend == 1 and Style == "Trend Adv" and high > TrendEMA and close > TrendEMA ? color.new(Blue, 80) :
trend == 1 and Style == "Trend Adv" ? color.new(Aqua, 80) :
trend == -1 and Style == "Trend Adv" and high < TrendEMA and close < TrendEMA ? color.new(Red, 80) :
trend == -1 and Style == "Trend Adv" ? color.new(Orange, 80) :
color.new(Gray, 90)
// ----------------- Extras -------------- //
upLabelY = ta.lowest(4)
dnLabelY = ta.highest(4)
// If set to draw a label on signal
HHLabel = bDoncLabel and HHSignal == true ? label.new(bar_index, upLabelY, "Donc", color = color.new(Green, 25), style = label.style_label_up, textcolor = White) : na
LLLabel = bDoncLabel and LLSignal == true ? label.new(bar_index, dnLabelY, "Donc", color = color.new(Red, 25), style = label.style_label_down, textcolor = White) : na
// Change color of the bar when given a buy/sell signal so it stands out
barcolor(bBars and (HHSignal or LLSignal) ? White : na, title="Signal Bar")
// ----------------- Plot Everything -------------- //
ema_trend = plot(TrendEMA, "Trend Line", color=Yellow, linewidth = 2)
// Plot the Donchian Channel
upperBand = plot(bDonchian ? upper : na, "Upper", color=ChannelColor, linewidth = 1, display=display.all) // (Blue,25)
middleLine = plot(bDonchian ? middle : na, "Basis", color=MiddleColor, linewidth = 1, display=display.all) // (Orange)
lowerBand = plot(bDonchian ? lower : na, "Lower", color=ChannelColor, linewidth = 1, display=display.all) // (Blue,25)
fill(upperBand, lowerBand, title="Background", color=fillColor) // (Blue,94)