Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtest Trades Not Matching Live With ImmediatelySubmit

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

    Backtest Trades Not Matching Live With ImmediatelySubmit

    Hi,

    I have a strategy (attached) that uses ImmediatelySubmit to resume a position and their corresponding orders after disabling and re-enabling it.

    The strategy is running with Calculate.OnEachTick, has a 1-tick data series added and tick replay enabled on the chart.

    The long positions are opened with the second data series (1 tick granularity):

    EnterLong(1,1,"Long");

    I run the strategy on "MNQ MAR25" with Playback and this is what happens when a new position is opened as shown in the attached image Playback.jpg:

    Position is opened at 21341.00 with Target at 21391.00 and stop at 21291.00

    Now, I disable the strategy and I re-enable few seconds later and I get what is shown in Backtest.jpg (the open position now is calculated as it is an historical one):

    Position is opened at 21340.50 with Target at 21390.50 and stop at 21290.50

    The StartBehavior is ImmediatelySubmit which means that the matching orders "should" be resumed when the order action, order type, quantity, limit price, and stop price are exactly identical.

    As reported above, the value for the open position in Backtest is obviously different from the one in Playback and this means that new target and stop values are computed (based on the strategy logic in the code).

    Therefore, when the strategy is re-enabled with ImmediatelySubmit the previous orders are cancelled (see Cancelled_Orders.jpg), as expected, since they do not match the new computed ones.

    The strategy has 1 tick granularity, tick replay enabled and running on each tick. I would not expect such a behavior.

    Is there any way to match Playback and Backtest positions in order to resume the previous orders?

    Thank you.
    Attached Files

    #2
    Hello Peppo,

    There are differences in real-time and historical data and between live and backtest orders. 1-tick intra-bar granularity and TickReplay can mitigate those differences but it's still possible for order fills to be different.

    This support article discusses, provides a video, and a test script that demonstrates how to write all of the information to file to see what specifically is causing differences.



    I would also suggest that you sync the PC clock, then remove and re-download all historical data. This can help to mitigate differences with timestamps.
    • Shutdown NinjaTrader *important
    • Windows 10
      • Navigate to the Windows Control Panel
      • Select Clock and Region -> then click 'Set the time and date'
        (Or click 'Date and Time' if this is visible)
      • Click on Internet Time tab
      • In the Server: drop-down select time.nist.gov
      • Click Update
    • Windows 11
      • Open Settings
      • Click on Time & language.
      • Click the Date & time page on the right side.
      • Under the “Additional settings” section, click the Sync now button.
    • If the message that appears says 'successful', your PC clock should now be updated
      If not, select a different server from the Server: drop-down and try again (repeat until one of the servers is successful)
    • Restart NinjaTrader
    • Connect to a data feed
    • Reload All Historical Data *important
    Below are a few links to 3rd party educational sites with steps to synchronize with internet time.

    Windows 7 - http://mintywhite.com/windows-7/7mai...t-time-how-to/

    Windows 10 - https://www.windowscentral.com/how-m...ers-windows-10
    - http://www.windows10update.com/2015/...t-time-server/

    Windows 11 - https://pureinfotech.com/fix-clock-out-sync-windows-11/
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      I followed your instructions: synchronized the clock and removed historical data.

      I have also removed the historical data from the folder: NinjaTrader 8\db doing a clean up of all sub-folders.

      I repeated the test above, taken also some other trades and I do NOT see any improvement. I send you the new screenshots I have taken.

      I understand that might be differences between historical and real-time but I could NOT find any trade that was matching (the open position price is always different between real-time and historical). The previous orders are systematically cancelled since they do not match the new ones.

      Do you get the same results on your side?

      Thank you.

      Regards.


      Attached Files

      Comment


        #4
        Hello Peppo,

        Unfortunately, I'm not able to debug your script on your behalf.

        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.

        May I have the output text files with the data and condition information written to file?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          the script is attached to my first post.

          It is only placing a long position on 1 tick data series with target and stop.

          I am sorry but I am not looking for a programmer. I am reporting an issue I am having and the script it is really simple.

          I can also post the code here:


          region Using declarations
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.ComponentModel.DataAnnotations;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Gui;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Gui.SuperDom;
          using NinjaTrader.Gui.Tools;
          using NinjaTrader.Data;
          using NinjaTrader.NinjaScript;
          using NinjaTrader.Core.FloatingPoint;
          using NinjaTrader.NinjaScript.Indicators;
          using NinjaTrader.NinjaScript.DrawingTools;
          #endregion

          //This namespace holds Strategies in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Strategies
          {
          public class TestResume : Strategy
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "TestResume";
          Calculate = Calculate.OnEachTick;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 30;
          IsFillLimitOnTouch = true;
          MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
          OrderFillResolution = OrderFillResolution.Standard;
          Slippage = 0;
          StartBehavior = StartBehavior.ImmediatelySubmit;
          TimeInForce = TimeInForce.Gtc;
          TraceOrders = false;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
          BarsRequiredToTrade = 20;
          IsInstantiatedOnEachOptimizationIteration = true;
          }
          else if (State == State.Configure)
          {
          AddDataSeries(Data.BarsPeriodType.Tick, 1);
          SetStopLoss(CalculationMode.Ticks, 200);
          SetProfitTarget(CalculationMode.Ticks, 200);
          }
          }

          protected override void OnBarUpdate()
          {
          //Add your custom strategy logic here.

          if (Position.MarketPosition == MarketPosition.Flat && IsFirstTickOfBar && ToTime(Time[0]) > 020000 && ToTime(Time[0]) <= 021000) {

          EnterLong(1,1,"Long");

          }
          }
          }
          }

          Comment


            #6
            Hello Peppo,

            Set methods cannot use 1-tick intra-bar granularity. You must use exit orders or submit order unmanaged as demonstrated to implement 1-tick intra-bar granularity.
            See the note 'Note on Set methods' in the 'Improving backtest order fill accuracy with intrabar granularity' article linked from the 'Comparing Strategy Results' article about this.
            Further, see the 'Back-testing NinjaScript Strategies with an Intrabar Granularity' reference sample linked from the 'Improving backtest order fill accuracy with intrabar granularity' which demonstrates.

            You will need to remove the SetStopLoss() and SetProfitTarget() calls and replace these with ExitLongStopMarket() and ExitLongLimit() submitted from OnExecutionUpdate().

            That said, there can still be differences within 1-tick and unfortunately this would be expected behavior. However, to actually see if this is the issue, you would need to debug by writing the information to file as directed in the 'Comparing Strategy Results' article. A video and Sample code is provided with the 'RealtimeReplayHistoricalComparisonsExample_NT8' example linked from this article.


            As you are not a programmer, if you would like to hire someone to code and debug the script on your behalf, you can search our extensive library of NinjaScript consultants through the link below. Simply enter a consultant name or search by using our filter categories. Once you have identified your consultants of choice, please visit each consultant's site for more information or contact them directly to learn more!
            Programming Services - https://ninjatraderecosystem.com/sea...mming-services
            Educators - https://ninjatraderecosystem.com/sea...ures=education

            You can locate the contact information for the consultants on their direct websites for any additional questions you may have. Since these consultants are third party services for NinjaTrader all pricing and support information will need to be obtained through the consultant.

            This NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.​
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              I agree that SetStopLoss() and SetProfitTarget() cannot be used with 1-tick data but this is not the cause of the mismatch. I can also remove them and put long entry and entry short at specific times (just to close the position) and for sure there is the possibility to have different fill prices between backtest and playback.

              I understand from your video "Developer video: Comparing Real-Time Performance with Historical or Playback Data​" that there is no way to make sure that historical and playback are going to match. There might be always a difference in the filling price and this can cause the target and stop orders (if any) to be cancelled once ImmediatelySubmit is in place.

              In this case I think Adopt account position​ is the best solution to resume the open position without surprises.

              Regards.

              Comment


                #8
                Hello Peppo,

                I would expect the differences in fill prices to not be different than the price change within 1 millisecond (the smallest granularity nt can process).

                AdoptAccountPosition could be helpful in this regard in that your script could react to the existing position marketposition and price.

                However I would like to analyze the output from data, condition information, and order information and pinpoint what is different.

                Please provide the requested output so that I may confirm the issue is with the change of price within 1 millisecond or an issue with the script.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  HI Chelsea,

                  sorry for the late answer.

                  I have decided to not proceed with this option ImmediatelySubmit.

                  I will resume the open position by means of AdoptAccountPosition. It sounds more suitable for my needs.

                  Thank & Regards.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  56 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  132 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  73 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  49 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X