Search code examples
pine-scriptpine-script-v5tradingalgorithmic-trading

Need Help / How to open only one trade per session?


I'm looking for a solution to my strategy. I want to open only one strategy.entry long and one strategy.entry short per session. The timeframe of my session looks like this.

sessionTime = input.session("0015-2245", title="Session time")
sessionZone = input.string("GMT+1", title="Session time zone")

inSession    = InSession(sessionTime, sessionZone) and timeframe.isintraday
sessionStart = inSession and not inSession[1]
SessionEnd = inSession[1] and not inSession

So there shouldn't be more than two new entries (1long Max/ 1Short Max) per day. Any ideas?


Solution

  • There are several ways of achieving this.

    One example would be using a var variable to check whether you have entered before in the same session. You should then reset this when it is a new session.

    var entered_before = false
    entered_before := sessionStart ? false : entered_before  // If it is a new session, reset the variable. Keep its value otherwise
    
    if (entry_condition and not entered_before)
        strategy.entry()
        entered_before := true
    

    Of course, you need two of these variables (one for short and one for long).

    Another idea would be to compare strategy.opentrades.entry_bar_index of the last trade with ta.barssince(sessionStart). If strategy.opentrades.entry_bar_index is less than ta.barssince(sessionStart), you would know that that entry took place in the current session.