Range Filtered Trend Signals [AlgoAlpha]

AlgoAlpha · study · 111 行 · 点赞 3,308 · TradingView 原页

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

Pine Script

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoAlpha
 
//@version=6
indicator("Range Filtered Trend Signals [AlgoAlpha]", "AlgoAlpha - Range Filtered", true)
 
groupKalman = "Kalman Filter"
kalmanAlpha = input.float(0.01, title="Kalman Alpha", tooltip="The Alpha parameter controls the smoothing factor of the Kalman filter. A smaller value results in more smoothing, while a larger value makes the filter more responsive to price changes.", group=groupKalman)
kalmanBeta = input.float(0.1, title="Kalman Beta", tooltip="The Beta parameter influences the rate of change in the Kalman filter. It adjusts the filter's sensitivity to trend changes.", group=groupKalman)
kalmanPeriod = input.int(77, title="Kalman Period", tooltip="The Period defines the number of bars used in the Kalman filter calculation, affecting the filter's responsiveness to market movements.", group=groupKalman)
dev = input.float(1.2, title="Deviation", tooltip="The Deviation parameter sets the multiplier for the deviation from the trend line, affecting the width of the trend band.", group=groupKalman)
 
groupSupertrend = "Supertrend"
supertrendFactor = input.float(0.7, title="Supertrend Factor", tooltip="This Factor determines the multiplier for the ATR in the Supertrend calculation, affecting the distance of the trend line from the price.", group=groupSupertrend)
supertrendAtrPeriod = input.int(7, title="ATR Period", tooltip="The ATR Period specifies the number of bars used to calculate the Average True Range, which is a component of the Supertrend indicator.", group=groupSupertrend)
 
groupColors = "Colors"
green = input.color(#00ffbb, title="Bullish Color", tooltip="This color is used to indicate a bullish trend in the chart.", group=groupColors)
red = input.color(#ff1100, title="Bearish Color", tooltip="This color is used to indicate a bearish trend in the chart.", group=groupColors)
 
kalman(a, b, alpha, beta) =>
    var float v1 = na
    var float v2 = 1.0
    var float v3 = alpha * b
    var float v4 = 0.0
    var float v5 = na
 
    if na(v1)
        v1 := a[1]
 
    v5 := v1
    v4 := v2 / (v2 + v3)
    v1 := v5 + v4 * (a - v5)
    v2 := (1 - v4) * v2 + beta / b
 
    v1
 
pine_supertrend(k, factor, atrPeriod) =>
    src = k
    atr = ta.atr(atrPeriod)
    upperBand = src + factor * atr
    lowerBand = src - factor * atr
    prevLowerBand = nz(lowerBand[1])
    prevUpperBand = nz(upperBand[1])
 
    lowerBand := lowerBand > prevLowerBand or k[1] < prevLowerBand ? lowerBand : prevLowerBand
    upperBand := upperBand < prevUpperBand or k[1] > prevUpperBand ? upperBand : prevUpperBand
    int _direction = na
    float superTrend = na
    prevSuperTrend = superTrend[1]
    if na(atr[1])
        _direction := 1
    else if prevSuperTrend == prevUpperBand
        _direction := k > upperBand ? -1 : 1
    else
        _direction := k < lowerBand ? 1 : -1
    superTrend := _direction == -1 ? lowerBand : upperBand
    [superTrend, _direction]
 
k = kalman(close, kalmanPeriod, kalmanAlpha, kalmanBeta)
 
[supertrend, direction] = pine_supertrend(k, supertrendFactor, supertrendAtrPeriod)
 
vola = ta.wma(high-low, 200)
 
upper = k+vola*dev
lower = k-vola*dev
 
midbody = math.avg(close, open)
 
var trend = 0
if close > upper
    trend := 1
else if close < lower
    trend := -1
 
ktrend = 0
if direction < 0
    ktrend := 1
else if direction > 0
    ktrend := -1
 
t = 70
t_ = 20
 
p1 = plot(ktrend * trend == 1 ? k : na, color=color.gray, style = plot.style_linebr, linewidth = 3)
 
m = plot(midbody, color=color.gray, display = display.none)
 
up = plot(trend == -1 or ktrend * trend == -1 ? upper : na, color=ktrend * trend == -1 ? color.gray : ktrend == -1 ? color.new(red, t) : color.gray, style = plot.style_circles)
 
lo = plot(trend == 1 or ktrend * trend == -1 ? lower : na, color=ktrend * trend == -1 ? color.gray : ktrend == 1 ? color.new(green, t) : color.gray, style = plot.style_circles)
 
plotchar(ta.crossover(ktrend * trend, 0) ? k : na, location = location.absolute, color=trend == 1 ? green : red, char="◉", size=size.tiny)
 
x = color.new(chart.bg_color, 80)
x_ = color.new(trend == -1 ? red : green, t)
 
fill(p1, m, k, midbody, x, x_)
 
fill(up, lo, ktrend * trend == -1 ? color.new(color.gray, 90) : na, "Range")
 
barcolor(ktrend * trend == -1 ? color.new(color.gray, t_) : trend == 1 ? color.new(green, t_) : color.new(red, t_))
 
////////////////////////Alerts
alertcondition(ta.crossover(ktrend * trend, 0), title="Market Trending", message="The market is trending {{trend == 1 ? 'up' : 'down'}}")
alertcondition(ta.crossunder(ktrend * trend, 0), title="Market Ranging", message="The market is ranging")
alertcondition(ta.crossover(trend, 0), title="Market Short Term Bullish", message="The market is bullish short term")
alertcondition(ta.crossunder(trend, 0), title="Market Short Term Bearish", message="The market is bearish short term")
alertcondition(ta.crossover(ktrend, 0), title="Market Long Term Bullish", message="The market is bullish long term")
alertcondition(ta.crossunder(ktrend, 0), title="Market Long Term Bearish", message="The market is bearish long term")

← 返回列表