本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
strategy(("UT BOT - SHAH 200/50 Ema"), overlay=true, pyramiding=2, initial_capital=10000)
//====================================================
// INPUTS
//====================================================
// EMA SETTINGS
emaFastLen = input.int(50, title="EMA 50")
emaSlowLen = input.int(200, title="EMA 200")
// RSI SETTINGS
rsiLen = input.int(14, title="RSI Length")
rsiBull = input.int(55, title="RSI Buy Level")
rsiBear = input.int(45, title="RSI Sell Level")
// ADX SETTINGS
adxLen = input.int(14, title="ADX Length")
adxMin = input.int(20, title="ADX Minimum")
// LOT SIZE
fixedQty = input.float(0.02, title="Lot Size")
// UT BOT SETTINGS
a = input.float(1.0, title="UT Sensitivity")
c = input.int(10, title="ATR Period")
//====================================================
// SESSION FILTERS
//====================================================
// ASIA SESSION
asiaSession = not na(time(timeframe.period, "1900-0400"))
// LONDON SESSION
londonSession = not na(time(timeframe.period, "0300-1200"))
// NEW YORK SESSION
newYorkSession = not na(time(timeframe.period, "0800-1700"))
// FINAL SESSION FILTER
sessionAllowed = asiaSession or londonSession or newYorkSession
//====================================================
// NEWS FILTER
//====================================================
atr = ta.atr(14)
atrAvg = ta.sma(atr, 50)
newsSpike = atr > atrAvg * 1.8
nyNewsBlock = not na(time(timeframe.period, "1230-1330"))
londonNewsBlock = not na(time(timeframe.period, "0630-0730"))
newsBlocked = newsSpike or nyNewsBlock or londonNewsBlock
//====================================================
// EMA / RSI / ADX
//====================================================
ema50 = ta.ema(close, emaFastLen)
ema200 = ta.ema(close, emaSlowLen)
rsi = ta.rsi(close, rsiLen)
[diplus, diminus, adx] = ta.dmi(adxLen, adxLen)
//====================================================
// UT BOT LOGIC
//====================================================
xATR = ta.atr(c)
nLoss = a * xATR
var float xATRTrailingStop = 0.0
xATRTrailingStop :=
close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0) ?
math.max(nz(xATRTrailingStop[1]), close - nLoss) :
close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0) ?
math.min(nz(xATRTrailingStop[1]), close + nLoss) :
close > nz(xATRTrailingStop[1], 0) ?
close - nLoss :
close + nLoss
ema1 = ta.ema(close, 1)
above = ta.crossover(ema1, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema1)
// FINAL UT SIGNALS
utBuy = close > xATRTrailingStop and above
utSell = close < xATRTrailingStop and below
//====================================================
// TREND FILTER
//====================================================
bullTrend = close > ema200 and ema50 > ema200
bearTrend = close < ema200 and ema50 < ema200
//====================================================
// SIGNAL LOCK
//====================================================
var int lastSignalBar = na
signalCooldown = 10
canSignal =
na(lastSignalBar) or
(bar_index - lastSignalBar > signalCooldown)
//====================================================
// ASIA EXTRA FILTER
//====================================================
asiaStrongTrend = adx > 25
normalTrend = adx > adxMin
finalADXFilter =
asiaSession ? asiaStrongTrend : normalTrend
//====================================================
// FINAL CONDITIONS
//====================================================
buyCondition =
utBuy and
bullTrend and
rsi > rsiBull and
finalADXFilter and
sessionAllowed and
not newsBlocked and
canSignal
sellCondition =
utSell and
bearTrend and
rsi < rsiBear and
finalADXFilter and
sessionAllowed and
not newsBlocked and
canSignal
//====================================================
// TRADE CONTROL
//====================================================
var int tradeCount = 0
if strategy.position_size == 0
tradeCount := 0
//====================================================
// BUY ENTRIES
//====================================================
if buyCondition and tradeCount == 0
strategy.entry("BUY_1", strategy.long, qty=fixedQty)
strategy.entry("BUY_2", strategy.long, qty=fixedQty)
tradeCount := 2
lastSignalBar := bar_index
//====================================================
// SELL ENTRIES
//====================================================
if sellCondition and tradeCount == 0
strategy.entry("SELL_1", strategy.short, qty=fixedQty)
strategy.entry("SELL_2", strategy.short, qty=fixedQty)
tradeCount := 2
lastSignalBar := bar_index
//====================================================
// TAKE PROFITS / STOP LOSS
//====================================================
entry = strategy.position_avg_price
// BUY LEVELS
buyTP1 = entry + 2
buyTP2 = entry + 4
buySL = entry - 5
// SELL LEVELS
sellTP1 = entry - 2
sellTP2 = entry - 4
sellSL = entry + 5
//====================================================
// BUY EXITS
//====================================================
strategy.exit(
"BUY_EXIT_1",
from_entry = "BUY_1",
limit = buyTP1,
stop = buySL)
strategy.exit(
"BUY_EXIT_2",
from_entry = "BUY_2",
limit = buyTP2,
stop = buySL)
//====================================================
// SELL EXITS
//====================================================
strategy.exit(
"SELL_EXIT_1",
from_entry = "SELL_1",
limit = sellTP1,
stop = sellSL)
strategy.exit(
"SELL_EXIT_2",
from_entry = "SELL_2",
limit = sellTP2,
stop = sellSL)
//====================================================
// PLOTS
//====================================================
// EMA 50
plot(
ema50,
color = color.new(color.green, 60),
linewidth = 2,
title = "EMA 50")
// EMA 200
plot(
ema200,
color = color.new(color.red, 60),
linewidth = 2,
title = "EMA 200")
// BUY LABEL
plotshape(
buyCondition,
title = "BUY",
style = shape.labelup,
location = location.belowbar,
color = color.green,
text = "BUY")
// SELL LABEL
plotshape(
sellCondition,
title = "SELL",
style = shape.labeldown,
location = location.abovebar,
color = color.red,
text = "SELL")
//====================================================
// DASHBOARD
//====================================================
var table dashboard = table.new(position.top_right, 2, 9, border_width=1)
if barstate.islast
table.cell(dashboard, 0, 0, "UT BOT HYBRID", bgcolor=color.gray, text_color=color.white)
table.cell(dashboard, 0, 1, "Trend")
table.cell(dashboard, 1, 1,
bullTrend ? "BULL" :
bearTrend ? "BEAR" : "NEUTRAL",
bgcolor =
bullTrend ? color.green :
bearTrend ? color.red :
color.gray)
table.cell(dashboard, 0, 2, "RSI")
table.cell(dashboard, 1, 2, str.tostring(rsi))
table.cell(dashboard, 0, 3, "ADX")
table.cell(dashboard, 1, 3, str.tostring(adx))
table.cell(dashboard, 0, 4, "Asia")
table.cell(dashboard, 1, 4,
asiaSession ? "ON" : "OFF",
bgcolor = asiaSession ? color.green : color.gray)
table.cell(dashboard, 0, 5, "London")
table.cell(dashboard, 1, 5,
londonSession ? "ON" : "OFF",
bgcolor = londonSession ? color.green : color.gray)
table.cell(dashboard, 0, 6, "New York")
table.cell(dashboard, 1, 6,
newYorkSession ? "ON" : "OFF",
bgcolor = newYorkSession ? color.green : color.gray)
table.cell(dashboard, 0, 7, "News")
table.cell(dashboard, 1, 7,
newsBlocked ? "BLOCKED" : "OK",
bgcolor = newsBlocked ? color.red : color.green)
table.cell(dashboard, 0, 8, "Signal")
table.cell(dashboard, 1, 8,
buyCondition ? "BUY" :
sellCondition ? "SELL" :
"WAIT",
bgcolor =
buyCondition ? color.green :
sellCondition ? color.red :
color.gray)
//====================================================
// ALERTS
//====================================================
alertcondition(buyCondition, title="BUY ALERT", message="BUY")
alertcondition(sellCondition, title="SELL ALERT", message="SELL")