Lingga Trader Scalper
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nicks1008

//@version=5
indicator(title='Lingga Trader', shorttitle='Lingga Scalper |3 Min', precision=2, overlay=true)

Rsi_value = input.int(14, title='RSI Length', step=1)
hl = input.int(80, title='Higher Value of RSI', step=1)
ll = input.int(20, title='Lower value of RSI', step=1)
rs = ta.rsi(close, Rsi_value)

sma_value = input.int(70, title='SMA Length', step=1)
sma1 = ta.sma(close, sma_value)

dist_SMA = 1
candle_length = 1

iff_1 = high < sma1 ? color.new(color.red,30) : color.new(color.yellow,30)
iff_2 = low > sma1 ? color.new(color.lime,30) : iff_1
mycolor = rs >= hl or rs <= ll ? color.new(color.yellow,30) : iff_2
gaps = sma1 + dist_SMA  //Gap between price and SMA for Sell
gapb = sma1 - dist_SMA  //Gap between price and SMA for Buy
chartgap = gaps or gapb  //for both below or above the SMA 
gap1 = sma1 + 5
gapvalue = open / 100 * candle_length  //setting % with its Share price
gapp = high - low > gapvalue  //or rs<50     // Condition for Min candle size to be eligible for giving signal - Buy Calls
gapp2 = high - low > gapvalue  //or rs>55    // Condition for Min candle size to be eligible for giving signal - Sell Calls
bull = open < close and high - low > 2 * gapvalue and close > (high + open) / 2
bear = open > close and high - low > 2 * gapvalue and close < (low + open) / 2


rev1 = rs > 68 and open > close and open > gaps and high - low > gapvalue + 0.5 and low != close  //over red candles  "S" - uptrend
rev1a = rs > 90 and open < close and close > gaps and high != close and open != low  // over green candles"S" - uptrend
sellrev = rev1 or rev1a

rev2 = rs < 50 and open < close and open < gapb and open == low  //over green candles"B"
rev3 = rs < 30 and open > close and high > gapb and open != high and barstate.isconfirmed != bear  //over red candles"B"
rev4 = rs < 85 and close == high and high - low > gapvalue and open < close  //over green candle in both trends
hlrev_s = ta.crossunder(rs, hl)
llrev_b = ta.crossover(rs, ll) and open < close

buycall = open < close and open > sma1 and ta.cross(close[1], sma1) and close > sma1
sellcall = ta.cross(close, sma1) and open > close
BUY = ta.crossover(close[1], sma1) and close[1] > open[1] and high[0] > high[1] and close[0] > open[0]
SELL = ta.crossunder(low[1], sma1) and close[1] < open[1] and low[0] < low[1] and close[0] < open[0]

plotshape(SELL, title='SELL', style=shape.labeldown, color=color.new(color.red, 30), text='S', textcolor=color.new(color.black, 30))
plotshape(BUY, title='BUY', style=shape.labelup, color=color.new(color.aqua, 30), text='B', textcolor=color.new(color.black, 30), location=location.belowbar)

plotshape(hlrev_s, title='Reversal1', style=shape.labeldown, color=color.new(color.yellow, 20), text='!', textcolor=color.new(color.black, 20))
plotshape(llrev_b, title='Reversal2', style=shape.labelup, color=color.new(color.yellow, 20), text='!', textcolor=color.new(color.black, 20), location=location.belowbar)

barcolor(BUY ? color.new(color.green, 0) : SELL ? color.new(color.maroon, 30) : hlrev_s or llrev_b ? color.new(color.yellow, 0) : na)
plot(sma1, title='SMA', color=mycolor, linewidth=1)
alertcondition(hlrev_s or llrev_b, title='Both Reversal Signal', message='Reversal Alert')
alertcondition(SELL or BUY, title='Buy & Sell Signal Both', message='Buy/Sell Alert')
alertcondition(BUY, title='Only Buy Signal', message='Buy Alert')
alertcondition(SELL, title='Only Sell Signal', message='Sell Alert')
alertcondition(hlrev_s , title='Reversal from Top', message='Down Reversal Alert')
alertcondition(llrev_b, title='Reversal from Down', message='Up Reversal Alert')

