Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close all open positions in a strategy

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

    Close all open positions in a strategy

    Hello,

    I have a strategy that trades up to 10 positions of the same instrument. I would like to close all positions at once when a conditions is met. Is there syntax to close all open positions at once? Is there an example of this?

    Thanks,
    Tom

    #2
    Hi Tom, thanks for writing in. You can check the position and the position quantity to exit the full position e.g.

    Code:
    if(<condition to exit>)
    {
        if(Position.MarketPosition == MarketPosition.Long)
        {
            ExitLong(Position.Quantity);
        }
    }

    Comment


      #3
      Thanks Chris. Would this work with multiple entry signal names? The signal names are different.

      Comment


        #4
        Hi Tom, Yes, it will work on the full position size. You can test it out by printing the Position.Quantity value after the two entries have been made to confirm it equals the total position size.

        Kind regards,
        -ChrisL

        Comment


          #5
          I have a strategy that uses the ExitLong(Position.Quantity) that gets activated on a custom button click. It works fine even with my unique order names. However when I try the same code when I have a candle cross a moving average I get an error (There already is a matching order with same prices and quantity).The error keeps recurring and locks up the strategy. Any idea what I need to do?

          Comment


            #6
            Hello tak145,

            Is the order method being called multiple times in the logic?

            Enable TraceOrders and print the order.ToString() in OnOrderUpdate() to see when orders are being submitted.


            After reproducing attach the output (right-click > Save as) text file to your next post.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi ChelseaB,

              I attached the file. I tried troubleshooting after seeing the output but I'm still confused.

              I noticed I place an order and give it a SignalName. I follow that with stop and limit orders with the same SignalName but that method requires I use a FromEntrySignal. Those names agree but my original order has FromEntrySignal as blank " " as shown in the file. I suspect this is part of the problem. I don't know if EnterLong allows me to create a FromEntrySignal name.

              Now when I'm trying to exit, the Market order doesn't have my assigned SignalName or FromEntrySignal. I suppose this too would be a problem.
              Attached Files

              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