Nifty36Scanner
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AtulGoswami
// this screener is coded to filter out buy and sell signal son the basis of Inidcators and crossovers. 
// credit : This is a modified version of a Scanner video done by Stock Data Analyst youtube channel
// you can select any indicator and crossover and runt the scan on any NSE 36 stocks , a selction of stocks can also be made by going int settings
//HalfTrend inidcator is used to scan the stocks, originally developed by Everget


//@version=5
indicator("Nifty50Scanner", overlay=true)

len=input.int(9, title="RSI Length")
macdcrossscreener=input.bool(true, title="MACD Screener", group="MACD")
EMandSTscanner=input.bool(true, title="EMA200 & Supertrend Cross", group="EMA200 + Supertrend")
Halftrend = input.bool(true, title="Half Trend", group="HalfTrend")

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//Screener function to calculate Bull and Bear status of teh signal equalent to Buy and Sell Signal
screener() =>
//Buy/Sell signal calculation if scaning for EMA200 cross over and Supertrend and close crossover
    if(EMandSTscanner==true and macdcrossscreener==false)
       
        [supertrend,direction] = ta.supertrend(2.5,10)

        buy    =ta.crossover(close, supertrend) and ta.crossover(open, supertrend) and ta.crossover(close, ta.ema(close, 200))
        sell   =ta.crossunder(close, supertrend) and ta.crossunder(open, supertrend) and ta.crossunder(close, ta.ema(close, 200))
   
        bull=   buy
        bear= sell
        [bull, bear]
    //Buy/Sell signal calculation if scaning for only MACD Crossover based buy/Sell signal
    if(macdcrossscreener==true and EMandSTscanner==false)

        [macdline, sigline, histline] =  ta.macd(close, 12, 26, 9)
        buy    =ta.crossover(macdline, sigline) 
        sell   =ta.crossunder(macdline, sigline) 
        bull= buy
        bear=sell
        [bull, bear]
     //Buy/Sell signal calculation if scaning for  MACD Crossover based buy/Sell signal and EMA200 cross over and Supertrend and close crossover
    if(macdcrossscreener==true and EMandSTscanner==true)
        [supertrend,direction] = ta.supertrend(2.5,10)
        [macdline, sigline, histline] =  ta.macd(close, 12, 26, 9)

        buy    =ta.crossover(close, supertrend)  and ta.crossover(macdline, sigline) and ta.crossover(close, ta.ema(close, 200))
        sell   =ta.crossunder(close, supertrend)  and ta.crossunder(macdline, sigline) and ta.crossunder(close, ta.ema(close, 200))
        bull= buy
        bear=sell
        [bull, bear]
    // Consider Half Trend based Buy/Sell signals in addition to EMA200 cross over and Supertrend and close crossover and MACD Crossover
    if(Halftrend==true)
    //Calculation of Half Trend Buy/Sell signals
        amplitude = input(title='Amplitude', defval=2)
        channelDeviation = input(title='Channel Deviation', defval=2)
        showArrows = input(title='Show Arrows', defval=true)
        showChannels = input(title='Show Channels', defval=true)

        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

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

        bull=buySignal
        bear=sellSignal
        [bull, bear]
    

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

//Calculating Timeframe 
tf=timeframe.period
//Assigining Stock symbols for scanning

