本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=4
// Dual Mean Reversion Channel (adjusted lower band)
// Author: CptFantastic, v1.0, 2021.06.28
// This code is to be considered open-source & public domain, to be reused freely, credits are optional yet appreciated
// All relevant infos are on the published indicator page
// credits to @xinolia for the original Volume weighted Keltner Channels and the readjustment technique for lower bands on a log scale
// thanks to @author=LucF for introducing me to Pine coding convention
// code written using PineCoders Coding Conventions for Pine: http://www.pinecoders.com/coding_conventions/
study(title="Dual Mean Reversion Channel (adjusted lower band)", shorttitle="dMRC", overlay=true)
// ----------- Inputs {
src = input(defval=close, title="Input Source", type=input.source)
i_volat_mult_min = input(defval=5, title="Min Volatility Multiplier", type=input.float, minval=2, maxval=100,step=1)
i_volat_mult_max = input(defval=8, title="Max Volatility Multiplier", type=input.float, minval=2, maxval=100,step=1)
i_smth_method = input(defval="vwma", title="Smoothing Method", type=input.string, options=["sma", "ema", "vwma"])
i_length_A = input(defval=50, title="Channel Length", type=input.integer, group="Channel A", minval=10, maxval=300, step=10)
i_hide_center_A = input(defval=false, title="Hide Center", type=input.bool, group="Channel A")
i_hide_channel_A = input(defval=false, title="Hide Channel", type=input.bool, group="Channel A")
i_length_B = input(defval=200, title="Channel length", type=input.integer, group="Channel B", minval=10, maxval=300, step=10)
i_hide_center_B = input(defval=false, title="Hide Center", type=input.bool, group="Channel B")
i_hide_channel_B = input(defval=false, title="Hide Channel", type=input.bool, group="Channel B")
//}
// ----------- Functions {
// Smoothing method selection
f_smooth_method(_smoothing_method, _var_to_smooth, _length) =>
if _smoothing_method == "sma"
sma(_var_to_smooth, _length)
else if _smoothing_method == "ema"
ema(_var_to_smooth, _length)
else if _smoothing_method == "vwma"
vwma(_var_to_smooth, _length)
// Band calculation
f_bands(_i_vol_mult, _channel_center, _channel_range) =>
if _i_vol_mult > 0
_channel_center + _i_vol_mult * _channel_range
else
_channel_center * _channel_center / (_channel_center + abs(_i_vol_mult) * _channel_range)
//}
// ----------- Calculations {
// Channel A
sma_A = sma(src, i_length_A)
center_A = f_smooth_method(i_smth_method, src, i_length_A)
channel_range_A = f_smooth_method(i_smth_method, tr, i_length_A)
channel_bottom_min_A = f_bands(-i_volat_mult_min, center_A, channel_range_A)
channel_bottom_max_A = f_bands(-i_volat_mult_max, center_A, channel_range_A)
channel_top_min_A = f_bands(i_volat_mult_min, center_A, channel_range_A)
channel_top_max_A = f_bands(i_volat_mult_max, center_A, channel_range_A)
// Channel B
sma_B = sma(src, i_length_B)
center_B = f_smooth_method(i_smth_method, src, i_length_B)
channel_range_B = f_smooth_method(i_smth_method, tr, i_length_B)
channel_bottom_min_B = f_bands(-i_volat_mult_min, center_B, channel_range_B)
channel_bottom_max_B = f_bands(-i_volat_mult_max, center_B, channel_range_B)
channel_top_min_B = f_bands(i_volat_mult_min, center_B, channel_range_B)
channel_top_max_B = f_bands(i_volat_mult_max, center_B, channel_range_B)
//}
// ----------- Plots {
p_sma_A = plot(i_hide_center_A?na:sma_A, title="SMA A", color=na)
p_center_A = plot(i_hide_center_A?na:center_A, title="Channel A base", color=color.new(color.blue, 70), linewidth=1)
channel_A_top_min = plot(i_hide_channel_A?na:channel_top_min_A, title="Channel A top min", color=na)
channel_A_top_max = plot(i_hide_channel_A?na:channel_top_max_A, title="Channel A top max", color=na)
channel_A_bot_min = plot(i_hide_channel_A?na:channel_bottom_min_A, title="Channel A bottom min", color=na)
channel_A_bot_max = plot(i_hide_channel_A?na:channel_bottom_max_A, title="Channel A bottom max", color=na)
fill(p_sma_A, p_center_A, title="Center/SMA A width", color=i_hide_center_A?na:color.new(color.blue, 85))
fill(channel_A_top_min, channel_A_top_max, title="Channel A width", color=i_hide_channel_A?na:color.new(color.blue, 85))
fill(channel_A_bot_min, channel_A_bot_max, title="Channel A width", color=i_hide_channel_A?na:color.new(color.blue, 85))
p_sma_B = plot(i_hide_center_B?na:sma_B, title="SMA B", color=na)
p_center_B = plot(i_hide_center_B?na:center_B, title="Channel B base", color=color.new(color.orange, 70), linewidth=1)
channel_B_top_min = plot(i_hide_channel_B?na:channel_top_min_B, title="Channel B top min", color=na)
channel_B_top_max = plot(i_hide_channel_B?na:channel_top_max_B, title="Channel B top max", color=na)
channel_B_bot_min = plot(i_hide_channel_B?na:channel_bottom_min_B, title="Channel B bottom min", color=na)
channel_B_bot_max = plot(i_hide_channel_B?na:channel_bottom_max_B, title="Channel B bottom max", color=na)
fill(p_sma_B, p_center_B, title="Center/SMA B width", color=i_hide_center_B?na:color.new(color.orange, 85))
fill(channel_B_top_min, channel_B_top_max, title="Channel B width", color=i_hide_channel_B?na:color.new(color.orange, 85))
fill(channel_B_bot_min, channel_B_bot_max, title="Channel B width", color=i_hide_channel_B?na:color.new(color.orange, 85))
//}