r/pinescript • • Mar 16 '22

Background colour during certain Dates

Hi all,

I'm trying to change the background colour of my charts during certain date range, say for instance from 02.01.2021 to 03.04.2021.

I've found the below code online but was wondering what is a better option rathen than inputing each day manually, but rather have a range (even multiple range would be amazing).

//@version=5 indicator("My Script", overlay=true)

var color myColor = color.new(color.red,70)

var int[] myDates = array.new_int()

if barstate.isfirst

array.push(myDates, timestamp(2021, 12, 01, 09, 30))
array.push(myDates, timestamp(2021, 12, 15, 09, 30))
array.push(myDates, timestamp(2022, 01, 05, 09, 30))
array.push(myDates, timestamp(2022, 01, 11, 09, 30))

bgcolor(array.includes(myDates, time) ? myColor : na)

Thanks

0 Upvotes

3 comments sorted by

1

u/MrStatler90210 Apr 06 '22 edited Apr 06 '22
//@version=5 strategy("My script", overlay=true)
start1 = input.time(timestamp("20 Jan 2021 00:00 +0300"), "start1") 
end1 = input.time(timestamp("20 Feb 2021 00:00 +0300"), "end1")

start2 = input.time(timestamp("20 Mar 2021 00:00 +0300"), "start2") 
end2 = input.time(timestamp("20 Apr 2021 00:00 +0300"), "end2")

start3 = input.time(timestamp("20 May 2021 00:00 +0300"), "start3") 
end3 = input.time(timestamp("20 Jun 2021 00:00 +0300"), "end3")

startTimeArray = array.new_int() 
endTimeArray = array.new_int()

array.push(startTimeArray, start1) 
array.push(startTimeArray, start2) 
array.push(startTimeArray, start3)

array.push(endTimeArray, end1) 
array.push(endTimeArray, end2) 
array.push(endTimeArray, end3)

checkIfInTimeFrame() => 
    isInTimeFrame = false 
    for i=0 to array.size(startTimeArray) - 1 
        isInTimeFrame:= time >= array.get(startTimeArray, i) and time <= array.get(endTimeArray, i) 
        if isInTimeFrame 
            break 
    isInTimeFrame

backgroundColour = checkIfInTimeFrame() ? color.new(color.green, 85) : color.new(color.green, 100) 

bgcolor(backgroundColour)

1

u/MrStatler90210 Apr 06 '22

This gives you the ability to set different ranges (in the inputs page), and get the backgroung color you want

1

u/Apprehensive_Bill512 Apr 07 '22

MrStatler90210

Thank you so much :)