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

Help backtesting strategy with intrabar granularity

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

    #16
    Hello nrgtrader,

    If you are placing the exit orders in OnExecution then you are not waiting for the next bar to place the exits, you are placing the exits immediately after the entry order has filled (intra-bar). If an order is placed intra-bar it can fill intra-bar.

    OnExecution triggers when an order fills, not when a bar closes.

    Instead of placing the exit immediately after the entry has filled, place the order after the bar has closed in OnBarUpdate as previously suggested.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      Hi NinjaTrader_ChelseaB. I'm expecting the entry order to fill only after the close of the signal bar, which in this case is the bar in which the orders took place!

      The signal does not trigger until the primary bar closes. Then I would expect the order to be submitted and fill only if price would have reached that limit level.

      So in this case (illustrated with the image in post #10), the signal bar closed at 31.16 at 9:46:55. The entry should at that point be submitted, working to be filled at 30.83.

      In this case, it shouldnt have filled!

      It could be I am not thinking thought the coding logic correctly, but I believe I just followed the example file I mentioned earlier, just substituting my specifics.
      Last edited by nrgtrader; 02-06-2017, 05:10 PM. Reason: important: shouldve to should NOT!

      Comment


        #18
        The sequence I want is:
        1. Signal bar close (BarsInProgress == 0)
        2. Entry submitted (BarsInProgress == 1)
        3. Stoploss/Profit target submitted. OnExecution(IExecution execution)

        Keeping stoploss in OnExecution seems important as its seems the most direct method to submit no?

        Comment


          #19
          NinjaTrader_ChelseaB I apologize if I'm not doing a good job of explaining the source of my confusion. Regardless of signal trigger and order sequence, it seems that a reference to price itself is missing from the backtest results.

          We know that the market price was at 31.16 at the close of the signal bar at 9:46:55. So how is it that the limit entry order was filled at a price 30.83 with a timestamp also of 9:46:55, and also the target exit was filled at 30.95, again with a timestamp of 9:46:55?

          Please let me know what I'm overlooking. Thanks in advance!

          Comment


            #20
            Hello nrgtrader,

            The entry order is being placed after the primary bar closes.

            The exit order is not being placed after the primary bar closes it is being placed in OnExecution intra-bar after the entry fills.

            The issue is that the exit is being placed intra-bar and filling intra-bar, is this correct?

            I have suggested that you move the code for the exit order to OnBarUpdate instead of OnExecution().

            May I see that you have made that modification in code?

            Please attach an export of the updated script.


            Or perhaps I'm not understanding the issue. Why have you have added intra-bar granularity?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #21
              Hi NinjaTrader_ChelseaB I have followed the example demonstrated here: http://ninjatrader.com/support/forum...ad.php?t=18890, placing the exit orders in OnExecution. As far as I can tell, I want the exit orders to be submitted immediately after (and only if) the entry order is filled, thus in OnExecution.

              The issue it seems to me, whether the exit orders are placed OnBarUpdate or OnExecution, is that they are being filled without reference to the price data in BarsInProgress==1. That is to say, the price was 31.16 at 9:46:55, so it would be impossible to fill a buy order with a limit of 30.83 at 9:46:55.

              Furthermore, I don't understand how the entry is filling intrabar (BarsInProgress==0) if the signal bar needs to close before the signal is generated! It seems to follow that the fastest entry possible for (BarsInProgress==1) would be the first tick after the close of the signal bar (BarsInProgress==0).

              This issue could boil down to my not correctly understanding how the backtest or code is designed to function -- please let me know.

              PS I've added intrabar granularity to be able to accurately track trades that where the entry and exit fill intrabar, as illustrated on post #1. In that case by the way, the signal bar was a couple bars back.
              Last edited by nrgtrader; 02-07-2017, 09:40 AM. Reason: The target order with a limit of 30.95 would have obviously filled if price was above it, but that order shouldnt have tiggered without the entry being filled at the lower price, which was impossible

              Comment


                #22
                Originally posted by nrgtrader View Post
                The issue it seems to me, whether the exit orders are placed OnBarUpdate or OnExecution, is that they are being filled without reference to the price data in BarsInProgress==1. That is to say, the price was 31.16 at 9:46:55, so it would be impossible to fill a buy order with a limit of 30.83 at 9:46:55.
                ...Its really the entry order that is not seeming to reference price data in BarsInProgress==1. The sell limit target order would have filled if price was above it, as was the case here.

                Comment


                  #23
                  Hello nrgtrader,

                  Code:
                  The issue it seems to me, whether the exit orders are placed OnBarUpdate or OnExecution, is that they are being filled without reference to the price data in BarsInProgress==1
                  Yes, I think this is where the issue lies. The orders are being submitted intra bar. Which BarsInProgress value is used in the order submission?

                  Would you like to focus on the entry instead of the exit?

                  Please add a print to the script that prints the time of the processing bar when the entry is triggered. Add another print that prints the execution time of the entry in OnExecution.
                  Include the output from the output window with your post.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Here's my entry code and location/condition structure. bIPindex is set to 1.

                    protected override void OnBarUpdate()
                    {
                    if (BarsInProgress == 0)
                    {
                    if (longSignal)
                    {
                    entryOrder = EnterLongLimit(bIPindex, true, 1, limitEntry, entryName);

                    And the output prints:
                    Code:
                    2/3/2016 9:46:55 AM longSignal = True
                    2/3/2016 9:46:55 AM Order='NT-00019/Backtest' Name='Long' State=PendingSubmit Instrument='CLM 02-17' Action=Buy Limit price=30.83 Stop price=0 Quantity=1 Strategy='STRATEGY' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='52395b24ae4f4b36a8d1b77bbcba271d' Gtd='12/1/2099 12:00:00 AM'
                    2/3/2016 9:46:55 AM Order='NT-00019/Backtest' Name='Long' State=Accepted Instrument='CLM 02-17' Action=Buy Limit price=30.83 Stop price=0 Quantity=1 Strategy='STRATEGY' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='52395b24ae4f4b36a8d1b77bbcba271d' Gtd='12/1/2099 12:00:00 AM'
                    2/3/2016 9:46:55 AM Order='NT-00019/Backtest' Name='Long' State=Working Instrument='CLM 02-17' Action=Buy Limit price=30.83 Stop price=0 Quantity=1 Strategy='STRATEGY' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='52395b24ae4f4b36a8d1b77bbcba271d' Gtd='12/1/2099 12:00:00 AM'
                    2/3/2016 9:46:55 AM Order='NT-00019/Backtest' Name='Long' State=Filled Instrument='CLM 02-17' Action=Buy Limit price=30.83 Stop price=0 Quantity=1 Strategy='STRATEGY' Type=Limit Tif=Day Oco='' Filled=1 Fill price=30.83 Token='52395b24ae4f4b36a8d1b77bbcba271d' Gtd='12/1/2099 12:00:00 AM'
                    2/3/2016 9:46:55 AM Execution='NT-00012' Instrument='CLM 02-17' Account='Backtest' Name='Long' Exchange=Default Price=30.83 Quantity=1 Market position=Long Commission=0 Order='NT-00019' Time='2/3/2016 9:46:55 AM'

                    Comment


                      #25
                      NinjaTrader_ChelseaB I followed the example here http://ninjatrader.com/support/forum...ead.php?t=6652 putting the entry order in if BarsInProgress == 0, even though it seemed questionable. I figured because the order references that data series it doesn't matter? -- And also becuase NinjaTrader_JoshP did it that way ;-)
                      Last edited by nrgtrader; 02-07-2017, 01:52 PM. Reason: that data series = BarsInProgress == 1

                      Comment


                        #26
                        Hello nrgtrader,

                        The orders are realistic. Targets and stops can fill intra-bar. Adding intra-bar granularity allows for this.

                        It seems that you want the intra-bar granularity but you don't want your script to behave as if it were processing real-time data.

                        You have modeled your script after an example that allows for orders to be placed and fill intra-bar, which is intended as this is how it would happen on a real-time data feed.

                        You can change your logic, however, to only allow actions such as placing the exit to occur after bars close. This would not be how the example you are using is designed.


                        So for the entry order, the buy limit order appears to be submitted at 9:46:55 at a price of 30.83 and fills immediately.
                        What is the time of the bar close that has triggered the order?
                        What is the time of the next bar on the chart?
                        What was the high, low, and close price when the order is submitted?
                        What was the high, low, and close price when the order filled in OnExecution?

                        The order is placed to barsInProgress 1, so this would fill using the 1 tick added series for data.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #27
                          NinjaTrader_ChelseaB,

                          I will respond the best I can to the individual questions and why it still doesn't make sense to me.

                          The orders are realistic. Targets and stops can fill intra-bar. Adding intra-bar granularity allows for this.
                          I do want the orders to be able fill intrabar, but I don't see how the entry order is possible in this example. Please see the illustration attached to this post: the market price never reached the buy limit entry, which was placed at the close of the signal bar.

                          It seems that you want the intra-bar granularity but you don't want your script to behave as if it were processing real-time data.
                          Please explain -- I do want the script to run as if it were processing real-time data!

                          You have modeled your script after an example that allows for orders to be placed and fill intra-bar, which is intended as this is how it would happen on a real-time data feed.
                          This is what I want, but the backtest results do not reflect what would have occurred in a real-time situation.

                          So for the entry order, the buy limit order appears to be submitted at 9:46:55 at a price of 30.83 and fills immediately.
                          Yes, this is the problem: Look at the price at the close of the signal bar and the time stamp -- it closes with a price of 31.16 at 9:46:55. How would a buy limit entry working at 30.83 fill when the market price was at 31.16?

                          What is the time of the bar close that has triggered the order?
                          9:46:55

                          What was the high, low, and close price when the order is submitted?
                          For what periodicity?

                          ...Seeing as that the order was submitted at the close of the signal bar, the next BarsInProgress == 0 bar would just be forming at the time the order was entered. If you meant the HLC of the signal bar we have 31.16, 30.74 and 31.16. And we can see that the bar of which the entry should have logically executed BarsInProgress == 0 completed without ever having reached the limit entry level -- this is my concern.

                          What was the high, low, and close price when the order filled in OnExecution?
                          Again -- for what periodicity? The 1 tick series is what we're referencing here, which would only have one price: the execution price, which was 30.83...

                          The order is placed to barsInProgress 1, so this would fill using the 1 tick added series for data.
                          Correct, thats what I want. I hope this helps, please let me know what next!
                          Attached Files
                          Last edited by nrgtrader; 02-08-2017, 10:52 AM. Reason: edited for clarity

                          Comment


                            #28
                            Hello nrgtrader,

                            When the execution occurs in OnExecutionUpdate(), print the price of the current tick so that we may see what the close price is when the order fills.

                            The other thing you could do is show a 1 tick ask chart between 9:46:52 and 9:46:55 so that we can see no ticks are below 30.83 during this time.

                            The order in your screen shot is above the low of the bar it appears on.
                            This means that after the order was submitted on the previous bar at 9:46:52, the market price absolutely dropped below the price of the limit before the bar the order filled on closed.

                            The limit shows 30.83 and if this was submitted when a bar closed as you have mentioned, it was submitted at 9:46:52.
                            The low of the bar it was filled on looks to be between 30.77 and 30.72.
                            This means at some point that bar dropped below 30.83 and this would cause a buy limit to be filled.

                            If you feel that the market price never dropped below 30.83 between 9:46:52 and 9:46:55 then we would need to see all of tick prices between these times to prove this.

                            From everything I can see on the chart, this order filled because it received a valid price.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #29
                              NinjaTrader_ChelseaB

                              Where are you getting the timestamp 9:46:52 from?

                              Based on the code I previously posted, it should be impossible for the entry order to fill within the span of the signal bar, as the entry trigger can only occur at the close of the signal bar. So the entry should not take place before 9:46:55. As you can see from the attached image, the bar after the signal bar never reaches a low of 30.83 in order to for the entry to be executed.

                              In a real-time situation, the entry could not occur before the signal occurs. And if the signal can only trigger at the close of a given bar, the entry should never occur within the signal bar's span.

                              That's my confusion with the backtest result.

                              Here's the sequence with print output:

                              1. Signal triggered on close of BarsInProgress == 0 at 9:46:55 AM
                              Code:
                              2/3/2016 9:46:55 AM longSignal = True
                              2. Order entered BarsInProgress == 1 at 9:46:55 AM (The fastest of which it could logically execute is at least one tick after the close of BarsInProgress == 0, as the signal has to trigger before the order is entered, and the signal can't trigger until the bar is closed.)
                              Code:
                              2/3/2016 9:46:55 AM Order='NT-00019/Backtest' Name='Long' State=PendingSubmit Instrument='CLM 02-17' Action=Buy Limit price=30.83 Stop price=0 Quantity=1 Strategy='STRATEGY' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='52395b24ae4f4b36a8d1b77bbcba271d' Gtd='12/1/2099 12:00:00 AM'
                              2/3/2016 9:46:55 AM Order='NT-00019/Backtest' Name='Long' State=Accepted Instrument='CLM 02-17' Action=Buy Limit price=30.83 Stop price=0 Quantity=1 Strategy='STRATEGY' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='52395b24ae4f4b36a8d1b77bbcba271d' Gtd='12/1/2099 12:00:00 AM'
                              2/3/2016 9:46:55 AM Order='NT-00019/Backtest' Name='Long' State=Working Instrument='CLM 02-17' Action=Buy Limit price=30.83 Stop price=0 Quantity=1 Strategy='STRATEGY' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='52395b24ae4f4b36a8d1b77bbcba271d' Gtd='12/1/2099 12:00:00 AM'
                              3. Order filled at 9:46:55 AM with a price of 30.83 (Remember, the close of the signal bar was 31.16, so for this to occur, the next tick would have needed to be 30.83 or lower, a full 33 tick gap!)
                              Code:
                              2/3/2016 9:46:55 AM Order='NT-00019/Backtest' Name='Long' State=Filled Instrument='CLM 02-17' Action=Buy Limit price=30.83 Stop price=0 Quantity=1 Strategy='STRATEGY' Type=Limit Tif=Day Oco='' Filled=1 Fill price=30.83 Token='52395b24ae4f4b36a8d1b77bbcba271d' Gtd='12/1/2099 12:00:00 AM'
                              2/3/2016 9:46:55 AM Execution='NT-00012' Instrument='CLM 02-17' Account='Backtest' Name='Long' Exchange=Default Price=30.83 Quantity=1 Market position=Long Commission=0 Order='NT-00019' Time='2/3/2016 9:46:55 AM'
                              For reference from the intrabar backtesting example:
                              /* When working with multiple bar series objects it is important to understand the sequential order in which the
                              OnBarUpdate() method is triggered. The bars will always run with the primary first followed by the secondary and
                              so on.

                              Important: Primary bars will always execute before the secondary bar series.
                              If a bar is timestamped as 12:00PM on the 5min bar series, the call order between the equally timestamped 12:00PM
                              bar on the 1min bar series is like this:
                              12:00PM 5min
                              12:00PM 1min
                              12:01PM 1min
                              12:02PM 1min
                              12:03PM 1min
                              12:04PM 1min

                              ... And I've added for further illustration:

                              12:05PM 5min --- Bar close 31.16 / Buy signal
                              12:05PM 1min --- Order entry buy limit 30.83, last price 31.16, etc.
                              12:06PM 1min --- Order working buy limit 30.83, last price 31.01. etc.
                              Attached Files

                              Comment


                                #30
                                NinjaTrader_ChelseaB perhaps the misunderstanding lies in which bar is the signal and entry trigger bar.

                                Please see the image on the previous post for clarification.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by funk10101, Today, 09:43 PM
                                0 responses
                                3 views
                                0 likes
                                Last Post funk10101  
                                Started by pkefal, 04-11-2024, 07:39 AM
                                11 responses
                                36 views
                                0 likes
                                Last Post jeronymite  
                                Started by bill2023, Yesterday, 08:51 AM
                                8 responses
                                44 views
                                0 likes
                                Last Post bill2023  
                                Started by yertle, Today, 08:38 AM
                                6 responses
                                26 views
                                0 likes
                                Last Post ryjoga
                                by ryjoga
                                 
                                Started by algospoke, Yesterday, 06:40 PM
                                2 responses
                                24 views
                                0 likes
                                Last Post algospoke  
                                Working...
                                X