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

Input function to disable script on higher time frame

Hello - how would I add an input option to only run a script on a certain time frame. For example, this script, I want it disabled when I switch from a lower time frame to a daily chart. I've seen this input discussed here before but can't find it, again.

Code: Select all

//@version=4

study(title = "Daily Open Labels", shorttitle = "IntraWeek Lbl", overlay=true)

//Inputs - setting EMA period
showDOPENMon        = input(true, title="Monday open - 5 PM EST")
showDOPENTue        = input(true, title="Tuesday Open - 5 PM EST")
showDOPENWed        = input(true, title="Wednesday Open - 5 PM EST")
showDOPENThur       = input(true, title="Thursday Open - 5 PM EST")
showDOPENFri        = input(true, title="Friday Open - 5 PM EST")

BarInSession(sess) =>
    not na(time(timeframe.period, sess))

// Shape Color selection - central location to edit variable
colDO               = color(#2cf93c)
locBB               = location.belowbar
styleDO             = shape.labelup
txColDO             = color.white
sizeHr              = size.tiny

// Label Daily Open - 5PM EST. *Daily open is 5 PM the previous day
plotshape(showDOPENMon ? hour == 17 and minute == 0 and dayofweek == dayofweek.sunday : false, title="Sun Open", text="MO", color=#047b0d, location=locBB, style=styleDO, textcolor=txColDO, size=size.small, transp=5)
plotshape(showDOPENTue ? hour == 17 and minute == 0 and dayofweek == dayofweek.monday : false, title="Tue Open", text="TO", color=colDO, location=locBB, style=styleDO, textcolor=txColDO, size=sizeHr, transp=15)
plotshape(showDOPENWed ? hour == 17 and minute == 0 and dayofweek == dayofweek.tuesday : false, title="Wed Open", text="WO", color=colDO, location=locBB, style=styleDO, textcolor=txColDO, size=sizeHr, transp=15)
plotshape(showDOPENThur ? hour == 17 and minute == 0 and dayofweek == dayofweek.wednesday : false, title="Thur Open", text="RO", color=colDO, location=locBB, style=styleDO, textcolor=txColDO, size=sizeHr, transp=15)
plotshape(showDOPENFri ? hour == 17 and minute == 0 and dayofweek == dayofweek.thursday : false, title="Fri Open", text="FO", color=colDO, location=locBB, style=styleDO, textcolor=txColDO, size=sizeHr, transp=15)

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

if FirstBarOfMonth
    LabelText = tostring(month(time),"Month ")
    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)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

// ****   END    ***
If have an article on how to do this that would be helpful, too. I cannot find it on Kodify.
Thank you.

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

Re: Input function to disable script on higher time frame

You can't disable a script using an input but you can use an input or a condition to determine whether your output is displayed.

I think the simplest way to accomplish what you described is to add a timeframe.isintraday condition to your plotshape statements, That way they will only display when the current timeframe is intraday, not daily weekly or monthly.

Code: Select all

// Label Daily Open - 5PM EST. *Daily open is 5 PM the previous day
plotshape(timeframe.isintraday and showDOPENMon ? hour == 17 and minute == 0 and dayofweek == dayofweek.sunday : false, title="Sun Open", text="MO", color=#047b0d, location=locBB, style=styleDO, textcolor=txColDO, size=size.small, transp=5)
plotshape(timeframe.isintraday and showDOPENTue ? hour == 17 and minute == 0 and dayofweek == dayofweek.monday : false, title="Tue Open", text="TO", color=colDO, location=locBB, style=styleDO, textcolor=txColDO, size=sizeHr, transp=15)
plotshape(timeframe.isintraday and showDOPENWed ? hour == 17 and minute == 0 and dayofweek == dayofweek.tuesday : false, title="Wed Open", text="WO", color=colDO, location=locBB, style=styleDO, textcolor=txColDO, size=sizeHr, transp=15)
plotshape(timeframe.isintraday and showDOPENThur ? hour == 17 and minute == 0 and dayofweek == dayofweek.wednesday : false, title="Thur Open", text="RO", color=colDO, location=locBB, style=styleDO, textcolor=txColDO, size=sizeHr, transp=15)
plotshape(timeframe.isintraday and showDOPENFri ? hour == 17 and minute == 0 and dayofweek == dayofweek.thursday : false, title="Fri Open", text="FO", color=colDO, location=locBB, style=styleDO, textcolor=txColDO, size=sizeHr, transp=15) 

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

Re: Input function to disable script on higher time frame

inputMaxInterval = input(30, title="Hides sessions above specified time")
loadIndicator = timeframe.multiplier <= inputMaxInterval

I use the above in my script, I hope it helps
https://uk.tradingview.com/script/Jv0Rt ... -Openings/

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

Re: Input function to disable script on higher time frame

Hi @frien_dd,

Nice Script! A great example of how to use an input to enable/disable output.

Since your loadIndicator is using timeframe.multiplier it would still show output on Daily,Weekly,and Monthly charts. Since those periods have a multiplier of 1, which is less than your inputMaxInterval of 30. You could improve your load indicator by adding timeframe.isminutes. That would ensure that only timeframes in minutes <= 30 would be displayed.

Code: Select all

loadIndicator = timeframe.isminutes and timeframe.multiplier <= inputMaxInterval
Hope that helps.


It's a bit more complicated to use an input to enable/disable output above or below a specific timeframe since timeframe.period outputs a string value rather than an integer. There's probably a better way to do it but I would do something like the code below which allows you to choose which groups of timeframe's you want your output to display.

Code: Select all

DisplayTF = input(defval="Intraday",title="Show Output in sessions", options=["Intraday","Daily","Weekly","Monthly","DailyWeeklyMonthly"])

ShowOutput= 
  DisplayTF=="Intraday" ? timeframe.isintraday :
  DisplayTF=="Daily" ? timeframe.isdaily :
  DisplayTF=="Weekly" ? timeframe.isweekly :
  DisplayTF=="Monthly" ? timeframe.ismonthly :
  DisplayTF=="DailyWeeklyMonthly" ? timeframe.isdwm : na
  
plot(ShowOutput ? close : na)

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

Re: Input function to disable script on higher time frame

kmarryat wrote:
Fri Feb 19, 2021 2:48 pm
Hi @frien_dd,

Nice Script! A great example of how to use an input to enable/disable output.

Since your loadIndicator is using timeframe.multiplier it would still show output on Daily,Weekly,and Monthly charts. Since those periods have a multiplier of 1, which is less than your inputMaxInterval of 30. You could improve your load indicator by adding timeframe.isminutes. That would ensure that only timeframes in minutes <= 30 would be displayed.

Code: Select all

loadIndicator = timeframe.isminutes and timeframe.multiplier <= inputMaxInterval
Hope that helps.

[/code]
kmarryat,
Amazing thank you so much for your input for change. I have updated my script. Thanks again.

Return to “Pine Script Q&A”