frien_dd
Pine Script Rookie
Pine Script Rookie
Posts: 7
Joined: December 11th, 2020
Location: United Kingdom

Convert day of week and time to other timezone

Good evening,

If possible I am trying to understand how to use "dayofmonth", I think that's what I am after.

That means most built-in variables and functions don't look at our price chart's time zone, but instead return values in the time zone of our chart's instrument.

So if your indicator uses the default UTC setting how would you code it so it uses the timezone that you can manually set.

Any advice will be gratefully received.

Frien_dd

Fxxtrader
Pine Script Rookie
Pine Script Rookie
Posts: 22
Joined: September 6th, 2020
Location: Toronto
Contact: Twitter TradingView Profile

Re: Convert day of week and time to other timezone

Hello, I have similar question. I would like the first daily bar for each month identified and labelled with the months name.

I do it for day of the week on intraday charts, but can't figure it out for identifying first day of the month. Here:

Code: Select all

study("Intraday Time", overlay=true)

src = input(open, title="Source")

//User input - which label to show
showDOPEN = input(true, title="Identify 5PM EST")
show5AMEST = input(true, title="Identify 5AM EST")

BarInSession(sess) =>
    not na(time(timeframe.period, sess))
    
// Identify 5PM EST opening bar. *Sun open identified which is also Mondays bar open
plotshape(showDOPEN ? hour == 17 and minute == 0 and dayofweek == dayofweek.sunday : true, title="Sun Open", text="MO/s", color=color.green, location=location.belowbar, offset=0, style=shape.triangleup, textcolor=color.white, size=size.normal, transp=15)

// Identify 5AM EST
plotshape(show5AMEST ? hour == 05 and minute == 0 and dayofweek == dayofweek.monday : false, title="Mon 5am", text="5AM", color=color.teal, offset=0, style=shape.triangledown, textcolor=color.blue, size=size.tiny, transp=15)
To save space, I did not put code for each day of the week, but I have identifiers Mon-Fri, 5 PM EST Open and 5 AM EST.

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: Convert day of week and time to other timezone

Sorry guys, I'm not entirely sure how to do this to be honest because I've never encountered this challenge in my own scripts.

But this blog article might help inspire some potential solutions: New Parameter for Date Input Added to Pine

Fxxtrader
Pine Script Rookie
Pine Script Rookie
Posts: 22
Joined: September 6th, 2020
Location: Toronto
Contact: Twitter TradingView Profile

Re: Convert day of week and time to other timezone

Matt - thanks for the link, this answered my question for coding strategy start and end time. The article gave a shorter script and a simple front end for selecting start and end time, for a strategy.

I am still looking for a way to plot name of month to chart.

Thank you.

kmarryat
Pine Script Rookie
Pine Script Rookie
Posts: 20
Joined: December 21st, 2020
Contact: TradingView Profile

Re: Convert day of week and time to other timezone

@frien_dd
I believe you can use the optional timezone parameter with the dayofweek or dayofmonth functions. It takes a string that represents the number of hours up or down you want to adjust the timezone. Hope that helps.

Code: Select all

DayOfWeek = dayofweek(time)

DayOfWeekTZminus3 = dayofweek(time,"-0300")

DayOfMonth = dayofmonth(time)

DayOfMonthTZplus3 = dayofmonth(time,"+0300")

kmarryat
Pine Script Rookie
Pine Script Rookie
Posts: 20
Joined: December 21st, 2020
Contact: TradingView Profile

Re: Convert day of week and time to other timezone

@Fxxtrader
To identify the first bar/day of the month you could just check if the current day of the month is less than the previous day of the month then print a label if it's true.

Something like this:

Code: Select all

FirstBarOfMonth = dayofmonth(time) < dayofmonth(time)[1]

if FirstBarOfMonth
    LabelText = tostring(month(time),"1st Bar\nMonth ")
    LabelAbove = label.new(x=bar_index, y=na, text=LabelText, yloc=yloc.abovebar, color=color.green, textcolor=color.white, 
      style=label.style_label_down, size=size.small)
    
