Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to know if an 'entry' order filled is closed after an 'exit' execution

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

    how to know if an 'entry' order filled is closed after an 'exit' execution

    Hello, I am working on an indicator that requires to know which orders are in/out for each 'entry'

    I am using OrderUpdate and OnExecutionUpdate events to track my orders and executions, I am storing them in Dictionaries using order.Id as key to keeping track, but the issue I am facing when closing one order using an order in the opposite direction, I still see 'entry' order as when initially filled to open the position without any modification, and I know the 'exit' order closed one of my open orders, but no which one/.

    For example:

    Adding 1 entry + ATM (stop and target) = ID: 1
    Adding 1 entry + ATM (stop and target) = ID: 2
    Adding 1 entry + ATM (stop and target) = ID: 3

    Adding a 1 Exit -- opposite order creates an 'exit' Order ID:4 and closing one of the open positions and closing the ATM -1 or order = ID:3 remaining orders ID:1 and ID:2,
    but when I track OrderUpdate (using order.Id ) or OnExecutionUpdate (Execution execution in account.Executions or execution.Order), I can still see all the orders and order.Id 3 used as 'entry' still has all the same properties without any change as before the 'exit' was executed.



    I would greatly appreciate any suggestions or insights on how to determine which 'entry' filled order has actually been closed after the opposite direction 'exit' order is filled.

    Regards,
    Ale



    #2
    Hello Ale,

    Thank you for your post.

    I would like some additional context to make sure I am understanding your inquiry correctly. Are you referring to ATM entries that are submitted manually or ATM entries that are submitted programmatically from a NinjaScript? Are you subscribing to Order and Execution updates from within your indicator using the Account class or are you creating the dictionaries from another script and accessing them from your indicator?

    If you go to Control Center > New > Trade Performance and review the "Trades" display, are you looking to have your script understand how the entries and exits are paired up in a similar way to how it is reported?

    I look forward to your reply.

    Comment


      #3
      ​ Hi Emily, I am submitting orders and using the ATM manually (selected in the chart trader). Then, I use an indicator to track orders and executions when using the ATM for 'entry', 'exits', 'close', 'stop1', and 'target1'. Every time an order is placed, I track OnOrderUpdate and OnExecutionUpdate. For the 'entry' and subsequent 'stop1' and 'target1', I use the account class and store the Account.Orders​ properties in dictionaries.

      My 'issue' lies in handling the 'entry' after an order is closed in order to identify if the 'entry' was closed for a single open position. It is easy to achieve this if I only have one open position. However, if I have multiple open positions and they are closed in a different order than they were opened, how can I identify which 'entry' is still active and which one is not?

      for example, I know the 'stop1' and 'target1' are canceled after a 'exit'
      but If I re-read Account.Orders​, I see 'entry' and filled; yes, it was filly initially when I opened the position, but there is somewhere something to know if it is active or not. I tried checking OnExecutionUpdate, and it still is there showing the same; I could count the in/out positions, but It will not be accurate to know if a specific order was closed.

      Click image for larger version

