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

How to determine the number of unfilled open positions

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

    How to determine the number of unfilled open positions

    Good Morning,

    I ran into a snag trading a few days ago where I accidentally ended up with 25 open orders at once. My code was basically reading my entry criteria and submitting orders on top of orders because I was checking to determine my total position quantity with Position.Quantity. And this was just specific to one order, and when unfilled it shows up as 0.

    So at this point Let's say that I have 5 open orders on different price levels, and I want my code to know that I have 5 total open orders and let's say my entry condition checks to make sure I have less than 5 open orders before submitting any additional orders. What I need is something like this:

    If ( Variable I need ) < 5 then Submit order, else Do nothing.

    What is the best way to get this? I have tried both Position.Quantity and PositionAccount.Quantity but they they only show me 0 unless I am filled, and then it is only specific to the order used at the time. Is there some sort of global method I can call to see my total unfilled size?

    Let me know if there is something out there that can do this.

    Thanks,

    Ian

    #2
    Hello,

    Thank you for the question.

    Generally, for tracking purposes, you could use Order objects to track orders that have not yet filled and are open.

    We have a sample of using Order objects for strategy management in the following posts:
    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

    When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until


    In both of these samples, the OnOrderUpdate and OnExecutionUpdate overrides are being used to capture orders and store them to a variable.
    When using an Order variable, you can check if the object is null to know that is has not been used, and once the order is located you can assign it to that variable so the variable is no longer null. This can be used logically to know which orders are still active and which are not. You can also use the overrides to know when one of these orders fills, cancels or gets rejected to reset its variable to null.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thank you for offering to help. I already use the order tracking methods you mentioned. I am going with the unmanaged approach...
      But I have yet to find anything that can aggregate my total quantity of open orders.

      For example, for a single entry I have something like this:

      if( Position.MarketPosition == MarketPosition.Flat && Short1 == null )

      But my issue is that I also have Short2, Short3, Short4, and Short5 as well as Long 1-5. Each of these gets submitted on a different price level K number of levels away from the best bid and ask. At any one point in time I only want to ever have only a maximum of 5 open orders.

      I am canceling and resubmitting these with various logic to try to achieve optimal position in the queue. The ones that don't have an optimal position in the queue are cancelled and resubmitted.

      But at any point in time, once I cancel and resubmit I need to know the total quantity of all open positions. Once this gets > 5, I will just wait until 1 more cancels occurs then I will resubmit.

      Basically I am trying to avoid having to build some custom logic to count each order and increment it up or down as orders are cancelled or added. This might be tricky, and I was hoping that there was some NT method that could quickly just gather the total open positions and give me a count.

      Is there anything out there like this, or would I need to code this logic myself?

      Thanks,

      Ian

      Comment


        #4
        Hello,

        Thank you for the reply.

        Yes in this case, if you already have the order object system in place you could implement other logic to count those orders that you have stored. This is not something there is a specific method for already but something you can control with your logic.

        The alternative approach would be to loop over the Orders collection in the Account object which would also require that you create logic to find open orders and ensure they are for this strategy. This would take more logic than just using what you have already created so I likely wouldn't suggest that. I would still suggest to track these using variables and use logic to identify if an order should or should not be placed at that time.

        As an open order is not yet a position, it would be in the Orders collection and not the Positions collection. Without being filled, you would need to have an instance of the order to know if it is still active or not. The OnOrderUpdate and OnExecutionUpdate give you a way to identify what has filled, what has been canceled or otherwise to delegate your logic and do counts etc.. The unmanaged approach is very aligned with using the Order objects as well, if you are trying to avoid using Order objects and logic surrounding them I couldn't suggest using the unmanaged approach.


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

        Comment


          #5
          Jesse,

          Thanks for the quick response. I will proceed to work on something custom using the Order objects then.

          Thanks,

          Ian

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fitspressoburnfat, Today, 04:25 AM
          0 responses
          2 views
          0 likes
          Last Post fitspressoburnfat  
          Started by Skifree, Today, 03:41 AM
          1 response
          4 views
          0 likes
          Last Post Skifree
          by Skifree
           
          Started by usazencort, Today, 01:16 AM
          0 responses
          1 view
          0 likes
          Last Post usazencort  
          Started by kaywai, 09-01-2023, 08:44 PM
          5 responses
          604 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          6 responses
          23 views
          0 likes
          Last Post xiinteractive  
          Working...
          X