Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple External User Control Prevents Indicator.OnBarUpdate in real time.

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

    Simple External User Control Prevents Indicator.OnBarUpdate in real time.

    • Simple user controls contained in an external dll. Added to NinjaTrader folder and added as a reference. All was working months ago.
    • Controls are hosted in an indicator similar to DrawingToolTile. DrawingToolTile was used as the template for creating a similar UI to host the user controls.
    Today it was discovered that OnBarUpdate is not being called when State == State.Realtime. Whether using a live data connection, or playback it refuses to be called. The following has been attempted to resolve the issue.
    • Disabling creating the user control makes the problem go away. Simply calling "new MyUserControl" is enough to cause OnBarUpdate to no longer be called. It doesn't need to be added to anything.
    • The DLL was still using .net 4.5.2 but with NinjaTrader updating to 4.8, the DLLs have been updated to no effect.
    • Created a stripped out UserControl that features just a TextBlock with no logic at all in the code and very simple Xaml.
    None of the above things worked.

    Additionally
    • Other controls are created and put into the floating control. All of these are built in WPF controls like Grid, and Label. Their presence does not impact OnBarUpdate.
    • The logs and trace have are identical whether the control is created or not.
    At this point it is not clear why creating an a WPF control from an external library would suddenly cause the indicator to not call OnBarUpdate. This code was working back when NinjaTrader was still on 4.5.2 for the .net Framework.

    #2
    Originally posted by ntbone View Post
    • Simple user controls contained in an external dll. Added to NinjaTrader folder and added as a reference. All was working months ago.
    • Controls are hosted in an indicator similar to DrawingToolTile. DrawingToolTile was used as the template for creating a similar UI to host the user controls.
    Today it was discovered that OnBarUpdate is not being called when State == State.Realtime. Whether using a live data connection, or playback it refuses to be called. The following has been attempted to resolve the issue.
    • Disabling creating the user control makes the problem go away. Simply calling "new MyUserControl" is enough to cause OnBarUpdate to no longer be called. It doesn't need to be added to anything.
    • The DLL was still using .net 4.5.2 but with NinjaTrader updating to 4.8, the DLLs have been updated to no effect.
    • Created a stripped out UserControl that features just a TextBlock with no logic at all in the code and very simple Xaml.
    None of the above things worked.

    Additionally
    • Other controls are created and put into the floating control. All of these are built in WPF controls like Grid, and Label. Their presence does not impact OnBarUpdate.
    • The logs and trace have are identical whether the control is created or not.
    At this point it is not clear why creating an a WPF control from an external library would suddenly cause the indicator to not call OnBarUpdate. This code was working back when NinjaTrader was still on 4.5.2 for the .net Framework.
    Can you upload your stripped down version?

    A reproducible sample with instructions will help the official response
    (and hopefully, a fix as well) move into hyper-speed much faster.

    Comment


      #3
      The stripped down user control, and even the original one work fine in a stripped down version of the indicator. The problem seems to be potentially in the indicator itself but its unclear what code in it is causing the issue. The indicator that is reproducing the problem has a lot of parts so figuring out which one is causing the issue is going to be challenging.

      Comment


        #4
        Hello ntbone,

        I've exported the SampleWPFModifications as an assembly which includes a custom DropMenu class control.
        Hello All, Moving forward this will be maintained in the help guide reference samples and no longer maintained on the forum. Creating Chart WPF (UI) Modifications from an Indicator - https://ninjatrader.com/support/help...ui)-modifi.htm (https://ninjatrader.com/support/helpGuides/nt8/creating-chart-wpf-(ui)-modifi.htm) I've


        Are you able to reproduce the behavior with this assembly?


        To find the issue in your code, you will need to make a copy of the script and then remove all code that is directly responsible for the behavior.

        Prints can also be used to see where the code stops executing. Add a print to every other line of code. The last print to appear is above the line with the error.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I am still in the process of trying to figure out what caused OnBarUpdate to not be called. Currently I have been testing it via Playback using print statements. Some additional notes discovered regarding the problem.
          • It seemed like adding additional data series was causing an issue, as removing it suddenly made it work. Adding it back in though showed this to be unreliable.
          • All testing is currently being done with playback. Testing on a different machine I discovered that during a live connection this issue doesn't necessarily reproduce.
          I have been doing trial and error both disabling code in the original indicator/copying and reproducing suspected problems to a new one in order to get something that reproduces. So far I have only ever been able to reproduce things in the original indicator.

          Here's the simple code I use to easily tell if the OnBarUpdate is being called.

          Code:
          int tickCounter;
          
          protected override void OnBarUpdate()
          {
              if(State == State.Realtime)
              {
                  Print("Processing CurrentBar " + CurrentBar + " and tick " + tickCounter);
                  tickCounter++;
              }
          }

          Comment


            #6
            I have reproduce-able code that seems to have nothing to do with the controls.

            Code:
            public class BreakingOnBarUpdateBug : Indicator    
            {
                protected override void OnStateChange()
                {
                    base.OnStateChange();
            
                    switch(State)
                    {
                        case State.SetDefaults:
                            Name = "Breaking On Bar Update";
            
                            Calculate                                   = Calculate.OnEachTick;
                            IsOverlay                                   = true;
                            DisplayInDataBox                            = true;
                            DrawOnPricePanel                            = true;
                            DrawHorizontalGridLines                     = true;
                            DrawVerticalGridLines                       = true;
                            PaintPriceMarkers                           = true;
                            ScaleJustification                          = ScaleJustification.Right;
                            //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                            //See Help Guide for additional information.
                            BarsRequiredToPlot = 0;
                            IsSuspendedWhileInactive                    = true;
                            break;
                        case State.Configure:
                            {
                                // uncomment this code to have OnBarUpdate fail to call in real time. The print statement in OnBarUpdate will not execute.
                                LoadExtraSeries();
                            }
                            break;
                    }
                }
            
                private void LoadExtraSeries()
                {
                    var dailyPeriod = new BarsPeriod()
                    {
                        BarsPeriodType = BarsPeriodType.Day,
                        Value = 1,
                        MarketDataType = MarketDataType.Last
                    };
            
                    // The TradhingHours.Name was determined from the forum from post 19 here
                    // https://ninjatrader.com/support/forum/forum/ninjatrader-8/strategy-development/95826-is-nt-has-a-chronic-bug/page2?postcount=19#post782465
                    AddDataSeries(null, dailyPeriod, 10, Instrument.MasterInstrument.TradingHours.Name, null);
                }
            
                int tickCounter;
            
                protected override void OnBarUpdate()
                {
                    if(State == State.Realtime)
                    {
                        Print("Processing CurrentBar " + CurrentBar + " and tick " + tickCounter);
                        tickCounter++;
                    }
                }
            }
            1. Create a new indicator using the code above.
            2. Create a chart with AAPL (or any ticker).
            3. Set the date to today, set the time to 24x7
            4. Add the new indicator to the chart.
            5. Start "Simulated Data Feed"
            If the LoadExtraSeries() is commented out, OnBarUpdate will execute in real time and you'll see the the ticks printed in the output window. Restore the LoadExtraSeries() line and OnBarUpdate will refuse to execute.

            Comment


              #7
              Hello ntbone,

              Unfortunately, this code is not supported by NinjaTrader.

              From the help guide:

              "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."


              Any variables or if statements with AddDataSeries() is dynamic. Dynamically adding series is not supported.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Replace the line of code

                Code:
                AddDataSeries(null, dailyPeriod, 10, Instrument.MasterInstrument.TradingHours.Name, null);
                with

                Code:
                AddDataSeries(null, dailyPeriod, 10, "US Equities RTH", null);
                Then go through my repro steps and you will find that its still broken. There are no variables now in this code. dailyperiod is hard coded local variable generated before the call instead of inline with the call. All other parameters are hard coded inline and nothing is dynamic.

                No errors show up in the log or trace and yet, OnBarUpdate will stop being called.

                When I tested this, it was outside the regular Market Hours for AAPL.

                Comment


                  #9
                  Hello ntbone,

                  I gave the syntax you provided a try however I see this working on my end. One note here is that for OnBarUpdate to be called when using the trading hours overload you would need to be within the both session hours otherwise OBU is not called. This is mentioned in the AddDataSeries page. If in your test the AAPL was outside of its market hours the AddDataSeries will be paused.


                  •If your NinjaScript object is using AddDataSeries() allowing to specify a tradingHoursName, please keep in mind that: An indicator / strategy with multiple DataSeries of the same instrument will only process realtime OnBarUpdate() calls when a tick occurs in session of the trading hour template of all added series. Any ticks not processed will be queued and processed as a tick comes in for all subsequent DataSeries.


                  With that being said, are you still seeing the problem when you are working within the primary bars trading hours?


                  I look forward to being of further assistance.
                  Last edited by NinjaTrader_Jesse; 02-12-2021, 08:50 AM.

                  Comment


                    #10
                    If I understand correctly, because the primary data series is set to Default 24 x 7 but the secondary data series is set to "US Equities RTH", unless it happens to be between 9:30 and 4 EST, OnBarUpdate will not be called?

                    If so, how can I make an indicator that will use whatever the chart is set to (Default 24 x 7 in this case) for additional data series while also specifying the "bars to load". The rigidity of AddDataSeries API is complicating and limiting its usefulness.

                    Comment


                      #11
                      Hello ntbone,

                      Your understanding on the first point is correct.

                      I gave a test to pass null as the tradingHoursName parameter similar to what we can do with the instrument name to inherit the primary data series instrument name, and I see that the Trading Hours template of the primary data series is inherited.


                      Code:
                          else if (State == State.Configure)
                          {
                              AddDataSeries(null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1440 }, 10, null, null);
                          }
                      }
                      
                      protected override void OnBarUpdate()
                      {
                          Print(BarsArray[1].TradingHours.ToString());
                      }
                      Please let us know if you have any additional questions.

                      Comment


                        #12
                        In my experience using 'Default 24x7' has been known to cause problems.
                        It forces midnight as a session break, which is a lie.

                        I'm not sure I understand the rationale to override the normal session
                        trading hours for an instrument. Is there really a need to do that?

                        PS:
                        I've learned to use 'Default 24x7' only for testing, and usually only on weekends.
                        I couple it with 'Simulated Data Feed', otherwise no data appears on the chart.

                        Comment


                          #13
                          I only use 'Default 24 x 7' for the same reason. In this case I happen to be testing with a simulated data feed and noticed OnBarUpdate was not triggering. I am not sure this was the original reason as to why it wasn't triggering as the trading hours were set correctly during market hours.

                          The only other time I modify the trading hours is when I want to view US regular trading hours vs extended trading hours but I do that at a chart level and really wanted the data series to just match what the chart was configured to.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          607 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          353 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          105 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          560 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          561 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X