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 CarlTrading, 03-31-2026, 09:41 PM
              1 response
              47 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by CarlTrading, 04-01-2026, 02:41 AM
              0 responses
              23 views
              0 likes
              Last Post CarlTrading  
              Started by CaptainJack, 03-31-2026, 11:44 PM
              0 responses
              33 views
              1 like
              Last Post CaptainJack  
              Started by CarlTrading, 03-30-2026, 11:51 AM
              0 responses
              51 views
              0 likes
              Last Post CarlTrading  
              Started by CarlTrading, 03-30-2026, 11:48 AM
              0 responses
              42 views
              0 likes
              Last Post CarlTrading  
              Working...
              X