本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("UT Bot Alerts – Time Filtered with Exits", overlay=true)
// Inputs
a = input.int(2, title="Key Value. 'This changes the sensitivity'")
c = input.int(7, title="ATR Period")
h = input.bool(false, title="Signals from Heikin Ashi Candles")
lrPeriod = input.int(20, title="Linear Regression Period")
exitPct = input.float(0.6, title="Exit Signal Deviation (%)", step=0.1)
enableExitSignals= input.bool(true, title="Enable Exit Signals")
exitDelay = input.int(1, title="Exit Signal Delay (bars)", minval=1)
// Inputs for time filtering
enableTimeRangeDisable = input.bool(false, title="Disable signals in defined time range")
disableSession = input.session("2200-0500", title="Disable Signals Session")
enableCooldown = input.bool(false, title="Enable cooldown period (minutes)")
cooldownMinutes = input.int(5, title="Cooldown Period (minutes)", minval=1)
// ATR and loss calculation
xATR = ta.atr(c)
nLoss = a * xATR
// Source (optional Heikin Ashi)
src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=barmerge.lookahead_off) : close
// Function to calculate the ATR trailing stop
f_calcTrailingStop(prev, src, nLoss) =>
if src > nz(prev, 0) and src[1] > nz(prev, 0)
math.max(nz(prev, 0), src - nLoss)
else if src < nz(prev, 0) and src[1] < nz(prev, 0)
math.min(nz(prev, 0), src + nLoss)
else
if src > nz(prev, 0)
src - nLoss
else
src + nLoss
// ATR trailing stop calculation
var float xATRTrailingStop = 0.0
xATRTrailingStop := f_calcTrailingStop(xATRTrailingStop[1], src, nLoss)
// Calculation of EMA
ema_val = ta.ema(src, 1)
// Strict buying and selling conditions
strong_buy = ta.crossover(ema_val, xATRTrailingStop) and src > xATRTrailingStop and ema_val > ema_val[1]
strong_sell = ta.crossover(xATRTrailingStop, ema_val) and src < xATRTrailingStop and ema_val < ema_val[1]
// Additional buffer zone for signals
buy_buffer = strong_buy and src > xATRTrailingStop + 0.2 * xATR
sell_buffer = strong_sell and src < xATRTrailingStop - 0.2 * xATR
// Persistent signal state: 1 = Long, -1 = Short, 0 = Neutral
var int utSignal = 0
utSignal := buy_buffer ? 1 : sell_buffer ? -1 : utSignal
// UT Bot Alert color: green for Long, red for Short, otherwise blue
utColor = utSignal == 1 ? color.green : utSignal == -1 ? color.red : color.blue
// Calculation of the LinReg midline
lr_line = ta.linreg(src, lrPeriod, 0)
// Additional exit conditions (without delay):
// Exit Short: current sell signal and price > (1 + exitPct/100) * LinReg midline
// Exit Long: current buy signal and price < (1 - exitPct/100) * LinReg midline
exitShortCond = utSignal == -1 and src > lr_line * (1 + exitPct / 100)
exitLongCond = utSignal == 1 and src < lr_line * (1 - exitPct / 100)
// Implement delay: counter increments as long as the respective condition is met
var int exitShortCounter = 0
var int exitLongCounter = 0
if exitShortCond
exitShortCounter += 1
else
exitShortCounter := 0
if exitLongCond
exitLongCounter += 1
else
exitLongCounter := 0
// Exit signals: If enableExitSignals is disabled, they are not triggered,
// otherwise only when the respective condition is met for at least 'exitDelay' bars.
exit_long = enableExitSignals ? (exitLongCounter >= exitDelay) : false
exit_short = enableExitSignals ? (exitShortCounter >= exitDelay) : false
// Exit signals only in the bar where they become true for the first time
exit_long_once = exit_long and not exit_long[1]
exit_short_once = exit_short and not exit_short[1]
// Only one exit signal per trade: On trade change, the "exit fired" flags are reset.
var bool exitLongFired = false
var bool exitShortFired = false
if utSignal != utSignal[1]
exitLongFired := false
exitShortFired := false
exit_long_once_final = exit_long_once and not exitLongFired
exit_short_once_final = exit_short_once and not exitShortFired
if exit_long_once_final
exitLongFired := true
if exit_short_once_final
exitShortFired := true
// Time filtering
// 1. Disable signals in a defined session
inDisabledSession = enableTimeRangeDisable and not na(time(timeframe.period, disableSession))
// 2. Cooldown period: No signals for N minutes after the last signal
var int lastSignalTime = 0
cooldownActive = enableCooldown and (time - lastSignalTime < cooldownMinutes * 60000)
disableSignals = inDisabledSession or cooldownActive
// Filtered signals: Only if none of the above restrictions are active
filteredBuy = buy_buffer and not disableSignals
filteredSell = sell_buffer and not disableSignals
filteredExitLong = exit_long_once_final and not disableSignals
filteredExitShort = exit_short_once_final and not disableSignals
// Update the time of the last signal when one of the filtered signals is triggered
if filteredBuy or filteredSell or filteredExitLong or filteredExitShort
lastSignalTime := time
// Display signals on chart
// Entry signals
plotshape(filteredBuy, title="Buy", text="Buy", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
plotshape(filteredSell, title="Sell", text="Sell", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny)
// Exit signals (only once per trade)
// Exit Long signal text in white
plotshape(filteredExitLong, title="Exit Long", text="Exit Long", style=shape.triangledown, location=location.abovebar, color=color.yellow, textcolor=color.white, size=size.tiny)
plotshape(filteredExitShort, title="Exit Short", text="Exit Short", style=shape.triangleup, location=location.belowbar, color=color.purple, textcolor=color.white, size=size.tiny)
// Horizontal breakout level lines (as solid lines)
// Upper line (Exit Short Level) in turquoise (color.aqua), lower (Exit Long Level) in yellow.
plot(lr_line * (1 + exitPct / 100), title="Exit Short Level", color=color.aqua, linewidth=1)
plot(lr_line * (1 - exitPct / 100), title="Exit Long Level", color=color.yellow, linewidth=1)
// Optional bar coloring according to UT Bot signal state
barcolor(utSignal == 1 ? color.new(color.green, 0) : utSignal == -1 ? color.new(color.red, 0) : na)
// ATR trailing stop and LinReg midline on the chart
plot(xATRTrailingStop, title="ATR Trailing Stop", color=utColor, linewidth=2)
plot(lr_line, title="LinReg Midline", color=color.gray, linewidth=1)
// Alerts
alertcondition(filteredBuy, title="UT Long", message="Confirmed UT Long Signal")
alertcondition(filteredSell, title="UT Short", message="Confirmed UT Short Signal")
alertcondition(filteredExitLong, title="Exit Long", message="Exit Long Signal")
alertcondition(filteredExitShort, title="Exit Short", message="Exit Short Signal")