PROTECTED SOURCE SCRIPT

BR-DowTheoryS

35
//version=6
indicator("道氏趋势指标 - 中长线持仓版", overlay=true, shorttitle="Dow_Position", precision=2)

// 核心参数(已为中长线持仓优化)
trend_length = input.int(12, title="趋势识别周期", minval=8)
vol_length = input.int(20, title="成交量周期", minval=10)
stop_loss = input.float(800.0, title="止损点数", tooltip="BTC填800,黄金填15,ETH填600")
tp_ratio = input.float(3.0, title="止盈倍数")
vol_threshold = input.float(1.1, title="放量阈值")
ma_length = input.int(50, title="趋势均线周期", tooltip="长周期用50,贴合道氏主要趋势")

// 道氏趋势均线 + 趋势判定
trend_ma = ta.ema(close, ma_length)
ma_uptrend = trend_ma > trend_ma[1] and trend_ma[1] > trend_ma[2]
ma_downtrend = trend_ma < trend_ma[1] and trend_ma[1] < trend_ma[2]
ma_sideways = not ma_uptrend and not ma_downtrend

// 趋势+放量信号
uptrend = high > ta.highest(high, trend_length)[1]
downtrend = low < ta.lowest(low, trend_length)[1]
vol_ma = ta.sma(volume, vol_length)
vol_bull = volume >= vol_ma * vol_threshold and close > open
vol_bear = volume >= vol_ma * vol_threshold and close < open

// 止盈止损重置逻辑
var string last_signal = na
var float active_entry = na
var float active_sl = na
var float active_tp = na

bool is_stopped_out = false
bool is_tp_hit = false
if not na(active_entry)
if last_signal == "long"
is_stopped_out := low <= active_sl
is_tp_hit := high >= active_tp
else if last_signal == "short"
is_stopped_out := high >= active_sl
is_tp_hit := low <= active_tp

if is_stopped_out or is_tp_hit
last_signal := na
active_entry := na
active_sl := na
active_tp := na

// 道氏趋势过滤:只做单方向
long_signal = ma_uptrend and uptrend and vol_bull and last_signal != "long"
short_signal = ma_downtrend and downtrend and vol_bear and last_signal != "short"

if long_signal
last_signal := "long"
active_entry := close
active_sl := active_entry - stop_loss
active_tp := active_entry + stop_loss * tp_ratio

if short_signal
last_signal := "short"
active_entry := close
active_sl := active_entry + stop_loss
active_tp := active_entry - stop_loss * tp_ratio

// 绘制元素
plot(trend_ma, title="趋势均线", color=ma_uptrend ? color.green : ma_downtrend ? color.red : color.gray, linewidth=2)
plotshape(long_signal, title="做多", location=location.belowbar, color=color.green, style=shape.labelup, text="L", textcolor=color.white, size=size.small)
plotshape(short_signal, title="做空", location=location.abovebar, color=color.red, style=shape.labeldown, text="S", textcolor=color.white, size=size.small)
plot(active_sl, title="止损", color=color.red, style=plot.style_linebr, linewidth=1)
plot(active_tp, title="止盈", color=color.blue, style=plot.style_linebr, linewidth=1)
bgcolor(ma_uptrend ? color.new(color.green, 90) : ma_downtrend ? color.new(color.red, 90) : color.new(color.gray, 90))

Aviso legal

As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.