Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to programatically close all orders in position

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

    how to programatically close all orders in position

    If I have a position with some OCO orders, some not OCO, some pending - how do I properly programatically close position from within ninjatrader code? Let's say - I got indicator condition that tells me to close all orders I might have.

    Also, not sure, but I've heard that I need to close oldest order first and newest last in order to be in synch with current forex regulations.

    Note: I am using managed position. But it would be good to know how to close for both - managed and unmanaged.

    Thanks in advance...

    #2
    music_p13, were those positions you intend to close programmatically opend from within a NinjaScript strategy?

    Comment


      #3
      Yes, from the strategy...

      In Metatrader, for example, you could get all orders and write your code to close each of them. In NinjaTrader I would assume I also can get all pending and active orders and close all of them using iOrder object. I would assume that it shouldn't matter whether it is manual or automatic - I should be able to select every order for specific pair. Is it correct statement?

      Forgot to mention, I am referencing NinjaTrader 7.

      Comment


        #4
        music_p13,

        If you want to just close all positions you can just submit ExitLong() if you are long and ExitShort() if you are short. This will close all positions generated from that strategy.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thanks, I saw sample of it, but I have few questions on this:
          1st - will exitlong() exit all longs there are for a strategy (let's assume that I scaled in with few longs)?
          2nd - will it cancel pending orders in case there are (if for example if I had some buylimit orders somewhere)
          3rd - if I want to use unsafe/unmanaged approach - can I find all orders that are open/pending for specific pair (let's assume I didn't keep track of them) and close them one by one?

          Comment


            #6
            music_p13,

            1. Yes, if you do not specify any signal name to exit from specifically.

            2. No, you would need to keep track of pending orders and then CancelOrder as needed. Another approach is just to let orders expire at each bar's end (default behavior in NT) and then just don't resubmit at your time x for things to stop.

            3. You would need to keep track of the IOrders issued, so you have a reference to cancel these orders then with.

            Comment


              #7
              For point #3 - is there way to find all active iOrders if I didn't keep track of them, for both scenarios - iOrders opened by my strategy and iOrders opened if trading manually; and then to check the trading pair of each iOrder in the list?

              Comment


                #8
                If you're working in the new unmanaged order submission approach in NT7, you would need keep references yourself in the code. Unfortunately you can't access orders placed / working manually outside of your NinjaScript strategy.

                Comment


                  #9
                  If I had a power outage, or if NinjaTrader has suddenly crashed - then I would loose all references for iOrder objects. Is there way to find those iOrder objects in NinjaTrader programatically - or will I have to just manually continue manage this strategy?

                  Comment


                    #10
                    The strategy would recalculate on next restart then in this case and try to match off what is possible, it could be you need to sync manually depending on how long the outage was if the missed data produced any position changes that would need to be worked in.

                    Comment


                      #11
                      Can I keep track of all Tokens - and maybe even write them to txt file, and then if NinjaTrader crashed and then restarted - get those Tokens from txt file and verify if iOrder with that specific Token exists, and then to go from there? Or will old Tokens loose all relevance after NT restart?

                      Comment


                        #12
                        Unfortunately as the tokens would be recalculated if you restart the strategy, there's no persistence in NT 65.

                        If you're working with NT 7 beta, we've added strategy persistence features and you can compare then the IOrders directly without the need for the token ID.

                        Comment


                          #13
                          I am talking about NT7 beta only. Where can I read more about strategy persistence features? How and where do I store iOrders objects for the crash event? Can I still find iOrders by Tokens? I don't think I can store object to txt file - I would guess I need to store some object's property in order to find object later on by that property.

                          Comment


                            #14
                            In NT7 you can just work directly with the IOrders to compare for equality, if you're planning to use the unmanaged approach to submit / manage orders free of the order handling rules, please see this overview page for info on this approach and it's 'caveats' applying -

                            Comment


                              #15
                              However, main question remains - if NinjaTrader was shut-down while strategy was running and then started again - I will loose all iOrder objects I was keeping track of. So how can I find/select those iOrder objects back? Can I find them by token that I could store in .txt file or is there any other way to find them after NinjaTrader restart?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              666 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              377 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              110 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X