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 Mindset, 04-21-2026, 06:46 AM
              0 responses
              87 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by M4ndoo, 04-20-2026, 05:21 PM
              0 responses
              132 views
              0 likes
              Last Post M4ndoo
              by M4ndoo
               
              Started by M4ndoo, 04-19-2026, 05:54 PM
              0 responses
              65 views
              0 likes
              Last Post M4ndoo
              by M4ndoo
               
              Started by cmoran13, 04-16-2026, 01:02 PM
              0 responses
              118 views
              0 likes
              Last Post cmoran13  
              Started by PaulMohn, 04-10-2026, 11:11 AM
              0 responses
              67 views
              0 likes
              Last Post PaulMohn  
              Working...
              X