I'm not aware of any way to extract the name of the month but you could always user a ternary to assign the month names.

Code: Select all

LabelText = 
  month(time) == 1 ? "Jan" :
  month(time) == 2 ? "Feb" : 
  "etc...."

frien_dd
Pine Script Rookie
Pine Script Rookie
Posts: 7
Joined: December 11th, 2020
Location: United Kingdom

Re: Convert day of week and time to other timezone

This is were I am at with this atm.

It displays Market Regular Opening hours as per:


For daily and weekly.

Tokyo https://www.tradinghours.com/markets/jpx/hours
London https://www.tradinghours.com/markets/lse/hours
New York https://www.tradinghours.com/markets/nyse/hours

It displays perfectly on the 1 min timeframe but I cant see why it will not display on other timeframes.

I thought it was the below line of code so changed it but still no joy.

Code: Select all

//AsiaWOpen = time("1", AsiaWOpenInput)
AsiaDOpen = time(timeframe.period, AsiaDOpenInput)
Any advice would be gratefully received.

Code: Select all

//@version=4
study("Market Opening Hours", overlay=true)

offset_val = input(title="Label Offset", type=input.integer, defval=15)

//--------------Asia Configuration
AsiaDOpenInput = input('2359-0000:234567', title="Asia Daily Open") //set the opening range you are interested in
AsiaWOpenInput = input('2359-0000:1', title="Asia Weekly Open") //set the opening range you are interested in

//AsiaDOpen = time("1", AsiaDOpenInput)
//AsiaWOpen = time("1", AsiaWOpenInput)
AsiaDOpen = time(timeframe.period, AsiaDOpenInput)
AsiaWOpen = time(timeframe.period, AsiaWOpenInput)


var AsiaDOpenPA = 0.0
if AsiaDOpen
    if not AsiaDOpen[1]
        AsiaDOpenPA := open

var AsiaWOpenPA = 0.0
if AsiaWOpen
    if not AsiaWOpen[1]
        AsiaWOpenPA := open


plot(not AsiaDOpen ? AsiaDOpenPA : na, title="Asia D Open", color=color.orange, linewidth=1, style=plot.style_linebr)
plotshape(AsiaDOpenPA, style=shape.labeldown, location=location.absolute, color=color.orange,  textcolor=color.white, show_last=1, text="Asia D Open",  offset = offset_val, transp=20, title="Asia D Open")

plot(not AsiaWOpen ? AsiaWOpenPA : na, title="Asia W Open", color=color.orange, linewidth=2, style=plot.style_linebr)
plotshape(AsiaWOpenPA, style=shape.labeldown, location=location.absolute, color=color.orange,  textcolor=color.white, show_last=1, text="Asia W Open",  offset = offset_val, transp=20, title="Asia W Open")



//--------------London Configuration
LonDOpenInput = input('0759-0800:134567', title="London Daily Open") //set the opening range you are interested in
LonWOpenInput = input('0759-0800:2', title="London Weekly Open") //set the opening range you are interested in

//LonDOpen = time("1", LonDOpenInput)
//LonWOpen = time("1", LonWOpenInput)
LonDOpen = time(timeframe.period, LonDOpenInput)
LonWOpen = time(timeframe.period, LonWOpenInput)


var LonDOpenPA = 0.0
if LonDOpen
    if not LonDOpen[1]
        LonDOpenPA := open

var LonWOpenPA = 0.0
if LonWOpen
    if not LonWOpen[1]
        LonWOpenPA := open


plot(not LonDOpen ? LonDOpenPA : na, title="London D Open", color=color.yellow, linewidth=1, style=plot.style_linebr)
plotshape(LonDOpenPA, style=shape.labeldown, location=location.absolute, color=color.yellow,  textcolor=color.white, show_last=1, text="London D Open",  offset = offset_val, transp=20, title="London D Open")

