OPEN-SOURCE SCRIPT

均线变色K线系统 with 转折箭头

129
//version=6
indicator("均线变色K线系统 with 转折箭头", overlay=true, max_lines_count=500, max_labels_count=200)

// 输入参数
ma_length = input.int(20, title="均线周期", minval=1)
atr_filter = input.bool(true, title="启用ATR波动过滤")
atr_length = input.int(14, title="ATR周期", minval=1)
atr_multiplier = input.float(1.5, title="ATR波动阈值", minval=0.1, step=0.1)
show_arrows = input.bool(true, title="显示转折箭头")
candle_coloring = input.bool(true, title="启用K线变色")

// 计算均线和ATR
ma = ta.sma(close, ma_length)
atr_value = ta.atr(atr_length)
avg_atr = ta.sma(atr_value, atr_length)

// 判断均线方向和趋势转折点
ma_rising = ta.rising(ma, 1)
ma_falling = ta.falling(ma, 1)

// 使用更严格的趋势转折检测(避免repainting)
ma_rising_prev = ta.rising(ma, 2)
ma_falling_prev = ta.falling(ma, 2)

// 检测趋势转折点(确保只在K线收盘确认时检测)
trend_change_up = ma_rising and not ma_rising_prev and (not atr_filter or atr_value >= avg_atr * atr_multiplier)
trend_change_down = ma_falling and not ma_falling_prev and (not atr_filter or atr_value >= avg_atr * atr_multiplier)

// 设置颜色
ma_color = ma_rising ? color.rgb(255, 0, 0) : color.rgb(0, 0, 255) // 红/蓝
candle_color = ma_rising ? color.rgb(255, 0, 0) : color.rgb(0, 0, 255)
border_color = ma_rising ? color.rgb(255, 0, 0) : color.rgb(0, 0, 255)
wick_color = ma_rising ? color.rgb(255, 0, 0) : color.rgb(0, 0, 255)

// 绘制彩色均线
plot(ma, color=ma_color, linewidth=2, title="变色均线")

// 使用plotcandle绘制彩色K线
plotcandle(candle_coloring ? open : na,
candle_coloring ? high : na,
candle_coloring ? low : na,
candle_coloring ? close : na,
title="变色K线",
color = candle_color,
wickcolor = wick_color,
bordercolor = border_color,
editable = true)

// 绘制趋势转折箭头(只在K线确认时显示)
if show_arrows and barstate.isconfirmed
if trend_change_up
label.new(bar_index, low * 0.998, "▲",
color=color.rgb(0, 255, 0),
textcolor=color.white,
style=label.style_label_up,
yloc=yloc.price,
size=size.normal)
else if trend_change_down
label.new(bar_index, high * 1.002, "▼",
color=color.rgb(255, 0, 0),
textcolor=color.white,
style=label.style_label_down,
yloc=yloc.price,
size=size.normal)

// 背景色轻微提示(可选)
bgcolor(ma_rising ? color.new(color.red, 95) : color.new(color.blue, 95), title="趋势背景提示")

Exención de responsabilidad

La información y las publicaciones no pretenden ser, ni constituyen, asesoramiento o recomendaciones financieras, de inversión, de trading o de otro tipo proporcionadas o respaldadas por TradingView. Más información en Condiciones de uso.