+ Donchian Channels

ClassicScott · study · 152 行 · 点赞 631 · TradingView 原页

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

Pine Script

// © ClassicScott
 
 
//@version=4
 
//This version of Donchian Channels uses two source options so that one can create a channel using highs and lows rather than one or the other or closes.
//My thinking was that this would create a more accurate portrayal of price action (or at least contain the greatest scope of it) as seen through the lens of
//a Donchian Channel. This was actually part of the genesis of my idea around my Ultimate Moving Average.
//Besides the single top and bottom plot for the DC's extremities, I've enabled the ability to create outer bands with a variable width that the user can adjust
//to their preference. I think it's quite nice. I use it in the DC in my other non-overlay indicators.
//Besides this additional functionality, the indicator has options to plot lines between the basis and the upper and lower bands, so, basically, splitting the
//upper and lower channel in half.
 
//There is no magic number to the lookback. I chose 233 as default because it's a fibonacci sequence number and I'm more interested in using the DC like a very
//long period bias indicator, and the longer lookback gives a much wider window (because highs and lows are so spread apart) with which other faster indicators
//(supertrend, shorter period moving averages, etc.) can work without making the screen a clutter.
 
//The color of the basis may also be made relevant to higher timeframe information. What I mean by this is that you can set it so that the basis of the current
//timeframe is colored based on the candle close of the higher timeframe of your choosing. If you're looking at an hourly chart, and you set the color to Daily,
//the basis will be colored based on the candle close (above or below the basis) of the previous day. If the previous daily close was above the basis, that
//positive color will be reflected in the basis, even if the current hourly candle closes are below the hourly basis. This could potentially be useful for setting
//a higher timeframe directional bias and reacting off price crossing the lower timeframe basis (or whatever your trigger for entering a trade might be).
//This is also optional in my Ultimate Moving Average indicator.
 
//You can also set the entire indicator to whatever time frame you want if you want to see where the actual basis, or other levels are on that higher timeframe.
 
//Further additions include fibonacci retracement levels. These are calculated off the high and the low of the Donchian Channels themselves.
//You will see that there are only three retracement levels (.786, .705, .382), one of which is not a fib level, but what some people call the 'OTE,' or optimal
//trade entry. If you want more info on the OTE just web search it. So, why no .618 or .236? Reason being that the .618 overlaps the .382, and the .236 is extremely
//close to the .786. This sounds confusing, but the retracement levels I'm using are derived from the high and low, so it was unnecessary to have all five levels
//from each.
//I could have just calculated from the high, or just from the low, and used all the levels, but I chose to just calculate three levels from the high and three
//from the low because that gives a sort of mirror image balance, and that appeals to me, and the utility of the indicator is the same.
 
//The plot lines are all colored, and I've filled certain zones between them. There is a center zone filled between both .382 levels, and an upper and lower zone
//filled between the .786 and either the high or the low.
 
//If you like the colored zones, but don't like the plots because they cause screen compression, turn off the plots under the "style" tab.
 
//There are alerts for candle closes across every line.
 
//I should state that, regarding the fibs, obviously the length of the Channels is going to affect to what levels price retraces to. A shorter lookback means you
//will see more changes in highs and lows, and therefore retraces are often going to be full retraces within the bands unless price is trending hard. A longer
//lookback means you will see smaller retraces. Using this in conjunction with key high timeframe levels and/or a moving average can give great confidence in a
//trade entry. Additionally, if you have a short bias it may help in finding levels or entering a trade on a pullback. It could also be good for trade targets.
//But again, the lookback you choose for this indicator is going to dictate its use in the system you're building or already have.
//A 9 EMA and a 200 EMA, while fundamentally the same, are going to be used somewhat differently while doing your chart analysis.
 
 
study("+ Donchian Channels", shorttitle="+ DC", overlay=true, resolution="")
 
 
////DONCHIAN CHANNEL BANDS
src_high = input(defval=high, title="Source for Top", group="Inputs")
src_low = input(defval=low, title="Source for Bottom", group="Inputs")
lkbk = input(defval=233, title="Lookback", group="Inputs")
i_band_width = input(type=input.float, defval=8, step=0.25, title="Band Thickness", group="Inputs", tooltip="Defines the thickness of the band. Larger number is thinner. Realistically you wouldn't want to go below 3, I would think, or above 10.")
 
 
////CALCULATIONS, PLOTS, AND FILLS FOR UPPER & LOWER BANDS
top = highest(src_high, lkbk)
bottom = lowest(src_low, lkbk)
 
band_width = (top - bottom) / i_band_width
 
inner_top = top - band_width
inner_bottom = bottom + band_width
 
p_top = plot(top, color=color.new(color.red, 50), title="Top Donchian", display=display.none)
p_inner_top = plot(inner_top, color=color.new(color.red, 50), title="Top Donchian| Inner", display=display.none)
p_inner_bottom = plot(inner_bottom, color=color.new(color.blue, 50), title="Bottom Donchian | Inner", display=display.none)
p_bottom = plot(bottom, color=color.new(color.blue, 50), title="Bottom Donchian", display=display.none)
 
