1

this is my first post in here and sorry for any mistake I could do in the post, moreover I'm not that practical with PineScript code yet, I'm in the "trial and error" phase.

as per title I would like to know if there is any chance to be able to color the background of the chart specifing how many days from the end of the month and how many days from the beginning of the month.

I did some research and I ended up in this post.

I managed to modify the Version 2 of the code provided by PineCoders-LucF in that post, the only problem is when it comes to weekends the code does not work anymore.

To be more specific:

//@version=4
study("BgColor", overlay=true)

fromDay         = input(-4)
toDay           = input(+4)
weekdaysOnly    = input(true)
useVline        = input(false)

dayIsOk = not weekdaysOnly or (dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday)

t1 = timestamp("GMT-5", year, month, fromDay, 00, 00)
t2 = timestamp("GMT-5", year, month, toDay,   00, 00)

timeIsOk = (time >= t1) and (time <= t2)
bgcolor( not useVline and timeIsOk and dayIsOk ? color.orange : na, transp = 80)

if useVline and timeIsOk and dayIsOk
    line.new(bar_index, low * .9999, bar_index, high * 1.0001, xloc.bar_index, extend.both, #FF8000ff, line.style_solid, 1)

It does somehow the job, but as you can see, it has some problems when the weekend is in between the n days specified from the end or beginning of the month.

My question is if its possible to tell to the code to skip the weekend days, and if it's a weekend day then to plot the background color on the first week day availiable.

The other question is: is ther any code to plot the following code in the future? For example specifing with an input how many months forward I would like to see the background color plotted.

Thanks for your kind attention.

Silo

Silo
  • 13
  • 6

1 Answers1

3

The below code will do what you intended.
There's a caveat though: it doesn't account for non-trading days, because I don't know of a consistent method to identifiy holidays in each year.
So, for example, you'll see an inconsistency around the 4th of july or around Thanksgiving.
For clarity, I've made the negative days red, and the positive days green.

//@version=4
study("BgColor", overlay=true)

fromDay         = input(-3, "fromDay", maxval=0)
toDay           = input( 3, "toDay",   minval=0)

var int[] TradingDays = array.new_int(na)

f_isLeapYear(_year) =>
    // Any year that is evenly divisible by 4 is a leap year
    // A year that is evenly divisible by 100 (for example, 1900) is a leap year only if it is also evenly divisible by 400.
    (_year % 100 == 0) and (_year % 400 != 0) ? true : (_year % 4 == 0)

f_daysInMonth(_year, _month) =>
    int dpm = na
    if _month == 2
        dpm := 28 + (f_isLeapYear(_year) ? 1 : 0)
    else 
        evenMonth = _month % 2 == 0
        beforeAug = _month < 8
        dpm := 30 + (((not evenMonth and beforeAug) or (evenMonth and not beforeAug)) ? 1 : 0)

f_initTradingDays(_arr, _month) =>
    array.clear(_arr)
    for dom = 1 to f_daysInMonth(year, _month)
        dow = dayofweek(timestamp(year, _month, dom, 0, 0, 0))
        if (dow != dayofweek.saturday) and (dow != dayofweek.sunday)
            array.push(_arr, dom)

f_getStartStopDay(_arr, _negDelta, _posDelta) =>
    dStart  = array.min(array.slice(TradingDays, array.size(TradingDays) - abs(_negDelta)))
    dStop   = array.max(array.slice(TradingDays, 0, abs(_posDelta)))
    [dStart, dStop]

if change(time("M")) or barstate.isfirst
    f_initTradingDays(TradingDays, month)

[startDay, stopDay] = f_getStartStopDay(TradingDays, fromDay, toDay)

showStartDay    = dayofmonth >= startDay
showStopDay     = dayofmonth <= stopDay

bgcolor(showStartDay ? color.red   : na)
bgcolor(showStopDay  ? color.green : na)

//Future
stopDaysShort   = max(0, stopDay - dayofmonth)

arrStop = array.new_int(na)
for i=1 to stopDaysShort
    array.push(arrStop, i)
    
bgcolor(barstate.islast and array.includes(arrStop, 1) ? color.green : na, offset=1)
bgcolor(barstate.islast and array.includes(arrStop, 2) ? color.green : na, offset=2)
bgcolor(barstate.islast and array.includes(arrStop, 3) ? color.green : na, offset=3)
bgcolor(barstate.islast and array.includes(arrStop, 4) ? color.green : na, offset=4)
Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42
  • Thanks Bjorn thats absolutely fantastic! Is there any chance to color future dates as well? For example: today is 1stDec and on my chart is correctly "colored" in green, is there any chance to color in green the 2nd and 3rd December, generally speaking i'm asking if its possible to color future dates. – Silo Dec 01 '20 at 17:30
  • Oh, you mean the immediate subsequent days, correct? I thought you meant several months ahead (because that can't be done imo) – Bjorn Mistiaen Dec 01 '20 at 17:46
  • Hi Bjorn, if the "several months ahead" option is not possible of course is not a problem, but if you have time do you think is possible to highlight, in this case the 2nd and 3rd Dec? – Silo Dec 01 '20 at 17:59
  • I amended my code to account for the forward looking in the beginning of the mont. Not sure how to do it for end of month though... – Bjorn Mistiaen Dec 01 '20 at 18:04
  • Thanks for your time Bjorn, I really appreciated your fantastic support. Is without any doubt what I was looking for, i really appreciated your time and support, my best wishes. – Silo Dec 01 '20 at 18:13