Search code examples
pine-scriptpine-script-v5

How can I execute trade only between a specific time like 10h to 15h?


I want to open trade only between 10h and 15h but nothing seem to work?

I tried to put this on my if condition for strategy.entry

TimeWindow=time(timeframe.period,"1000-1500", "GMT")

I received this message

The expr1 parameter of the operator and function accepts a 'bool' argument. To avoid potential unexpected results, pass a 'bool' value or expression to this parameter.

TimeWindow (user-defined variable)

type series int


Solution

  • Here we go:

    Allowedhours = input.session(defval="1000-1500", title="Allowed hours")
    inSession = not na(time(timeframe.period, Allowedhours))
    
    if barstate.isconfirmed and enterLong and  inSession 
        strategy.entry('L', strategy.long,      alert_message= message_long )