本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed
//@version=4
study("Donchian Channel - trend filter", overlay = true)
pvtLenR = input(4)
pvtLenL = input(2)
waitforclose = input(true)
ShowHHLL = input(true)
DCBase = input(title="Channel Base", defval="Price", options=["Price", "Pivots", "Moving Average", "Combined"])
MAType = input(title="Moving Average Type", defval="ema", options=["ema", "sma", "hma", "rma", "vwma", "wma"])
DCLength = input(20, minval=10, step=10)
trendCountLimit = input(5)
showChannel = input(true)
donchian(highSource, lowSource, rangeLength)=>
top = highest(highSource, rangeLength)
bottom = lowest(lowSource, rangeLength)
middle = (top+bottom)/2
[middle, top, bottom]
f_getMovingAverage(source, MAType, length)=>
ma = (MAType == "sma")? sma(source,length):
(MAType == "ema")? ema(source,length):
(MAType == "hma")? hma(source,length):
(MAType == "rma")? rma(source,length):
(MAType == "vwma")? vwma(source,length):
(MAType == "wma")? wma(source,length):
(highest(length)+lowest(length))/2
ma
f_calculatePivots(Shunt)=>
pvthi_ = pivothigh(high, pvtLenL, pvtLenR)
pvtlo_ = pivotlow(low, pvtLenL, pvtLenR)
pvthi = pvthi_[Shunt]
pvtlo = pvtlo_[Shunt]
pvthighline = pvthi
pvthighline := na(pvthighline)? nz(pvthighline[1]): pvthighline
pvtlowline = pvtlo
pvtlowline := na(pvtlowline)? nz(pvtlowline[1]): pvtlowline
higherhigh = na(pvthi) ? na : ( valuewhen(pvthi, high[pvtLenR+Shunt], 1) < valuewhen(pvthi, high[pvtLenR+Shunt], 0) ) ? pvthi : na
lowerhigh = na(pvthi) ? na : ( valuewhen(pvthi, high[pvtLenR+Shunt], 1) > valuewhen(pvthi, high[pvtLenR+Shunt], 0) ) ? pvthi : na
higherlow = na(pvtlo) ? na : ( valuewhen(pvtlo, low[pvtLenR+Shunt], 1) < valuewhen(pvtlo, low[pvtLenR+Shunt], 0) ) ? pvtlo : na
lowerlow = na(pvtlo) ? na : ( valuewhen(pvtlo, low[pvtLenR+Shunt], 1) > valuewhen(pvtlo, low[pvtLenR+Shunt], 0) ) ? pvtlo : na
[pvthighline, pvtlowline, higherhigh, lowerhigh, higherlow, lowerlow]
f_getCustomDonchianChannel(DCBase)=>
Shunt = waitforclose? 1 : 0
[pvthighline, pvtlowline, higherhigh, lowerhigh, higherlow, lowerlow] = f_calculatePivots(Shunt)
[middleP, topP, bottomP] = donchian(pvthighline, pvtlowline, DCLength)
[middleC, topC, bottomC] = donchian(high, low, DCLength)
[middleDC, topDC, bottomDC] = donchian(f_getMovingAverage(high, MAType, DCLength), f_getMovingAverage(low, MAType, DCLength), DCLength)
top = DCBase == "Combined"? max(topP,topC, topDC) : DCBase == "Pivots" ? topP : DCBase == "Price" ? topC : topDC
bottom = DCBase == "Combined"? min(bottomP,bottomC,bottomDC) : DCBase == "Pivots" ? bottomP : DCBase == "Price" ? bottomC : bottomDC
middle = DCBase == "Combined"? (middleP+middleC+middleDC)/3 : DCBase == "Pivots" ? middleP : DCBase == "Price" ? middleC : middleDC
[middle, top, bottom]
[middle, top, bottom] = f_getCustomDonchianChannel(DCBase)
upCount = 0
downCount = 0
highSource = DCBase == "Moving Average" ? f_getMovingAverage(high, MAType, DCLength) : DCBase == "Combined" ? max(high, f_getMovingAverage(high, MAType, DCLength)) :high
lowSource = DCBase == "Moving Average" ? f_getMovingAverage(low, MAType, DCLength) : DCBase == "Combined" ? min(low, f_getMovingAverage(low, MAType, DCLength)) :low
upCount := highSource > top[1]? nz(upCount[1],0) + 1 : lowSource < bottom[1]? 0 : nz(upCount[1],0)
downCount := lowSource < bottom[1]? nz(downCount[1],0) + 1 : highSource > top[1]? 0 : nz(downCount[1],0)
barColor = upCount > trendCountLimit? color.lime : downCount > trendCountLimit? color.orange : color.silver
barcolor(barColor)
BBU = plot(top, title="Upper", color=color.green, linewidth=1, transp=50, style=plot.style_linebr)
BBM = plot(middle, title="Middle", color=color.maroon, linewidth=1, transp=50, style=plot.style_linebr)
BBL = plot(bottom, title="Lower", color=color.red, linewidth=1, transp=50, style=plot.style_linebr)
fill(BBU, BBM, title="Upper", color=color.green, transp=90)
fill(BBM, BBL, title="Lower", color=color.red, transp=90)