本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator("London Session + EMA 200 + UT Bot", overlay = true, max_bars_back = 500, max_boxes_count = 500, max_labels_count = 500)
//==============================================================================
// UT BOT SETTINGS
//==============================================================================
keyvalue = input.float(3, title = "UT Bot Key Value", step = 0.5, group = "UT Bot")
atrperiod = input.int(10, title="UT Bot ATR Period", group = "UT Bot")
show_utbot = input(true, "Show UT Bot", group = "UT Bot")
// UT Bot Color Settings
utbot_buy_color = input.color(color.green, "Buy Signal Color", group = "UT Bot Colors")
utbot_sell_color = input.color(color.red, "Sell Signal Color", group = "UT Bot Colors")
utbot_uptrend_color = input.color(color.green, "Uptrend ATR Line Color", group = "UT Bot Colors")
utbot_downtrend_color = input.color(color.red, "Downtrend ATR Line Color", group = "UT Bot Colors")
utbot_neutral_color = input.color(color.blue, "Neutral ATR Line Color", group = "UT Bot Colors")
//==============================================================================
// EMA 200 SETTINGS
//==============================================================================
show_ema = input(true, "Show EMA", group = "EMA")
ema_length = input.int(200, "EMA Length", group = "EMA")
ema_src = input(close, title="EMA Source", group = "EMA")
ema_color = input.color(color.red, "EMA Color", group = "EMA")
ema_offset = input.int(title="EMA Offset", defval=0, minval=-500, maxval=500, group = "EMA")
//==============================================================================
// LONDON SESSION SETTINGS
//==============================================================================
show_london = input(true, '', inline = 'london', group = 'London Session')
london_txt = input('London', '', inline = 'london', group = 'London Session')
london_ses = input.session('0700-1600', '', inline = 'london', group = 'London Session')
london_css = input.color(#2157f3, '', inline = 'london', group = 'London Session')
london_range = input(true, 'Range Box', group = 'London Session')
bg_transp = input.int(90, 'Box Transparency', group = 'London Session')
show_outline = input(true, 'Box Outline', group = 'London Session')
show_txt = input(true, 'Box Label', group = 'London Session')
//Timezone
tz_incr = input.int(0, 'UTC (+/-)', group = 'London Session')
use_exchange = input(false, 'Use Exchange Timezone', group = 'London Session')
//==============================================================================
// UT BOT CALCULATION
//==============================================================================
src = close
xATR = ta.atr(atrperiod)
nLoss = keyvalue * xATR
xATRTrailingStop = 0.0
xATRTrailingStop := if src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0)
math.max(nz(xATRTrailingStop[1]), src - nLoss)
else if src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0)
math.min(nz(xATRTrailingStop[1]), src + nLoss)
else if src > nz(xATRTrailingStop[1], 0)
src - nLoss
else
src + nLoss
pos = 0
pos := if src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0)
1
else if src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0)
-1
else
nz(pos[1], 0)
xcolor = pos == -1 ? utbot_downtrend_color : pos == 1 ? utbot_uptrend_color : utbot_neutral_color
buy = ta.crossover(src, xATRTrailingStop)
sell = ta.crossunder(src, xATRTrailingStop)
barcolor_condition = src > xATRTrailingStop
//==============================================================================
// EMA CALCULATION
//==============================================================================
ema_out = ta.ema(ema_src, ema_length)
//==============================================================================
// LONDON SESSION CALCULATION
//==============================================================================
n = bar_index
tf = timeframe.period
var tz = use_exchange ? syminfo.timezone : str.format('UTC{0}{1}', tz_incr >= 0 ? '+' : '-', math.abs(tz_incr))
is_london = math.sign(nz(time(tf, london_ses, tz)))
//Set session range function
get_range(session, session_name, session_css)=>
var t = 0
var float max = na
var float min = na
var box bx = na
var label lbl = na
if session > session[1]
t := time
max := high
min := low
bx := box.new(n, max, n, min
, bgcolor = color.new(session_css, bg_transp)
, border_color = show_outline ? session_css : na
, border_style = line.style_dotted)
if show_txt
lbl := label.new(t, max, session_name
, xloc = xloc.bar_time
, textcolor = session_css
, style = label.style_label_down
, color = color.new(color.white, 100)
, size = size.tiny)
if session and session == session[1]
max := math.max(high, max)
min := math.min(low, min)
box.set_top(bx, max)
box.set_rightbottom(bx, n, min)
if show_txt
label.set_xy(lbl, int(math.avg(t, time)), max)
[session ? na : max, session ? na : min]
// London Session Range
var float max_london = na
var float min_london = na
if show_london and london_range
[max, min] = get_range(is_london, london_txt, london_css)
max_london := max
min_london := min
//==============================================================================
// PLOTS
//==============================================================================
// UT Bot
plot(show_utbot ? xATRTrailingStop : na, color = xcolor, title = "UT Bot Trailing Stop", linewidth = 2)
plotshape(show_utbot and buy, title = "Buy", text = 'Buy', style = shape.labelup, location = location.belowbar, color = utbot_buy_color, textcolor = color.white, size = size.tiny)
plotshape(show_utbot and sell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color = utbot_sell_color, textcolor = color.white, size = size.tiny)
barcolor(show_utbot ? (barcolor_condition ? utbot_uptrend_color : utbot_downtrend_color) : na)
// EMA
plot(show_ema ? ema_out : na, color = ema_color, title = "EMA", linewidth = 2, offset = ema_offset)
//==============================================================================
// ALERTS
//==============================================================================
alertcondition(buy, title='UT BOT Buy', message='UT BOT Buy Signal')
alertcondition(sell, title='UT BOT Sell', message='UT BOT Sell Signal')