Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop strategy from immediately entering again in same direction

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Stop strategy from immediately entering again in same direction

    How can I stop a strategy from re-entering a position in the same direction if the conditions are still in effect once the profit target is met? For example, my conditions are met for Set 1 which enters a long position. The profit target is set to X amount of ticks from the user input. Once the target has been hit the strategy will enter the trade again immediately if the conditions are still met.

    Once the target is met I would like for the strategy to only enter a trade again if the conditions for Set 2 which enters a short position are met or vice versa if the initial entry where a short.

    The only thing I have seen is stopping a strategy based on the P&L but that amount is going to vary based on the profit target in ticks inputted by the user. Also, if the script disabled the strategy because a profit target was hit I would not want it to miss entering a new position if conditions changed for a position in the opposite direction.

    What I would like is that once a position is completed by the strategy either due to hitting the profit target or the stop loss another trade is not entered unless it is in the opposite direction of the trade that was last exited. Is this possible and can anyone provide insight on how to add this with the strategy builder or how and what code I would need to enter manually? Thanks in advance for any help.

    #2
    One way to approach this is to keep track (with a Series<bool> or just variables that you set at the close of each bar) of whether the signal was valid on the previous bar. Only enter if the signal is "true" this bar but "false" last bar, so that you know it's a new one.

    When you do enter in a direction (which you can detect because Position.MarketPosition != MarketPosition.Flat or via OnPositionUpdate) just store in a variable that you're in that direction e.g. long or short, and if you only want to enter the opposite direction next time, you can make that one of the filter rules for your trade so if last time it was short, only longs are allowed this time, etc. if that is what you are trying to do.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Originally posted by QuantKey_Bruce View Post
      if you only want to enter the opposite direction next time, you can make that one of the filter rules for your trade so if last time it was short, only longs are allowed this time, etc. if that is what you are trying to do.
      Thank you, Bruce. This is precisely what I am attempting to do but I do not know how to go about storing the variable for the market position and applying the filter to determine whether only a long or shot is allowed this time. Is this something I would have to unlock the code for and add manually or can this be added through the strategy builder?

      Comment


        #4
        No, to the best of my awareness, this cannot be done in Strategy Builder. NinjaTrader Support, is there a way in Strategy Builder to achieve this without unlocking the code?
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Hello dlr258,

          You could use an int variable to determine what the last direction was.

          In your long set a group condition would be needed to check if the int variable is 0 or negative -1, in the action of the long set you would set the variable to positive 1 to note that a long trade happened.
          In your short set a group condition would be needed to check if the int variable is 0 or positive 1, in the action of the short set you would set the variable to negative -1 to note that a short trade happened.

          The variable would need a default value of 0 as no trade had happened initially. Each group condition could then work if no trade happened otherwise it would only work if the opposite value was seen in the variable due to the opposite condition triggering.

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello dlr258,

            You could use an int variable to determine what the last direction was.

            In your long set a group condition would be needed to check if the int variable is 0 or negative -1, in the action of the long set you would set the variable to positive 1 to note that a long trade happened.
            In your short set a group condition would be needed to check if the int variable is 0 or positive 1, in the action of the short set you would set the variable to negative -1 to note that a short trade happened.

            The variable would need a default value of 0 as no trade had happened initially. Each group condition could then work if no trade happened otherwise it would only work if the opposite value was seen in the variable due to the opposite condition triggering.
            This was very helpful and I believe I have this setup now. I will do some testing later in replay to confirm.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            20 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            119 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            63 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            41 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            45 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X