s1=input.symbol('NSE:NIFTY1!', title='Symbol1', group="Nifty50List", inline='0')
s2=input.symbol('NSE:BANKNIFTY1!', title='Symbol2', group="Nifty50List", inline='0')
s3=input.symbol('NSE:SBIN', title='Symbol3', group="Nifty50List", inline='0')
s4=input.symbol('NSE:ADANIENT', title='Symbol4', group="Nifty50List", inline='0')
s5=input.symbol('NSE:RELIANCE', title='Symbol5', group="Nifty50List", inline='0')
s6=input.symbol('NSE:HDFCBANK', title='Symbol6', group="Nifty50List", inline='0')
s7=input.symbol('NSE:INDUSINDBK', title='Symbol7', group="Nifty50List", inline='0')
s8=input.symbol('NSE:MARUTI', title='Symbol8', group="Nifty50List", inline='0')
s9=input.symbol('NSE:ICICIBANK', title='Symbol9', group="Nifty50List", inline='0')
s10=input.symbol('NSE:TATASTEEL', title='Symbol1', group="Nifty50List", inline='0')
s11=input.symbol('NSE:TATAMOTORS', title='Symbol2', group="Nifty50List", inline='0')
s12=input.symbol('NSE:ADANIPORTS', title='Symbol3', group="Nifty50List", inline='0')
s13=input.symbol('NSE:APOLLOHOSP', title='Symbol4', group="Nifty50List", inline='0')
s14=input.symbol('NSE:ASIANPAINT', title='Symbol5', group="Nifty50List", inline='0')
s15=input.symbol('NSE:AXISBANK', title='Symbol6', group="Nifty50List", inline='0')
s16=input.symbol('NSE:BAJAJFINSV', title='Symbol7', group="Nifty50List", inline='0')
s17=input.symbol('NSE:BAJFINANCE', title='Symbol8', group="Nifty50List", inline='0')
s18=input.symbol('NSE:BAJAJ_AUTO', title='Symbol9', group="Nifty50List", inline='0')
s19=input.symbol('NSE:DRREDDY', title='Symbol1', group="Nifty50List", inline='0')
s20=input.symbol('NSE:HCLTECH', title='Symbol2', group="Nifty50List", inline='0')
s21=input.symbol('NSE:EICHERMOT', title='Symbol3', group="Nifty50List", inline='0')
s22=input.symbol('NSE:HEROMOTOCO', title='Symbol4', group="Nifty50List", inline='0')
s23=input.symbol('NSE:HINDALCO', title='Symbol5', group="Nifty50List", inline='0')
s24=input.symbol('NSE:ITC', title='Symbol6', group="Nifty50List", inline='0')
s25=input.symbol('NSE:LT', title='Symbol7', group="Nifty50List", inline='0')
s26=input.symbol('NSE:KOTAKBANK', title='Symbol8', group="Nifty50List", inline='0')
s27=input.symbol('NSE:M_M', title='Symbol9', group="Nifty50List", inline='0')
s28=input.symbol('NSE:BPCL', title='Symbol1', group="Nifty50List", inline='0')
s29=input.symbol('NSE:ONGC', title='Symbol2', group="Nifty50List", inline='0')
s30=input.symbol('NSE:TCS', title='Symbol3', group="Nifty50List", inline='0')
s31=input.symbol('NSE:TECHM', title='Symbol4', group="Nifty50List", inline='0')
s32=input.symbol('NSE:TITAN', title='Symbol5', group="Nifty50List", inline='0')
s33=input.symbol('NSE:ULTRACEMCO', title='Symbol6', group="Nifty50List", inline='0')
s34=input.symbol('NSE:UPL', title='Symbol7', group="Nifty50List", inline='0')
s35=input.symbol('NSE:WIPRO', title='Symbol8', group="Nifty50List", inline='0')
s36=input.symbol('NSE:TATACONSUM', title='Symbol9', group="Nifty50List", inline='0')
///////////////////////////////////////////////////////////////////////////////////

// Assigning Buy /Sell signals to each stock identified in the given time frame
[c1,v1]=request.security(s1, tf, screener())
[c2,v2]=request.security(s2, tf, screener())
[c3,v3]=request.security(s3, tf, screener())
[c4,v4]=request.security(s4, tf, screener())
[c5,v5]=request.security(s5, tf, screener())
[c6,v6]=request.security(s6, tf, screener())
[c7,v7]=request.security(s7, tf, screener())
[c8,v8]=request.security(s8, tf, screener())
[c9,v9]=request.security(s9, tf, screener())
[c10,v10]=request.security(s10, tf, screener())
[c11,v11]=request.security(s11, tf, screener())
[c12,v12]=request.security(s12, tf, screener())
[c13,v13]=request.security(s13, tf, screener())
[c14,v14]=request.security(s14, tf, screener())
[c15,v15]=request.security(s15, tf, screener())
[c16,v16]=request.security(s16, tf, screener())
[c17,v17]=request.security(s17, tf, screener())
[c18,v18]=request.security(s18, tf, screener())
[c19,v19]=request.security(s19, tf, screener())
[c20,v20]=request.security(s20, tf, screener())
[c21,v21]=request.security(s21, tf, screener())
[c22,v22]=request.security(s22, tf, screener())
[c23,v23]=request.security(s23, tf, screener())
[c24,v24]=request.security(s24, tf, screener())
[c25,v25]=request.security(s25, tf, screener())
[c26,v26]=request.security(s26, tf, screener())
[c27,v27]=request.security(s27, tf, screener())
[c28,v28]=request.security(s28, tf, screener())
[c29,v29]=request.security(s29, tf, screener())
[c30,v30]=request.security(s30, tf, screener())
[c31,v31]=request.security(s31, tf, screener())
[c32,v32]=request.security(s32, tf, screener())
[c33,v33]=request.security(s33, tf, screener())
[c34,v34]=request.security(s34, tf, screener())
[c35,v35]=request.security(s35, tf, screener())
[c36,v36]=request.security(s36, tf, screener())


