pine-script

Use dayofweek user input


I'm trying to highlight a specific day of the week and I want the day to be user definable. I'm getting errors related to type casting but don't know how to fix it. THis is my code:

enum days
    monday = "Monday"
    tuesday = "Tuesday"
    wednesday = "Wednesday"
    thursday = "Thursday"
    friday = "Friday"

aDay = input.enum(days.monday, 'Day', group = 'A')
bgcolor((dayofweek == aDay) ? color.rgb(0, 0, 0, 75) : na, title = "A")

Solution

  • input.enum returns enum and dayofweek is of type int. So, you need to convert your input to an int.

    enum_to_int(e) =>
        switch (e)
            days.monday => 2
            days.tuesday => 3
            days.wednesday => 4
            days.thursday => 5
            days.friday => 6
            => 0
    
    day_int = enum_to_int(aDay)
    
    bgcolor((dayofweek == day_int) ? color.rgb(0, 0, 0, 75) : na, title = "A")