本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator(title="UT Bot + LinReg Candles (Dual Sensitivity)", overlay=true)
// ====================== UT Bot Inputs =======================
a_buy = input.float(1, title="Buy Sensitivity (Multiplier)")
c_buy = input.int(10, title="Buy ATR Period")
a_sell = input.float(1, title="Sell Sensitivity (Multiplier)")
c_sell = input.int(10, title="Sell ATR Period")
// ====================== LinReg Inputs =======================
signal_length = input.int(11, title="Signal Smoothing", minval=1, maxval=200, group="LinReg")
sma_signal = input.bool(true, title="Simple MA (Signal Line)", group="LinReg")
lin_reg = input.bool(true, title="Use LinReg", group="LinReg")
linreg_length = input.int(11, title="Linear Regression Length", minval=1, maxval=200, group="LinReg")
// ===================== UT Bot Buy Calculation ===============
src_buy = close
atr_buy = ta.atr(c_buy)
nLoss_buy = a_buy * atr_buy
trail_buy = 0.0
trail_buy := na(trail_buy[1]) ? src_buy - nLoss_buy :
src_buy > trail_buy[1] and src_buy[1] > trail_buy[1] ? math.max(trail_buy[1], src_buy - nLoss_buy) :
src_buy < trail_buy[1] and src_buy[1] < trail_buy[1] ? math.min(trail_buy[1], src_buy + nLoss_buy) :
src_buy > trail_buy[1] ? src_buy - nLoss_buy : src_buy + nLoss_buy
ema_buy = ta.ema(src_buy, 1)
above_buy = ta.crossover(ema_buy, trail_buy)
buy_signal = src_buy > trail_buy and above_buy
// ===================== UT Bot Sell Calculation ==============
src_sell = close
atr_sell = ta.atr(c_sell)
nLoss_sell = a_sell * atr_sell
trail_sell = 0.0
trail_sell := na(trail_sell[1]) ? src_sell + nLoss_sell :
src_sell > trail_sell[1] and src_sell[1] > trail_sell[1] ? math.max(trail_sell[1], src_sell - nLoss_sell) :
src_sell < trail_sell[1] and src_sell[1] < trail_sell[1] ? math.min(trail_sell[1], src_sell + nLoss_sell) :
src_sell > trail_sell[1] ? src_sell - nLoss_sell : src_sell + nLoss_sell
ema_sell = ta.ema(src_sell, 1)
below_sell = ta.crossover(trail_sell, ema_sell)
sell_signal = src_sell < trail_sell and below_sell
// =================== Plot UT Bot Signals ====================
plotshape(buy_signal, title="UT Buy", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny, text="Buy")
plotshape(sell_signal, title="UT Sell", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny, text="Sell")
barcolor(buy_signal ? color.new(color.green, 75) : na)
barcolor(sell_signal ? color.new(color.red, 75) : na)
alertcondition(buy_signal, "UT Long", "UT Long")
alertcondition(sell_signal, "UT Short", "UT Short")
// ===================== LinReg Calculation ===================
_bopen = lin_reg ? ta.linreg(open, linreg_length, 0) : open
_bhigh = lin_reg ? ta.linreg(high, linreg_length, 0) : high
_blow = lin_reg ? ta.linreg(low, linreg_length, 0) : low
_bclose = lin_reg ? ta.linreg(close, linreg_length, 0) : close
r = _bopen < _bclose
signal = sma_signal ? ta.sma(_bclose, signal_length) : ta.ema(_bclose, signal_length)
plotcandle(r ? _bopen : na, r ? _bhigh : na, r ? _blow: na, r ? _bclose : na, title="LinReg Green", color=color.green, wickcolor=color.green, bordercolor=color.green)
plotcandle(r ? na : _bopen, r ? na : _bhigh, r ? na : _blow, r ? na : _bclose, title="LinReg Red", color=color.red, wickcolor=color.red, bordercolor=color.red)
plot(signal, color=color.white, linewidth=2, title="LinReg Signal")