Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Unmanaged - Close all positions

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

    Unmanaged - Close all positions

    I'm working on error handling using Unmanaged orders and would like a command to Close all positions while leaving the strategy running. Is there a CloseAll() or something similar?

    #2
    Hello Tdschulz,

    Thank you for your note.

    There is not a method in NinjaScript that will close all positions. You will need to exit the orders with SubmitOrder() if you wish to exit the positions without disabling your strategy.

    There are unsupported methods to do this however. As with the following link to a post in a thread on closing all positions: http://www.ninjatrader.com/support/f...16&postcount=6

    The line of code at the link above should work for both Unmanaged and Managed approaches.

    Please let me know if I may be of further assistance.

    Comment


      #3
      Thanks for the reply. I believe FlattenEverything also closses the strategy though.

      Comment


        #4
        Originally posted by Tdschulz View Post
        Thanks for the reply. I believe FlattenEverything also closses the strategy though.
        The thread of which that is a post is exactly the question that you asked. You might want to read the whole thread.

        Comment


          #5
          I actually didn't even realize there was more to the thread. Thanks for that.
          I read through it and I think I understand your suggestion. However, doesn't the SubmitOrder need to be attached to an IOrder? For example, I have:
          Code:
          if (Position.MarketPosition == MarketPosition.Long)
          			{
          			if (targetOrderlong1 != null && targetOrderlong1 == order)
          				if (order.OrderState == OrderState.Rejected && order.Filled == 0)
          				{
          					targetOrderlong1 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "LongOCOID1", "RejectedOrderExit");			
          				}
          Am I making this overly complicated? This is my first Unmanaged attempt so I'm sifting through threads but there doesn't seem to be a vast amount of info which I've found.

          Comment


            #6
            Originally posted by Tdschulz View Post
            I actually didn't even realize there was more to the thread. Thanks for that.
            I read through it and I think I understand your suggestion. However, doesn't the SubmitOrder need to be attached to an IOrder? For example, I have:
            Code:
            if (Position.MarketPosition == MarketPosition.Long)
                        {
                        if (targetOrderlong1 != null && targetOrderlong1 == order)
                            if (order.OrderState == OrderState.Rejected && order.Filled == 0)
                            {
                                targetOrderlong1 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "LongOCOID1", "RejectedOrderExit");            
                            }
            Am I making this overly complicated? This is my first Unmanaged attempt so I'm sifting through threads but there doesn't seem to be a vast amount of info which I've found.
            I think that you may be making it more complicated than it is. I will repeat my earlier assertion that, given that your only real intent is to unconditionally exit your entire position, the only real question is: "Do I have a position on or not, and if so, how large is the position?" Once you answer that question, it is a matter of entering an order, with the correct quantity to exit the entire position. You can then cancel (if necessary) and/or nullify all IOrders, also unconditionally, using a null filter.
            Last edited by koganam; 01-06-2016, 08:16 PM. Reason: Corrected spelling.

            Comment


              #7
              koganam,

              How would you do this for multiple instruments. If I'm checking BarsInProgress == 1 and meet a condition to exit all positions and orders, I can't use Position.MarketPosition, as that'll only refer to the instrument related to BarsInProgress == 1.

              Or how would I create a method that would do this for me?

              So far I believe the solution is to use OnPositionUpdate and keep track of your positions and quantities through your own variables, but I'm still wondering if there's a cleaner way.

              Code:
              protected override void OnBarUpdate(){
              
                  if (BarsInProgress == 1){
              
                           if (condition to exit all positions){
              
                                   //Position.MarketPosition only refers to BarsInProgress == 1
                                    CloseFlatten();            
                            }
                  }
              
              private void CloseFlatten(){
                 //how do I implement this?
              }

              Comment


                #8
                Originally posted by :::grimReaper::: View Post
                koganam,

                How would you do this for multiple instruments. If I'm checking BarsInProgress == 1 and meet a condition to exit all positions and orders, I can't use Position.MarketPosition, as that'll only refer to the instrument related to BarsInProgress == 1.

                Or how would I create a method that would do this for me?

                So far I believe the solution is to use OnPositionUpdate and keep track of your positions and quantities through your own variables, but I'm still wondering if there's a cleaner way.

                Code:
                protected override void OnBarUpdate(){
                 
                    if (BarsInProgress == 1){
                 
                             if (condition to exit all positions){
                 
                                     //Position.MarketPosition only refers to BarsInProgress == 1
                                      CloseFlatten();            
                              }
                    }
                 
                private void CloseFlatten(){
                   //how do I implement this?
                }
                I am not sure that I see the issue here. If there are multiple instruments, each has a position, no? Are you saying that you want to exit for all BIP when only BIP == 1 signals an exit? If so, set a class bool variable to use as a test condition. Each BIP checks the selfsame bool, and exits accordingly.

                For each BIP, your CloseFlatten() does exactly as suggested: checks Position,Quantity, and sends the appropriate exit order.

                Otherwise, of course, your idea to keep a running tally of each instrument position using OnPositionUpdate() seems eminently viable.

                Comment


                  #9
                  Originally posted by koganam View Post
                  Are you saying that you want to exit for all BIP when only BIP == 1 signals an exit?
                  Correct. A calculation is performed involving other instruments to determine whether all positions should be abandoned . Likewise, I do the same for the other BIPs.


                  Originally posted by koganam View Post
                  For each BIP, your CloseFlatten() does exactly as suggested: checks Position,Quantity, and sends the appropriate exit order.
                  This is where I'm stuck. How do I ask Position (Position.MarketPosition) about a specific BIP?

                  Comment


                    #10
                    Originally posted by :::grimReaper::: View Post
                    ... This is where I'm stuck. How do I ask Position (Position.MarketPosition) about a specific BIP?
                    Have you tried keeping a running tally inside each BIP block?

                    if (BIP == n)
                    {
                    qty_n = Position.Quantity;
                    //etc
                    }

                    Then you can put the quantities in a List, and use foreach to iterate through the list, exiting the relevant quantity that is indexed in the list.

                    Even simpler, you can just iterate through the Positions array, that holds all the positions. ref: http://www.ninjatrader.com/support/h.../positions.htm

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by kevinenergy, Yesterday, 12:01 PM
                    6 responses
                    22 views
                    0 likes
                    Last Post kevinenergy  
                    Started by DawnTreader, 05-08-2024, 05:58 PM
                    15 responses
                    48 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by ZeroKuhl, Yesterday, 04:31 PM
                    7 responses
                    40 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by xepher101, Today, 12:19 PM
                    1 response
                    22 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by jpeep, 08-16-2020, 08:31 AM
                    16 responses
                    498 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Working...
                    X