It seems interesting for trend catch. It is not present in the tradingview library, a friend sent it to me. I attach the code.
Code: Select all
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator(shorttitle='GBS', title='Gain Buy Sell', overlay=true)
// --- settings
ATRperiod = input.int(defval=5, title='ATR Period', minval=1)
BBperiod = input.int(defval=21, title='Bollinger Bands Period', minval=1)
BBdeviation = input.float(defval=1.00, title='Bollinger Bands Deviation', minval=0.1, step=0.1)
UseATRfilter = input(defval=true, title='ATR Filter On/Off')
showsignals = input(title='Show Signals ', defval=false)
// --- end of settings
// Bollinger Bands calculation
BBUpper = ta.sma(close, BBperiod) + ta.stdev(close, BBperiod) * BBdeviation
BBLower = ta.sma(close, BBperiod) - ta.stdev(close, BBperiod) * BBdeviation
// ATR calculation
atrValue = ta.atr(ATRperiod)
// Signal initialization
var float EliteSignal = na
var int BBSignal = 0
// Determine BB signal
if (close > BBUpper)
BBSignal := 1
else if (close < BBLower)
BBSignal := -1
// Buy signal logic
if (BBSignal == 1)
if (UseATRfilter)
EliteSignal := low - atrValue
else
EliteSignal := low
if (EliteSignal < nz(EliteSignal[1]))
EliteSignal := nz(EliteSignal[1])
// Sell signal logic
if (BBSignal == -1)
if (UseATRfilter)
EliteSignal := high + atrValue
else
EliteSignal := high
if (EliteSignal > nz(EliteSignal[1]))
EliteSignal := nz(EliteSignal[1])
// Trend direction determination
var int iTrend = 0
if (nz(EliteSignal) > nz(EliteSignal[1]))
iTrend := 1
else if (nz(EliteSignal) < nz(EliteSignal[1]))
iTrend := -1
// Trend line color based on trend direction
lineColor = iTrend > 0 ? color.rgb(6, 94, 28) : #f70000
// Define three lines with reduced gap (0.5x ATR)
gapSize = atrValue * 0.5
EliteSignalUpper = EliteSignal + gapSize
EliteSignalLower = EliteSignal - gapSize
// Buy & sell conditions
buy = 0.0
sell = 0.0
buy := iTrend[1] == -1 and iTrend == 1 ? 1 : na
sell := iTrend[1] == 1 and iTrend == -1 ? 1 : na
// Alerts
alertcondition(sell == 1, title="Sell", message="Gain Buy Sell Sell")
alertcondition(buy == 1, title="Buy", message="Gain Buy Sell Buy")
alertcondition(buy == 1 or sell == 1, title="Signal", message="Gain Buy Sell Signal")
// Plot the three trend lines with same width and reduced gap
plot(EliteSignal, color=lineColor, linewidth=3, title="Gain Buy Sell Main")
plot(EliteSignalUpper, color=lineColor, linewidth=3, title="Gain Buy Sell Upper")
plot(EliteSignalLower, color=lineColor, linewidth=3, title="Gain Buy Sell Lower")
plotshape(buy == 1 and showsignals ? EliteSignal - atrValue : na, text='BUY', style=shape.labelup, location=location.absolute, color=color.rgb(6, 94, 28), textcolor=#050202, offset=0, transp=0, size=size.normal)
plotshape(sell == 1 and showsignals ? EliteSignal + atrValue : na, text='SELL', style=shape.labeldown, location=location.absolute, color=#f70000, textcolor=#050202, offset=0, transp=0, size=size.normal)
// ==========================================================================================