Pinescript basic question: Open a trade at a set time each day

Viewed 381

I'd like to create a Pinescript strategy that opens a trade at 6pm ET each day. SL: 2xATR. TP: 2x SL.

I am struggling with setting a Boolean variable to be TRUE if the close time of the bar is 6pm ET.

I'm (obviously) very new to Pinescript and would appreciate any assistance.

best,

--Cris

1 Answers

This is possible via time() function. You can pass the necessary session you want and create a condition to only enter at the start of the session. Here's the code:

//@version=4
study("buy-first-bar-session", overlay=true)
sessionTime = input("1800-1800", "Session Time", type=input.session)
t = time("D", sessionTime) // 1440=60*24 is the number of minutes in a whole day. You may use "0930-1600" as second session parameter
is_first = na(t[1]) and not na(t) or t[1] < t

bgcolor(t ? color.blue : na)
plotshape(is_first, "Buy", style=shape.labelup, location=location.belowbar, textcolor=color.white, color=color.green, text="Buy")

enter image description here

Related