0% found this document useful (1 vote)
482 views7 pages

Trading Indicator Script

The document is a Pine Script code for a trading indicator that combines multiple trend analysis techniques including SuperTrend, HalfTrend, and RangeFilter. It allows users to customize various parameters such as ATR period, multipliers, and signal visibility for buy/sell alerts. The script includes plotting functions for visualizing trends, signals, and target bands on a trading chart.

Uploaded by

Aj Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
482 views7 pages

Trading Indicator Script

The document is a Pine Script code for a trading indicator that combines multiple trend analysis techniques including SuperTrend, HalfTrend, and RangeFilter. It allows users to customize various parameters such as ATR period, multipliers, and signal visibility for buy/sell alerts. The script includes plotting functions for visualizing trends, signals, and target bands on a trading chart.

Uploaded by

Aj Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

//@version=5

indicator('all trend', overlay=true, format=format.price, precision=2,


timeframe='')

xs = input(defval=true,title='Show SuperTrend?')
xh = input(defval=false,title='Show HalfTrend?')
xr = input(defval=true,title='Show RangeFilter?')
xf = input(defval=false,title='Show SwingArm?')

Periods = input(title='ATR Period', defval=10,group="SUPER TREND")


srcs = input(hl2, title='Source',group="SUPER TREND")
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3.0,group="SUPER
TREND")
changeATR = input(title='Change ATR Calculation Method ?', defval=true,group="SUPER
TREND")
showsignals = input(title='Show Buy/Sell Signals ?', defval=true,group="SUPER
TREND")
highlighting = input(title='Highlighter On/Off ?', defval=true,group="SUPER TREND")
atr2s = ta.sma(ta.tr, Periods)
atrs = changeATR ? ta.atr(Periods) : atr2s
ups = srcs - Multiplier * atrs
up1s = nz(ups[1], ups)
ups := close[1] > up1s ? math.max(ups, up1s) : ups
dns = srcs + Multiplier * atrs
dn1s = nz(dns[1], dns)
dns := close[1] < dn1s ? math.min(dns, dn1s) : dns
trends = 1
trends := nz(trends[1], trends)
trends := trends == -1 and close > dn1s ? 1 : trends == 1 and close < up1s ? -1 :
trends
upPlot = plot(xs and trends == 1 ? ups : na, title='Up Trend',
style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0),transp=40)
buySignals = trends == 1 and trends[1] == -1
plotshape(xs and buySignals ? ups : na, title='UpTrend Begins',
location=location.absolute, style=shape.circle, size=size.tiny,
color=color.new(color.green, 0))
plotshape(xs and buySignals and showsignals ? ups : na, title='Buy', text='Buy',
location=location.absolute, style=shape.labelup, size=size.tiny,
color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
dnPlot = plot(xs and trends == -1 ? dns : na, title='Down Trend',
style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0))
sellSignals = trends == -1 and trends[1] == 1
plotshape(xs and sellSignals ? dns : na, title='DownTrend Begins',
location=location.absolute, style=shape.circle, size=size.tiny,
color=color.new(color.red, 0))
plotshape(xs and sellSignals and showsignals ? dns : na, title='Sell', text='Sell',
location=location.absolute, style=shape.labeldown, size=size.tiny,
color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
mPlot = plot(xs ? ohlc4 : na, title='', style=plot.style_circles,
linewidth=0,display=display.none)
longFillColor = highlighting ? trends == 1 ? color.green : color.white :
color.white
shortFillColor = highlighting ? trends == -1 ? color.red : color.white :
color.white
//fill(mPlot , upPlot, title='UpTrend Highligter',
color=color.new(longFillColor,90))
//fill(mPlot , dnPlot, title='DownTrend Highligter',
color=color.new(shortFillColor,90))
//alertcondition(buySignals, title='SuperTrend Buy', message='SuperTrend Buy!')
//alertcondition(sellSignals, title='SuperTrend Sell', message='SuperTrend Sell!')
changeCond = trends != trends[1]
//alertcondition(changeCond, title='SuperTrend Direction Change',
message='SuperTrend has changed direction!')

///////////////////

amplitude = input(title='Amplitude', defval=2,group="HALF TREND")


channelDeviation = input(title='Channel Deviation', defval=2,group="HALF TREND")
showArrows = input(title='Show Arrows', defval=true,group="HALF TREND")
showChannels = input(title='Show Channels', defval=true,group="HALF TREND")

var int trend = 0


var int nextTrend = 0
var float maxLowPrice = nz(low[1], low)
var float minHighPrice = nz(high[1], high)

var float up = 0.0


var float down = 0.0
float atrHigh = 0.0
float atrLow = 0.0
float arrowUp = na
float arrowDown = na

atr2 = ta.atr(100) / 2
dev = channelDeviation * atr2