//@version=5


bool        msb_sv          = input.bool        (true,                           title='Plot MSB lines')
bool        box_sv          = input.bool        (true,                           title='Plot Orderblocks')
bool        m_sv            = input.bool        (true,                          title='Plot Breakerblocks')
bool        range_sv        = input.bool        (true,                           title='Plot Range')
bool        range_eq_sv     = input.bool        (true,                           title='Plot Range 0.5 Line')
bool        range_q_sv      = input.bool        (true,                           title='Plot Range 0.25 and 0.75 Lines')
color       u_s             = input.color       (color.new(#089981, 80),         title='Untested Supply Color')
color       t_s             = input.color       (color.new(#b2b5be, 80),         title='Tested Supply Color')
color       u_d             = input.color       (color.new(#f23645, 80),         title='Untested Demand Color')
color       t_d             = input.color       (color.new(#b2b5be, 80),         title='Tested Demand Color')
color       u_b             = input.color       (color.new(#ff9800, 80),         title='Untested Breaker Color')
color       t_b             = input.color       (color.new(#b2b5be, 80),         title='Tested Breaker Color')

var float[] pvh1_price      = array.new_float   (30, na)            // high
var int[]   pvh1_time       = array.new_int     (30, na)
var float[] pvl1_price      = array.new_float   (30, na)            // low
var int[]   pvl1_time       = array.new_int     (30, na)
var float[] pvh2_price      = array.new_float   (10, na)            // higher high
var int[]   pvh2_time       = array.new_int     (10, na)
var float[] pvl2_price      = array.new_float   (10, na)            // lower low
var int[]   pvl2_time       = array.new_int     (10, na)
var float   htcmrll_price   = na                                    // high that created most recent ll
var int     htcmrll_time    = na
var float   ltcmrhh_price   = na                                    // low that created most recent hh
var int     ltcmrhh_time    = na
var box[]   long_boxes      = array.new_box()                       // orderblocks
var box[]   short_boxes     = array.new_box()
var box[]   m_long_boxes    = array.new_box()                       // breakerblocks
var box[]   m_short_boxes   = array.new_box()
var line[]  bull_bos_lines  = array.new_line()                      // MSB lines
var line[]  bear_bos_lines  = array.new_line()
var line[]  range_h_lines   = array.new_line()                      // Range lines
var line[]  range_25_lines  = array.new_line()
var line[]  range_m_lines   = array.new_line()
var line[]  range_75_lines  = array.new_line()
var line[]  range_l_lines   = array.new_line()
var label[] la_ph2          = array.new_label()                     // 2nd order pivots
var label[] la_pl2          = array.new_label()
var float   temp_pv_0       = na
var float   temp_pv_1       = na
var float   temp_pv_2       = na
var int     temp_time       = na
var float   last_range_h    = na
var float   last_range_l    = na
var line    range_m         = na
var line    range_25        = na
var line    range_75        = na
var float   box_top         = na
var float   box_bottom      = na
bool        pvh             = high < high[1] and high[1] > high[2]
bool        pvl             = low > low[1] and low[1] < low[2]
int         pv1_time        = bar_index[1]
float       pv1_high        = high[1]
float       pv1_low         = low[1]
bool        new_ph_2nd      = false
bool        new_pl_2nd      = false
string      alert           = na

//weekendicator
multiplier = input.float(3, minval=0, maxval=10, step=0.1, group = "Weekend")

day_clr = dayofweek < 2 or dayofweek > 6 ? color.new(#9598a1, 80) : color.new(#9598a1, 100)
plot(((high / low) * ohlc4) * (1 + multiplier / 100), style=plot.style_line, color=day_clr)
plot(((low / high) * ohlc4) * (1 - multiplier / 100), style=plot.style_area, color=day_clr)

// range blocks etc
if barstate.isconfirmed
	if pvh
		array.pop(pvh1_price)
		array.pop(pvh1_time)
		array.unshift(pvh1_price, pv1_high)
		array.unshift(pvh1_time, pv1_time)
		if array.size(pvh1_price) > 2
			temp_pv_0 := array.get(pvh1_price, 0)
			temp_pv_1 := array.get(pvh1_price, 1)
			temp_pv_2 := array.get(pvh1_price, 2)
			if temp_pv_0 < temp_pv_1 and temp_pv_1 > temp_pv_2
				array.pop(pvh2_price)
				array.pop(pvh2_time)
				array.unshift(pvh2_price, temp_pv_1)
				array.unshift(pvh2_time, array.get(pvh1_time, 1))
				new_ph_2nd := true
				if temp_pv_1 > array.get(pvh2_price, 1)
					for i = 0 to array.size(pvl2_time) - 1 by 1
						temp_ltcmrhh_time = array.get(pvl2_time, i)
						if temp_ltcmrhh_time < array.get(pvh2_time, 0)
							ltcmrhh_price := array.get(pvl2_price, i)
							ltcmrhh_time := temp_ltcmrhh_time
							break
			if temp_pv_0 < ltcmrhh_price
				if msb_sv
					array.push(bear_bos_lines, line.new(x1=ltcmrhh_time, y1=ltcmrhh_price, x2=bar_index, y2=ltcmrhh_price, color=color.fuchsia, width=2))
				box_top := array.get(pvh2_price, 0)
				box_bottom := math.max(low[bar_index - array.get(pvh2_time, 0)], low[bar_index - array.get(pvh2_time, 0) + 1])
				array.push(short_boxes, box.new(left=array.get(pvh2_time, 0), top=box_top, right=bar_index, bottom=box_bottom, bgcolor= box_sv ? u_s : na , border_color=na, extend=extend.right))
				ltcmrhh_price := na
	if pvl
		array.pop(pvl1_price)
		array.pop(pvl1_time)
		array.unshift(pvl1_price, pv1_low)
		array.unshift(pvl1_time, pv1_time)
		if array.size(pvl1_price) > 2
			temp_pv_0 := array.get(pvl1_price, 0)
			temp_pv_1 := array.get(pvl1_price, 1)
			temp_pv_2 := array.get(pvl1_price, 2)
			if temp_pv_0 > temp_pv_1 and temp_pv_1 < temp_pv_2
				array.pop(pvl2_price)
				array.pop(pvl2_time)
				array.unshift(pvl2_price, temp_pv_1)
				array.unshift(pvl2_time, array.get(pvl1_time, 1))
				new_pl_2nd := true
				if temp_pv_1 < array.get(pvl2_price, 1)
					for i = 0 to array.size(pvh2_time) - 1 by 1
						temp_htcmrll_time = array.get(pvh2_time, i)
						if temp_htcmrll_time < array.get(pvl2_time, 0)
							htcmrll_price := array.get(pvh2_price, i)
							htcmrll_time := temp_htcmrll_time
							break
			if temp_pv_0 > htcmrll_price
				if msb_sv
					array.push(bull_bos_lines, line.new(x1=htcmrll_time, y1=htcmrll_price, x2=bar_index, y2=htcmrll_price, color=color.olive, width=2))
				box_top := math.min(high[bar_index - array.get(pvl2_time, 0)], high[bar_index - array.get(pvl2_time, 0) + 1])
				box_bottom := array.get(pvl2_price, 0)
				array.push(long_boxes, box.new(left=array.get(pvl2_time, 0), top=box_top, right=bar_index, bottom=box_bottom, bgcolor= box_sv ? u_d : na, border_color=na, extend=extend.right))
				htcmrll_price := na
	if array.size(short_boxes) > 0
		for i = array.size(short_boxes) - 1 to 0 by 1
			tbox = array.get(short_boxes, i)
			top = box.get_top(tbox)
			bottom = box.get_bottom(tbox)
			ago = box.get_left(tbox)
			if array.get(pvh1_price, 0) > bottom 
				if box_sv
					box.set_bgcolor(tbox, t_s)
			if array.get(pvl1_price, 0) > top
				if m_sv
					box.set_bgcolor(tbox, u_b)
					array.push(m_long_boxes, tbox)
				else
					box.delete(tbox)
				array.remove(short_boxes, i)
				if msb_sv    
					line.delete(array.get(bear_bos_lines, i))
					array.remove(bear_bos_lines, i)
	if array.size(long_boxes) > 0
		for i = array.size(long_boxes) - 1 to 0 by 1
			lbox = array.get(long_boxes, i)
			top = box.get_top(lbox)
			bottom = box.get_bottom(lbox)
			ago = box.get_left(lbox)
			if array.get(pvl1_price, 0) < top
				if box_sv
					box.set_bgcolor(lbox, t_d)
			if array.get(pvh1_price, 0) < bottom
				if m_sv
					box.set_bgcolor(lbox, u_b)
					array.push(m_short_boxes, lbox)
				else
					box.delete(lbox)
				array.remove(long_boxes, i)
				if msb_sv
					line.delete(array.get(bull_bos_lines, i))
					array.remove(bull_bos_lines, i)
	if array.size(m_short_boxes) > 0
		for i = array.size(m_short_boxes) - 1 to 0 by 1
			tbox = array.get(m_short_boxes, i)
			top = box.get_top(tbox)
			bottom = box.get_bottom(tbox)
			ago = box.get_left(tbox)
			if array.get(pvh1_price, 0) > bottom 
				box.set_bgcolor(tbox, t_b)
			if array.get(pvl1_price, 0) > top
				box.delete(tbox)
				array.remove(m_short_boxes, i)
	if array.size(m_long_boxes) > 0
		for i = array.size(m_long_boxes) - 1 to 0 by 1
			lbox = array.get(m_long_boxes, i)
			top = box.get_top(lbox)
			bottom = box.get_bottom(lbox)
			ago = box.get_left(lbox)
			if array.get(pvl1_price, 0) < top
				box.set_bgcolor(lbox, t_b)
				
			if array.get(pvh1_price, 0) < bottom
				box.delete(lbox)
				array.remove(m_long_boxes, i)
	if range_sv and (new_ph_2nd or new_pl_2nd) and (array.get(pvh2_price, 0) < array.get(pvh2_price, 1) and array.get(pvl2_price, 0) > array.get(pvl2_price, 1) and array.get(pvh2_price, 0) > array.get(pvl2_price, 1) and array.get(pvl2_price, 0) < array.get(pvh2_price, 1)) and (array.get(pvl2_price, 1) > nz(last_range_h) or na(last_range_l)? true : (array.get(pvh2_price, 1) < last_range_l))
		temp_time := math.min(array.get(pvh2_time, 1), array.get(pvl2_time, 1))
		last_range_h    := array.get(pvh2_price, 1)
		last_range_l    := array.get(pvl2_price, 1)
		temp_pv_0 := (last_range_h + last_range_l)/2
		temp_pv_1 := (last_range_h + temp_pv_0)/2
		temp_pv_2 := (last_range_l + temp_pv_0)/2
		array.push(range_h_lines, line.new(x1=temp_time, y1=last_range_h, x2=bar_index, y2=last_range_h, color=color.black, width=2, extend=extend.right))
		array.push(range_l_lines, line.new(x1=temp_time, y1=last_range_l, x2=bar_index, y2=last_range_l, color=color.black, width=2, extend=extend.right))
		if range_eq_sv
			array.push(range_m_lines, line.new(x1=temp_time, y1=temp_pv_0, x2=bar_index, y2=temp_pv_0, color=color.gray, width=2, extend=extend.right))
		if range_q_sv
			array.push(range_25_lines, line.new(x1=temp_time, y1=temp_pv_1, x2=bar_index, y2=temp_pv_1, style=line.style_dashed, color=color.gray, width=1, extend=extend.right))
			array.push(range_75_lines, line.new(x1=temp_time, y1=temp_pv_2, x2=bar_index, y2=temp_pv_2, style=line.style_dashed, color=color.gray, width=1, extend=extend.right))
		
	if array.size(range_h_lines) > 0
		for i = array.size(range_h_lines) - 1 to 0 by 1
			range_h = array.get(range_h_lines, i)
			top = line.get_y1(range_h)
			range_l = array.get(range_l_lines, i)
			bottom = line.get_y1(range_l)
			temp_time := line.get_x1(range_h)
			if range_eq_sv
				range_m := array.get(range_m_lines, i)
			if range_q_sv
				range_25 := array.get(range_25_lines, i)
				range_75 := array.get(range_75_lines, i)
			if array.get(pvh1_price, 0) < bottom or array.get(pvl1_price, 0) > top
				line.delete(range_h)
				array.remove(range_h_lines, i)
				line.delete(range_l)
				array.remove(range_l_lines, i)
				if range_eq_sv
					line.delete(range_m)                
					array.remove(range_m_lines, i)
				if range_q_sv
					line.delete(range_25)                
					array.remove(range_25_lines, i)
					line.delete(range_75)                
					array.remove(range_75_lines, i)
				last_range_h    := na
				last_range_l    := na

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

//new york open etc
iMDisplay = input.bool    (true, "Display", group="New York Midnight Open")
iMTime    = input.session ('0400-0405:1234567', "Session", group="New York Midnight Open")
iMStyle   = input.string  ("Dashed", "Line Style", options=["Solid", "Dotted", "Dashed"], group="New York Midnight Open")
iMColor   = input.color   (#58A2B0, "Color", group="New York Midnight Open")
iMHistory = input.bool    (false, "History", group="New York Midnight Open")
iMLabel   = input.bool    (true, "Show Label", group="New York Midnight Open")

i8Display = input.bool    (true, "Display", group="New York 8:30 Open")
i8Time    = input.session ('1230-1235:1234567', "Session", group="New York 8:30 Open")
i8Style   = input.string  ("Dotted", "Line Style", options=["Solid", "Dotted", "Dashed"], group="New York 8:30 Open")
i8Color   = input.color   (#58A2B0, "Color", group="New York 8:30 Open")
i8History = input.bool    (false, "History", group="New York 8:30 Open")
i8Label   = input.bool    (true, "Show Label", group="New York 8:30 Open")

tMidnight = time          ("1", iMTime)
t830      = time          ("1", i8Time)

_MStyle   = iMStyle ==    "Solid" ? line.style_solid : iMStyle == "Dotted" ? line.style_dotted : line.style_dashed
_8Style   = i8Style ==    "Solid" ? line.style_solid : i8Style == "Dotted" ? line.style_dotted : line.style_dashed

//==== Midnight Open ====
if iMDisplay
	var openMidnight  = 0.0
	if tMidnight
		if not tMidnight[1]
			openMidnight  := open
		else
			openMidnight := math.max(open, openMidnight)

	var label lb = na
	var line lne = na
	if openMidnight != openMidnight[1]
		if barstate.isconfirmed
			line.set_x2(lne, tMidnight)
			lne := line.new(tMidnight, openMidnight, last_bar_time + 14400000/2, openMidnight, xloc.bar_time, extend.none, iMColor, _MStyle, 1)
			if iMLabel
				lb  := label.new(last_bar_time + 14400000/2, openMidnight, "Midnight", xloc.bar_time, yloc.price, na, label.style_none, iMColor, size.normal, text.align_right)
			label.delete(lb[1])
			if not iMHistory
				line.delete(lne[1])
//===========================

//==== 8:30 Open ====
if i8Display
	var open830  = 0.0
	if t830
		if not t830[1]
			open830  := open
		else
			open830 := math.max(open, open830)

	var label lb2 = na
	var line lne2 = na
	if open830 != open830[1]
		if barstate.isconfirmed
			line.set_x2(lne2, t830 - 30600000)
			lne2 := line.new(t830, open830, last_bar_time + 14400000/2, open830, xloc.bar_time, extend.none, i8Color, _8Style, 1)
			if i8Label
				lb2  := label.new(last_bar_time + 14400000/2, open830, "8:30", xloc.bar_time, yloc.price, na, label.style_none, i8Color, size.normal, text.align_right)
			label.delete(lb2[1])
			if not i8History
				line.delete(lne2[1])
//===========================

// Balanced Price Range
bpr_threshold = input.float(0, step = 0.25, title = "BPR Threshold", tooltip = "Valid BPR's must have a range greater than this number")
bars_since = input(15, "Bars to Look Back for BPR", tooltip = "Only look for BPR's when a sequence of bearish and bullish FVG's are within this many bars of each other")
only_clean_bpr = input(false, "Only Clean BPR", tooltip = "Only show BPR's when price does not interfere with the range prior to its completion")
delete_old_bpr = input(true, "Delete Old BPR", tooltip = "Delete all BPR's that have been invalidated or overwritten. Only show current/active BPR's")
bearish_bpr_color = input.color(color.new(color.green, 70))
bullish_bpr_color = input.color(color.new(color.green, 70))

float box_high = na
float box_low = na
int box_left = 0
int box_right = 0
var box box_bearish = na
var box box_bullish = na

new_fvg_bearish = low[2] - high > 0
new_fvg_bullish = low - high[2] > 0

valid_high = high[1] > high[2] and high[1] > high[0]
valid_low = low[1] < low[2] and low[1] < low[0]

midline = (high - low) / 2 + low
valid_hammer = open > midline and close > midline
valid_shooter = open < midline and close < midline

// Bullish BPR
bull_num_since = ta.barssince(new_fvg_bearish)
bull_bpr_cond_1 = new_fvg_bullish and bull_num_since <= bars_since 
bull_bpr_cond_2 = bull_bpr_cond_1 ? high[bull_num_since] + low[bull_num_since + 2] + high[2] + low > math.max(low[bull_num_since + 2], low) - math.min(high[bull_num_since], high[2]) : na

bull_combined_low = bull_bpr_cond_2 ? math.max(high[bull_num_since], high[2]) : na
bull_combined_high = bull_bpr_cond_2 ? math.min(low[bull_num_since + 2], low) : na

bull_bpr_cond_3 = true

if only_clean_bpr 
    for h = 2 to (bull_num_since)
        if high[h] > bull_combined_low
            bull_bpr_cond_3 := false

bull_result = bull_bpr_cond_1 and bull_bpr_cond_2 and bull_bpr_cond_3 and (bull_combined_high - bull_combined_low >= bpr_threshold)

if bull_result[1]
    if delete_old_bpr and not na(box_bullish)
        box.delete(box_bullish)
    box_bullish := box.new(bar_index - bull_num_since - 1, bull_combined_high[1], bar_index + 1, bull_combined_low[1], border_color = bullish_bpr_color, border_width = 1, bgcolor = bullish_bpr_color)
alertcondition(bull_result[1], "Bullish Alert", "New Bullish BPR")

if not na(box_bullish) and low > box.get_bottom(box_bullish)
    box.set_right(box_bullish, bar_index + 1)
else if not na(box_bullish) and low < box.get_bottom(box_bullish)
    if delete_old_bpr
        box.delete(box_bullish)
    else
        box_bullish := na 

// Bearish BPR
bear_num_since = ta.barssince(new_fvg_bullish)
bear_bpr_cond_1 = new_fvg_bearish and bear_num_since <= bars_since 
bear_bpr_cond_2 = bear_bpr_cond_1 ? high[bear_num_since] + low[bear_num_since + 2] + high[2] + low > math.max(low[bear_num_since + 2], low) - math.min(high[bear_num_since], high[2]) : na

bear_combined_low = bear_bpr_cond_2 ? math.max(high[bear_num_since + 2], high) : na
bear_combined_high = bear_bpr_cond_2 ? math.min(low[bear_num_since], low[2]) : na

bear_bpr_cond_3 = true

if only_clean_bpr 
    for h = 2 to (bear_num_since)
        if low[h] < bear_combined_high
            bear_bpr_cond_3 := false

bear_result = bear_bpr_cond_1 and bear_bpr_cond_2 and bear_bpr_cond_3 and (bear_combined_high - bear_combined_low >= bpr_threshold)

if bear_result[1]
    if delete_old_bpr and not na(box_bearish)
        box.delete(box_bearish)
    box_bearish := box.new(bar_index - bear_num_since - 1, bear_combined_high[1], bar_index + 1, bear_combined_low[1], border_color = bearish_bpr_color, border_width = 1, bgcolor = bearish_bpr_color)
alertcondition(bear_result[1], "Bearish Alert", "New Bearish BPR")

if not na(box_bearish) and high < box.get_top(box_bearish)
    box.set_right(box_bearish, bar_index + 1)
else if not na(box_bearish) and high > box.get_top(box_bearish)
    if delete_old_bpr
        box.delete(box_bearish)
    else
        box_bearish := na

////////////////////////////////////////////////////////////////////////////////
//CME gap show and fill
mode = input.string(
     defval="1 - Close from current symbol",
     title="Mode",
     options=["1 - Close from current symbol", "2 - CME original close crice", "3 - CME settlement price"],
     tooltip="In Mode 1 the closing price is determined in the current symbol but with the tradinghours from the CME futures contract. Mode 2 and 3 obtain the price directly from the CME futures contract and paint it in the chart of the current symbol. But note, that modes 2 and 3  may not give you the expected result, due to price differences in futures and spot prices.")

cme = request.security("CME:BTC1!", "60", close)
cmeSettlement = request.security("CME:BTC1!", "D", close, lookahead=barmerge.lookahead_on)

//Function to get friday closing price according to CME trading hours
getCloseCME() =>
    cmeClose = 0.0
    cmeClosePrev = nz(cmeClose[1], cmeClose)
    showLine = 0
    showLine := nz(showLine[1], showLine)
    
    if mode == "1 - Close from current symbol"
        cmeClose := dayofweek == 6 and time == timestamp('GMT-5', year, month, dayofmonth, 16, 0, 0) ? close[1] : cmeClosePrev
    else if mode == "2 - CME original close crice"
        cmeClose := dayofweek == 6 and time == timestamp('GMT-5', year, month, dayofmonth, 16, 0, 0) ? cme : cmeClosePrev
    else if mode == "3 - CME settlement price"
        cmeClose := dayofweek == 6 and time == timestamp('GMT-5', year, month, dayofmonth, 16, 0, 0) ? cmeSettlement : cmeClosePrev
    showLine := showLine == 0 and time >= timestamp('GMT-5', year, month, dayofmonth, 16, 0, 0) and dayofweek >= 6 ? 1 : showLine == 1 and dayofweek <= 1 and time >= timestamp('GMT-5', year, month, dayofmonth, 17, 0, 0) ? 0 : showLine

    [cmeClose, showLine]

[cmeClose, showLine] = getCloseCME()

//Plotting
plot1 = plot(showLine == 1 ? cmeClose : na, 'CME Friday Close', style=plot.style_linebr, linewidth=2, color=color.new(color.blue, 0))
plot2 = plot(close, 'Dummy plot for background', color=na)
fill(plot1, plot2, title='Background', color=close > cmeClose ? color.new(color.green, 80) : close < cmeClose ? color.new(color.red, 80) : na)