BankNifty Scripts
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nanujogi


// More info on cRSI visit below URL
// cRSI code thanks flies to RozaniGhani-RG
// https://in.tradingview.com/script/2zKoLmRP-Cyclic-RSI-High-Low-With-Noise-Filter/

// In this script cRSI > 70 Focus Sell & < 30 Focus Buy. 

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

len  = input.int( 50, minval=1, title="Length", tooltip = "Change as per your choice" )

var value_cRSI = array.new_float(10)
var _myRows = 0
var _myCols = 0

// User Table Position is now flexible

string  tableYposInput = input.string("top", "Table position", inline = "11", options = ["top", "middle", "bottom"])
string  tableXposInput = input.string("right", "", inline = "11", options = ["left", "center", "right"])


///// COLORS

SCRIPT_NAME = color.rgb(8,196,236, 30)
HEMA_COLOR = color.rgb(227,255,20)
NEON_GREEN = color.rgb(57,255,20)
HEX_RED = #FF1818

////// EMA calculation for 1 Hour 1 Day and 1 Week

var _DEMA = 0
var _HEMA = 1
var _WEMA = 2

var IDX_CLOSE = 3


// Initialize an empty array to store variables

var global = array.new_float(52)

var previous_close = array.new_float(10)

var table emaTable = table.new(tableYposInput + "_" + tableXposInput, columns=7, rows=15, border_color = color.white, frame_color = color.white, frame_width = 1, border_width = 1)

var script_name = " "