highPrice = high[math.abs(ta.highestbars(amplitude))]
lowPrice = low[math.abs(ta.lowestbars(amplitude))]
highma = ta.sma(high, amplitude)
lowma = ta.sma(low, amplitude)

if nextTrend == 1
maxLowPrice := math.max(lowPrice, maxLowPrice)

if highma < maxLowPrice and close < nz(low[1], low)


trend := 1
nextTrend := 0
minHighPrice := highPrice
minHighPrice
else
minHighPrice := math.min(highPrice, minHighPrice)

if lowma > minHighPrice and close > nz(high[1], high)


trend := 0
nextTrend := 1
maxLowPrice := lowPrice
maxLowPrice

if trend == 0
if not na(trend[1]) and trend[1] != 0
up := na(down[1]) ? down : down[1]
arrowUp := up - atr2
arrowUp
else
up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
up
atrHigh := up + dev
atrLow := up - dev
atrLow
else
if not na(trend[1]) and trend[1] != 1
down := na(up[1]) ? up : up[1]
arrowDown := down + atr2
arrowDown
else
down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
down
atrHigh := down + dev
atrLow := down - dev
atrLow

ht = trend == 0 ? up : down

var color buyColor = color.blue


var color sellColor = color.red

htColor = trend == 0 ? buyColor : sellColor


htPlot = plot(xh ? ht : na, title='HalfTrend', linewidth=2, color=htColor)

atrHighPlot = plot(xh and showChannels ? atrHigh : na, title='ATR High',


style=plot.style_circles, color=color.new(sellColor, 0))
atrLowPlot = plot(xh and showChannels ? atrLow : na, title='ATR Low',
style=plot.style_circles, color=color.new(buyColor, 0))

fill(htPlot, atrHighPlot, title='ATR High Ribbon', color=color.new(sellColor, 90))


fill(htPlot, atrLowPlot, title='ATR Low Ribbon', color=color.new(buyColor, 90))

buySignal = not na(arrowUp) and trend == 0 and trend[1] == 1


sellSignal = not na(arrowDown) and trend == 1 and trend[1] == 0

plotshape(xh and showArrows and buySignal ? atrLow : na, title='Arrow Up',


style=shape.triangleup, location=location.absolute, size=size.tiny,
color=color.new(buyColor, 0))
plotshape(xh and showArrows and sellSignal ? atrHigh : na, title='Arrow Down',
style=shape.triangledown, location=location.absolute, size=size.tiny,
color=color.new(sellColor, 0))

//alertcondition(buySignal, title='Alert: HalfTrend Buy', message='HalfTrend Buy')


//alertcondition(sellSignal, title='Alert: HalfTrend Sell', message='HalfTrend
Sell')

///////////////

// Source

src = input(defval=close, title='Source',group="RANGE FILTER")

// Sampling Period
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters

per = input.int(defval=100, minval=1, title='Sampling Period',group="RANGE FILTER")

