Nonfarm [TjnTjn]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TjnTjn

//@version=5
//UTC+7 Sessions
indicator("Nonfarm [TjnTjn]", overlay = true, max_labels_count = 500, max_lines_count = 500, max_boxes_count = 500, max_bars_back = 5000)

///////////////
ColorH      = input.color(color.new(color.red, 50), "Hour Nonfarm")
ColorD      = input.color(color.new(color.green, 80), "Day Nonfarm")
Day_Month   = dayofmonth(time, "GMT+0700")
Month       = month(time, "GMT+0700")
Friday      = ((dayofweek(time, "GMT+0700") == dayofweek.friday) ? true:false)    //Check if it's Friday
Thursday    = ((dayofweek(time, "GMT+0700") == dayofweek.thursday) ? true:false)  //Check if it's Thursday
Wednesday   = ((dayofweek(time, "GMT+0700") == dayofweek.wednesday) ? true:false) //Check if it's Wednesday
Saturday    = ((dayofweek(time, "GMT+0700") == dayofweek.saturday) ? true:false)  //Check if it's Saturday

// Check if the first day of January
// If it's the 1nd, the nonfarm day will be the 8th
// If it's the 2nd, the nonfarm day will be the 9th
// If it's the 3rd, the nonfarm day will be the 10th
Day_Month_1     = Month == 1 and Friday and Day_Month == 1? true: Month == 1 and Friday and Day_Month == 15?false:na 
Day_Month_11    = fixnan(Day_Month_1) and Month == 1 and Friday and Day_Month == 8
Day_Month_2     = Month == 1 and Friday and Day_Month == 2? true: Month == 1 and Friday and Day_Month == 16?false:na 
Day_Month_21    = fixnan(Day_Month_2) and Month == 1 and Friday and Day_Month == 9
Day_Month_3     = Month == 1 and Friday and Day_Month == 3? true: Month == 1 and Friday and Day_Month == 17?false:na 
Day_Month_31    = fixnan(Day_Month_3) and Month == 1 and Friday and Day_Month == 10
Day_Month_4567  = Month == 1 and Friday and (Day_Month == 4 or Day_Month == 5 or Day_Month == 6 or Day_Month == 7) ?true:false

////Check if current Friday is 1st or 2nd or 3rd but the previous month was 30 days or 29 days or 28 days.
////If true, the nonfarm day will be the next Friday of that month.
Month_2_12  = Month == 1 ?false:true  //Current month is not January
Day1        = Month_2_12 and Day_Month[1] == 28 and Wednesday and Day_Month==1?true:Month_2_12 and Saturday and Day_Month==11?false:na 
Day11       = fixnan(Day1)  and Friday and Day_Month == 10
Day2        = Month_2_12 and (Day_Month[1] == 28 or Day_Month[1] == 29) and Thursday and Day_Month==1?true:Month_2_12 and Saturday and Day_Month==10?false:na 
Day21       = fixnan(Day2) and Friday and Day_Month == 9
Day3        = Month_2_12 and (Day_Month[1] == 28 or Day_Month[1] == 29 or Day_Month[1] == 30) and Friday and Day_Month==1?true:Month_2_12 and Saturday and Day_Month==9?false:na 
Day31       = fixnan(Day3) and Friday and Day_Month == 8
NDay1       = Month_2_12 and (Day_Month[1] == 29 or Day_Month[1] == 30 or Day_Month[1] == 31) and Wednesday and Day_Month==1?true:Month_2_12 and Saturday and Day_Month==4?false:na 
NDay11      = fixnan(NDay1)  and Friday and Day_Month == 3
NDay2       =  Month_2_12 and (Day_Month[1] == 30 or Day_Month[1] == 31) and Thursday and Day_Month==1?true:Month_2_12 and Saturday and Day_Month==3?false:na 
NDay21      = fixnan(NDay2)  and Friday and Day_Month == 2
NDay3       = Month_2_12 and (Day_Month[1] == 31) and Friday and Day_Month==1?true:Month_2_12 and Saturday and Day_Month==2?false:na 
NDay31      = fixnan(NDay3) and Friday and Day_Month == 1

////Check if the current date is 4th 5th 6th, then nonfarm day is the first Friday of the month
Day4_7      = (Day_Month == 4 or Day_Month == 5 or Day_Month == 6 or Day_Month == 7? true:false)
N_Day       = Day4_7 and Friday

///////////////
Day_Nonfarm = Day_Month_11 or Day_Month_21 or Day_Month_31 or Day_Month_4567 or Day11 or Day21 or Day31 or N_Day or NDay11 or NDay21 or NDay31
Moth_cool   = time(timeframe.period, "0730-0831") and Day_Nonfarm
bgcolor(Day_Nonfarm? ColorD:na, editable=false)
bgcolor(Moth_cool? ColorH:na, editable=false)