plot(not LonWOpen ? LonWOpenPA : na, title="London W Open", color=color.yellow, linewidth=2, style=plot.style_linebr)
plotshape(LonWOpenPA, style=shape.labeldown, location=location.absolute, color=color.yellow,  textcolor=color.white, show_last=1, text="London W Open",  offset = offset_val, transp=20, title="London W Open")



//--------------New York Configuration
NYDOpenInput = input('1429-1430:134567', title="New York Daily Open") //set the opening range you are interested in
NYWOpenInput = input('1429-1430:2', title="New York Weekly Open") //set the opening range you are interested in

//NYDOpen = time("1", NYDOpenInput)
//NYWOpen = time("1", NYWOpenInput)
NYDOpen = time(timeframe.period, NYDOpenInput)
NYWOpen = time(timeframe.period, NYWOpenInput)



var NYDOpenPA = 0.0
if NYDOpen
    if not NYDOpen[1]
        NYDOpenPA := open

var NYWOpenPA = 0.0
if NYWOpen
    if not NYWOpen[1]
        NYWOpenPA := open


plot(not NYDOpen ? NYDOpenPA : na, title="New York D Open", color=color.blue, linewidth=1, style=plot.style_linebr)
plotshape(NYDOpenPA, style=shape.labeldown, location=location.absolute, color=color.blue,  textcolor=color.white, show_last=1, text="New York D Open",  offset = offset_val, transp=20, title="New York D Open")

plot(not NYWOpen ? NYWOpenPA : na, title="New York W Open", color=color.blue, linewidth=2, style=plot.style_linebr)
plotshape(NYWOpenPA, style=shape.labeldown, location=location.absolute, color=color.blue,  textcolor=color.white, show_last=1, text="New York W Open",  offset = offset_val, transp=20, title="New York W Open")

Fxxtrader
Pine Script Rookie
Pine Script Rookie
Posts: 22
Joined: September 6th, 2020
Location: Toronto
Contact: Twitter TradingView Profile

Re: Convert day of week and time to other timezone

kmarryat wrote:
Thu Jan 21, 2021 9:37 pm
@Fxxtrader

Code: Select all

FirstBarOfMonth = dayofmonth(time) < dayofmonth(time)[1]

if FirstBarOfMonth
    LabelText = tostring(month(time),"1st Bar\nMonth ")
    LabelAbove = label.new(x=bar_index, y=na, text=LabelText, yloc=yloc.abovebar, color=color.green, textcolor=color.white, 
      style=label.style_label_down, size=size.small)
    
@kmarryat - thanks for the code. I manually draw monthly volume profiles, so quickly finding first day of the month is helpful. If the first trading day of the month begins on a Sunday 5 PM, the label is one bar off. For example, Feb 2021 label is off by one bar. Any suggestions for a fix?

Thanks.

frien_dd
Pine Script Rookie
Pine Script Rookie
Posts: 7
Joined: December 11th, 2020
Location: United Kingdom

Re: Convert day of week and time to other timezone

Below is the link to the end product.

https://uk.tradingview.com/script/Jv0Rt ... -Openings/

The change to the below code resolved the issue:

Previous:

Code: Select all

var AsiaDOpenPA = 0.0
if AsiaDOpen
    if not AsiaDOpen[1]
        AsiaDOpenPA := open
Now:

Code: Select all

var AsiaDOpenPA = float(na)
if AsiaDOpen and not AsiaDOpen[1]
    AsiaDOpenPA := open

kmarryat
Pine Script Rookie
Pine Script Rookie
Posts: 20
Joined: December 21st, 2020
Contact: TradingView Profile

Re: Convert day of week and time to other timezone

@Fxxtrader - the time variable is based on the bar open time. try changing the source of the dayofmonth function to time_close.

Code: Select all

FirstBarOfMonth = dayofmonth(time_close) < dayofmonth(time_close)[1]

if FirstBarOfMonth
    LabelText = tostring(month(time_close),"1st Bar\nMonth ")
    LabelAbove = label.new(x=bar_index, y=na, text=LabelText, yloc=yloc.abovebar, color=color.green, textcolor=color.white, 
      style=label.style_label_down, size=size.small)

Return to “Pine Script Q&A”