Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Possible bug. Strategy Performance window. Backtest Last bar

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

    Possible bug. Strategy Performance window. Backtest Last bar

    When testing an automated strategy only in historical data, with the particular situation that in the most recent loaded bar, it closes 1 trade and simultaneously it opens a new trade, all this taking place in the same most recent bar and with the last trade being automatically closed by the platform because there are no more bars on the right, then even when the chart seems to show correctly these 2 last trades, in Strategy Performance window side it is happening that the last trade is being recognized in the opposite direction, i.e. being a short trade when it is a long trade.

    After making some tests with 2 different strategies where happens this situation in the most recent bar, with firstly 1 trade exit and secondly 1 trade entry in the Open and exit in the Close, then the platform show the same situation in both cases, where the list of trades seems to be ok except for the last trade that is shown with the opposite direction in Strategy Performance / Trades ($) section. So, to see this situation you could just test with a sample strategy that has an exit and an entry exactly in the last bar, a strategy like a SMA crossover where in the last bar there is a trade exit then immediately there is a new trade entry, with everything in a default and very simple way: market orders, all orders in OnBarUpdate(), no stop-losses because an exit is met when an entry is also met.

    Other details are that the 2 strategies tested don’t contain any specific conditions in the codes about some action in the most recent bar in the chart, and the last trade is being closed possibly by a default platform action when all the loaded data is historical and there are no more new bars on the right side.

    Other way to confirm this problem was by going to the Strategy Performance / Orders ($) section, where as you can see in the pictures, it seems to show the last trade in the correct way, but at the same time the Strategy Performance / Trades ($) section shows the last trade in the opposite way, taking the most recent bar Close as the entry price and the most recent bar Open as the exit price (opposite).

    One of the most relevant problems seen with this situation is that even when the chart and the Strategy Performance / Orders ($) section, both show the correct information about the last trade, then the Strategy Performance / Summary ($) section show the information and the statistics considering that last trade in the opposite way, i.e. adding the last trade in the picture to the short trades list when that trade is in fact a long trade, so, being the summary information inaccurate: net profit, profit factor, number of trades, etc.

    Please see the details in the 2 attached pictures and thank you for the support about this.
    Attached Files

    #2
    Hello futurenow,

    May I confirm the script is using 1-tick intra-bar granularity or has Order Fill Resolution set to High 1-tick?

    May I confirm the logic is not allowing an entry order and exit order to be submitted on the same bar update?
    (An entry method and exit method called on the same bar update will result in a historical overfill)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post

      May I confirm the script is using 1-tick intra-bar granularity or has Order Fill Resolution set to High 1-tick?

      May I confirm the logic is not allowing an entry order and exit order to be submitted on the same bar update?
      (An entry method and exit method called on the same bar update will result in a historical overfill)

      In this case the backtest is directly from the chart / Strategy Performance / Historical


      In the strategy for the tests there is a structure as follow:​

      Code:
      Calculate = Calculate.OnBarClose;
      OrderFillResolution = OrderFillResolution.Standard;
      
      //...
      
      protected override void OnBarUpdate()
      {
          //...
      
          if (Position.MarketPosition == MarketPosition.Flat && condition1)
          {
              EnterLong(1, "string1");
          }
          else if (Position.MarketPosition == MarketPosition.Short && condition1 && condition2)
          {
              ExitShort(1);
              EnterLong(1, "string2");
          }
      
      
          if (Position.MarketPosition == MarketPosition.Flat && condition3)
          {
              EnterShort(1, "string3");
          }
          else if (Position.MarketPosition == MarketPosition.Long && condition3 && condition4)
          {
              ExitLong(1);
              EnterShort(1, "string4");
          }
      
      
          if (Position.MarketPosition == MarketPosition.Long && condition5)
              ExitLong(1);
                  
          if (Position.MarketPosition == MarketPosition.Short && condition6)
              ExitShort(1);
      }

      Everything is as given by default in the strategy wizard, with all very simplified in daily data. The only special thing is that in the most recent bar take place a trade exit, also at the same time a trade entry, and finally the exit of that last trade entry that is a done automatically by the platform (to not to leave any trade open?) but not by the strategy logic.




      Comment


        #4
        Hello futurenow,

        You have:
        ExitShort(1);
        EnterLong(1, "string2");​​

        This is going to cause a historical overfill.

        See the forum post below that provides details.



        Do not call an Exit method and Entry method on the same bar update.

        Just call the entry method and NinjaTrader will automatically exit the existing position and enter the reverse position.

        Calling an Exit and Entry on the same bar update will cause a historical overfill and in real-time will double the position quantity in opposite direction.


        Also, set Order Fill Resolution to High 1-tick in the Strategy Analyzer window.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        110 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        59 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        37 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        41 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        78 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X