本页源码来自 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/
// Arranged by © ClassicScott
//@version=5
// This is my second Donchian Channels indicator (and will probably be my last because how many does one really need).
// This version is different from my other one in that, well, it's 'dynamic' which simply means that it self adjusts based on the same formula that my Ultimate
// Moving Average does. What does that mean? It just means that the script takes an average of 8 different length, in this case, highest highs and lowest lows.
// The user doesn't need to pick a lookback/length/period/what-have-you. The indicator does it all itself. This, I think, makes for a very nice baseline or bias
// indicator to fit within a system that utilizes something like that. I also think it makes for a more accurate gauge of higher highs and lower lows within a
// timeframe because honestly what does it mean to make a lower low over 20 periods or 8 periods or 50 periods? I don't know. What I do know is that traditional
// Donchian Channels never made much sense to me, but this does.
// Additionally I've kept (I guess that's not 'additionally') the fibonacci retracement levels from my other Donchian Channels indicator. 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, an upper and lower zone
// filled between the .786 and either the high or the low, and a zone between the .705 and .785
// 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, or much more simply
// right click on the price scale and click 'scale price chart only.' Voila! No more screen compression due to a moving average or some other annoyance.
// Besides that basis being a nice baseline indicator the various fib bands (or just the high and low bands) make for excellent mean reversion extremes in ranging
// environments.
// There are alerts for candle closes across every line.
indicator('+ Dynamic Fibo-Donchian Channels', shorttitle='+ DFDC', overlay=true, timeframe='')
////HIGH AND LOW CALCULATIONS
l_144 = ta.lowest(low, 144)
l_89 = ta.lowest(low, 89)
l_55 = ta.lowest(low, 55)
l_34 = ta.lowest(low, 34)
l_21 = ta.lowest(low, 21)
l_13 = ta.lowest(low, 13)
l_8 = ta.lowest(low, 8)
l_5 = ta.lowest(low, 233)
bottom = (l_144 + l_89 + l_55 + l_34 + l_21 + l_13 + l_8 + l_5) / 8
h_144 = ta.highest(high, 144)
h_89 = ta.highest(high, 89)
h_55 = ta.highest(high, 55)
h_34 = ta.highest(high, 34)
h_21 = ta.highest(high, 21)
h_13 = ta.highest(high, 13)
h_8 = ta.highest(high, 8)
h_5 = ta.highest(high, 233)
top = (h_144 + h_89 + h_55 + h_34 + h_21 + h_13 + h_8 + h_5) / 8
////CALCULATIONS, PLOTS, AND FILLS FOR UPPER & LOWER BANDS
i_band_width = input.float(defval=13, step=0.5, 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.')
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='Upper Level', display=display.none)
p_inner_top = plot(inner_top, color=color.new(color.red, 50), title='Upper Level| Inner', display=display.none)
p_inner_bottom = plot(inner_bottom, color=color.new(color.blue, 50), title='Lower Level | Inner', display=display.none)
p_bottom = plot(bottom, color=color.new(color.blue, 50), title='Lower Level', display=display.none)
fill(p_top, p_inner_top, color=color.new(color.red, 90), title='Upper Band')
fill(p_bottom, p_inner_bottom, color=color.new(color.blue, 90), title='Lower Band')
////////////////////////////////////////////////////////////////////////////////
////BASIS FOR DONCHIAN CHANNEL
i_basis = input.bool(defval=true, title='Display Basis?', group='Basis')
i_tf = input.timeframe(title='Resolution for Basis Color', defval='', group='Basis', tooltip='Pick your timeframe and the basis will be colored based on if current price is above or below the previous close of that other timeframe\'s basis.')
basis = math.avg(top, bottom)
plot(i_basis ? basis : na, color=request.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 CHANNELS
i_mid_top = input.bool(defval=false, title='Display Upper Mid Channel Line?', group='Minor Channels')
i_mid_bottom = input.bool(defval=false, title='Display Lower Mid Channel Line?', group='Minor Channels')
i_high_fib_786 = input.bool(defval=true, title='Display Upper .786 Fib?', group='Fib Channels')
i_high_fib_705 = input.bool(defval=false, title='Display Upper .705 Fib?', group='Fib Channels')
i_low_fib_382 = input.bool(defval=true, title='Display Upper .382 Fib?', group='Fib Channels')
i_high_fib_382 = input.bool(defval=true, title='Display Lower .382 Fib?', group='Fib Channels')
i_low_fib_705 = input.bool(defval=false, title='Display Lower .705 Fib?', group='Fib Channels')
i_low_fib_786 = input.bool(defval=true, title='Display Lower .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
p_high_fib_786 = plot(i_high_fib_786 ? high_fib_786 : na, color=color.new(color.yellow, 50), title='.786 Upper')
p_high_fib_705 = plot(i_high_fib_705 ? high_fib_705 : na, color=color.new(color.silver, 50), title='.705 Upper')
p_mid_top = plot(i_mid_top ? mid_top : na, color=color.new(color.silver, 50), title='.5 Upper')
p_low_fib_382 = plot(i_low_fib_382 ? low_fib_382 : na, color=color.new(color.teal, 50), title='.382 Upper')
p_high_fib_382 = plot(i_high_fib_382 ? high_fib_382 : na, color=color.new(color.teal, 50), title='.382 Lower')
p_mid_bottom = plot(i_mid_bottom ? mid_bottom : na, color=color.new(color.silver, 50), title='.5 Lower')
p_low_fib_705 = plot(i_low_fib_705 ? low_fib_705 : na, color=color.new(color.silver, 50), title='.705 Lower')
p_low_fib_786 = plot(i_low_fib_786 ? low_fib_786 : na, color=color.new(color.purple, 50), title='.786 Lower')
fill(p_top, p_high_fib_786, color=color.new(color.yellow, 85), title='High Zone')
fill(p_mid_top, p_high_fib_705, color=color.new(color.orange, 85), title='High Mid Zone')
fill(p_high_fib_382, p_low_fib_382, color=color.new(color.teal, 90), title='Mid Zone')
fill(p_mid_bottom, p_low_fib_705, color=color.new(color.blue, 85), title='Low Mid Zone')
fill(p_bottom, p_low_fib_786, color=color.new(color.purple, 85), title='Low Zone')
////////////////////////////////////////////////////////////////////////////////
////ALERT CONDITIONS
alertcondition(ta.cross(close, basis), title='Price crosses Basis', message='Price as crossed the basis of the Donchian Channels.')
alertcondition(ta.cross(close, inner_bottom), title='Price crosses into low zone', message='Price has crossed into the low zone of the Donchian Channels.')
alertcondition(ta.cross(close, inner_top), title='Price crosses into high zone', message='Price has crossed into the high zone of the Donchian Channels.')
alertcondition(ta.cross(close, mid_top), title='Price crosses Top Channel Mid', message='Price as crossed the middle upper fib of the Donchian Channels.')
alertcondition(ta.cross(close, mid_bottom), title='Price crosses Bottom Channel Mid', message='Price as crossed the middle lower fib of the Donchian Channels.')
alertcondition(ta.cross(close, high_fib_786), title='Price crosses Upper .786', message='Price as crossed the Upper .786 (high zone) fib of the Donchian Channels.')
alertcondition(ta.cross(close, high_fib_705), title='Price crosses Upper .705', message='Price as crossed the Upper .705 fib of the Donchian Channels.')
alertcondition(ta.cross(close, low_fib_382), title='Price crosses Upper .382', message='Price as crossed the Upper .382 (mid zone) fib of the Donchian Channels.')
alertcondition(ta.cross(close, high_fib_382), title='Price crosses Lower .382', message='Price as crossed the Lower .382 (mid zone fib of the Donchian Channels.')
alertcondition(ta.cross(close, low_fib_705), title='Price crosses Lower .705', message='Price as crossed the Lower .705 fib of the Donchian Channels.')
alertcondition(ta.cross(close, low_fib_786), title='Price crosses Lower .786', message='Price as crossed the Lower .786 (low zone) fib of the Donchian Channels.')