本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(" AZ Dynamic Trend Indicator with Heikin-Ashi", "AZ Trend Indicator A (v2.7)", true)
// ---------------------
// Inputs
// ---------------------
// Setup
ma_type = input.string('EMA', 'MA Type', ['ALMA','HMA','SMA','SWMA','VWMA','WMA','ZLEMA','EMA'], group = 'Setup')
ma_period = input.int(9, 'MA Period', 1, group='Setup')
trend_tf = input.string("", "Trend Timeframe", tooltip="e.g., 1, 15, 60, D. Leave empty for current", group='Setup')
use_heikinashi = input.bool(true, "Use Heikin-Ashi", group='Setup')
//len = input.int(9, "Length", 1, group='Setup')
//src = input(close, "Source")
alma_offset = input.float(0.85, 'ALMA Shift', 0, 1, 0.05, group = 'Setup (ALMA)')
alma_sigma = input.int(6, 'ALMA Deviation', 1, group = 'Setup (ALMA)')
rev_bars_window = input.int(3, "Reversal Window", 1, group='Reversal Settings')
// Visuals
show_plots = input.bool(true, "Show Signals", group="Visuals")
show_reversal_signals = input.bool(true, "Show Reversals", group="Visuals")
//show_out_line = input.bool(true, 'EMA Line', group = 'Lines')
show_close_line = input.bool(true, 'Close Line', group = 'Lines')
show_hl_lines = input.bool(false, 'High/Low Lines', group = 'Lines')
show_fill = input.bool(true, 'Fill Colors', group = 'Colors')
// Colors
color_positive = input.color(#26A69A, 'Bullish', group = 'Colors')
color_negative = input.color(#EF5350, 'Bearish', group = 'Colors')
color_neutral = input.color(#808080, 'Neutral', group = 'Colors')
color_rev_bull = input.color(#2196F3, "Reversal Bull", group="Colors")
color_rev_bear = input.color(#FF9800, "Reversal Bear", group="Colors")
// ---------------------
// Core Calculations
// ---------------------
// MA Calculation Function
f(x) =>
switch ma_type
'ALMA' => ta.alma(x, ma_period, alma_offset, alma_sigma)
'HMA' => ta.hma(x, ma_period)
'SMA' => ta.sma(x, ma_period)
'SWMA' => ta.swma(x)
'VWMA' => ta.vwma(x, ma_period)
'WMA' => ta.wma(x, ma_period)
'ZLEMA' => ta.ema(x + (x - x[math.floor((ma_period - 1) / 2)]), ma_period)
=> ta.ema(x, ma_period)
// Security Functions
getTicker() => use_heikinashi ? ticker.heikinashi(syminfo.tickerid) : syminfo.tickerid
getTimeframe() => trend_tf == "" ? timeframe.period : trend_tf
// Heikin-Ashi Calculation
var float ha_open = na
var float ha_close = na
ha_close := (open + high + low + close) / 4
ha_open := na(ha_open[1]) ? (open + close) / 2 : (ha_open[1] + ha_close[1]) / 2
actual_close = use_heikinashi ? ha_close : close
actual_open = use_heikinashi ? ha_open : open
// MA Values
ma_open = f(request.security(getTicker(), getTimeframe(), open))
ma_close = f(request.security(getTicker(), getTimeframe(), close))
ma_high = f(request.security(getTicker(), getTimeframe(), high))
ma_low = f(request.security(getTicker(), getTimeframe(), low))
// Trend Calculation
trend = 100 * (ma_close - ma_open) / (ma_high - ma_low)
color_trend = trend > 0 ? color_positive : color_negative
// EMA Calculation
//out = ta.ema(src, len)
// ---------------------
// Signal Logic
// ---------------------
// Original Signals
longCondition = ta.crossover(ma_close, ma_open) //and close[1] > out
shortCondition = ta.crossunder(ma_close, ma_open) //and close[1] < out
// Track recent signals
var int lastLongBar = na
var int lastShortBar = na
if longCondition
lastLongBar := bar_index
if shortCondition
lastShortBar := bar_index
// Reversal Signals
longReversal = false
shortReversal = false
if not na(lastShortBar)
bars_since_short = bar_index - lastShortBar
if bars_since_short <= rev_bars_window and bars_since_short > 0
longReversal := close > ma_close and actual_close > actual_open
if not na(lastLongBar)
bars_since_long = bar_index - lastLongBar
if bars_since_long <= rev_bars_window and bars_since_long > 0
shortReversal := close < ma_close and actual_close < actual_open
// ---------------------
// Plotting
// ---------------------
// Main Trend Plots
plot_open = plot(ma_open, 'Open', color=na)
plot_close = plot(show_close_line ? ma_close : na, 'Close', color=color_trend, linewidth=2)
plot_high = plot(show_hl_lines ? ma_high : na, 'High', color=color_neutral)
plot_low = plot(show_hl_lines ? ma_low : na, 'Low', color=color_neutral)
// Wick Plots
plot_highest = plot(math.max(ma_high, ma_low), 'High Wick', color=na)
plot_lowest = plot(math.min(ma_high, ma_low), 'Low Wick', color=na)
// Fill Plots
fill_color_oc = show_fill ? color.new(color_trend, 50) : na
fill_color_high = show_fill ? color.new(color_neutral, 87) : na
fill_color_low = show_fill ? color.new(color_neutral, 87) : na
fill(plot_open, plot_close, fill_color_oc, "OC Fill")
fill(plot_high, plot_highest, fill_color_high, "High Fill")
fill(plot_lowest, plot_low, fill_color_low, "Low Fill")
// EMA Line
//plot(show_out_line ? out : na, "EMA", color=#F606CE, linewidth=2)
// Signal Markers
plotshape(show_plots and longCondition, "Long", shape.triangleup, location.belowbar, color.green, size=size.small)
plotshape(show_plots and shortCondition, "Short", shape.triangledown, location.abovebar, color.red, size=size.small)
plotshape(show_plots and show_reversal_signals and longReversal, "Rev Long", shape.triangleup, location.belowbar, color_rev_bull, size=size.small)
plotshape(show_plots and show_reversal_signals and shortReversal, "Rev Short", shape.triangledown, location.abovebar, color_rev_bear, size=size.small)