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

MarketPosition Question

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

    MarketPosition Question

    How soon after order fulfillment does Position.MarketPosition get updated and what triggers the update?

    Is it possible to force the update of Position.MarketPosition?

    I have an algorithm that allows for re-entry immediately after exit. It appears that Position.MarketPosition is not being updated quickly enough to catch entries and exits that happen within a few seconds of each other.

    For example. I start from MarketPosition.Flat. I EnterShort and immediately fire an ExitShortLimit and ExitShortStop. Shortly after the stop is hit. The logic then immediately does a short reentry. Then a few seconds later I ping Position.MarketPosition shows that I am short two contracts. Apparently the stop Position.MarketPosition did not "see" the stop being hit.

    So. triggers the update of Position.MarketPosition?

    #2
    Hello x703jko,

    Thank you for your post.

    A position update event will call the OnPositionUpdate() method. It would be recommended to call the position from OnPositionUpdate() to see the correct position once updated. There is no means to force the event to occur.

    There can be cases where updates occur in a rapid succession causing over fills or incorrect reporting. I can review the strategy if you would like to see if I can find any items that may better assist you in the logic.

    You can attach your strategy to your response by going to File > Utilities > Export NinjaScript > Export selected source files > select your Strategy > select the right arrow > Export. The file will be located under (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript.

    Comment


      #3
      I will have to prepare a stripped down version of the code. Give me a few hours to do this/

      Thanks

      Comment


        #4
        Originally posted by NinjaTrader_PatrickH View Post
        Hello x703jko,

        Thank you for your post.

        A position update event will call the OnPositionUpdate() method. It would be recommended to call the position from OnPositionUpdate() to see the correct position once updated. There is no means to force the event to occur.

        There can be cases where updates occur in a rapid succession causing over fills or incorrect reporting. I can review the strategy if you would like to see if I can find any items that may better assist you in the logic.

        You can attach your strategy to your response by going to File > Utilities > Export NinjaScript > Export selected source files > select your Strategy > select the right arrow > Export. The file will be located under (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript.
        Patrick. I went back and added some diagnostic statements to my code and I found where the problem was.

        My code includes the ability to immediately re-enter after a stop or target fill (assuming my entry criteria are met). The code is in my OnExecution alg so I can re-enter as soon as I process the stop or target execution.

        My primary entry logic (when entering from a flat market) is on OnBarUpdate.

        So, in the particular instance where I was having an issue, the re-entry order was placed, but MarketPosition is showing flat since the previous order was closed. I then re-entered, after which an OnBarUpdate occurs. However, at that time the re-entry order had not reached the filled state, so the MarketPosition was still flat. Then my primary code saw a flat market and attempted another entry. Once MarketPosition caught up, I had two positions when I should have only had one.

        And, since there were then no Stop or Target orders, the position never exited.

        The solution is to check to insure that, before entry, entryOrder is null in addition to seeing a flat market.

        My code is now doing what I intend.

        However, I now have another question. I understand that OnOrderUpdate runs before OnExecution. However, I see MarketPosition changing from flat to Long or Short (depending on my entry alg) when OnOrderUpdate shows an entryOrder reaching filled but before OnExecution fires. This would imply that if I wanted the lowest latency to entering stop and target orders the logic should be in OnBarUpdate and not OnExecution. This is opposite to what Josh says in the following link:

        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()


        I am now confused...should I leave my stop and target entries in OnExecution or move them to OnOrderUpdate?

        Comment


          #5
          Technically in your specific circumstance you want to wait until the Position Update shows the position as you wish. You could use OnBarUpdate() to check Position.MarketPosition or you could use OnPositionUpdate(): http://ninjatrader.com/support/helpG...tionupdate.htm

          Comment


            #6
            Originally posted by NinjaTrader_PatrickH View Post
            Technically in your specific circumstance you want to wait until the Position Update shows the position as you wish. You could use OnBarUpdate() to check Position.MarketPosition or you could use OnPositionUpdate(): http://ninjatrader.com/support/helpG...tionupdate.htm
            In my specific case checking the position during OnBarUpdate gave the incorrect answer as there was an entry order pending. Since it wasn't filled the MarketPosition was flat so my logic tried to enter. What I needed to do was to check if entryOrder was null first.

            But, my larger question is that the same order, after I fixed the problem is showing MarketPosition not flat (long and short) while the order was filled (as shown in OnOrderUpdate) before OnExecution is called? I can understand that the execution took place before OnExecution was called, but does that not imply if I want to do stop and target orders or re-enter after a stop or target fill, that I would be better off (timing wise) doing it on OnOrderUpdate rather than OnExecution since OnOrderUpdate is called before OnExecution? Which leads me to the same question I posed in another thread - which method is better if I want the fastest action after a change?

            Comment


              #7
              Hello x703jko,

              I would suggest testing both in OnExecution and OnOrderUpdate through the real-time data on the Sim101 account or using Market Replay connection to test out which would work faster.

              The reason OnExecution is suggested is that this is when the execution event is processed versus just the order updating (whether the order is filled or not).

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                Hello x703jko,

                I would suggest testing both in OnExecution and OnOrderUpdate through the real-time data on the Sim101 account or using Market Replay connection to test out which would work faster.

                The reason OnExecution is suggested is that this is when the execution event is processed versus just the order updating (whether the order is filled or not).
                Understand the difference between the two methods. What I am asking is why market update is changing before execution.

                I see this during backtest. Not sure if that makes a difference (I can see why it might).

                Comment


                  #9
                  Hello x703jko,

                  Thank you for your response.

                  OnExecution would be the queued execution report, if OnBarUpdate() is queued first and the position has updated then it will be present.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by gentlebenthebear, Today, 01:30 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by Aviram Y, Today, 05:29 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by cls71, Today, 04:45 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by TradeForge, Today, 02:09 AM
                  1 response
                  22 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by elirion, Today, 01:36 AM
                  2 responses
                  14 views
                  0 likes
                  Last Post elirion
                  by elirion
                   
                  Working...
                  X