Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trail Stop on only part of position?

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

    Trail Stop on only part of position?

    Hello,

    Is there a way to check whether certain entry orders are still active in a strategy?

    I have my strategy set to enter with five different orders, named "Entry01" to "Entry05". They all have their own profit targets. I also have a variable that is incremented based on an Indicator plot which is acting as my Trailing Stop.

    When price crosses the Trailing Stop value, I want it to exit all but one of the remaining open positions. I know how to exit everything, and I know how to close individual entry orders, but the Trailing Stop doesn't even become active until after the Indicator variable has gone past my entry point, by which time at least one or two of my profit targets have already been hit. I won't know how many of the TPs have been hit, though, so I need to individually close only some of the original orders.

    Rather than sending an individual exit order for all entries, thereby forcing an error on those that have already hit their targets, I want to check for the presence of the remaining orders and only send the relevant exit orders. I figure it would look something like this:

    Code:
         if(ExistsIn.Account.Positions​("Entry03")
           {
                       ExitShort("Entry03");
           }​
    but I have no idea what the real syntax for that would be in NinjaScript. Any suggestions?

    I have seen where some people use a foreach loop to cycle through the active positions, but then how do I compare the name of each position to the list of possible still actives, and only close up to "Entry04"?

    Thanks in advance!
    Last edited by NjTMatthew; 09-14-2024, 08:29 AM.

    #2
    Hello NjTMatthew,

    The most simple approach would be to use the signal name system and exit orders like you described, that would ignore orders where the position no longer exists. The other way would be to use OnOrderUpdate and OnExecutionUpdate to collect variables for the orders and clear variables based on order fills. You can see how to trace an order through its life in the following link.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello NjTMatthew,

      The most simple approach would be to use the signal name system and exit orders like you described, that would ignore orders where the position no longer exists.
      I try to avoid writing code that generates error messages, but after trying it I realized that it isn't really an error, it's just a notification that it's ignoring the instructions to close certain orders because they've already been closed. And since it doesn't affect the performance of the strategy otherwise, or even output anything other than a note in the Output window, I figured that that is an okay way to do it.

      Thanks!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      53 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      70 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X