OPEN-SOURCE SCRIPT

Sniper Strategy [Short Only]

116
//version=6
strategy("Sniper Strategy [Short Only]", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, currency=currency.NONE)

// --- 参数设置 ---
atrPeriod = input.int(10, "ATR")
factor = input.float(3.0, "Factor")
sl_buffer = input.float(0.0, "SL Buffer", tooltip="在Supertrend上方增加一点缓冲")

// --- 核心计算 ---
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
// direction > 0 代表空头(红), < 0 代表多头(绿)
short_signal = ta.change(direction) > 0
exit_signal = ta.change(direction) < 0

// --- 变量记录 ---
var float entry_price = na
var float sl_price = na
var float tp_price = na
var bool tp_hit = false

// --- 1. 进场逻辑 (Entry) ---
if short_signal
// 做空止损放在 Supertrend 价格上方
float initial_sl = supertrend + sl_buffer

entry_price := close
sl_price := initial_sl

// 计算风险值 (价格差)
float risk_val = sl_price - entry_price
// 1R 止盈目标 (做空是向下减)
tp_price := entry_price - risk_val
tp_hit := false

strategy.entry("S", strategy.short)

// 标签:显示具体止损价格 + 亏损百分比
float risk_pct = (risk_val / entry_price) * 100
label_txt = "SL: " + str.tostring(sl_price, "#.##") + "\nRisk: " + str.tostring(risk_pct, "#.2") + "%"

// 做空标签显示在K线上方
label.new(bar_index, high, text=label_txt, style=label.style_label_down, color=color.new(color.red, 40), textcolor=color.white, size=size.small)

// --- 2. 持仓管理 (Management) ---
if strategy.position_size < 0
// 1R 目标达成:平仓 50%
strategy.exit("1R", "S", stop=sl_price, qty_percent=50, limit=tp_price)
// 剩余仓位止损
strategy.exit("End", "S", stop=sl_price)

// 1R 视觉提示 (只提示一次)
if not tp_hit and low <= tp_price
tp_hit := true
label.new(bar_index, low, text="1R 💰", style=label.style_label_up, color=color.new(color.yellow, 20), textcolor=color.black, size=size.small)

// --- 3. 趋势反转离场 (Exit) ---
if exit_signal
strategy.close_all()
entry_price := na
sl_price := na
tp_price := na

Pernyataan Penyangkalan

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.