本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// WRP ATR Bot is an independent, UT-style ATR trailing-stop strategy.
// It is not the original UT Bot and does not reproduce any closed-source code.
// Backtest/paper-use strategy only; it does not route orders to a broker.
// Entry: confirmed close crosses above the ATR trailing stop. Exit: opposite cross, ATR stop, or risk/reward target.
// Position size risks a configurable share of equity and is capped by maximum exposure.
strategy("WRP ATR Bot - Risk Managed", overlay=true, initial_capital=10000, currency=currency.USD, pyramiding=0, default_qty_type=strategy.fixed, commission_type=strategy.commission.percent, commission_value=0.05, slippage=1, process_orders_on_close=false)
keyValue = input.float(2.0, "UT-style key value", minval=0.1, step=0.1)
atrPeriod = input.int(14, "ATR period", minval=1)
stopAtrMult = input.float(2.0, "Stop distance (ATR)", minval=0.1, step=0.1)
rewardRisk = input.float(1.65, "Target risk/reward", minval=0.1, step=0.05)
riskPercent = input.float(1.0, "Equity risk per trade (%)", minval=0.1, maxval=5, step=0.1)
maxExposure = input.float(100.0, "Max exposure (% equity)", minval=1, maxval=100, step=1)
src = close
atr = ta.atr(atrPeriod)
loss = keyValue * atr
var float trail = na
if not na(atr)
if na(trail[1])
trail := src - loss
else
prevTrail = trail[1]
trail := src > prevTrail and src[1] > prevTrail ? math.max(prevTrail, src - loss) : src < prevTrail and src[1] < prevTrail ? math.min(prevTrail, src + loss) : src > prevTrail ? src - loss : src + loss
buySignal = not na(trail) and ta.crossover(src, trail)
sellSignal = not na(trail) and ta.crossunder(src, trail)
stopDistance = atr * stopAtrMult
riskCash = strategy.equity * riskPercent / 100.0
riskQty = stopDistance > 0 ? riskCash / stopDistance : 0.0
capQty = close > 0 ? strategy.equity * maxExposure / 100.0 / close : 0.0
orderQty = math.floor(math.min(riskQty, capQty))
var float entryRiskDistance = na
if barstate.isconfirmed and buySignal and strategy.position_size == 0 and orderQty >= 1
entryRiskDistance := stopDistance
strategy.entry("Long", strategy.long, qty=orderQty, alert_message="WRP ATR Bot: LONG {{ticker}} qty={{strategy.order.contracts}} price={{strategy.order.price}}")
if strategy.position_size > 0 and not na(entryRiskDistance)
stopPrice = math.round_to_mintick(strategy.position_avg_price - entryRiskDistance)
targetPrice = math.round_to_mintick(strategy.position_avg_price + entryRiskDistance * rewardRisk)
strategy.exit("ATR stop / RR target", from_entry="Long", stop=stopPrice, limit=targetPrice, alert_message="WRP ATR Bot: EXIT {{ticker}} qty={{strategy.order.contracts}} price={{strategy.order.price}}")
if barstate.isconfirmed and strategy.position_size > 0 and sellSignal
strategy.close("Long", comment="ATR trail cross", alert_message="WRP ATR Bot: SELL {{ticker}} qty={{strategy.order.contracts}} price={{strategy.order.price}}")
plot(trail, "ATR trailing stop", color=close >= trail ? color.lime : color.red, linewidth=2)
plotshape(buySignal, title="Long entry", text="BUY", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
plotshape(sellSignal, title="Exit signal", text="SELL", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny)