本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
strategy("XAUUSD 3M UT BOT PRO FIXED", overlay=true, pyramiding=0, initial_capital=10000)
// INPUTS
emaFastLen = input.int(50, title="EMA Fast")
emaSlowLen = input.int(200, title="EMA Slow")
adxLen = input.int(14, title="ADX Length")
adxMin = input.int(22, title="ADX Minimum")
atrPeriod = input.int(10, title="ATR Period")
keyValue = input.float(1.0, title="UT Bot Key")
stATR = input.int(10, title="SuperTrend ATR")
stFactor = input.float(2.5, title="SuperTrend Factor")
cooldownBars = input.int(5, title="Cooldown Bars")
// EMA
ema50 = ta.ema(close, emaFastLen)
ema200 = ta.ema(close, emaSlowLen)
bullTrend = ema50 > ema200 and close > ema200
bearTrend = ema50 < ema200 and close < ema200
// 15M FILTER
htfEMA50 = request.security(syminfo.tickerid, "15", ta.ema(close, emaFastLen))
htfEMA200 = request.security(syminfo.tickerid, "15", ta.ema(close, emaSlowLen))
htfBull = htfEMA50 > htfEMA200
htfBear = htfEMA50 < htfEMA200
// ADX
[diplus, diminus, adx] = ta.dmi(adxLen, adxLen)
adxStrong = adx > adxMin
// SUPERTREND
[supertrend, direction] = ta.supertrend(stFactor, stATR)
superBull = direction < 0
superBear = direction > 0
// VOLUME FILTER
volumeFilter = volume > ta.sma(volume, 20)
// SESSION FILTER
londonSession = not na(time(timeframe.period, "0300-1200"))
newYorkSession = not na(time(timeframe.period, "0800-1700"))
sessionAllowed = londonSession or newYorkSession
// UT BOT
xATR = ta.atr(atrPeriod)
nLoss = keyValue * xATR
src = close
var float xATRTrailingStop = na
if na(xATRTrailingStop)
xATRTrailingStop := src - nLoss
if src > xATRTrailingStop and src[1] > xATRTrailingStop
xATRTrailingStop := math.max(xATRTrailingStop, src - nLoss)
else if src < xATRTrailingStop and src[1] < xATRTrailingStop
xATRTrailingStop := math.min(xATRTrailingStop, src + nLoss)
else
xATRTrailingStop := src > xATRTrailingStop ? src - nLoss : src + nLoss
buySignal = ta.crossover(src, xATRTrailingStop)
sellSignal = ta.crossunder(src, xATRTrailingStop)
// COOLDOWN
var int lastTradeBar = na
canTrade = na(lastTradeBar) or (bar_index - lastTradeBar > cooldownBars)
// FINAL CONDITIONS
buyCondition = buySignal and bullTrend and htfBull and adxStrong and superBull and volumeFilter and sessionAllowed and canTrade
sellCondition = sellSignal and bearTrend and htfBear and adxStrong and superBear and volumeFilter and sessionAllowed and canTrade
// BUY ENTRY
if buyCondition and strategy.position_size <= 0
strategy.entry("BUY TP1", strategy.long, qty=0.02)
strategy.entry("BUY TP2", strategy.long, qty=0.02)
strategy.entry("BUY TP3", strategy.long, qty=0.02)
strategy.entry("BUY TP4", strategy.long, qty=0.02)
strategy.entry("BUY TP5", strategy.long, qty=0.02)
lastTradeBar := bar_index
// SELL ENTRY
if sellCondition and strategy.position_size >= 0
strategy.entry("SELL TP1", strategy.short, qty=0.02)
strategy.entry("SELL TP2", strategy.short, qty=0.02)
strategy.entry("SELL TP3", strategy.short, qty=0.02)
strategy.entry("SELL TP4", strategy.short, qty=0.02)
strategy.entry("SELL TP5", strategy.short, qty=0.02)
lastTradeBar := bar_index
// TP LEVELS
buyTP1 = strategy.position_avg_price + 2
buyTP2 = strategy.position_avg_price + 4
buyTP3 = strategy.position_avg_price + 6
buyTP4 = strategy.position_avg_price + 8
buyTP5 = strategy.position_avg_price + 10
sellTP1 = strategy.position_avg_price - 2
sellTP2 = strategy.position_avg_price - 4
sellTP3 = strategy.position_avg_price - 6
sellTP4 = strategy.position_avg_price - 8
sellTP5 = strategy.position_avg_price - 10
// INITIAL STOP LOSS
buyInitialSL = strategy.position_avg_price - 5
sellInitialSL = strategy.position_avg_price + 5
// TP TRACKERS
var bool buyTP1Hit = false
var bool buyTP2Hit = false
var bool buyTP3Hit = false
var bool buyTP4Hit = false
var bool sellTP1Hit = false
var bool sellTP2Hit = false
var bool sellTP3Hit = false
var bool sellTP4Hit = false
// RESET
if strategy.position_size == 0
buyTP1Hit := false
buyTP2Hit := false
buyTP3Hit := false
buyTP4Hit := false
sellTP1Hit := false
sellTP2Hit := false
sellTP3Hit := false
sellTP4Hit := false
// BUY TP DETECTION
if strategy.position_size > 0
if high >= buyTP1
buyTP1Hit := true
if high >= buyTP2
buyTP2Hit := true
if high >= buyTP3
buyTP3Hit := true
if high >= buyTP4
buyTP4Hit := true
// SELL TP DETECTION
if strategy.position_size < 0
if low <= sellTP1
sellTP1Hit := true
if low <= sellTP2
sellTP2Hit := true
if low <= sellTP3
sellTP3Hit := true
if low <= sellTP4
sellTP4Hit := true
// DYNAMIC SL
buySLDynamic = buyTP4Hit ? buyTP3 : buyTP3Hit ? buyTP2 : buyTP2Hit ? buyTP1 : buyTP1Hit ? strategy.position_avg_price : buyInitialSL
sellSLDynamic = sellTP4Hit ? sellTP3 : sellTP3Hit ? sellTP2 : sellTP2Hit ? sellTP1 : sellTP1Hit ? strategy.position_avg_price : sellInitialSL
// BUY EXITS
strategy.exit("EXIT BUY TP1", from_entry="BUY TP1", limit=buyTP1, stop=buySLDynamic)
strategy.exit("EXIT BUY TP2", from_entry="BUY TP2", limit=buyTP2, stop=buySLDynamic)
strategy.exit("EXIT BUY TP3", from_entry="BUY TP3", limit=buyTP3, stop=buySLDynamic)
strategy.exit("EXIT BUY TP4", from_entry="BUY TP4", limit=buyTP4, stop=buySLDynamic)
strategy.exit("EXIT BUY TP5", from_entry="BUY TP5", limit=buyTP5, stop=buySLDynamic)
// SELL EXITS
strategy.exit("EXIT SELL TP1", from_entry="SELL TP1", limit=sellTP1, stop=sellSLDynamic)
strategy.exit("EXIT SELL TP2", from_entry="SELL TP2", limit=sellTP2, stop=sellSLDynamic)
strategy.exit("EXIT SELL TP3", from_entry="SELL TP3", limit=sellTP3, stop=sellSLDynamic)
strategy.exit("EXIT SELL TP4", from_entry="SELL TP4", limit=sellTP4, stop=sellSLDynamic)
strategy.exit("EXIT SELL TP5", from_entry="SELL TP5", limit=sellTP5, stop=sellSLDynamic)
// PLOTS
plot(ema50, color=color.orange, linewidth=2)
plot(ema200, color=color.blue, linewidth=2)
plotshape(buyCondition, style=shape.labelup, location=location.belowbar, color=color.green, text="BUY")
plotshape(sellCondition, style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL")
bgcolor(buyCondition ? color.new(color.green, 90) : na)
bgcolor(sellCondition ? color.new(color.red, 90) : na)
// ALERTS
alertcondition(buyCondition, title="BUY ALERT", message="BUY SIGNAL")
alertcondition(sellCondition, title="SELL ALERT", message="SELL SIGNAL")