本页源码来自 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/
// © blackcat1402
//@version=5
indicator("[blackcat] L3 Twin Range Filter Pro", overlay=true)
// Inputs
price_source = input(defval=close, title='Price Source')
show_trade_signals = input(title='Show Buy/Sell Signals ?', defval=true)
fast_period = input.int(defval=9, minval=1, title='Fast period')
fast_range_multiplier = input.float(defval=1.6, minval=0.1, title='Fast range multiplier')
slow_period = input.int(defval=34, minval=1, title='Slow period')
slow_range_multiplier = input.float(defval=2, minval=0.1, title='Slow range multiplier')
// Custom functions
calculate_smooth_range(price, period, multiplier) =>
window_period = period * 2 - 1
average_range = ta.ema(math.abs(price - price[1]), period)
smooth_range = ta.ema(average_range, window_period) * multiplier
smooth_range
apply_range_filter(price, range_value) =>
range_filtered_price = price
range_filtered_price := price > nz(range_filtered_price[1]) ? price - range_value < nz(range_filtered_price[1]) ? nz(range_filtered_price[1]) : price - range_value : price + range_value > nz(range_filtered_price[1]) ? nz(range_filtered_price[1]) : price + range_value
range_filtered_price
// Calculations
fast_smooth_range = calculate_smooth_range(price_source, fast_period, fast_range_multiplier)
slow_smooth_range = calculate_smooth_range(price_source, slow_period, slow_range_multiplier)
average_smooth_range = (fast_smooth_range + slow_smooth_range) / 2
filtered_price = apply_range_filter(price_source, average_smooth_range)
upward_trend = 0.0
upward_trend := filtered_price > filtered_price[1] ? nz(upward_trend[1]) + 1 : filtered_price < filtered_price[1] ? 0 : nz(upward_trend[1])
downward_trend = 0.0
downward_trend := filtered_price < filtered_price[1] ? nz(downward_trend[1]) + 1 : filtered_price > filtered_price[1] ? 0 : nz(downward_trend[1])
upper_range_boundary = filtered_price + average_smooth_range
lower_range_boundary = filtered_price - average_smooth_range
upper_bound = 0.0
upper_bound := upper_range_boundary < nz(upper_bound[1]) or close[1] > nz(upper_bound[1]) ? upper_range_boundary : nz(upper_bound[1])
lower_bound = 0.0
lower_bound := lower_range_boundary > nz(lower_bound[1]) or close[1] < nz(lower_bound[1]) ? lower_range_boundary : nz(lower_bound[1])
trend_range_filter = 0.0
trend_range_filter := nz(trend_range_filter[1]) == upper_bound[1] and close <= upper_bound ? upper_bound : nz(trend_range_filter[1]) == upper_bound[1] and close >= upper_bound ? lower_bound : nz(trend_range_filter[1]) == lower_bound[1] and close >= lower_bound ? lower_bound : nz(trend_range_filter[1]) == lower_bound[1] and close <= lower_bound ? upper_bound : upper_bound
// Buy/Sell Conditions
buy_signal = ta.crossover(close, trend_range_filter)
sell_signal = ta.crossunder(close, trend_range_filter)
// Plot labels
if show_trade_signals and buy_signal
label.new(bar_index, trend_range_filter, "BUY", color=color.new(color.green, 0), style=label.style_label_up)
if show_trade_signals and sell_signal
label.new(bar_index, trend_range_filter, "SELL", color=color.new(color.red, 0), style=label.style_label_down)
// Alerts
alertcondition(buy_signal, title="Buy Signal Alert", message="BUY triggered on {{ticker}}")
alertcondition(sell_signal, title="Sell Signal Alert", message="SELL triggered on {{ticker}}")
// Plots
trend_range_filter_plot = plot(trend_range_filter, color=close > trend_range_filter ? color.new(color.lime, 10) :close < trend_range_filter ? color.new(color.red, 10) : na, title="Trend Range Filter")
price_plot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0)
up_trend_color = close > trend_range_filter ? color.new(color.lime, 80) : na
down_trend_color = close < trend_range_filter ? color.new(color.red, 80) : na
fill(price_plot, trend_range_filter_plot, title='UpTrend Highlighter', color=up_trend_color, transp=90)
fill(price_plot, trend_range_filter_plot, title='DownTrend Highlighter', color=down_trend_color, transp=90)