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

Bug: Series Indexes not working when called from EventHandlers

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

    Bug: Series Indexes not working when called from EventHandlers

    I've found a weird issue that has arisen in 8.0.6.0 and 8.0.6.1

    When BarSeries data are accessed from a UI event handler, the indexer methods for accessing the data are not working properly
    e.g. Time[barsAgo] doesn't work but Time.GetValueAt(barIndex) works.

    I've knocked together the attached indicator to show the issue. Which does the following:

    OnBarUpdate prints out CurrentBar and Time[0] to show that the data are being populated. The correct bar times and indexes are printed out for all bars on the chart as well as a hashcode to identify the executing object instance.

    A button is added to chart trader which has a click event handled in the indicator.

    Clicking the button prints a few debug checks

    CurrentBar (999 as expected)
    Time.GetValueAt(999) - gets the time of the most recent bar, as expected
    Time.GetValueAt(1) - gets the time of the first bar, as expected.

    Now using indexes, it gets weird:

    Time[0] should give the most recent bar (same as GetValueAt(999), but instead gives the first bar.... same as GetValueAt(1)
    Time[1] and Time[2] throw a NullReferenceException


    SuspendedWhileInactive = false and a call to Update() don't resolve this either.


    Here's the actual output:


    Code:
    Bar Update 1007 BarTime:26/04/2017 00:55:00 HashCode: 6765737
    Bar Update 1008 BarTime:26/04/2017 01:00:00 HashCode: 6765737
    Bar Update 1009 BarTime:26/04/2017 01:05:00 HashCode: 6765737
    Bar Update 1010 BarTime:26/04/2017 01:10:00 HashCode: 6765737
    Bar Update 1011 BarTime:26/04/2017 01:15:00 HashCode: 6765737
    Bar Update 1012 BarTime:26/04/2017 01:20:00 HashCode: 6765737
    Bar Update 1013 BarTime:26/04/2017 01:25:00 HashCode: 6765737
    Bar Update 1014 BarTime:26/04/2017 01:30:00 HashCode: 6765737
    Bar Update 1015 BarTime:26/04/2017 01:35:00 HashCode: 6765737
    
    ==== Start Debug Output ====
    HashCode: 6765737
    Total Bars (CurrentBar): 1015
    Time.GetValueAt(CurrentBar 1015): 26/04/2017 01:35:00
    Time.GetValueAt(1): 20/04/2017 12:50:00
    Time[0]: 20/04/2017 12:45:00
    Time[1]: Object reference not set to an instance of an object.
    Time[2]: Object reference not set to an instance of an object.
    ==== End Debug Output ====
    Attached Files

    #2
    Hello,

    Thank you for the post.

    In this situation I would expect the result you are seeing, this is generally the same concept as trying to use BarsAgo from OnRender. Out of the event driven context of the script, these items fall out of sync so to speak. To resolve this you have one solution already or to use the Get methods. TriggerCustomEvent may also be able to be used depending on the use case. http://ninjatrader.com/support/helpG...ghtsub=trigger

    This was a change during the beta phase and was noted in the help guide here: http://ninjatrader.com/support/helpG...s/8_0_0_11.htm
    This would be the "Implementation change" note. Although this was specifically noted about OnRender, this affects button event handlers or pretty much anything that is not part of the event driven overrides.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hey Jesse,

      Shouldn't a call to Update() synchronise the Handlers? Something has changed behaviour-wise in the last version because it was working before I upgraded.

      I also don't understand why the printed output from OnBarUpdate where I print out Time[0] works - and prints out every bar's time. But then when I try to access this same series from the handler its no longer populated.

      I've not tried TriggerCustomEvent - doesn't this just call update before running the handler? Because as I said, Update doesn't work, You'll see I call it first in the handler.

      Comment


        #4
        Hello,

        Thank you for the reply.

        Update would not be the same concept here. Update just ensures that an indicator's OnBarUpdate is called when it is being used in a hosted manner. Update would generally be used if you are accessing a public property as one example, see the ZigZag's LowBar or HighBar methods for an example when this is used. The help guide highlights this, in the example if Update is not called, the public property would not have a calculated value. This would not specifically help the situation you are asking about.


        TriggerCustomEvent ensures that when your code in the events method body (see helpguide example) is executed, the NinjaScript properties and pointers are all in sync. This would mean that without this, if OnBarUpdate is processing bar 999, when you request [1] BarsAgo from your custom event, who knows what value would be returned because we are not in sync. Syncing the properties before doing your logic would prevent the errors and return the normal values.


        The reason OnBarUpdate works every time is that it is one of the event driven methods so everything is in sync like it should be. When you are out of sync or out of the Event driven methods like OnBarUpdate using BarsAgo would not be expected to work. The Get methods are the most suggested to use out of context as this ensure precise indexes are being used.

        A button event handler is no different from OnRender in terms of not being in sync, this is a change from NT7 and even earlier versions of NT8 due to the multi-threaded nature of the platform. While this can be confusing, it is easy when you just assume that using BarsAgo will not work unless used in OnBarUpdate, or the other few overrides.


        Please let me know if I may be of additional assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hey Jesse,

          I retested the code using TriggerCustomEvent and it worked! You were right!

          I stand by my assertion that the code I posted worked for me until 8.0.6.0 - I've been using similar code every day, hundreds of times a day for Months! There was definitely an undocumented breaking change in this release, it cost me a days work, but its my fault for upgrading mid week I guess - but it should have been documented in the release notes (which I always read).

          Thanks for your help!

          Kevin

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by kujista, Today, 05:44 AM
          0 responses
          6 views
          0 likes
          Last Post kujista
          by kujista
           
          Started by ZenCortexCLICK, Today, 04:58 AM
          0 responses
          8 views
          0 likes
          Last Post ZenCortexCLICK  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          172 responses
          2,281 views
          0 likes
          Last Post sidlercom80  
          Started by Irukandji, Yesterday, 02:53 AM
          2 responses
          18 views
          0 likes
          Last Post Irukandji  
          Started by adeelshahzad, Today, 03:54 AM
          0 responses
          11 views
          0 likes
          Last Post adeelshahzad  
          Working...
          X