//////////////////////////////////////////////////////////////////////////////////
//Assiging BUY Label to each stock
buy_label1='---BUY SIGNAL---\n'

buy_label1:= c1?buy_label1+str.tostring(s1)+'\n': buy_label1
buy_label1:= c2?buy_label1+str.tostring(s2)+'\n': buy_label1
buy_label1:= c3?buy_label1+str.tostring(s3)+'\n': buy_label1
buy_label1:= c4?buy_label1+str.tostring(s4)+'\n': buy_label1
buy_label1:= c5?buy_label1+str.tostring(s5)+'\n': buy_label1
buy_label1:= c6?buy_label1+str.tostring(s6)+'\n': buy_label1
buy_label1:= c7?buy_label1+str.tostring(s7)+'\n': buy_label1
buy_label1:= c8?buy_label1+str.tostring(s8)+'\n': buy_label1
buy_label1:= c9?buy_label1+str.tostring(s9)+'\n': buy_label1
buy_label1:= c10?buy_label1+str.tostring(s10)+'\n': buy_label1
buy_label1:= c11?buy_label1+str.tostring(s11)+'\n': buy_label1
buy_label1:= c12?buy_label1+str.tostring(s12)+'\n': buy_label1
buy_label1:= c13?buy_label1+str.tostring(s13)+'\n': buy_label1
buy_label1:= c14?buy_label1+str.tostring(s14)+'\n': buy_label1
buy_label1:= c15?buy_label1+str.tostring(s15)+'\n': buy_label1
buy_label1:= c16?buy_label1+str.tostring(s16)+'\n': buy_label1
buy_label1:= c17?buy_label1+str.tostring(s17)+'\n': buy_label1
buy_label1:= c18?buy_label1+str.tostring(s18)+'\n': buy_label1
buy_label1:= c19?buy_label1+str.tostring(s19)+'\n': buy_label1
buy_label1:= c20?buy_label1+str.tostring(s20)+'\n': buy_label1
buy_label1:= c21?buy_label1+str.tostring(s21)+'\n': buy_label1
buy_label1:= c22?buy_label1+str.tostring(s22)+'\n': buy_label1
buy_label1:= c23?buy_label1+str.tostring(s23)+'\n': buy_label1
buy_label1:= c24?buy_label1+str.tostring(s24)+'\n': buy_label1
buy_label1:= c25?buy_label1+str.tostring(s25)+'\n': buy_label1
buy_label1:= c26?buy_label1+str.tostring(s26)+'\n': buy_label1
buy_label1:= c27?buy_label1+str.tostring(s27)+'\n': buy_label1
buy_label1:= c28?buy_label1+str.tostring(s28)+'\n': buy_label1
buy_label1:= c29?buy_label1+str.tostring(s29)+'\n': buy_label1
buy_label1:= c30?buy_label1+str.tostring(s30)+'\n': buy_label1
buy_label1:= c31?buy_label1+str.tostring(s31)+'\n': buy_label1
buy_label1:= c32?buy_label1+str.tostring(s32)+'\n': buy_label1
buy_label1:= c33?buy_label1+str.tostring(s33)+'\n': buy_label1
buy_label1:= c34?buy_label1+str.tostring(s34)+'\n': buy_label1
buy_label1:= c35?buy_label1+str.tostring(s35)+'\n': buy_label1
buy_label1:= c36?buy_label1+str.tostring(s36)+'\n': buy_label1
// Plotting each Buy/Sell signal in teh label

lab_buy=label.new(bar_index+10, close, buy_label1, color=color.new(color.lime, 70), textcolor = color.white, style=label.style_label_up, yloc=yloc.price, textalign = text.align_left)

//Deleting Buy/Sell signal of the previous candle to remove clutter from the screen
label.delete(lab_buy[1])
//Assigining SELL signal to each stock if SELL siganl comes in for teh same
sell_label1='---SELL SIGNAL---\n'

sell_label1:= v1?sell_label1+str.tostring(s1)+'\n': sell_label1
sell_label1:= v2?sell_label1+str.tostring(s2)+'\n': sell_label1
sell_label1:= v3?sell_label1+str.tostring(s3)+'\n': sell_label1
sell_label1:= v4?sell_label1+str.tostring(s4)+'\n': sell_label1
sell_label1:= v5?sell_label1+str.tostring(s5)+'\n': sell_label1
sell_label1:= v6?sell_label1+str.tostring(s6)+'\n': sell_label1
sell_label1:= v7?sell_label1+str.tostring(s7)+'\n': sell_label1
sell_label1:= v8?sell_label1+str.tostring(s8)+'\n': sell_label1
sell_label1:= v9?sell_label1+str.tostring(s9)+'\n': sell_label1
sell_label1:= v10?sell_label1+str.tostring(s10)+'\n': sell_label1
sell_label1:= v11?sell_label1+str.tostring(s11)+'\n': sell_label1
sell_label1:= v12?sell_label1+str.tostring(s12)+'\n': sell_label1
sell_label1:= v13?sell_label1+str.tostring(s13)+'\n': sell_label1
sell_label1:= v14?sell_label1+str.tostring(s14)+'\n': sell_label1
sell_label1:= v15?sell_label1+str.tostring(s15)+'\n': sell_label1
sell_label1:= v16?sell_label1+str.tostring(s16)+'\n': sell_label1
sell_label1:= v17?sell_label1+str.tostring(s17)+'\n': sell_label1
sell_label1:= v18?sell_label1+str.tostring(s18)+'\n': sell_label1
sell_label1:= v19?sell_label1+str.tostring(s19)+'\n': sell_label1
sell_label1:= v20?sell_label1+str.tostring(s20)+'\n': sell_label1
sell_label1:= v21?sell_label1+str.tostring(s21)+'\n': sell_label1
sell_label1:= v22?sell_label1+str.tostring(s22)+'\n': sell_label1
sell_label1:= v23?sell_label1+str.tostring(s23)+'\n': sell_label1
sell_label1:= v24?sell_label1+str.tostring(s24)+'\n': sell_label1
sell_label1:= v25?sell_label1+str.tostring(s25)+'\n': sell_label1
sell_label1:= v26?sell_label1+str.tostring(s26)+'\n': sell_label1
sell_label1:= v27?sell_label1+str.tostring(s27)+'\n': sell_label1
sell_label1:= v28?sell_label1+str.tostring(s28)+'\n': sell_label1
sell_label1:= v29?sell_label1+str.tostring(s29)+'\n': sell_label1
sell_label1:= v30?sell_label1+str.tostring(s30)+'\n': sell_label1
sell_label1:= v31?sell_label1+str.tostring(s31)+'\n': sell_label1
sell_label1:= v32?sell_label1+str.tostring(s32)+'\n': sell_label1
sell_label1:= v33?sell_label1+str.tostring(s33)+'\n': sell_label1
sell_label1:= v34?sell_label1+str.tostring(s34)+'\n': sell_label1
sell_label1:= v35?sell_label1+str.tostring(s35)+'\n': sell_label1
sell_label1:= v36?sell_label1+str.tostring(s36)+'\n': sell_label1

//plotting SELL signal in the label for stcks if it meets the SELL criteria set above
lab_sell=label.new(bar_index+15, close, sell_label1, color=color.new(color.red, 70), textcolor = color.white, style=label.style_label_down, yloc=yloc.price, textalign = text.align_left)
//Deleting signal of previos candle to remove too many of them showing on screen
label.delete(lab_sell[1])



