本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
strategy("UT Bot Options Seller (5-Min Spike Protected)", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// ==========================================
// 1. INPUTS
// ==========================================
// UT Bot Settings
utKey = input.float(2.0, title="UT Bot Key Value")
utAtrPeriod = input.int(10, title="UT Bot ATR Period")
// RSI & ADX Settings
rsiPeriod = input.int(14, title="RSI Period")
rsiPeMin = input.int(3, title="PE Sell Min RSI")
rsiPeMax = input.int(60, title="PE Sell Max RSI")
rsiCeMin = input.int(40, title="CE Sell Min RSI")
rsiCeMax = input.int(100, title="CE Sell Max RSI")
adxPeriod = input.int(14, title="ADX Period")
adxThreshold = input.float(20.0, title="Minimum ADX")
// --- SPIKE FILTER SETTINGS ---
spikeMultiplier = input.float(2.0, title="Max Candle Spike Multiplier", tooltip="If the candle is this many times larger than the average candle, block the trade.")
// ==========================================
// 2. INDICATOR CALCULATIONS
// ==========================================
// UT Bot
atr = ta.atr(utAtrPeriod)
nLoss = utKey * atr
// FIXED: Re-structured standard if/else logic to prevent line continuation errors
var float xATRTrailingStop = 0.0
float prevStop = nz(xATRTrailingStop[1], 0.0)
if close > prevStop and close[1] > prevStop
xATRTrailingStop := math.max(prevStop, close - nLoss)
else if close < prevStop and close[1] < prevStop
xATRTrailingStop := math.min(prevStop, close + nLoss)
else if close > prevStop
xATRTrailingStop := close - nLoss
else
xATRTrailingStop := close + nLoss
buySignal = ta.crossover(close, xATRTrailingStop)
sellSignal = ta.crossunder(close, xATRTrailingStop)
// RSI & ADX
rsiValue = ta.rsi(close, rsiPeriod)
[diPlus, diMinus, adxValue] = ta.dmi(adxPeriod, adxPeriod)
// --- SPIKE FILTER LOGIC ---
currentCandleSize = high - low
averageCandleSize = ta.atr(14)
isNotASpike = currentCandleSize <= (averageCandleSize * spikeMultiplier)
// ==========================================
// 3. CONDITIONS & TIME FILTERS
// ==========================================
inTradeWindow = time(timeframe.period, "0915-1500:1234567", "Asia/Kolkata")
// Entry Logic (Requires 'isNotASpike' to be true)
enterPeSell = buySignal and (rsiValue >= rsiPeMin and rsiValue <= rsiPeMax) and adxValue > adxThreshold and isNotASpike
enterCeSell = sellSignal and (rsiValue >= rsiCeMin and rsiValue <= rsiCeMax) and adxValue > adxThreshold and isNotASpike
// ==========================================
// 4. STRATEGY EXECUTION
// ==========================================
if inTradeWindow
if enterPeSell
strategy.entry("PE_Sell_Sim", strategy.long)
if enterCeSell
strategy.entry("CE_Sell_Sim", strategy.short)
if not inTradeWindow
strategy.close_all(comment="3 PM Exit")
// Visualizing the UT Bot
plot(xATRTrailingStop, color=close > xATRTrailingStop ? color.green : color.red, title="UT Bot Stop", linewidth=2)