fill(p_top, p_inner_top, color=color.new(color.red, 90), title="Top Band")
fill(p_bottom, p_inner_bottom, color=color.new(color.blue, 90), title="Bottom Band")
 
 
////////////////////////////////////////////////////////////////////////////////
 
 
////BASIS FOR DONCHIAN CHANNEL
i_basis = input(defval=true, title="Display Basis?", group="Basis")
i_tf = input(type=input.resolution, title="Resolution for Basis Color", defval="", group="Basis", tooltip="Pick your timeframe and the basis will be colored based on if price is above or below the previous close of that other timeframe's basis.")
 
basis = avg(top, bottom)
 
plot(i_basis ? basis : na, color=security(syminfo.tickerid, i_tf, close > basis[1] ? color.new(#00bcd4, 50) : close < basis[1] ? color.new(#e91e63, 50) : na), title="Basis", linewidth=2)
 
 
////////////////////////////////////////////////////////////////////////////////
 
 
////FIBONACCI AND MINOR CHANNELS
i_mid_top = input(defval=true, title="Display Middle Top Channel Line?", group="Minor Channels")
i_mid_bottom = input(defval=true, title="Display Middle Bottom Channel Line?", group="Minor Channels")
 
i_high_fib_786 = input(defval=false, title="Display High .786 Fib?", group="Fib Channels")
i_high_fib_705 = input(defval=false, title="Display High .705 Fib?", group="Fib Channels")
i_low_fib_382 = input(defval=false, title="Display Low .382 Fib?", group="Fib Channels")
i_high_fib_382 = input(defval=false, title="Display High .382 Fib?", group="Fib Channels")
i_low_fib_705 = input(defval=false, title="Display Low .705 Fib?", group="Fib Channels")
i_low_fib_786 = input(defval=false, title="Display Low .786 Fib?", group="Fib Channels")
 
 
////CALCULATIONS
mid_top = (top + basis) / 2
mid_bottom = (bottom + basis) / 2
 
fib_calc = top - bottom
 
high_fib_786 = bottom + fib_calc * .786
high_fib_705 = bottom + fib_calc * .705
low_fib_382 = top - fib_calc * .382
 
high_fib_382 = bottom + fib_calc * .382
low_fib_705 = top - fib_calc * .705
low_fib_786 = top - fib_calc * .786
 
 
////PLOTS & FILLS
plot(i_mid_top ? mid_top : na, color=color.new(color.silver, 50), title="Top Channel Center Line")
plot(i_mid_bottom ? mid_bottom : na, color=color.new(color.silver, 50), title="Bottom Channel Center Line")
 
p_high_fib_786 = plot(i_high_fib_786 ? high_fib_786 : na, color=color.new(color.yellow, 50), title=".786 High")
p_high_fib_705 = plot(i_high_fib_705 ? high_fib_705 : na, color=color.new(color.silver, 50), title=".705 High")
p_low_fib_382 = plot(i_low_fib_382 ? low_fib_382 : na, color=color.new(color.teal, 50), title=".382 Low")
 
p_high_fib_382 = plot(i_high_fib_382 ? high_fib_382 : na, color=color.new(color.teal, 50), title=".382 High")
p_low_fib_705 = plot(i_low_fib_705 ? low_fib_705 : na, color=color.new(color.silver, 50), title=".705 Low")
p_low_fib_786 = plot(i_low_fib_786 ? low_fib_786 : na, color=color.new(color.purple, 50), title=".786 Low")
 
 
fill(p_top, p_high_fib_786, color=color.new(color.yellow, 85), title="High Zone")
fill(p_bottom, p_low_fib_786, color=color.new(color.purple, 85), title="Low Zone")
fill(p_high_fib_382, p_low_fib_382, color=color.new(color.teal, 90), title="Mid Zone")
 
 
////////////////////////////////////////////////////////////////////////////////
 
 
////ALERT CONDITIONS
alertcondition(cross(close, basis), title="Price crosses Basis", message="Price as crossed the basis of the Donchian Channels.")
alertcondition(cross(close, inner_bottom), title="Price crosses into low zone", message="Price has crossed into the low zone of the Donchian Channels.")
alertcondition(cross(close, inner_top), title="Price crosses into high zone", message="Price has crossed into the high zone of the Donchian Channels.")
alertcondition(cross(close, mid_top), title="Price crosses Top Channel Centerline", message="Price as crossed the center line of the top channel of the Donchian Channels.")
alertcondition(cross(close, mid_bottom), title="Price crosses Bottom Channel Centerline", message="Price as crossed the center line of the bottom channel of the Donchian Channels.")
alertcondition(cross(close, high_fib_786), title="Price crosses High .786", message="Price as crossed the high .786 (high zone) fib of the Donchian Channels.")
alertcondition(cross(close, high_fib_705), title="Price crosses High .705", message="Price as crossed the high .705 fib of the Donchian Channels.")
alertcondition(cross(close, low_fib_382), title="Price crosses Low .382", message="Price as crossed the low .382 (mid zone) fib of the Donchian Channels.")
alertcondition(cross(close, high_fib_382), title="Price crosses High .382", message="Price as crossed the high .382 (mid zone fib of the Donchian Channels.")
alertcondition(cross(close, low_fib_705), title="Price crosses Low .705", message="Price as crossed the low .705 fib of the Donchian Channels.")
alertcondition(cross(close, low_fib_786), title="Price crosses Low .786", message="Price as crossed the l0w .786 (low zone) fib of the Donchian Channels.")

← 返回列表