SGX Nifty OHLC for Nifty 50 Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arun_K_Bhaskar

//@version=5

indicator(title="SGX Nifty OHLC for Nifty 50 Index", shorttitle="SGX Nifty OHLC", overlay=true)

///////////////////////////////////////////////////////// Menu

symbol = input.symbol(defval="NSEIX:NIFTY1!", title='SGX NIFTY Symbol', tooltip='', inline='', group='') //SGX Nifty Futures

gpSGX = "SGX Nifty"
show_sgx_h = input.bool(true, title="High", group=gpSGX, inline="01")
show_sgx_l = input.bool(true, title="Low", group=gpSGX, inline="01")
show_sgx_o = input.bool(false, title="Open", group=gpSGX, inline="01")
show_sgx_c = input.bool(true, title="Close", group=gpSGX, inline="01")

gpNif = "NIFTY 50"
show_nifty_h = input.bool(false, title="High", group=gpNif, inline="01")
show_nifty_l = input.bool(false, title="Low", group=gpNif, inline="01")
show_nifty_o = input.bool(false, title="Open", group=gpNif, inline="01")
show_nifty_c = input.bool(false, title="Close", group=gpNif, inline="01")
i_past_levels = input.int(1, title="Previous", minval=0, group=gpNif, inline="02")

gpCol = "Colors"
o_col = input.color(#2962FF, title="Open", group=gpCol, inline="01")
c_col = input.color(#F59302, title="Close", group=gpCol, inline="01")
h_col = input.color(#EF5350, title="High", group=gpCol, inline="01")
l_col = input.color(#26A69A, title="Low", group=gpCol, inline="01")

gpSet = "Settings"
show_labels = input.bool(true, title="Labels", group=gpSet, inline="01")
show_warn = input.bool(true, title="Warn Label", group=gpSet, inline="01")
show_last = input.bool(true, title="Hide Historical", group=gpSet, inline="02")
islast = show_last ? request.security(syminfo.tickerid, "D", barstate.islast, lookahead=barmerge.lookahead_on) : true

///////////////////////////////////////////////////////// Warning
warn_txt = "Use 1 min timeframe for accurate OHLC.\n In other timeframes OHLC will have negligible difference, it won't be huge.\nThis indicator will appear only on NIFTY Index and Futures chart.\nTo hide this warning label go to the indicator Menu."
warn_label = label(na)
if (timeframe.period != "1") and show_warn
    warn_label := label.new(bar_index-90, close, warn_txt, xloc.bar_index, textcolor=color.white, color=#EF5350, style=label.style_label_center, size=size.large)
    label.delete(warn_label[1])

///////////////////////////////////////////////////////// SGX Nifty OHLC

open_time = time(timeframe.period, "1529-1530")
hlc_time = time(timeframe.period, "0915-0916")

// Open
_open = request.security(symbol, "D", open, barmerge.gaps_off, barmerge.lookahead_on)

// Close
_close = request.security(symbol, "1", close)
sgx_close = ta.valuewhen(hlc_time, _close, 0)

// High
_high = request.security(symbol, "1", ta.highest(high, 720))
sgx_high = ta.valuewhen(hlc_time, _high, 0)

// Low
_low = request.security(symbol, "1", ta.lowest(low, 720))
sgx_low = ta.valuewhen(hlc_time, _low, 0)

// Plot
nifty = syminfo.root == "NIFTY" or syminfo.root == "NIFTY1!" or syminfo.root == "NIFTY2!"
pivot_time = time(timeframe.period, "0916-1530")

plot(nifty and pivot_time and show_sgx_o and islast ? _open : na, "SGX Nifty Open", style=plot.style_linebr, color=o_col, linewidth=2)
plot(nifty and pivot_time and show_sgx_c and islast ? sgx_close : na, "SGX Nifty Close", style=plot.style_linebr, color=c_col, linewidth=2)
plot(nifty and pivot_time and show_sgx_h and islast ? sgx_high : na, "SGX Nifty High", style=plot.style_linebr, color=h_col, linewidth=2)
plot(nifty and pivot_time and show_sgx_l and islast ? sgx_low : na, "SGX Nifty Low", style=plot.style_linebr, color=l_col, linewidth=2)

// Today Start & End
day_start = timestamp(year, month, dayofmonth, 00, 00)
day_end = day_start + 86400000

// Lines & Labels
if show_sgx_o and nifty
    sgx_open_line = line.new(day_start, _open, day_end, _open, xloc.bar_time, color=o_col, width=2)
    line.delete(sgx_open_line[1])
    sgx_open_label = label.new(day_end, _open, xloc=xloc.bar_time, text=str.tostring(_open, "#.##") + "  (O SGX Nifty)", textcolor=o_col, color=color.new(color.white, 100), size=size.normal, style=label.style_label_left)
    label.delete(sgx_open_label[1])
    
if show_sgx_c and nifty
    sgx_close_line = line.new(day_start, sgx_close, day_end, sgx_close, xloc.bar_time, color=c_col, width=2)
    line.delete(sgx_close_line[1])
    sgx_close_label = label.new(day_end, sgx_close, xloc=xloc.bar_time, text=str.tostring(sgx_close, "#.##") + "  (C SGX Nifty)", textcolor=c_col, color=color.new(color.white, 100), size=size.normal, style=label.style_label_left)
    label.delete(sgx_close_label[1])
    
if show_sgx_h and nifty
    sgx_high_line = line.new(day_start, sgx_high, day_end, sgx_high, xloc.bar_time, color=h_col, width=2)
    line.delete(sgx_high_line[1])
    sgx_high_label = label.new(day_end, sgx_high, xloc=xloc.bar_time, text=str.tostring(sgx_high, "#.##") + "  (H SGX Nifty)", textcolor=h_col, color=color.new(color.white, 100), size=size.normal, style=label.style_label_left)
    label.delete(sgx_high_label[1])
    
if show_sgx_l and nifty
    sgx_low_line = line.new(day_start, sgx_low, day_end, sgx_low, xloc.bar_time, color=l_col, width=2)
    line.delete(sgx_low_line[1])
    sgx_low_label = label.new(day_end, sgx_low, xloc=xloc.bar_time, text=str.tostring(sgx_low, "#.##") + "  (L SGX Nifty)", textcolor=l_col, color=color.new(color.white, 100), size=size.normal, style=label.style_label_left)
    label.delete(sgx_low_label[1])

////////////////////////////////////////////////////////////////////// Nifty OHLC

// OHLC
nifty_o = request.security(syminfo.tickerid, "D", open[i_past_levels], barmerge.gaps_off, barmerge.lookahead_on)
nifty_h = request.security(syminfo.tickerid, "D", high[i_past_levels], barmerge.gaps_off, barmerge.lookahead_on)
nifty_l = request.security(syminfo.tickerid, "D", low[i_past_levels], barmerge.gaps_off, barmerge.lookahead_on)
nifty_c = request.security(syminfo.tickerid, "D", close[i_past_levels], barmerge.gaps_off, barmerge.lookahead_on)

// Plot Lines
plot(nifty and islast and show_nifty_o ? nifty_o : na, title="Nifty Open", style=plot.style_circles, color=color.new(o_col, 0), linewidth=1)
plot(nifty and islast and show_nifty_h ? nifty_h : na, title="Nifty High", style=plot.style_circles, color=color.new(h_col, 0), linewidth=1)
plot(nifty and islast and show_nifty_l ? nifty_l : na, title="Nifty Low", style=plot.style_circles, color=color.new(l_col, 0), linewidth=1)
plot(nifty and islast and show_nifty_c ? nifty_c : na, title="Nifty Close", style=plot.style_circles, color=color.new(c_col, 0), linewidth=1)

////////////////////////////////////////////////////////////////////// END