0

I'm working on a TradingView script (Pine) and i'm trying to use my Daily-bar strategy on the 5-minute chart. To do this, I need to basically only check conditions once per day.

I can do this by having a boolean variable such as dailyCheck = false and set it to true when I run that code and then reset it on a new day.

Does anyone know how to go about doing this? From what I read in the pine manual it says you can get unix time...but I don't know how to work with this and I can't print anything except numbers in the form of plot, so I can't figure out how to tell when a new day has started. Thanks in advance!

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Wayne Filkins
  • 317
  • 1
  • 5
  • 14

1 Answers1

4

Version 1

There are lots of ways to detect a change of day. The Session and time information User Manual page shows a few.

I like detecting a change in the dayofweek or dayofmonth built-in variables:

//@version=4
study("Once per Day")
var dailyTaskDone = false
newDay = change(dayofweek)
doOncePerDay = rising(close, 2)     // Your condition here.
dailyTaskDone := doOncePerDay or (dailyTaskDone and not newDay)

plotchar(newDay, "newDay", "▼", location.top, transp = 60)
plotchar(doOncePerDay, "doOncePerDay", "•", location.top, transp = 0)
bgcolor(dailyTaskDone ? color.orange : na)

enter image description here

Version 2

Following Michel's comment, this uses a more robust detection of the day change:

//@version=4
study("Once per Day")
var dailyTaskDone = false
newDay = change(time("D"))
doOncePerDay = rising(close, 2)     // Your condition here.
dailyTaskDone := doOncePerDay or (dailyTaskDone and not newDay)

plotchar(newDay, "newDay", "▼", location.top, transp = 60)
plotchar(doOncePerDay, "doOncePerDay", "•", location.top, transp = 0)
bgcolor(dailyTaskDone ? color.orange : na)

And for the OP, a v3 version:

//@version=3
study("Once per Day v3")
dailyTaskDone = false
newDay = change(time("D"))
doOncePerDay = rising(close, 2)     // Your condition here.
dailyTaskDone := doOncePerDay or (dailyTaskDone[1] and not newDay)

plotchar(newDay, "newDay", "▼", location.top, transp = 60)
plotchar(doOncePerDay, "doOncePerDay", "•", location.top, transp = 0)
bgcolor(dailyTaskDone ? orange : na)
Community
  • 1
  • 1
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Do you happen to know how to do it in v3? – Wayne Filkins Jan 23 '20 at 03:16
  • Ohh dayofweek...I didn't even know that existed and I been using pine for a couple of years haha. I think I got it now, these are all in v3 too :) TY! – Wayne Filkins Jan 23 '20 at 03:19
  • Luc, it might happen that there no trades in a week (e.g. there were long holidays), so there won't be changes in `change(dayofweek)`. IMHO `change(time("D"))` is more precise here. – Michel_T. Jan 23 '20 at 06:14
  • Ah y, see what you mean. Would require the holiday to start on one weekday and end on the preceding one, right? In that case `dayofmonth` would be more robust I guess, but I get your point. Thx for the info. Appreciate. – PineCoders-LucF Jan 23 '20 at 06:48