本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Multi-Timeframe Composite MA", shorttitle="MTF Composite MA", overlay=true, dynamic_requests=true)
// ==============================================================================
// 1. INPUTS
// ==============================================================================
maLength = input.int(7, title="Moving Average Length", minval=1)
maType = input.string("SMA", title="MA Type", options=["SMA", "EMA"])
// ==============================================================================
// 2. MA CALCULATION FUNCTION
// ==============================================================================
// Calculates either an SMA or EMA in the requested timeframe context.
f_ma(src, len, type) =>
type == "SMA" ? ta.sma(src, len) : ta.ema(src, len)
// ==============================================================================
// 3. MULTI-TIMEFRAME DATA
// ==============================================================================
// Fixed timeframes used by the composite:
// 4H + 1H + 30M + 15M + 5M
// Returns the requested MA correctly whether the requested timeframe is
// higher than, equal to, or lower than the chart timeframe.
f_mtf_ma(tf) =>
float result = na
int chartSeconds = timeframe.in_seconds(timeframe.period)
int requestedSeconds = timeframe.in_seconds(tf)
if requestedSeconds >= chartSeconds
// Same or higher timeframe.
result := request.security(
syminfo.tickerid,
tf,
f_ma(close, maLength, maType)
)
else
// Lower timeframe.
// Retrieve all available intrabars and use the latest one.
array<float> ltfValues = request.security_lower_tf(
syminfo.tickerid,
tf,
f_ma(close, maLength, maType)
)
if array.size(ltfValues) > 0
result := array.last(ltfValues)
result
// ==============================================================================
// 4. FETCH THE FIVE FIXED TIMEFRAMES
// ==============================================================================
ma_4H = f_mtf_ma("240")
ma_1H = f_mtf_ma("60")
ma_30m = f_mtf_ma("30")
ma_15m = f_mtf_ma("15")
ma_5m = f_mtf_ma("5")
// ==============================================================================
// 5. COMPOSITE CALCULATION
// ==============================================================================
// Equal weighting:
// 4H = 20%
// 1H = 20%
// 30M = 20%
// 15M = 20%
// 5M = 20%
bool allValuesAvailable =
not na(ma_4H) and
not na(ma_1H) and
not na(ma_30m) and
not na(ma_15m) and
not na(ma_5m)
float compositeMA = allValuesAvailable
? (ma_4H + ma_1H + ma_30m + ma_15m + ma_5m) / 5.0
: na
// ==============================================================================
// 6. COMPOSITE DIRECTION
// ==============================================================================
// Rising = Green
// Falling = Red
// Flat = Gray
lineColor =
na(compositeMA) or na(compositeMA[1])
? color.gray
: compositeMA > compositeMA[1]
? color.new(#26A69A, 0)
: compositeMA < compositeMA[1]
? color.new(#EF5350, 0)
: color.new(color.gray, 0)
// ==============================================================================
// 7. MAIN COMPOSITE MA
// ==============================================================================
plot(
compositeMA,
title="Composite MTF MA",
color=lineColor,
linewidth=3
)
// ==============================================================================
// 8. OPTIONAL INDIVIDUAL TIMEFRAME MAs
// ==============================================================================
// Hidden by default to keep the chart clean.
// Users can enable them from the Style settings.
plot(
ma_4H,
title="4H MA",
color=color.new(color.purple, 70),
display=display.none
)
plot(
ma_1H,
title="1H MA",
color=color.new(color.blue, 70),
display=display.none
)
plot(
ma_30m,
title="30M MA",
color=color.new(color.teal, 70),
display=display.none
)
plot(
ma_15m,
title="15M MA",
color=color.new(color.orange, 70),
display=display.none
)
plot(
ma_5m,
title="5M MA",
color=color.new(color.yellow, 70),
display=display.none
)