本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator(title = "Enhanced Keltner Trend", shorttitle = "EKT",overlay = true)
// User inputs for Keltner Channel settings
atrBool = input.bool(title = "Use Average True Range", defval = true, tooltip = "Or it will use price High and Low Values")
atrLen = input.int(title = "ATR Period", defval = 9, minval = 1)
atrMult = input.float(title = "ATR Multiplier", defval = 0.7, minval = 0.1, step = 0.1)
maLen = input.int(title = "Period", defval = 11, minval = 1)
src = input(close, title = "Source")
highLowRange = ta.highest(high,maLen) - ta.lowest(low,maLen)
// Moving Average Calculation Function
ma(src, len, type) =>
type == "SMA" ? ta.sma(src, len) :
type == "EMA" ? ta.ema(src, len) :
type == "RMA" ? ta.rma(src, len) :
type == "WMA" ? ta.wma(src, len) :
type == "VWMA" ? ta.vwma(src, len) :
type == "HMA" ? ta.hma(src, len) :
na
// Input for MA Type
maType = input.string("EMA", title = "MA Type", options=["SMA", "EMA", "RMA", "WMA", "VWMA", "HMA"])
// Keltner Channel Calculation
priceBasis = ma(src, maLen, maType)
channelWidth = atrBool ? ta.atr(atrLen) : highLowRange // Uses ATR volatility or Highest High - Lowest Low over defined period
movFactor = ma(channelWidth, maLen, maType) * atrMult //Calculates how much of the volatility the user want to factor into the channels
upperBand = priceBasis + movFactor
lowerBand = priceBasis - movFactor
// Bullish and Bearish Conditions
bulTrend = src > upperBand[1] //if the source is bigger than the last bar's upper Keltner band
bearTrend = src < lowerBand[1] //if the source is lower than the last bar's lower Keltner band
// Trend Color Based on Conditions
trend = ta.barssince(bulTrend and not bearTrend) <= ta.barssince(bearTrend and not bulTrend) ? #0097a7 : #f47d02
// Plot Final Trend
plot(priceBasis, color = trend, linewidth = 3)
// Alert Conditions
alertcondition(bulTrend, title="Bullish Trend", message="Bullish Trend - Price crossed above the upper band")
alertcondition(bearTrend, title="Bearish Trend", message="Bearish Trend - Price crossed below the lower band")