'How to prevent strategy looking at premarket bars using pinescript in tradingview?

I have t = time(timeframe.period, "1430-2100:12345")

But when my strategy counts previous bars at the start of the day it counts/looks at premarket bars.

I'm trading and index where I cannot turn off extended hours in the settings so is there a way to make the code ignore premarket bars?

`strategy("testing", overlay = true)

t = time(timeframe.period, "1430-2100:12345")

buy = low < low[1] and t sell = high > high[2] and t

short = high > high[1] and t cover = low < low[2] and t

strategy.entry("Long Entry", strategy.long, 1, when = buy) strategy.close("Long Entry", when = sell)

strategy.entry("Short Entry", strategy.short, 1, when = short) strategy.close("Short Entry", when = cover)

if (not t) strategy.close_all()`



Solution 1:[1]

You can use the session.ismarket or session.ispremarket built-in variables.

For example:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vitruvius

//@version=5
indicator("My script")

t = time(timeframe.period, "0900-2100:12345")
b = session.ismarket and t

plot(b ? 1 : 0)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vitruvius