SSR Advance Psychological Level V1
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Babuliray
//@version=5

indicator(title="SSR Advance Psychological Level  for Nifty and BankNifty V1", overlay=true)

string tfInput = input.timeframe("D", "Timeframe")

// Create variables
var D_Open = 0.0
var PSY = 0.0
var HiPSY = float(na)
var LoPSY = float(na)
var hipsy_X = float(na)
var hipsy_Y = float(na)
var lopsy_X = float(na)
var lopsy_Y = float(na)
var box hipsyBox = na
var box lopsyBox = na
var line hiline = na
var line loline = na
var bool newDayStart = na

//if syminfo.type =="index"
        // See if a new calendar day started on the intra-day time frame
newDayStart := dayofmonth != dayofmonth[1]   and 
     timeframe.isintraday 

// If a new day starts, set the set the Psychological labels from opening price.
if newDayStart
    D_Open := open
    PSY := math.round(D_Open/1000)*1000

    if (PSY > D_Open)
        HiPSY := PSY
        LoPSY := PSY - 500
    
    else
        HiPSY := PSY +500
        LoPSY := PSY
        
        hipsy_X := HiPSY + 50
        hipsy_Y := HiPSY - 50
        lopsy_X := LoPSY + 50
        lopsy_Y := LoPSY - 50
else
    // if level breaks either color change for violation for psychocological levels
    if close > hipsy_X or close < lopsy_Y
        
        if close > hipsy_X
            box.set_bgcolor(hipsyBox, color.new(color.orange,  50))
            box.set_bgcolor(lopsyBox, color.new(color.lime, 50))
        if close < lopsy_Y
            box.set_bgcolor(lopsyBox, color.new(color.lime,  50))
            box.set_bgcolor(hipsyBox, color.new(color.orange,  50))
        
        PSY := math.round(close/1000)*1000
        if PSY > close
        
            HiPSY := PSY
            LoPSY := PSY - 500
        else
            
            HiPSY := PSY +500
            LoPSY := PSY
        
        hipsy_X := HiPSY + 50
        hipsy_Y := HiPSY - 50
        lopsy_X := LoPSY + 50
        lopsy_Y := LoPSY - 50

        hipsyBox := box.new(bar_index - 1, hipsy_X, bar_index, hipsy_Y, border_color = color.new(color.red,85), bgcolor = color.new(color.red, 25 ))
        lopsyBox := box.new(bar_index - 1, lopsy_X, bar_index, lopsy_Y, border_color = color.new(color.green,85), bgcolor = color.new(color.green,25))
        //hiline := line.new(bar_index , D_Open, bar_index, D_Open, color = color.new(color.green, 30), width = 10)
        //loline := line.new(bar_index , BoxLev, bar_index, BoxLev, color = color.new(color.green, 30), width = Lwidth)
    // color changing during price enter/reverse into/from Psychlogical level    
    if close > hipsy_Y and close < HiPSY
        box.set_bgcolor(hipsyBox, color.new(color.red,  math.round(close)- hipsy_Y +25))
        
    if close < hipsy_X and close > HiPSY
        box.set_bgcolor(hipsyBox, color.new(color.green,  hipsy_X - math.round(close)+25))
        
    if close < lopsy_X and close > LoPSY
        box.set_bgcolor(lopsyBox, color.new(color.green, lopsy_X - math.round(close) +25 ))
        
    if close > lopsy_Y and close < LoPSY
        box.set_bgcolor(lopsyBox, color.new(color.red,  math.round(close) - lopsy_Y +25))
    
    if close < hipsy_Y
        box.set_bgcolor(hipsyBox, color.new(color.red, 25 ))
        
    if close > lopsy_X
        box.set_bgcolor(lopsyBox, color.new(color.green,25))
        
// When a new day start, create both phychological levels.
// Else, during the day, update that day's box.
bool newTF = ta.change(time(tfInput))
if newTF     
    if newDayStart
        hipsyBox := box.new(bar_index - 1, hipsy_X, bar_index, hipsy_Y, border_color = color.new(color.red, 85) , bgcolor = color.new(color.red, 25 ))
        lopsyBox := box.new(bar_index - 1, lopsy_X, bar_index, lopsy_Y, border_color = color.new(color.green, 85), bgcolor = color.new(color.green,25))
        //hiline := line.new(bar_index , D_Open, bar_index, D_Open, color = color.new(color.green, 30), width = 10)
else
    box.set_right(hipsyBox, bar_index)
    box.set_right(lopsyBox, bar_index)
    //line.set_x2(hiline, bar_index)
    //label.new(bar_index, low, "OK")