本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoTrade_Pro
// This script includes an adapted Average Force function originally published as open-source on TradingView by its author - racer8
//@version=6
indicator("SuperTrend Fusion — ATP", overlay = true, timeframe = "", timeframe_gaps = true)
//──────────────────────────
// INPUTS
//──────────────────────────
// 1) SuperTrend – Trend component
var g_trend = "SuperTrend – Trend"
atrPeriod = input.int(10, "ATR Length", minval = 1, group = g_trend)
factor = input.float(3, "Factor", minval = 0.01, step = 0.01, group = g_trend)
// 2) Average Force – Momentum filter
var g_mom = "Average Force – Momentum"
useAfFilter = input.bool(true, "Use AF Filter", group = g_mom)
afPeriod = input.int(18, "AF Period", minval = 1, group = g_mom)
afSmooth = input.int(6, "AF Smooth", minval = 1, group = g_mom)
// 3) Choppiness Index – Volatility / trend filter
var g_chop = "Choppiness Index – Volatility"
useChopFilter = input.bool(true, "Use Chop Filter", group = g_chop)
chopLength = input.int(14, "Chop Length", minval = 1, group = g_chop)
chopThreshold = input.int(50, "Chop Threshold", minval = 1, maxval = 100, group = g_chop)
//──────────────────────────
// CORE CALCULATIONS
//──────────────────────────
// 1) SuperTrend base
[stValue, stDirection] = ta.supertrend(factor, atrPeriod)
isUpTrend = stDirection < 0
isDownTrend = stDirection > 0
// 2) Average Force function
af(src, hi, lo, length, postSmooth) =>
p = math.max(1, int(length))
highestHi = ta.highest(hi, p)
lowestLo = ta.lowest(lo, p)
ranges = highestHi - lowestLo
raw = ranges == 0.0 ? 0.0 : (src - lowestLo) / ranges - 0.5
ta.sma(raw, math.max(1, int(postSmooth)))
afValue = af(close, high, low, afPeriod, afSmooth)
// 3) Choppiness Index
highestHiChop = ta.highest(high, chopLength)
lowestLoChop = ta.lowest(low, chopLength)
rangeChop = highestHiChop - lowestLoChop
choppiness = rangeChop != 0 ? 100 * math.log10(math.sum(ta.atr(1), chopLength) / rangeChop) / math.log10(chopLength) : 100
//──────────────────────────
// FUSION LOGIC
//──────────────────────────
bullMomentum = afValue > 0
bearMomentum = afValue < 0
marketTrending = choppiness < chopThreshold
bullFilterOk = (not useAfFilter or bullMomentum) and (not useChopFilter or marketTrending)
bearFilterOk = (not useAfFilter or bearMomentum) and (not useChopFilter or marketTrending)
rawLongFlip = ta.change(stDirection) < 0
rawShortFlip = ta.change(stDirection) > 0
longCondition = rawLongFlip and bullFilterOk
shortCondition = rawShortFlip and bearFilterOk
rawLongFlipFiltered = rawLongFlip and not longCondition
rawShortFlipFiltered = rawShortFlip and not shortCondition
//──────────────────────────
// VISUAL OUTPUT — SINGLE LINES
//──────────────────────────
// Premium signal colors
longColor = color.new(color.aqua, 0)
shortColor = color.new(color.orange, 0)
// Fusion SuperTrend (bold)
plot(longCondition or shortCondition ? stValue : na, "Fusion Trend",
color = longCondition ? longColor : shortCondition ? shortColor : na,
linewidth = 3, style = plot.style_linebr)
// Base SuperTrend with colors
plot(stValue, "Base SuperTrend",
color = isUpTrend ? color.new(color.aqua, 0) : color.new(color.orange, 0),
style = plot.style_linebr, linewidth = 2)
// Bar colors
barcolor(longCondition ? color.new(longColor, 70) : shortCondition ? color.new(shortColor, 70) : na)
// Choppy background
bgcolor(useChopFilter and not marketTrending ? color.new(color.orange, 92) : na, force_overlay = true)
// Entry markers (WHITE text color, more pro wording)
plotshape(longCondition, "Fusion Long Entry",
style = shape.triangleup, location = location.belowbar,
size = size.small, color = longColor)
plotshape(shortCondition, "Fusion Short Entry",
style = shape.triangledown, location = location.abovebar,
size = size.small, color = shortColor)
// Filtered signals (X marks)
plotshape(rawLongFlipFiltered, "Filtered Long Flip",
style = shape.xcross, location = location.belowbar,
size = size.tiny, color = color.new(color.orange, 0))
plotshape(rawShortFlipFiltered, "Filtered Short Flip",
style = shape.xcross, location = location.abovebar,
size = size.tiny, color = color.new(color.orange, 0))
//──────────────────────────
// ALERTS
//──────────────────────────
alertcondition(longCondition, title = "Fusion Long", message = "SuperTrend Fusion — Long signal")
alertcondition(shortCondition, title = "Fusion Short", message = "SuperTrend Fusion — Short signal")