Day Clue
/// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Vignesh_vish

//@version=5
indicator('Day Clue', 'Day Clue', overlay=true,max_labels_count=500)

//input
var startTime = time("D")
timeDiff= (time("D")-startTime)/86400000
noofDaysback=input.int(15,"No. of Days Back:",minval=0,maxval=50,tooltip="How many Number of Days From the Past You Want to Plot Day Clue Indicator, if is Set to Zero'0' then Day Clue Indicator plot for current Day Only")
ema20check=input.bool(true,"Show/ Hide EMA-20",group="EMA")
ema20color=input.color(color.new(color.gray,50),"Color of EMA-20",group="EMA")
ema200check=input.bool(true,"Show/ Hide EMA-200",group="EMA")
ema200color=input.color(color.new(color.gray,50),"Color of EMA-200",group="EMA")

PDHandPDLcheck= input.bool(true,"Show/ Hide PDH & PDL",group="PDR")
PDHandPDLcolor= input.color(color.new(#b71c1c,0),"Color of PDH & PDL",group="PDR")

CPRcheck=input.bool(true,"Show/ Hide CPR",group="CPR")
CCPRColor=input.color(color.new(#e84dc6,60),"Color of Central Pivot",group="CPR")
TBCPRColor=input.color(color.new(#4d83e8,60),"Color of Top & Bottom CPR",group="CPR")
S1color=input.color(color.new(#FFA500,60),"Color of S1",group="CPR")
R1color=input.color(color.new(#FFA500,60),"Color of R1",group="CPR")

barCursorCheck=input.bool(true,"Show/ Hide Bar-Cursor",group="Bar Coursor")
barCursorcolor=input.color(color.black,"Bar-Cursor Color",group="Bar Coursor")

IBRcheck=input.bool(true,"Show/ Hide IBR",group="IBR")
IBlabelColor=input.color(color.black,"IB Label Color",group="IBR")
IBlineColor=input.color(color.new(color.black,70),"IB Line Color",group="IBR")

dataLabelCheck=input.bool(true,"Show/ Hide Data-Label",group="Data Label")
dataLabelColor=input.color(color.black,"Data Label Color",group="Data Label")

//dayNameCheck=input.bool(false,"Show/ Hide Day-Name",group="Day Name")



secu(tikr, res, source) =>
    request.security(tikr, res, source, lookahead=barmerge.lookahead_on)

startPoltDate=timestamp(year(timenow),month(timenow),dayofmonth(timenow)-timeDiff,09, 15, 00)
var string Ibtimes= na
var ibRange = 0.0
var ibhigh =0.0
var iblow =0.0
var sessionBarCount = 0
var pdayhigh=0.0
var pdaylow=0.0
var pdayclose=0.0
var px1=0
var px2=0
var cprx1=0
var nexttradingday =1
tomorrow=0
var holydayfor = '◉'
mkt_start_hh=09
mkt_start_mm=15
mkt_end_hh=15
mkt_end_mm=25
ib_session=""
if (syminfo.prefix=="NSE" or syminfo.prefix=="BSE_DLY")
    mkt_start_hh:=09
    mkt_start_mm:=15
    mkt_end_hh:=15
    mkt_end_mm:=25
    ib_session:="0915-1015"
else if syminfo.prefix=="MCX"
    mkt_start_mm:=00
    mkt_end_hh:=23
    ib_session:="0900-1000"

ib = na(time('D', ib_session + ':23456')) == false
drange() =>
    math.round(secu(syminfo.ticker, 'D', high) - secu(syminfo.ticker, 'D', low),0)
if ib
    ibRange := math.round(secu(syminfo.ticker, '60', high) - secu(syminfo.ticker, '60', low),0)
    ibhigh := secu(syminfo.ticker, '60', high)
    iblow :=secu(syminfo.ticker, '60', low)
nl = '\n'
Ibtimes:=math.round((drange()/ibRange),1)>=2?" ("+str.tostring(int((drange()/ibRange)))+"-IB)":na
 

//1000->1K 100000->1L Convertor
numberShort(number) =>
    len = str.length(str.tostring(number))
    if len >= 1  //and len <= 5
        str.tostring(math.round(number / 1000, 0)) + 'K'
vix() =>
    str.tostring(math.round(secu("INDIAVIX","D",close),2))+" ("+str.tostring(math.round(((secu("INDIAVIX","D",close)-secu("INDIAVIX","D",close[1]))/secu("INDIAVIX","D",close[1]))*100,2))+"%)"
TrRMA(pre=14)=>
    tRange=na(secu(syminfo.ticker,"D",high[1]))? secu(syminfo.ticker,"D",high)-secu(syminfo.ticker,"D",low): math.max(secu(syminfo.ticker,"D",high)-secu(syminfo.ticker,"D",low),math.abs(secu(syminfo.ticker,"D",high)-secu(syminfo.ticker,"D",close[1])),math.abs(secu(syminfo.ticker,"D",low)-secu(syminfo.ticker,"D",close[1])))
    ta.rma(tRange,pre)
_atr() =>
    math.round(ta.atr(20), 0)
_atravg() =>
    math.round(ta.atr(20) / 6, 0)

dayVolume(tf) =>
    dayVol = if syminfo.ticker == 'NIFTY'
        secu('NIFTY1!', tf, volume)
    else if syminfo.ticker == 'BANKNIFTY'
        secu('BANKNIFTY1!', tf, volume)
    else
        secu(syminfo.ticker, tf, volume)
    numberShort(dayVol)

futspotpricediff = if syminfo.ticker == 'NIFTY' or syminfo.ticker == 'NIFTY1!'
    secu('NIFTY1!', timeframe.period, close) - secu('NIFTY', timeframe.period, close) 
else if syminfo.ticker == 'BANKNIFTY' or syminfo.ticker == 'BANKNIFTY1!'
    secu('BANKNIFTY1!', timeframe.period, close) - secu('BANKNIFTY', timeframe.period, close) 
//combaine Text and and formatting 
strHelper(prefix, stat) =>
    res = str.tostring(stat)
    nl + prefix + ': ' + res
dynamicText() =>
    strHelper('IBR', str.tostring(ibRange)+Ibtimes) + strHelper('Day Range', drange()) + strHelper('Day Volume', dayVolume('D')) + strHelper('ATR', _atr())+", "+str.tostring(int(TrRMA())) + strHelper('VIX', vix()) + strHelper('Fut Delta', futspotpricediff)

holiday() => 
    //
    disc= switch timestamp(year(time), month(time), dayofmonth(time)+nexttradingday)
        timestamp(2022,01,26) => "Republic Day"
        timestamp(2022,03,01) => "Mahashivratri"
        timestamp(2022,03,18) => "Holi"
        timestamp(2022,04,14) => "Dr. Baba Saheb Ambedkar Jayanti/ Mahavir Jayanti"
        timestamp(2022,04,15) => "Good Friday"
        timestamp(2022,05,03) => "Id-Ul-Fitr (Ramzan ID)"
        timestamp(2022,08,09) => "Moharram"
        timestamp(2022,08,15) => "Independence Day"
        timestamp(2022,08,31) => "Ganesh Chaturthi"
        timestamp(2022,10,05) => "Dussehra"
        timestamp(2022,10,24) => "Diwali-Laxmi Pujan*"
        timestamp(2022,10,26) => "Diwali-Balipratipada"
        timestamp(2022,11,08) => "Gurunanak Jayanti"
        =>'◉'
    disc

_label(barInx) =>
    x1=timestamp(year(time), month(time), dayofmonth(time), mkt_start_hh, mkt_start_mm, 00)
    x2=timestamp(year(time), month(time), dayofmonth(time), mkt_end_hh, mkt_end_mm, 00)
    if dataLabelCheck
        lbl = label.new(x1, secu(syminfo.ticker, 'D', low) - (syminfo.mintick * 5), dynamicText(),xloc=xloc.bar_time, style=label.style_label_upper_left, textalign=text.align_left, color=color.new(#d0cec2,100), textcolor=dataLabelColor)
        if ta.barssince(ta.change(time("D")))>=1 or dayofmonth(time)==dayofmonth(startPoltDate)
            label.delete(lbl[1])
    if IBRcheck
        ibh=label.new(bar_index-(barInx-1),ibhigh,text="IBH ▶",style=label.style_label_right,textalign=text.align_right,color=color.new(#d0cec2,100),textcolor=IBlabelColor)
        ibl=label.new(bar_index-(barInx-1),iblow,text="IBL ▶",style=label.style_label_right,textalign=text.align_right,color=color.new(#d0cec2,100),textcolor=IBlabelColor)

        linibh=line.new(x1,ibhigh,x2,ibhigh,xloc.bar_time,color=IBlineColor,style= line.style_solid)
        linibl=line.new(x1,iblow,x2,iblow,xloc.bar_time,color=IBlineColor,style= line.style_solid)
        if ta.barssince(ta.change(time("D")))>=1 or dayofmonth(time)==dayofmonth(startPoltDate)
            label.delete(ibh[1])
            label.delete(ibl[1])
            line.delete(linibh[1])
            line.delete(linibl[1])
    
    day= switch dayofweek(timenow)
        1 => "Sunday"
        2 => "Monday"
        3 => "Tuesday"
        4 => "Wednesday"
        5 => "Thursday"
        6 => "Friday"
        7 => "Saturday"
    holydaycolor= dayofweek(timenow) == 1 or dayofweek(timenow) == 7 or holydayfor!="◉"?color.new(color.red,50):color.new(color.teal,50)
    holydaysymcolor=holydayfor!="◉"?color.new(color.red,0):color.new(color.teal,0)
    //if dayNameCheck
    //    daytable=table.new(position.bottom_right,columns=2,rows=1,border_width=0)
    //    if barstate.islast
    //        table.cell(table_id=daytable,column=0,row=0,text=day,bgcolor=color.new(color.teal,50))
    //        table.cell(table_id=daytable,column=1,row=0,text=holydayfor,bgcolor=holydaycolor)
    //        table.cell_set_text_color(table_id=daytable, column=1, row=0, text_color=holydaysymcolor)
   
    
loopno=1 
if ta.change(time("D"))
    sessionBarCount:=1
    nexttradingday:=1
    if dayofweek == 6
        nexttradingday:= nexttradingday+2
    while holiday()!="◉" or dayofweek(timestamp(year(time), month(time), dayofmonth(time)+nexttradingday))== 7  or dayofweek(timestamp(year(time), month(time), dayofmonth(time)+nexttradingday))== 1
        holy=holiday()
        if holy != "◉" and loopno == 1
            holydayfor:= holy
            loopno:=loopno+1
            nexttradingday:=nexttradingday+1
        else if holy != "◉" and loopno > 1
            holydayfor:=holydayfor+", "+holy
            loopno:=loopno+1
            nexttradingday:=nexttradingday+1
        else
            loopno:=loopno+1
            nexttradingday:=nexttradingday+1


    
if (ta.barssince(ta.change(time("D")))>=1 or ta.change(time("D"))) and (timeframe.period== "30" or timeframe.period == "5" or timeframe.period=="15" or timeframe.period=="1") and (syminfo.prefix=="NSE" or syminfo.prefix=="BSE_DLY" or syminfo.prefix=="MCX" ) and time >=  startPoltDate
    sessionBarCount += 1
    _label(sessionBarCount - 2)
//plot line for next day PDH, PDL and daily CPR with R1 and S1
mins=timeframe.period=="15"?15:timeframe.period=="1"?29:25
    
if time>=timestamp(year(time), month(time), dayofmonth(time), mkt_end_hh,mins, 00) and (timeframe.period == "5" or timeframe.period=="15" or timeframe.period=="1") and (syminfo.prefix=="NSE" or syminfo.prefix=="BSE_DLY" or syminfo.prefix=="MCX")
    
    px1:=timestamp(year(time), month(time), dayofmonth(time), mkt_start_hh, mkt_start_mm, 00)//x1 position for PDH and PDL
    cprx1:=timestamp(year(time), month(time), dayofmonth(time)+nexttradingday, mkt_start_hh, mkt_start_mm, 00)//x1 position for CPR
    px2:=timestamp(year(time), month(time), dayofmonth(time)+nexttradingday, mkt_end_hh, mins, 00)//x2 position for CPR,PDH and PDL
    pdayhigh:=secu(syminfo.ticker, 'D', high)
    pdaylow:=secu(syminfo.ticker, 'D', low)
    pdayclose:=secu(syminfo.ticker, 'D', close)
    Ccpr=(pdayhigh+pdaylow+pdayclose)/3
    Bcpr=(pdayhigh+pdaylow)/2
    Tcpr=(Ccpr-Bcpr)+Ccpr
    r1=(2*Ccpr)-pdaylow
    s1=(2*Ccpr)-pdayhigh
    if PDHandPDLcheck
        pdh=line.new(px1,pdayhigh,px2,pdayhigh,xloc.bar_time,color=PDHandPDLcolor,style= line.style_dotted,width=2)
        line.delete(pdh[1])
        pdl=line.new(px1,pdaylow,px2,pdaylow,xloc.bar_time,color=PDHandPDLcolor,style= line.style_dotted,width=2)
        line.delete(pdl[1])
    if CPRcheck    
        CcprLine=line.new(cprx1,Ccpr,px2,Ccpr,xloc.bar_time,color=CCPRColor,style= line.style_dotted,width=3)
        line.delete(CcprLine[1])
        BcprLine=line.new(cprx1,Bcpr,px2,Bcpr,xloc.bar_time,color=TBCPRColor,style= line.style_dotted,width=3)
        line.delete(BcprLine[1])
        TcprLine=line.new(cprx1,Tcpr,px2,Tcpr,xloc.bar_time,color=TBCPRColor,style= line.style_dotted,width=3)
        line.delete(TcprLine[1])
        s1Line=line.new(cprx1,s1,px2,s1,xloc.bar_time,color=S1color,style= line.style_dotted,width=3)
        line.delete(s1Line[1])
        r1Line=line.new(cprx1,r1,px2,r1,xloc.bar_time,color=R1color,style= line.style_dotted,width=3)
        line.delete(r1Line[1])
//current bar price and volume label
if barCursorCheck
    sidelbl = label.new(time + 5, close,xloc=xloc.bar_time, text=str.tostring(close) + nl + str.tostring(dayVolume(timeframe.period)), style=label.style_label_left, textalign=text.align_left, color=color.new(#d0cec2,100), textcolor=barCursorcolor)
    label.delete(sidelbl[1])

    
intradaytrendindicator=if na(volume)==false and timeframe.isintraday
    ta.vwap(hlc3)

else
    ta.ema(close,20)
    

plot(ema20check?intradaytrendindicator:na,"EMA-20", na(volume)==false?color.orange:ema20color)
plot(ema200check?ta.ema(close,200):na,"EMA-200",color=ema200color,linewidth=2)
