Strategy · Pine Script v5 · On the chart

Linear Regression Candles and UT Bot Strategy

by geraked

This strategy combines Linear Regression Candles with UT Bot trailing stop signals to generate trading setups.

View on GitHub

GitHub stars104on the repository
Pine versionv5runs as is
Lines of code107
Settings8inputs
LicenceMIT
Last update3 years ago29 Oct 2023

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

107 lines of Pine Script v5 from strategies/LRCUTB.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

LRCUTB.pine
// A strategy using Linear Regression Candles and UT Bot Alerts
// The strategy has been explained in this video: https://youtu.be/0ZzLlA9NFxo
// It should be noted that the inversion of the strategy works better! :D
// © 2023 Geraked, Rabist
// Licensed under MIT

// Backtesting result:
//      Symbol:             USDCHF
//      Timeframe:          15 MIN
//      Range:              2023-08-01 — 2023-10-27
//      Percent Profitable: 70.57%
//      Total Trades:       435
//      Profit Factor:      1.604

//@version=5
strategy("Linear Regression Candles and UT Bot Strategy", "LRCUTB", default_qty_type=strategy.percent_of_equity, overlay=true, currency=currency.USD)

slLookback = input.int(7, "SL Lookback", group="Strategy")
reverse = input.bool(true, "Reverse Signal", group="Strategy")

// Humble LinReg Candles
signal_length = input.int(title='Signal Smoothing', minval=1, maxval=200, defval=7, group="LinReg Candles")
sma_signal = input(title='Simple MA (Signal Line)', defval=true, group="LinReg Candles")
lin_reg = input(title='Lin Reg', defval=true, group="LinReg Candles")
linreg_length = input.int(title='Linear Regression Length', minval=1, maxval=200, defval=11, group="LinReg Candles")

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 Candles', color=color.green, wickcolor=color.green, bordercolor=color.green, editable=true)
plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title='LinReg Candles', color=color.red, wickcolor=color.red, bordercolor=color.red, editable=true)
plot(signal, color=color.new(color.white, 0))


// UT Bot Alerts
a = input(2, title='Key Vaule (changes the sensitivity)', group="UT Bot Alerts")
c = input(1, title='ATR Period', group="UT Bot Alerts")

xATR = ta.atr(c)
nLoss = a * xATR

xATRTrailingStop = 0.0
iff_1 = close > nz(xATRTrailingStop[1], 0) ? close - nLoss : close + nLoss
iff_2 = close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), close + nLoss) : iff_1
xATRTrailingStop := close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), close - nLoss) : iff_2

pos = 0
iff_3 = close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0) ? 1 : iff_3

ema = ta.ema(close, 1)
above = ta.crossover(ema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema)

buy = close > xATRTrailingStop and above
sell = close < xATRTrailingStop and below

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)
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)


// Strategy
long = buy and close > signal
short = sell and close < signal

longSl = ta.lowest(low, slLookback)
shortSl = ta.highest(high, slLookback)

longClose = bclose[1] > bopen[1] and bclose < bopen
shortClose = bclose[1] < bopen[1] and bclose > bopen

if longClose
    if not reverse
        strategy.close(id="long", comment="Close long")
    else
        strategy.close(id="short", comment="Close short")

if shortClose
    if not reverse
        strategy.close(id="short", comment="Close short")
    else
        strategy.close(id="long", comment="Close long")

if long
    if not reverse
        id = "long"
        strategy.entry(id, strategy.long)
        strategy.exit("exitLong", id, stop=longSl)
    else
        id = "short"
        strategy.entry(id, strategy.short)
        strategy.exit("exitShort", id, limit=longSl)

if short
    if not reverse
        id = "short"
        strategy.entry(id, strategy.short)
        strategy.exit("exitShort", id, stop=shortSl)
    else
        id = "long"
        strategy.entry(id, strategy.long)
        strategy.exit("exitLong", id, limit=shortSl)

Overview

Linear Regression Candles and UT Bot Strategy calculates smoothed linear regression open, high, low, and close values alongside a signal line, while computing an ATR-based trailing stop. Long and short entries trigger when price crosses the trailing stop in alignment with the regression signal line, with an option to invert signals. Stop losses are determined by extreme high or low prices over a defined lookback period. It plots custom candles and execution labels directly on the main price chart.

Details

Published by

geraked on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Moving averages, Volatility & bands

Markets

Forex

Timeframes

M15-H1

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

backtesting-trading-strategiesexpert-advisorexpert-advisorsforex-botgerakedmetatraderpinescriptrabiststock-markettrading-algorithms

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
SL Lookbackint7slLookback
Reverse Signalbooltruereverse
Signal Smoothingint7signal_length
Simple MA (Signal Line)booltruesma_signal
Lin Regbooltruelin_reg
Linear Regression Lengthint11linreg_length
Key Vaule (changes the sensitivity)int2a
ATR Periodint1c
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This mean-reversion strategy plots Bollinger Bands and a long-term moving average directly on the price chart. It enters trades when price candles penetrate the outer Bollinger Bands beyond a user-defined body percentage, anticipating a price reversal. Entries

Strategy v5 Mean reversionVolatility & bands
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner PRO [Alorse]
257 stars

This overlay strategy targets one-minute ES futures and plots volume-weighted moving-average bands from the open, scaled by a standard-deviation multiple, plus a slower band from ten-minute opens. A higher-timeframe exponential moving average can filter trades

Strategy v6 Moving averagesVolatility & bands
Updated 6 weeks ago · None stated GitHub repository of ES - Futures 1M No Repaint

STRG-BBForce

by harryguiacorn
219 stars

STRG-BBForce is a Bollinger Bands trend-following strategy. It calculates standard Bollinger Bands and tracks directional alignment across the upper, lower, and basis lines. When all three lines slope upward, it opens a long position and closes short positions

Strategy v5 TrendVolatility & bands
Updated 1 year ago · None stated GitHub repository of STRG-BBForce
106 stars

This trading strategy combines three Exponential Moving Averages (8, 14, and 50 periods by default) with Stochastic RSI and Average True Range (ATR). It enters a long position when EMA 1 is above EMA 2, EMA 2 is above EMA 3, and the Stochastic RSI %K line cros

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 3 x EMA + Stochastic RSI + ATR

Bollinger Bands Strategy

by cklose2000
105 stars

This strategy generates buy and sell signals based on Bollinger Band crossovers combined with a simple moving average trend filter. It enters a long position when price crosses above the lower band while trading above the SMA filter line, and enters a short po

Strategy v5 Volatility & bandsMoving averages
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers