本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
strategy("EMA 9 + VWAP Strategy with ATR Trailing Stop - WIN THE TRADE", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, process_orders_on_close=true, calc_on_every_tick=true)
// ——— Inputs
atrLength = input.int(14, title="ATR Length")
atrMult = input.float(2, title="ATR Multiplier for Trailing Stop")
// ——— Indicators
ema9 = ta.ema(close, 9)
vwap = ta.vwap(close)
atr = ta.atr(atrLength)
// ——— Entry Conditions
longCondition = ta.crossover(ema9, vwap)
shortCondition = ta.crossunder(ema9, vwap)
// ——— Entries
if longCondition
strategy.entry("Long", strategy.long)
alert('{"symbol":"{{ticker}}","date":"{{timenow}}","data":"buy","quantity":1,"price":"{{close}}","tp":0,"sl":0,"dollar_sl":0,"percentage_sl":0,"trail":1,"trail_stop":50,"trail_trigger":0,"trail_freq":10,"token":"your_token","account_id":"your_account_id"}', alert.freq_once_per_bar_close)
if shortCondition
strategy.entry("Short", strategy.short)
alert('{"symbol":"{{ticker}}","date":"{{timenow}}","data":"sell","quantity":1,"price":"{{close}}","tp":0,"sl":0,"dollar_sl":0,"percentage_sl":0,"trail":1,"trail_stop":50,"trail_trigger":0,"trail_freq":10,"token":"your_token","account_id":"your_account_id"}', alert.freq_once_per_bar_close)
// ——— Trailing Stop Exits
strategy.exit("Long Exit", from_entry="Long", trail_points=atr * atrMult, trail_offset=atr * atrMult)
strategy.exit("Short Exit", from_entry="Short", trail_points=atr * atrMult, trail_offset=atr * atrMult)
// ——— Trailing Stop Lines
var float longStopLine = na
var float shortStopLine = na
if strategy.position_size > 0
longStopLine := na(longStopLine) ? close - atr * atrMult : math.max(longStopLine, close - atr * atrMult)
else
longStopLine := na
if strategy.position_size < 0
shortStopLine := na(shortStopLine) ? close + atr * atrMult : math.min(shortStopLine, close + atr * atrMult)
else
shortStopLine := na
// ——— Plotting
plot(ema9, color=color.orange, title="EMA 9", linewidth=2)
plot(vwap, color=color.blue, title="VWAP", linewidth=2)
plot(longStopLine, color=color.new(color.green, 20), title="Long Trail Stop", style=plot.style_linebr, linewidth=1)
plot(shortStopLine, color=color.new(color.red, 20), title="Short Trail Stop", style=plot.style_linebr, linewidth=1)
// ——— Signals
plotshape(longCondition, title="Buy Signal", style=shape.labelup, location=location.belowbar, color=color.green, text="BUY", textcolor=color.white, size=size.small)
plotshape(shortCondition, title="Sell Signal", style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL", textcolor=color.white, size=size.small)