Donchian Channel - Pivot High-Low

Trendoscope · study · 58 行 · 点赞 1,248 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// 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 - Pivot High-Low", overlay=true)
pvtLen = input(3)
waitforclose = input(true)
ShowHHLL = input(true)
DCBase = input(title="Channel Base", defval="Combined", options=["Price", "Pivots", "Combined"])
DCLength = input(20)
showChannel = input(true)
donchian(highSource, lowSource, rangeLength)=>
    top = highest(highSource, rangeLength)
    bottom = lowest(lowSource, rangeLength)
    middle = (top+bottom)/2
    [middle, top, bottom]
 
f_calculatePivots(pvtLen, Shunt)=>
    pvthi_ = pivothigh(high, pvtLen, pvtLen)
    pvtlo_ = pivotlow(low, pvtLen, pvtLen)
    
    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[pvtLen+Shunt], 1) < valuewhen(pvthi, high[pvtLen+Shunt], 0) ) ? pvthi : na
    lowerhigh  = na(pvthi) ? na : ( valuewhen(pvthi, high[pvtLen+Shunt], 1) > valuewhen(pvthi, high[pvtLen+Shunt], 0) ) ? pvthi : na
    higherlow  = na(pvtlo) ? na : ( valuewhen(pvtlo, low[pvtLen+Shunt], 1)  < valuewhen(pvtlo, low[pvtLen+Shunt], 0) )  ? pvtlo : na
    lowerlow   = na(pvtlo) ? na : ( valuewhen(pvtlo, low[pvtLen+Shunt], 1)  > valuewhen(pvtlo, low[pvtLen+Shunt], 0) )  ? pvtlo : na
    
    [pvthighline, pvtlowline, higherhigh, lowerhigh, higherlow, lowerlow]
 
Shunt = waitforclose? 1 : 0    
[pvthighline, pvtlowline, higherhigh, lowerhigh, higherlow, lowerlow] = f_calculatePivots(pvtLen, Shunt)
 
[middleP, topP, bottomP] = donchian(pvthighline, pvtlowline, DCLength)
[middleC, topC, bottomC] = donchian(high, low, DCLength)
 
 
top = DCBase == "Combined"? max(topP,topC) : DCBase == "Pivots" ? topP : topC
bottom = DCBase == "Combined"? min(bottomP,bottomC) : DCBase == "Pivots" ? bottomP : bottomC
middle = DCBase == "Combined"? (middleP+middleC)/2 : DCBase == "Pivots" ? middleP : middleC
 
plotshape(ShowHHLL ? higherhigh : na, title='HH', style=shape.triangleup,   location=location.abovebar, color=color.green, text="HH", offset=-pvtLen-Shunt, transp=50)
plotshape(ShowHHLL ? higherlow  : na, title='HL', style=shape.triangleup,   location=location.belowbar, color=color.green, text="HL", offset=-pvtLen-Shunt, transp=0)
plotshape(ShowHHLL ? lowerhigh  : na, title='LH', style=shape.triangledown, location=location.abovebar, color=color.maroon, text="LH", offset=-pvtLen-Shunt, transp=0)
plotshape(ShowHHLL ? lowerlow   : na, title='LL', style=shape.triangledown, location=location.belowbar, color=color.maroon, text="LL", offset=-pvtLen-Shunt, transp=50)
 
BBup = plot(showChannel ? top: na, title="Top", color=color.red, linewidth=1, transp=50, style=plot.style_linebr)
BBdown = plot(showChannel ? bottom: na, title="Bottom", color=color.green, linewidth=1, transp=50, style=plot.style_linebr)
BBmid = plot(showChannel ? middle: na, title="Middle", color=color.blue, linewidth=1, transp=50, style=plot.style_linebr)
fill(BBup, BBmid, title="High Zone", color=color.green, transp=90)
fill(BBdown, BBmid, title="Low Zone", color=color.red, transp=90)

← 返回列表