// Range Multiplier
mult = input.float(defval=3.0, minval=0.1, title='Range Multiplier',group="RANGE
FILTER")

// Smooth Average Range

smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
smoothrng
smrng = smoothrng(src, per, mult)

// Range Filter

rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r
: x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
rngfilt
filt = rngfilt(src, smrng)

// Filter Direction

upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 :
nz(downward[1])

// Target Bands

hband = filt + smrng


lband = filt - smrng

// Colors

filtcolor = upward > 0 ? color.lime : downward > 0 ? color.red : color.orange


barcolor = src > filt and src > src[1] and upward > 0 ? color.lime : src > filt and
src < src[1] and upward > 0 ? color.green : src < filt and src < src[1] and
downward > 0 ? color.red : src < filt and src > src[1] and downward > 0 ?
color.maroon : color.orange

filtplot = plot(xr ? filt : na, color=filtcolor, linewidth=3, title='Range Filter')

// Target

hbandplot = plot(xr ? hband : na, color=color.new(color.aqua, 100), title='High


Target')
lbandplot = plot(xr ? lband : na, color=color.new(color.fuchsia, 100), title='Low
Target')

// Fills

fill(hbandplot, filtplot, color=color.new(color.aqua, 90), title='High Target


Range',display=display.all)
fill(lbandplot, filtplot, color=color.new(color.fuchsia, 90), title='Low Target
Range',display=display.all)
// Bar Color

barcolor(xr ? barcolor : na)

// Break Outs

longCond = bool(na)
shortCond = bool(na)
longCond := src > filt and src > src[1] and upward > 0 or src > filt and src <
src[1] and upward > 0
shortCond := src < filt and src < src[1] and downward > 0 or src < filt and src >
src[1] and downward > 0

CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1

//Alerts

plotshape(xr and longCondition, title='Buy Signal', text='BUY',


textcolor=color.new(color.white, 0), style=shape.labelup, size=size.normal,
location=location.belowbar, color=color.new(color.green, 0))
plotshape(xr and shortCondition, title='Sell Signal', text='SELL',
textcolor=color.new(color.white, 0), style=shape.labeldown, size=size.normal,
location=location.abovebar, color=color.new(color.red, 0))

//alertcondition(longCondition, title='Buy Alert', message='BUY')


//alertcondition(longCondition, title='Buy Alert', message='BUY')
//alertcondition(longCondition, title='Buy Alert', message='BUY')
//alertcondition(shortCondition, title='Sell Alert', message='SELL')
/////////////

// inputs //
//{
trailType = input.string('modified', 'Trailtype', options=['modified',
'unmodified'],group="SWING ARM")
ATRPeriod = input(7, 'ATR Period',group="SWING ARM")
ATRFactor = input(2, 'ATR Factor',group="SWING ARM")
//show_fib_entries = input(true, 'Show Fib Entries?',group="SWING ARM")

////////////////////////////

////////////////

norm_o = request.security(ticker.new(syminfo.prefix, syminfo.ticker),


timeframe.period, open)
norm_h = request.security(ticker.new(syminfo.prefix, syminfo.ticker),
timeframe.period, high)
norm_l = request.security(ticker.new(syminfo.prefix, syminfo.ticker),
timeframe.period, low)
norm_c = request.security(ticker.new(syminfo.prefix, syminfo.ticker),
timeframe.period, close)
//}
//////// FUNCTIONS //////////////
//{
// Wilders ma //
Wild_ma(_src, _malength) =>
_wild = 0.0
_wild := nz(_wild[1]) + (_src - nz(_wild[1])) / _malength
_wild

/////////// TRUE RANGE CALCULATIONS /////////////////


HiLo = math.min(norm_h - norm_l, 1.5 * nz(ta.sma(close, ATRPeriod)))

HRef = norm_l <= norm_h[1] ? norm_h - norm_c[1] : norm_h - norm_c[1] - 0.5 *


(norm_l - norm_h[1])

LRef = norm_h >= norm_l[1] ? norm_c[1] - norm_l : norm_c[1] - norm_l - 0.5 *


(norm_l[1] - norm_h)

trueRange = trailType == 'modified' ? math.max(HiLo, HRef, LRef) : math.max(close,


math.abs(norm_h - norm_c[1]), math.abs(norm_l - norm_c[1]))
//}

/////////// TRADE LOGIC ////////////////////////


//{
loss = ATRFactor * Wild_ma(trueRange, ATRPeriod)

Up = norm_c - loss
Dn = norm_c + loss

TrendUp = Up
TrendDown = Dn
Trend = 1

TrendUp := norm_c[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up


TrendDown := norm_c[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn

Trend := norm_c > TrendDown[1] ? 1 : norm_c < TrendUp[1] ? -1 : nz(Trend[1], 1)


trail = Trend == 1 ? TrendUp : TrendDown

ex = 0.0
ex := ta.crossover(Trend, 0) ? norm_h : ta.crossunder(Trend, 0) ? norm_l : Trend ==
1 ? math.max(ex[1], norm_h) : Trend == -1 ? math.min(ex[1], norm_l) : ex[1]
//}

// //////// PLOT TP and SL /////////////


//{
plot(xf ? trail : na, 'Trailingstop', style=plot.style_line, color=Trend == 1 ?
color.green : Trend == -1 ? color.red : na)
plot(xf ?ex : na, 'Extremum', style=plot.style_circles, color=Trend == 1 ?
color.lime : Trend == -1 ? color.fuchsia : na)
//}

////// FIBONACCI LEVELS ///////////


//{
state = Trend == 1 ? 'long' : 'short'

fib1Level = 61.8
fib2Level = 78.6
fib3Level = 88.6
f1 = ex + (trail - ex) * fib1Level / 100
f2 = ex + (trail - ex) * fib2Level / 100
f3 = ex + (trail - ex) * fib3Level / 100
l100 = trail + 0

Fib1 = plot(xf ? f1 : na, 'Fib 1', style=plot.style_line,


color=color.new(color.black, 0))
Fib2 = plot(xf ? f2 : na, 'Fib 2', style=plot.style_line,
color=color.new(color.black, 0))
Fib3 = plot(xf ? f3 : na, 'Fib 3', style=plot.style_line,
color=color.new(color.black, 0))
L100 = plot(xf ? l100 : na, 'l100', style=plot.style_line,
color=color.new(color.black, 0))

fill(Fib1, Fib2, color=state == 'long' ? color.new(color.green,90) : state ==


'short' ? color.new(color.red,90) : na)
fill(Fib2, Fib3, color=state == 'long' ? color.new(color.green, 70) : state ==
'short' ? color.new(color.red, 70) : na)
fill(Fib3, L100, color=state == 'long' ? color.new(color.green, 60) : state ==
'short' ? color.new(color.red, 60) : na)

//////////////

You might also like