Buy/Sell Signals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mayurssupe

//@version=5
indicator("Buy & Sell Signals",overlay=true,shorttitle="Tail Signals")

//colors
signal1 = input.bool(defval=false,title="Buy Sell Signals",group="Options",tooltip="Works on Every Ticker")
_filterSignals = input.bool(defval=true,title="Supertrend with ATR ",group="Options",tooltip="Detect Signals Based on Supertrend & ATR Only works on higher tf")
_snakeLine =input.bool(defval=false,title="Stoploss",group="Options",tooltip = "Stoploss line is use to maintain stoploss and not get hit by operator, accurancy : 60%")

//colors
color1 = input.color(color.aqua ,title="Long Color",group="Colors")
color2 = input.color(#e91e63,title="Short Color",group="Colors")

//atr Period for Stoploss Line
atrPeriod = input.int(defval = 10 , minval = 10,maxval = 100,step = 2,title="Stoploss Period",group = "Inputs",tooltip = "Atr Value : Default to 10")
_suptf = input.timeframe(defval="",title="Set Default Timeframe of Signals on Chart",group="Timeframe")

//---------------------//

//Custom Security Function
fsec(exp) => request.security(ticker.heikinashi(syminfo.tickerid),_suptf,exp,gaps=barmerge.gaps_on,lookahead=barmerge.lookahead_off)
symsec(tf,exp)=> request.security(syminfo.tickerid,tf,exp)

//Green/Red Bar
greenBar =close > open
redBar = close < open
//------------

// Reversal using Arnold Legoux MA
var color pink = #e91e63
var color white = color.white
var color aqua = color.aqua

//Supertrend
var int atrlen = 14
var int multi = 2

//0.8 and 1.6 factor
var int exp1 = 9
var int exp2 =21
var int rsiPeriod = 9
//RSI Zones
var int zone1 = 70
var int zone2 = 30

//BANKNIFTY1!
rsi = ta.rsi(hlc3,rsiPeriod)
//rsi
rsiB = rsi < zone1 
rsiS = rsi > zone2 


//Supertrend Indicator using EMA crossover 9,13
[supertrend,direction] = ta.supertrend(multi,atrlen)


_neckLine = ta.ema(ta.sma(hl2,14),14)
atr1 = ta.rma(ta.tr(true),14)

p1 = ta.crossover(hl2,supertrend)  and rsiB
p2 = ta.crossunder(hl2,supertrend)  and rsiS



//Boolean Conditions
longSup  = barstate.isconfirmed and fsec(p1) and hl2 > _neckLine
shortSup =barstate.isconfirmed and fsec(p2) and hl2 < _neckLine

has_longsup = longSup ? 1 : 0
has_shortsup = shortSup ? 1 : 0

bar_index_has = signal1 ? ta.valuewhen(has_longsup,bar_index,0) : na
high_value_has = signal1 ? ta.valuewhen(has_longsup,high,0) : na

bar_down = signal1 ? ta.valuewhen(has_shortsup,bar_index,0) : na
low_sup = signal1 ? ta.valuewhen(has_shortsup,low,0) : na


//LongSup
line1 = line.new(bar_index_has,high_value_has,bar_index_has + 1 ,high_value_has,color=color.green,extend=extend.right,width=3)
line.delete(line1[1])

//ShortSup
line2 = line.new(bar_down,low_sup,bar_down + 1 ,low_sup,color=color.red,extend=extend.right,width=3)
line.delete(line2[1])

plotshape(signal1 and longSup ? longSup : 0,title="Long",style=shape.labelup,text="Buy",textcolor=color.white,color=color1,location=location.belowbar)
plotshape(signal1 and shortSup ? shortSup : 0,title="Short",style=shape.labeldown,text="Sell",textcolor=color.white,color=color2,location=location.abovebar)

//average true range
[sup1,direction1] = ta.supertrend(0.8,14)
[sup2,direction2] = ta.supertrend(1.6,21)

supcross =fsec(ta.crossover(sup1,sup2) and timeframe.isintraday)  and hl2 > _neckLine
supunder =fsec(ta.crossunder(sup1,sup2) and timeframe.isintraday) and hl2 < _neckLine

if(_filterSignals == true)
    signal1:=false
else
    signal1:=true  

plotshape(_filterSignals and supcross[1] ? supcross[1] : 0,title="Buy",style=shape.labelup,text="Long",textcolor=color.white,color=color1,location=location.belowbar)
plotshape(_filterSignals and supunder[1] ? supunder[1] : 0,title="Sell",style=shape.labeldown,text="Short",textcolor=color.white,color=color2,location=location.abovebar)


//Stoploss
ma20 = ta.ema(ta.sma(hl2,14) , 14)
atrstop = ta.rma(ta.tr(true),atrPeriod)
stopLoss = close > ma20 ? (low - atrstop) : close < ma20 ? (high + atrstop) : na

plot(_snakeLine and stopLoss ? stopLoss : 2,style = plot.style_stepline,color=color.rgb(255, 209, 56),title = "SnakeLine StoplossH")

