Search code examples
pine-scriptpine-script-v5

Adding Trading session time in Pine Script v5


I want to initiate trade 10 minutes once the exchange opens (i.e., at 0925) and closes all open positions (Universal exit) 10 minutes before the exchange closes (i.e., at 01520). How can I do that? Please help.

I tried:

tradeTimes = input.session("0925-1520", title="Trading Times") But its not working.

Then tried:

session = input("0925-1520") t = time(timeframe.period, session)

As a result in the dialogue box a new label "input" appeared in which the session time was mentioned but trades were not taken accordingly.


Solution

  • You need to pass the session information you get from the use input to the time() function.

    session_time = input.session("1200-1500", "Session")
    is_in_session = time(timeframe.period, session_time + ":1234567")
    

    Then use is_in_session as part of your entry condition. You can do the same for your exits.

    Note: time() will use your exchange`s timezone. If you need a different timezone, you can pass the timezone information as a third parameter.

    time(timeframe, session, timezone) → series int
    

    For example:

    is_in_session = time(timeframe.period, session_time + ":1234567", "GMT+1")