OPEN-SOURCE SCRIPT
Sniper Strategy [Short Only]

//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
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
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.