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

Strange results with On Price Change strategy and Historical Data

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

    Strange results with On Price Change strategy and Historical Data

    I have a strategy that is running On Price Change. It is working fine in realtime but in Historical backtesting it almost looks like it can only function on bar close. Its exiting way later than it should and is missing entries.

    This is how it should be entering trades. When it reaches the green lines it enter. And exits when it hits the white line (SMA).

    Click image for larger version

Name:	Untitled1.jpg
Views:	184
Size:	134.7 KB
ID:	1145719

    This is what its doing in the historical. It is exiting way away from the white line. Basically after a bar as closed over/under the line.

    Click image for larger version

Name:	Untitled2.jpg
Views:	160
Size:	167.5 KB
ID:	1145720

    Is there a setting for this? Or does it need to be accounted for in the code, and if so what needs to be changed?

    #2
    Hello Plzwork123,

    Thanks for your post.

    This would be expected behavior. Historical data is comprised of the bars open, high, low, close, volume and time, and does not contain all of the ticks to build the bar. When your strategy is run on historical data, regardless of your Calculate setting, the code can only run as if it was using Calculate.OnBarClose. The current bar is always a just closed bar and your code can only run once per bar.

    If you want your historical trades to perform the same as live trades you would need to modify your strategy to add a secondary 1 tick series to your strategy and enable Tick Replay for the data series.
    References:



    Please note that you will need to make sure you have tick data for the duration of the historical portion of the chart.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I will look into that. Thank you for the reply!

      Comment


        #4
        I might need more help on this than I originally thought. Ive added this
        Code:
        AddDataSeries("NQ 03-21", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
        I tried running the script with just that and it still was appearing to exit and enter based on bar close.

        I then added
        Code:
        if (BarsInProgress == 0 && State == State.Realtime)
        and
        Code:
        else if (BarsInProgress != 0 && State == State.Historical)
        and put my original entire block of code in each. But now i dont see any historical trades taken.

        Does anyone know a little more specifically what needs to be done to only have it trade the tick data series in State = Historical?

        Comment


          #5
          Hello Plzwork123,

          Thanks for your reply.

          First, make sure you enable Tick Replay as this will get your indicators/strategy code functioning historically on each tick (or OnPricechange as you have selected) so that your entry conditions will be detected as it occurs.

          The added tick series is only for the executions as otherwise the orders wouldn't fill until the next chart bar and you need only add the series and then in OnBarUpdate() as the first-line use: if (BarsInprogress != 0) return; // return if not the chart bars.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Regrettably, this did not work. The first time I added the data series this is how the code was set up and it is still functioning as if its OnBarClose.
            Code:
            if (BarsInProgress != 0) return;
            is in a strategy by default from what Ive seen.


            Comment


              #7
              Hello Plzwork123,

              Thanks for your reply.

              Please double check that you have tick data for the entire backtest period (or try just a few days as a quick test).

              If that does not resolve the issue then please post a reduced code example of your strategy that replicates the issue.

              If you would prefer not to post your code on a public forum, you are welcome to write into PlatformSupport[at]NinjaTrader[dot]Com. In the email subject please put Atten Paul Ticket# 3005377. In the e-mail attach your source code file which can be found in Documents>NinjaTrader8>bin>custom>Strategies. the file will be as you named it with a .cs file extension. Finally, include a link to this thread for reference.

              Paul H.NinjaTrader Customer Service

              Comment


                #8
                I just want to confirm all of these settings are correct.

                Click image for larger version

Name:	Untitled.jpg
Views:	152
Size:	284.1 KB
ID:	1146354

                Comment


                  #9
                  Hello Plzwork123,

                  Thanks for your reply.

                  Yes, they look correct.

                  The only thing you are not showing is if you have clicked the check box for Tick Replay usage on the strategy analyzer itself. The setting you are showing for tick replay in your screenshot is what would then allow you to select (by check box) tick replay in a data series of a chart or on the strategy analyzer itself (see attached for a reference point)

                  Click image for larger version

Name:	Plzwork123-1.PNG
Views:	254
Size:	22.7 KB
ID:	1146357
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Oh this cannot be done by just running the Strategy on a 2 minute chart? I have been adding the strategy to the chart and just checking the box in the Account Data section.

                    Comment


                      #11
                      Hello Plzwork123,

                      Thanks for your reply.

                      Before you apply the strategy to a chart, you would need to right mouse click on the chart and select "data series..". In the data series window, you should see the same "Tick Replay" check box located below the "value" field for the data series. Please check the Tick Replay checkbox and then apply your strategy.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Ok I see that now. Something has definitely changed with the backtest results now, But It looks like the results are still erratic at best. They do not reflect how the strategy would trade in realtime at all.
                        In this picture you can see. It is still entering in a very odd area where normally in Realtime it would be entering at the red horizontal lines ive drawn.

                        Click image for larger version

Name:	Untitled.jpg
Views:	159
Size:	66.3 KB
ID:	1146373

                        Comment


                          #13
                          Hello Plzwork123,

                          Thanks for your reply.

                          I really could not comment on how your strategy performs.

                          At this point, I would suggest reviewing this post by my colleague Chelsea for further information: https://ninjatrader.com/support/foru...mance?t=102504

                          Perhaps a better alternative for you would be to use the Playback connection with Market replay data. While this is also historical data, it provides all of the ticks and builds the bar tick by tick. Reference: https://ninjatrader.com/support/help...connection.htm

                          Paul H.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Rapine Heihei, 04-23-2024, 07:51 PM
                          2 responses
                          30 views
                          0 likes
                          Last Post Max238
                          by Max238
                           
                          Started by Shansen, 08-30-2019, 10:18 PM
                          24 responses
                          943 views
                          0 likes
                          Last Post spwizard  
                          Started by Max238, Today, 01:28 AM
                          0 responses
                          9 views
                          0 likes
                          Last Post Max238
                          by Max238
                           
                          Started by rocketman7, Today, 01:00 AM
                          0 responses
                          4 views
                          0 likes
                          Last Post rocketman7  
                          Started by wzgy0920, 04-20-2024, 06:09 PM
                          2 responses
                          28 views
                          0 likes
                          Last Post wzgy0920  
                          Working...
                          X