本页源码来自 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/ MPL-2.0
//@version=6
indicator("4H + 15m Alignment Trend Candles", shorttitle = "MTF Trend Candles", overlay = true)
// --- Inputs ---
string htfInput = input.timeframe("240", "Higher timeframe", tooltip = "Higher timeframe used to confirm the trend; 240 is 4 hours.", group = "Trend")
string ltfInput = input.timeframe("15", "Lower timeframe", tooltip = "Lower timeframe that must agree with the higher-timeframe trend; 15 is 15 minutes.", group = "Trend")
int fastLengthInput = input.int(21, "Fast EMA length", minval = 1, tooltip = "Trend is bullish when the fast EMA is above the slow EMA and price is above the fast EMA.", group = "Trend")
int slowLengthInput = input.int(50, "Slow EMA length", minval = 2, tooltip = "Trend is bearish when the fast EMA is below the slow EMA and price is below the fast EMA.", group = "Trend")
color bullishColorInput = input.color(#089981, "Bullish aligned", tooltip = "Candle color when both timeframes are bullish.", group = "Colors")
color bearishColorInput = input.color(#f23645, "Bearish aligned", tooltip = "Candle color when both timeframes are bearish.", group = "Colors")
color neutralColorInput = input.color(#808080, "Not aligned / neutral", tooltip = "Candle color when the timeframes disagree or either trend is neutral.", group = "Colors")
bool colorNeutralInput = input.bool(true, "Color neutral candles", tooltip = "When disabled, candles that are not aligned keep the chart's original colors.", group = "Colors")
// --- Trend Logic ---
f_trendState() =>
float fastEma = ta.ema(close, fastLengthInput)
float slowEma = ta.ema(close, slowLengthInput)
close > fastEma and fastEma > slowEma ? 1 : close < fastEma and fastEma < slowEma ? -1 : 0
int higherTrend = request.security(syminfo.tickerid, htfInput, f_trendState(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
int lowerTrend = request.security(syminfo.tickerid, ltfInput, f_trendState(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
color candleColor = higherTrend == 1 and lowerTrend == 1 ? bullishColorInput : higherTrend == -1 and lowerTrend == -1 ? bearishColorInput : colorNeutralInput ? neutralColorInput : na
barcolor(candleColor)