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

Sequence of events in backtesting

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

    #16
    Hi Cal,

    any updates on this issue?

    thanks

    Comment


      #17
      Maese,

      Yes.

      What is occurring is in the Multiple Instrument script the StopGoingDn order is placed first before the CloseGoingDn order. BarsInProgress 0, the 1/5 bar update is called and the FillType is called which see's the order being filled on the 1/6 bar at 1.76 for the StopGoingDn order. The logic for the CloseGoingDn is not met as this is not the BarsInProgress 1 update.

      In the single instrument, on the 1/5 bar update, the condition for the CloseGoingDn order is met and placed. Since this is a Market order it will get processed first from the fill type. The strategy is now flat and the Stop order is cancelled.
      Cal H.NinjaTrader Customer Service

      Comment


        #18
        Thanks Cal,

        it seems we agree on the diagnosis of what is happening.
        So back to my questions now:
        1. Why is the behaviour different in a multi-instrument framework and a single-instrument framework?
        2. How can I code the multi-instrument strategy to behave like the single-instrument one?

        regards

        Maese

        Comment


          #19
          Maese,

          You would need to custom develop you own fill type that would ignore working orders on the wrong BarsInProgress update.

          The reason it is currently set like this is because you can submit orders to another instrument so each bar update in a multi series is checking the orders for potential fills.
          Cal H.NinjaTrader Customer Service

          Comment


            #20
            "You would need to custom develop you own fill type that would ignore working orders on the wrong BarsInProgress update"

            When you write 'custom develop' do you mean writing an NT strategy with NT native methods? or do you mean writing some other customized C# methods?
            If the former, what methods should I use?

            Comment


              #21
              Maese,

              I mean that you would need to write your own script that would be its own Fill Type.

              You can view the current FillTypes by going to (My) Documents -> NinjaTrader 7 -> bin -> Custom -> Type -> @DefaultFillType and @LiberalFillType

              Please note that custom FillTypes are not supported with NinjaTrader and do not have any documentation on the matter
              Cal H.NinjaTrader Customer Service

              Comment


                #22
                I am quite surprised that coding a multi-instruments strategy becomes so complicated. Maybe we are misunderstanding each other.

                The following are 2 statements that I believe I got right from your explanations:
                - Non-executed orders for all instruments are processed on update of the primary bar
                - Market orders placed on close of a bar are filled at the opening of the next bar

                The next 2 are not so clear:
                - Are stop orders processed at the beginning or at the end of the primary bar update?
                - Are stop orders filled against the prices on the bar that just closed or the prices of the next bar?

                Could you please confirm?

                thanks

                Emilio

                Comment


                  #23
                  Maese,

                  All working orders are processed at the end of the bar update. However, in the FillType, market orders are processed first then the other order types.

                  Stop and Limit orders are filled against the next bar. Both FillTypes look a that the next bar for fills.
                  Cal H.NinjaTrader Customer Service

                  Comment


                    #24
                    "Stop and Limit orders are filled against the next bar"

                    Is this really how it works in backtest and CalculateOnBarClose=true?
                    That doesn't make sense to me because it would imply that the strategy can see in the future when backtesting.
                    This is how I think the sequence of events in backtesting should be:
                    1. At the close of today's bar check those stop and limit orders that were 'working' at the open of today's bar
                    2. Fill them against today's bar prices
                    3. If the strategy script triggers some new orders go and process them.

                    Comment


                      #25
                      Hello Maese,

                      Thank you for your response.

                      In backtesting the orders are filled on historical data, this means after the bar closes. With CalculatOnBarClose = true in real-time trading the orders would fill on the intra-bar data.

                      Comment


                        #26
                        "In backtesting the orders are filled on historical data, this means after the bar closes"

                        Let me see if I got it precisely right with an example. Let's say I have a strategy running in backtest from 01JAN14 to 30SEP14
                        - At the close of 20AUG14 the strategy placed a stop order to close a long at 95. The order got to the 'working' status
                        - The high and low for 21AUG14 were at 105 and 100.
                        - The high and low for 22AUG14 were at 100 and 90.

                        In backtesting mode I would expect that system only knows the stop order is filled after the 22AUG12 bar is completed. Correct?

                        If the above is correct, then what piece is processed first: the code inside the OnBarUpdate() method, or the filling of the stop order?

                        thanks

                        Maese

                        Comment


                          #27
                          Maese,

                          The OnBarUpdate() will get called first. If there is an order submission in the middle of the OBU, then that order will get processed, meaning the order will go through the order submission steps, PendingSubmit, Accepted, Working. When the order reaches Working or Accepted, then the FillType is called and checks for fill price on the next bar. The reason for the next bar is because the last bar has closed and we are in between the closed bar and the next bar.

                          If the order cannot be filled, then we move on to the next bar processing. OnBarUpdate() gets called then FillType is called again for the working orders left over. You would know the Stop order is filled before the 22nd bar update is called.

                          After the 21st, the bar update is called and the FillType is processing the working order for the fill on the next bar.
                          Cal H.NinjaTrader Customer Service

                          Comment


                            #28
                            Thanks a lot Cal

                            I think I fully understand the details now.
                            With all your help I have managed to code the multi-instrument strategy to behave as I expected.
                            I'm attaching here the script in case someone wants to use it as an example.

                            Maese
                            Attached Files

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by kujista, Today, 06:23 AM
                            4 responses
                            14 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by traderqz, Yesterday, 09:06 AM
                            2 responses
                            16 views
                            0 likes
                            Last Post traderqz  
                            Started by traderqz, Today, 12:06 AM
                            3 responses
                            6 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by RideMe, 04-07-2024, 04:54 PM
                            5 responses
                            28 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by f.saeidi, Today, 08:13 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Working...
                            X