Name:	orders.png
Views:	449
Size:	410.8 KB
ID:	1251149
      Regards,
      Ale
      Last edited by alejandro888; 05-11-2023, 04:43 PM.

      Comment


        #4
        Why can't you just use https://ninjatrader.com/support/help...onquantity.htm or https://ninjatrader.com/support/help...etposition.htm to see if the ATM still has an open position?
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          I don't think OP has any access to that information. He's tracking orders
          using the Account methods, which are able to see the raw events as they
          happen -- I don't believe these Account methods supply the 'atmStrategyId'
          needed to do the lookups you're suggesting.

          Comment


            #6
            Filled is a terminal state.
            The order and order state does not change further.

            Whether the position created by that fill is still 'active',
            that is not available by looking at the order.

            Comment


              #7
              @QuantKey_Bruce​ I am in an indicator namespace; using these methods not from strategy only? It should be possible to use from an indicator adding and strategy namespace, but will it behave well?

              I found a workaround by changing OrderState to Cancelled to the 'entry' order tied to the 'stop1' and 'target1', but I am not sure if it will be the safest option because If, for any chance, I got the wrong order, its possible I will be ending with a 'ghost' open position.

              @bltdavid​ If it filled state is a terminal state, it won't harm, won't harm changing in that way?​

              Comment


                #8
                Originally posted by alejandro888 View Post
                @QuantKey_Bruce​ I am in an indicator namespace; using these methods not from strategy only? It should be possible to use from an indicator adding and strategy namespace, but will it behave well?

                I found a workaround by changing OrderState to Cancelled to the 'entry' order tied to the 'stop1' and 'target1', but I am not sure if it will be the safest option because If, for any chance, I got the wrong order, its possible I will be ending with a 'ghost' open position.

                @bltdavid​ If it filled state is a terminal state, it won't harm, won't harm changing in that way?​
                I don't understand, how does changing the entry order state to Cancelled serve
                as a workaround?

                How are you determining that (see red) relationship between the three orders?

                I'm not sure exactly, I suspect these Order objects are still in use elsewhere in
                NinjaTrader .. but I don't know where.

                Comment


                  #9
                  @bltdavid​ I am taking anything else than 'Cancelled' as an active order, so in that way, I could track which ones are active and which are not.
                  About the relationship between the three orders, I use the last 'entry' to tie the 'stop' and 'target' order Ids; if one gets canceled now, I can cancel the related 'entry.' I still have to do a lot of testing to confirm it is a reliable way

                  Comment


                    #10
                    Ah, ok ... thanks.

                    Are you trying to catch all orders from all sources,
                    or just orders from Chart Trader?

                    What happens when a order is submitted via Chart
                    Trader with ATM set to 'None'?

                    Comment


                      #11
                      Originally posted by NinjaTrader_Emily View Post
                      If you go to Control Center > New > Trade Performance and review the "Trades" display, are you looking to have your script understand how the entries and exits are paired up in a similar way to how it is reported?
                      +1

                      Hi Emily, could you address this? I'd appreciate any insights you can offer.

                      Comment


                        #12
                        I think I am following now. I misunderstood what you are trying to do at first. If you seek an alternative to changing them to canceled as a way of flagging them as finished, what if you just kept a List<Order> of the ones you've processed already, so when you see an order you could check if it's a new one? I might still not be completely following the problem, but that's my current thought.
                        Bruce DeVault
                        QuantKey Trading Vendor Services
                        NinjaTrader Ecosystem Vendor - QuantKey

                        Comment


                          #13
                          Originally posted by bltdavid View Post

                          +1

                          Hi Emily, could you address this? I'd appreciate any insights you can offer.
                          Hello David and Ale,

                          Thank you for your patience.

                          I brought up the "Trades" display in the Trade Performance report because of how entries and exits are paired for reporting and the main concern here seems to be identifying which entries are tied to active/open positions and which entries are tied to close positions. Ale had mentioned, "However, if I have multiple open positions and they are closed in a different order than they were opened, how can I identify which 'entry' is still active and which one is not?"

                          The answer is tricky due to FIFO (First in first out) optimization:


                          Another factor at play here is that you are working with manually placed ATM strategy orders and not programmatic orders. When scaling in/out of ATM strategy positions or opening multiple ATM strategies in the same market, the ATM Strategy Selection Mode also comes into play:


                          There are no documented methods/properties using the Account class (Add-on approach) specific to identifying manual ATM strategy information. NinjaScript is already very limited with its interactions with ATM strategies that are placed programmatically from a script rather than manually, so monitoring manual ATM strategy information is not documented and goes beyond the support that we are able to offer. You could print information from the Account class such as Order, Execution, and Position information and observe how the information is updated when manually placing ATM strategy orders and scaling in/out or closing positions. You could even write the output to a file using StreamWriter and then see if you are able to make connections related to how the Order/Execution/Positions are reported.

                          There are not any supported methods for identifying the selected ATM strategy from the Chart Trader, though you could try gathering information about the selected ATM strategy from the Chart Trader as discussed here:


                          Otherwise, you could also try adding NinjaTrader Controls to an Add-On script and use those controls to place orders. You could keep track programmatically of which orders are submitted via the add-on's controls and that could help you with additional insight into when orders are scaled in/out and which strategy is applied to which orders:
                          Please let me know if I may be of further assistance.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          646 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          367 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          108 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          570 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          573 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X