3

Some of my rules (to detect something) gives false alarms for an hour after Market Open (or simply just not defined well enough anyways, ha-ha). Since I can locate the moment of Market Open, I can address bars relatively to it which is kinda cool. Is there a way to specify offset by time to make it undependent to (sub-hour) timeframe?

For example in my code, MarketOpen rule is true if the current the moment of Market Open is still in the current bar (so it addresses the bar of MarketOpen in any timeframe). In 1H timeframe, it would be simple: not(MarketOpen) would exclude the first hour, but it should work in sub-hour timeframes, too.

Vendrel
  • 865
  • 9
  • 19
  • Can you elaborate a bit your question ? Can you show us how your code specify offset by bars, so we can propose you how to do the same with time ? – G.Lebret Oct 22 '22 at 11:26
  • Thank you for trying to help this long after. :) It was long ago and I found a workaround in that specific case. How to define this Boolean variable: THING_HappenedTwoHoursAgoOrMore – Vendrel Oct 23 '22 at 12:14

1 Answers1

3

If you want to trade only in specific hours, you can use a session:

session = input.session("1100-2000")
inSession = not na(time(timeframe.period, session))

long = inSession and yourCondition

In any case, if you want to get the time of 1 hour after the market is open, you can use this:

newDay = ta.change(time("D"))
var float oneHourAfterMarketOpen = na

if newDay
    oneHourAfterMarketOpen := time + (60 * 60 * 1000)

canTrade = time > oneHourAfterMarketOpen

bgcolor(canTrade?color.green:color.red)
mr_statler
  • 1,913
  • 2
  • 3
  • 14