sunspore
sunspore

Reputation: 147

How to use the dayofweek function

I am trying to apply code to a certain day of the week to put an indicator on that day. Can I use the dayofweek function, or is there a better way?

last_day = friday

plotshape(last_day, style=shape.diamond, location=location.belowbar, color=green, size=size.tiny)

I was hoping this would put a shape under all Fridays, but instead it puts the shape under every day.

Upvotes: 0

Views: 10519

Answers (2)

carloswm85
carloswm85

Reputation: 2382

For Pine Script v4.

I'm not sure if you, or anyone else would find this useful, but it could be applicable as a workarounds to this kind of problems.

This code plots a background line every day, except every first day of week, when it plots another background (with a different color). It plots the backgrounds depending ON the hour, OVER and ABOVE the candlestick.

enableTLa = input(defval=true, type=input.bool, title="Enable Timelines 1D (TLa)")
colorA = input(title="Color TLa", type=input.color, defval=color.white)
enableTLd = input(defval=true, type=input.bool, title="Enable Timelines 1W (TLd)")
colorD = input(title="Color TLd", type=input.color, defval=color.yellow)
transpTL = input(defval = 80, title = "Timelines Transparency")

targetTime0     = timestamp(year, month, dayofmonth, 00, 00, 00) //  1D

timeframeRangeA = timeframe.period == "1" or timeframe.period == "3" or timeframe.period == "5" or timeframe.period == "15" or timeframe.period == "30" or timeframe.period == "45" or timeframe.period == "60"
timeframeRangeD = timeframe.period == "1" or timeframe.period == "3" or timeframe.period == "5" or timeframe.period == "15" or timeframe.period == "30" or timeframe.period == "45" or timeframe.period == "60" or timeframe.period == "120" or timeframe.period == "180" or timeframe.period == "240" or timeframe.period == "D"

// 1W, first day of the week, at first hour
bgcolor(targetTime0 == time and enableTLd and timeframeRangeD and dayofweek == dayofweek.monday ? colorD : na, transp=transpTL, editable=false)
// 1D, day of the week, at first hour
bgcolor(targetTime0 == time and enableTLa and timeframeRangeA and dayofweek != dayofweek.monday ? colorA : na, transp=transpTL, editable=false)

This is the result on 1h timeframe:

enter image description here

As you can see, it does not plot background into the future.

Upvotes: 1

PineCoders-LucF
PineCoders-LucF

Reputation: 8779

In v3, as you seem to be using, would need to be:

plotshape(dayofweek==friday, style=shape.diamond, location=location.belowbar, color=green, size=size.tiny)

Note that is in the exchange's time zone. https://www.tradingview.com/pine-script-reference/v3/#var_dayofweek

Upvotes: 2

Related Questions