1stDay_3rdFriday of Month by RM
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// by © raul3429, with thanks to @StaticNoise et all.
// This script is meant for use on a daily chart on indices like SPY to set a line in the sand for Bullish/Bearish trends
// This indicator marks the 1st and 3rd Friday of current month in the chart with: Vertical, High-Low lines 
// 

//@version=5
indicator('1stDay_3rdFriday of Month by RM', '1stDay_3rdFri by RM', true)
gr9 = '***** 1st Day and 3rd Friday of the Month ******'

// ---------   Colors
c_orange	= #f17f17	//Orange
c_green		= #006400	//Green
c_green100	= #006400FF//Green 100%
c_greenLight= #388e3c	//Green Light
c_red		= #8B0000	//Red
c_red100	= #8B0000FF//Red 100%
c_redLight	= #b71c1c	//Red Light
c_white		= #ffffff	//White
c_grey		= #808080	//Grey
c_navy      = #000080  //Navy
c_yellow    = #FFFF00  //Yellow
c_teal      = #008080  //Teal     

Clr = input.color(title='Color 1stMo', defval=c_orange, group=gr9)
Clr2 = input.color(title='Color 3rdFri', defval=c_yellow, group=gr9)
line_Style = input.string(defval = 'Dashed', title = "Line Style" , options = ['Solid', 'Dotted', 'Dashed'])
Lst = line_Style == 'Solid' ? line.style_solid : line_Style == 'Dashed' ? 
                 line.style_dashed : line_Style == 'Dotted' ? line.style_dotted : line.style_solid
LB = input.bool(title='Show Date', defval=true, group=gr9)

days_in_month = 30 + month % 2
if month == 2
    leap_day = year % 4 == 0 ? 1 : 0
    days_in_month := 28 + leap_day
    days_in_month

bar_date_ts = timestamp(year(time),month(time),dayofmonth(time),0,0,0)
is_new_date = ta.change(bar_date_ts)     
is_newbar(timeframe.period) => ta.change(time(timeframe.period)) != 0 

high_rangeL = ta.valuewhen(is_newbar('D'),high,0) 
low_rangeL = ta.valuewhen(is_newbar('D'),low,0) 

if (is_new_date) and dayofmonth == 1 
    thisMonth = month (time)

isMon = dayofweek == dayofweek.monday
isFri = dayofweek == dayofweek.friday
isSat = dayofweek == dayofweek.saturday
isSun = dayofweek == dayofweek.sunday
isWeekend = isSun or isSat ? true:false
isWeekDay = not isWeekend

w1 = isFri and dayofmonth(time) >= 1 and dayofmonth(time) <= 7 ? true : false
w2 = isFri and dayofmonth(time) > 7 and dayofmonth(time) <= 14 ? true : false
w3 = isFri and dayofmonth(time) > 14 and dayofmonth(time) <= 21 ? true : false
w4 = isFri and dayofmonth(time) > 21 and dayofmonth(time) <= 28 ? true : false
w5 = isFri and dayofmonth(time) > 28 and dayofmonth(time) <= days_in_month ? true : false

//firstOfMonth = firstOfMonthWeekday ? true : firstOfMonthSat and dayofmonth == 3 ? true : firstOfMonthSun and dayofmonth == 2 ? true : false
//firstOfMonth = (is_new_date) and dayofmonth(time) == 1 and not isWeekend ? true : dayofmonth(time) == 2 and dayofweek.monday ? true :  dayofmonth(time) == 3 and dayofweek.monday ? true : false

var line l1 = na
var line l2 = na
var line l3 = na
var line l4 = na
var line V = na
var label lbl = na

bool is3rdFri = na
bool is1sMonWkDay = na
bool is1sMonSat = na
bool is1sMonSun = na

if (is_new_date)
    is3rdFri := w3 ? true : na 
    is1sMonWkDay := dayofmonth(time) == 1 and isWeekDay ? true : na
    is1sMonSat := not is1sMonWkDay and isMon and dayofmonth(time) == 2 ? true : na
    is1sMonSun := not is1sMonWkDay and isMon and dayofmonth(time) == 3 ? true : na

if is3rdFri or is1sMonWkDay or is1sMonSat or is1sMonSun
    line.delete(l1)
    line.delete(l2)
    line.delete(V)
    line.delete(l3)
    line.delete(l4)
    V  := line.new (x1=bar_index, x2=bar_index, y1=1, y2=2, extend=extend.both, color=Clr2, style=line.style_dashed, width=1)
    l1 := line.new (x1=bar_index, y1=high , x2=bar_index+10, y2=high , extend=extend.right, color = Clr2, style = Lst, width=1) 
    l2 := line.new (x1=bar_index, y1=low ,  x2=bar_index+10, y2=low , extend=extend.right, color = Clr2, style = Lst, width=1)
    linefill.new(l1, l2, color=color.new(c_white,90))
    if LB
        label.delete(lbl[1])
        lbl := label.new(bar_index, low, xloc =  xloc.bar_index , yloc=yloc.belowbar , text = str.format("{0, date, full}", time), color= c_yellow , size = size.normal, style = label.style_label_up)
