UT Bot v2 — ATR Trailing Stop

QuantNomad · strategy · 79 行 · 点赞 2,706 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantNomad
 
//@version=6
strategy(title = 'UT Bot v2 — ATR Trailing Stop', shorttitle = "UT Bot v2", overlay = true)
 
////////////
// INPUTS /{
 
mult        = input.float(1,      title = 'Multipier',  minval = 0.1, step = 0.1, tooltip = 'Controls the distance of the trailing stop. Higher values create wider stops and fewer trend flips.', group = "Config")
atr_len     = input.int(10,       title = 'ATR Period', minval = 1, tooltip = 'Number of bars used to calculate ATR volatility.', group = "Config")
source      = input.source(close, title = "Source", tooltip = 'Price source used for trailing stop calculations.', group = "Config")
 
show_signals = input.bool(true,  'Show Buy/Sell Signals',   group = "Display")
color_bars   = input.bool(false, 'Color Bars by Trend',     group = "Display")
show_tsl     = input.bool(true,  'Show Trailing Stop Line', group = "Display")
 
//}
 
//////////////////
// CALCULATUONS /{
 
atr = ta.atr(atr_len)
sl_value = mult * atr
 
tsl_price = 0.0
tsl_price :=  source > tsl_price[1] and source[1] > tsl_price[1] ? math.max(nz(tsl_price[1]), source - sl_value) : 
              source < tsl_price[1] and source[1] < tsl_price[1] ? math.min(nz(tsl_price[1]), source + sl_value) : 
              source > tsl_price[1] ? source - sl_value : 
              source + sl_value
 
buy  = ta.crossover(source, tsl_price) and barstate.isconfirmed
sell = ta.crossover(tsl_price, source) and barstate.isconfirmed
 
//}
 
///////////
// PLOTS /{
 
bull_color = color.new(color.green, 0)
bear_color = color.new(color.red, 0)
 
tsl_color  = bull_color
tsl_color := sell ? bear_color : buy ? bull_color : tsl_color[1]
 
barcolor(tsl_color, display = color_bars ? display.all : display.none)
 
plot(tsl_price, color = tsl_color, linewidth = 2, display = show_tsl ?  display.all : display.none )
 
plotshape(buy,  title = 'Buy',  text = 'Buy',  style = shape.labelup,   location = location.belowbar, 
 color = color.new(color.green, 0), textcolor = color.new(color.white, 0), size = size.tiny, display = show_signals ? display.all : display.none)
 
plotshape(sell, title = 'Sell', text = 'Sell', style = shape.labeldown, location = location.abovebar, 
 color = color.new(color.red, 0),   textcolor = color.new(color.white, 0), size = size.tiny, display = show_signals ? display.all : display.none)
 
 
//}
 
////////////
// ALERTS /{
 
if buy
    alert('UT Bot Buy', alert.freq_once_per_bar_close)
 
if sell 
    alert('UT Bot Sell', alert.freq_once_per_bar_close)
 
//}
 
//////////////
// STRATEGY /{
 
if buy
    strategy.entry("Long", strategy.long)
 
if sell 
    strategy.entry('Short', strategy.short)
 
//}

← 返回列表