f_display_table_cell(_txt, _col0, _col1, _col2, _col3, _row, ix_dema, ix_hema, ix_wema, ix_close) => 


    if barstate.islast
        // We only populate the table on the last bar.
        table.cell(emaTable, 0, 0, "  Script Name  ", text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Daily EMA")
        table.cell(emaTable, 1, 0, "  DEMA  " + "(" + str.format("{0, number}", len)+")", text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Daily EMA")
        table.cell(emaTable, 2, 0, "  HEMA  " + "(" + str.format("{0, number}", len)+")", text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Hourly EMA")
        table.cell(emaTable, 3, 0, "  WEMA  " + "(" + str.format("{0, number}", len)+")", text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Weekly EMA")
        
        table.cell(emaTable, 4, 0, "  CMP  ", text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Weekly EMA")

        table.cell(emaTable, 5, 0, "  Chg  ", text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Weekly EMA")
 
        table.cell(emaTable, 6, 0, "  cRSI  ", text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="")
 

        // #00A2ED
        table.cell(emaTable, _col0, _row, _txt, text_color = color.white, text_halign = text.align_left, text_size = size.normal)
        

        // table.cell(emaTable, _col1, _row, str.format("{0, number, #.##}", math.round(array.get(global, ix_dema))) + _display, text_color = #00A2ED, text_halign = text.align_center, text_size = size.normal)
        
        if array.get(global, ix_close) > array.get(global, ix_dema)
            _display = " ▲"
            table.cell(emaTable, _col1, _row, str.format("{0, number, #.##}", math.round(array.get(global, ix_dema))) + _display, text_color = #00A2ED, text_halign = text.align_right, text_size = size.normal)
        else
            _display = " ▼"
            table.cell(emaTable, _col1, _row, str.format("{0, number, #.##}", math.round(array.get(global, ix_dema))) + _display, text_color = #00A2ED, text_halign = text.align_right, text_size = size.normal)
        
        
        if array.get(global, ix_close) > array.get(global, ix_hema)
            _display = " ▲"
            table.cell(emaTable, _col2, _row, str.format("{0, number, #.##}", math.round(array.get(global, ix_hema))) + _display, text_color = HEMA_COLOR, text_halign = text.align_right, text_size = size.normal)
        else
            _display = " ▼"
            table.cell(emaTable, _col2, _row, str.format("{0, number, #.##}", math.round(array.get(global, ix_hema))) + _display, text_color = HEMA_COLOR, text_halign = text.align_right, text_size = size.normal)

        // table.cell(emaTable, _col2, _row, str.format("{0, number, #.##}", math.round(array.get(global, ix_hema))), text_color = color.lime, text_halign = text.align_center, text_size = size.normal)
        
        
        if array.get(global, ix_close) > array.get(global, ix_wema)
            _display = " ▲"
            table.cell(emaTable, _col3, _row, str.format("{0, number, #.##}", math.round(array.get(global, ix_wema))) + _display, text_color = color.orange, text_halign = text.align_right, text_size = size.normal)
        else
            _display = " ▼"
            table.cell(emaTable, _col3, _row, str.format("{0, number, #.##}", math.round(array.get(global, ix_wema))) + _display, text_color = color.orange, text_halign = text.align_right, text_size = size.normal)


        table.cell(emaTable, 4, _row, str.format("{0, number, #.##}", array.get(global, ix_close)), text_color = array.get(global, ix_close) > array.get(previous_close, 0) ? NEON_GREEN: HEX_RED, text_halign = text.align_right, text_size = size.normal)

        var percentage_calculation = ( array.get(global, ix_close) * 100 / array.get(previous_close, 0) ) - 100
        
        table.cell(emaTable, 5, _row, str.format("{0, number, #.##}", math.round(percentage_calculation, 2)) + "%", text_color = array.get(global, ix_close) > array.get(previous_close, 0) ? NEON_GREEN : HEX_RED, text_halign = text.align_right, text_size = size.normal)

        
        if array.get(value_cRSI, 0) > 70
        
            table.cell(emaTable, 6, _row, str.format("{0, number, #.##}", array.get(value_cRSI, 0) ) + " Focus Sell", text_color = HEMA_COLOR, text_halign = text.align_right, text_size = size.normal)

        else if array.get(value_cRSI, 0) < 30
            table.cell(emaTable, 6, _row, str.format("{0, number, #.##}", array.get(value_cRSI, 0) ) + " Focus Buy", text_color = HEMA_COLOR, text_halign = text.align_right, text_size = size.normal)
        
        else
            table.cell(emaTable, 6, _row, str.format("{0, number, #.##}", array.get(value_cRSI, 0) ), text_color = color.fuchsia, text_halign = text.align_right, text_size = size.normal)

f_get_EMA(_ticker, idx_dema, idx_hema, idx_wema, idx_close)=>


/// cRSI

    src = close
    
    domcycle = input.int(20, minval=10, title="Dominant Cycle Length")
    crsi = 0.0
    
    cyclelen = domcycle / 2
    vibration = 10
    
    leveling = 10.0
    
    torque = 2.0 / (vibration + 1)
    phasingLag = (vibration - 1) / 2.0
    
    cyclicmemory = domcycle * 2

//// cRSI ENDS

    [_close, p_close, _dema, up, down] = request.security(_ticker, "D", [close, close[1], ta.ema(close, len), ta.rma(math.max(ta.change(src), 0), cyclelen),ta.rma(-math.min(ta.change(src), 0), cyclelen) ])

    rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
    crsi := torque * (2 * rsi - rsi[phasingLag]) + (1 - torque) * nz(crsi[1])

    _hema = request.security(_ticker, "60", ta.ema(close, len))
    _wema = request.security(_ticker, "W", ta.ema(close, len))
    

    array.set(global, idx_dema, math.round(_dema))
    array.set(global, idx_hema, math.round(_hema))
    array.set(global, idx_wema, math.round(_wema))
    
    array.set(global, idx_close, _close)
    
    // previous_close array just sotres the previous close so that we can display CMP in green or red.
    // is the current price above previous day close then green else red.
    
    array.set(previous_close, 0, p_close)
    
    array.set(value_cRSI, 0, crsi)
    

// f_get_EMA(_ticker, idx_dema, idx_hema, idx_wema, idx_close)

//f_display_table_cell(_txt, _col0, _col1, _col2, _col3, _row, ix_dema, ix_hema, ix_wema, ix_close) => 
//f_display_table_cell("HDFCBANK", 
// 0, 1, 2, 3, 3, 0+4*2, 1+4*2, 2+4*2, 3+4*2)
// 0 = DEMA 1 = HEMA 2 = WEMA 3 = CMP, 3 = Rows, 
// 0+4*2 = index of dema in array for display in table cell
// 1+4*2 = index of hema in array for display in table cell
// 2+4*2 = index of wema in array for display in table cell
// 3+4*2 = index of CMP current market price for display in table cell

// we keepng multiplying with DEMA

// 0

f_get_EMA("NSE:HDFCBANK", _DEMA, _HEMA, _WEMA, IDX_CLOSE)
f_display_table_cell(syminfo.ticker("NSE:HDFCBANK"), 0, 1, 2, 3, 1, 0, 1, 2, 3)

_myCols := 1
_myRows := 1
//1
f_get_EMA("NSE:ICICIBANK", _DEMA+4, _HEMA+4, _WEMA+4, IDX_CLOSE+4)
f_display_table_cell("ICICI Bank", 0, 1, 2, 3, 2, 0+4, 1+4, 2+4, 3+4)

//2
f_get_EMA("NSE:SBIN", _DEMA+4*2, _HEMA+4*2, _WEMA+4*2, IDX_CLOSE+4*2)
f_display_table_cell("SBIN", 0, 1, 2, 3, 3, 0+4*2, 1+4*2, 2+4*2, 3+4*2)

// // 3
f_get_EMA("NSE:AXISBANK", _DEMA+4*3, _HEMA+4*3, _WEMA+4*3, IDX_CLOSE+4*3)
f_display_table_cell("AXISBANK", 0, 1, 2, 3, 4, 0+4*3, 1+4*3, 2+4*3, 3+4*3)

// 4
f_get_EMA("NSE:KOTAKBANK", _DEMA+4*4, _HEMA+4*4, _WEMA+4*4, IDX_CLOSE+4*4)
f_display_table_cell("KOTAKBANK", 0, 1, 2, 3, 5, 0+4*4, 1+4*4, 2+4*4, 3+4*4)

// 5
f_get_EMA("NSE:INDUSINDBK", _DEMA+4*5, _HEMA+4*5, _WEMA+4*5, IDX_CLOSE+4*5)
f_display_table_cell("INDUSINDBK", 0, 1, 2, 3, 6, 0+4*5, 1+4*5, 2+4*5, 3+4*5)

// 6
f_get_EMA("NSE:BANKBARODA", _DEMA+4*6, _HEMA+4*6, _WEMA+4*6, IDX_CLOSE+4*6)
f_display_table_cell("Bank of Baroda", 0, 1, 2, 3, 7, 0+4*6, 1+4*6, 2+4*6, 3+4*6)

// 7
f_get_EMA("NSE:BANDHANBNK", _DEMA+4*7, _HEMA+4*7, _WEMA+4*7, IDX_CLOSE+4*7)
f_display_table_cell("Bandhan Bank", 0, 1, 2, 3, 8, 0+4*7, 1+4*7, 2+4*7, 3+4*7)

// 8
f_get_EMA("NSE:IDFCFIRSTB", _DEMA+4*8, _HEMA+4*8, _WEMA+4*8, IDX_CLOSE+4*8)
f_display_table_cell("IDFC First Bank", 0, 1, 2, 3, 9, 0+4*8, 1+4*8, 2+4*8,  3+4*8)

//9
f_get_EMA("NSE:AUBANK", _DEMA+4*9, _HEMA+4*9, _WEMA+4*9, IDX_CLOSE+4*9)
f_display_table_cell("AU Bank", 0, 1, 2, 3, 10, 0+4*9, 1+4*9, 2+4*9,  3+4*9)

// 10
f_get_EMA("NSE:FEDERALBNK", _DEMA+4*10, _HEMA+4*10, _WEMA+4*10, IDX_CLOSE+4*10)
f_display_table_cell("Federal Bank", 0, 1, 2, 3, 11, 0+4*10, 1+4*10, 2+4*10,  3+4*10)

// 11
f_get_EMA("NSE:PNB", _DEMA+4*11, _HEMA+4*11, _WEMA+4*11, IDX_CLOSE+4*11)
f_display_table_cell("PNB Bank", 0, 1, 2, 3, 12, 0+4*11, 1+4*11, 2+4*11,  3+4*11)

// 12
f_get_EMA("NSE:BANKNIFTY", _DEMA+4*12, _HEMA+4*12, _WEMA+4*12, IDX_CLOSE+4*12)
f_display_table_cell("Bank Nifty", 0, 1, 2, 3, 13, 0+4*12, 1+4*12, 2+4*12,  3+4*12)


