Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsSinceNewTradingDay can be NEGATIVE 10,000+ in genetic optimization+etc

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

    BarsSinceNewTradingDay can be NEGATIVE 10,000+ in genetic optimization+etc

    Optimization configuration excluding defaults:

    Test Param: Does nothing, 100 values to optimize
    Instrument: MES, Last 5 minutes
    Time frame: 2023-01-01 to 2023-05-01
    Optimizer: Genetic
    GO Generation size: 5 (CANNOT reproduce when size*generations > optimization value count, same with default optimizer)

    IsInstantiatedOnEachOptimizationIteration=false seems to be important (EDIT: Maybe not)

    Strategy code:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class MyCustomStrategy : Strategy
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "MyCustomStrategy";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = true;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    IsInstantiatedOnEachOptimizationIteration    = false;
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < BarsRequiredToTrade)
                    return;
    
                PrintTo = PrintTo.OutputTab2;
                Print(Bars.BarsSinceNewTradingDay);
                PrintTo = PrintTo.OutputTab1;
    ​
                if (MACD(12, 26, 9).Diff[0] > 0.0)
                {
                    EnterLong(1);
                }
                else
                {
                    ExitLong(1);
                }
            }
    
            #region Properties
    
            [NinjaScriptProperty]
            [Display(Name="Test", Order=1, GroupName="Parameters")]
            public int Test
            { get; set; }
    
            #endregion
        }
    }​
    Output:

    Code:
    ...
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    -22975
    -22974
    -22973
    -22972
    -22971
    -22970
    -22969
    -22968
    -22967
    -22966
    -22965
    -22964
    -22963
    -22962
    -22961
    -22960
    -22959
    ...​
    Workaround:

    Code:
            private int GetBarsSinceTradingDay(int barsInProgress)
            {
                Bar lastDay = BarsArray[barsInProgress].GetDayBar(1);
                if (lastDay != null)
                {
                    return CurrentBars[barsInProgress] - BarsArray[barsInProgress].GetBar(lastDay.Time) - 1;
                }
                else
                {
                    return CurrentBars[barsInProgress];
                }
            }
    ​
    Edit: Just use the workaround described by QuantKey_Bruce
    Last edited by nathanfranke; 05-06-2023, 11:12 AM.

    #2
    nathanfranke You're absolutely right and I can reproduce this.

    NinjaTrader needs to add guard code to check for the situation where there has not been a new trading day yet, or to assume that the first bar of the series is the start of a new trading day.

    Good catch.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      nathanfranke Also, I just want to point out that (and I know you're just trying to demonstrate the problem here, but... while you're waiting for a fix...) GetDayBar by a time is absolutely dog slow. I'm sure you have noticed this - it's impossible to miss how much slower the optimizer runs with that in there. What you may want to do instead, while you're waiting for them to fix this, is like this, using IsFirstBarOfSession which is lightning fast:

      Code:
      #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 MyCustomStrategy : Strategy
          {
              private int SessionStartBar0 = -1;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Strategy here.";
                      Name                                        = "MyCustomStrategy";
                      Calculate                                    = Calculate.OnBarClose;
                      EntriesPerDirection                            = 1;
                      EntryHandling                                = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy                = true;
                      ExitOnSessionCloseSeconds                    = 30;
                      IsFillLimitOnTouch                            = false;
                      MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                      OrderFillResolution                            = OrderFillResolution.Standard;
                      Slippage                                    = 0;
                      StartBehavior                                = StartBehavior.WaitUntilFlat;
                      TimeInForce                                    = TimeInForce.Gtc;
                      TraceOrders                                    = true;
                      RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                      StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                      BarsRequiredToTrade                            = 20;
                      IsInstantiatedOnEachOptimizationIteration    = false; // Important
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  if (CurrentBars[0] < BarsRequiredToTrade)
                      return;
      
                  // this way is efficient - GetDayBar is dog slow
                  if (SessionStartBar0 < 0 || Bars.IsFirstBarOfSession) SessionStartBar0 = CurrentBars[0];
                  int BarsSinceNewTradingDay = CurrentBars[0] - SessionStartBar0;
      
                  PrintTo = PrintTo.OutputTab2;
                  Print(Bars.BarsSinceNewTradingDay + "  <-> " + BarsSinceNewTradingDay);
                  PrintTo = PrintTo.OutputTab1;
              }
      
              #region Properties
              [NinjaScriptProperty]
              [Display(Name="Test", Order=1, GroupName="Parameters")]
              public int Test
              { get; set; }
              #endregion
          }
      }​​
      The problem is in the bar code not the trading code itself so no trades are required to demonstrate.

      As an aside, whatever NinjaTrader is doing internally for BarsSinceNewTradingDay should probably be chucked and just replaced with this type of approach if figuring out the right guard takes more time. It's a two-liner and runs in essentially zero time.
      Last edited by QuantKey_Bruce; 05-06-2023, 05:36 AM.
      Bruce DeVault
      QuantKey Trading Vendor Services
      NinjaTrader Ecosystem Vendor - QuantKey

      Comment


        #4
        Hello nathanfranke,

        Thanks for your post.

        I was able to reproduce the behavior you are reporting.

        We will be investigating this behavior further and I will reach out to you with more information as soon as it is available.

        Thanks for reporting this behavior.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hello nathanfranke,

          I have reported the issue to the Development team so that they may look into the cause of this behavior.

          I will reach back out to you with more information as soon as the Development team gets back to me with their findings.

          Thanks for your patience.
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment


            #6
            Confirming I have faced this issue. Please see https://forum.ninjatrader.com/forum/...18#post1271518

            Comment


              #7
              Hello wzgy0920,

              Thanks for your note.

              A fix for this behavior will be implemented by the Development team in the next major version release of NinjaTrader 8.

              We do not have an ETA for when the next major version will be released at this time.
              <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              601 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              347 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              559 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              558 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X