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

CurrentBar not always providing a good value

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

    CurrentBar not always providing a good value

    Custom indicator "A" uses a private dataseries object to keep track of certain bars. It uses CurrentBar in its calculations. I expose several properties that use the contents of this datastore to calculate their results. Then I call these properties from Custom indicator "B". All this works as expected when using CalculateOnBarClose set to true.

    But when I set CalculateOnBarClose = false, I started having bad values returned from these properties. I tracked it down to references to CurrentBar and it seems that CurrentBar isn't always dependable the way I'm using it.

    I did some testing and it seems that if I put CurrentBar in an integer property then it's always one behind when the variable/property is referenced another indicator. Also, when I put CurrentBar in a dataseries and then reference this dataseries via a property from another indicator then it's always zero.

    I seem to remember something about CurrentBar not being dependable in certain contexts. Is this a know situation? Could this be a bug?

    Here is my test code: (not sure if the Update()s are needed - same results with or without them)

    IndicatorA:
    Code:
     
    public class IndicatorA: Indicator
    {
      private IntSeries testDS;
      private int testInt = 10; // initialize to something - later set to CurrentBar
     
      protected override void Initialize()
      {
        CalculateOnBarClose = false;
        testDS = new IntSeries(this);
      }
     
      protected override void OnBarUpdate()
      {
        testDS.Set(CurrentBar); // put CurrentBar value in the data series
        testInt = CurrentBar; // put CurrentBar value in an int
      }
     
      [Browsable(false)]
      [XmlIgnore()]
      public int TestDS
      {
        get { Update(); return (testDS[0]); }
      } 
      [Browsable(false)]
      [XmlIgnore()]
      public int TestInt
      {
        get { Update(); return (testInt); }
      }
    }
    IndicatorB - calls IndicatorA above
    Code:
     
    public class IndicatorB: Indicator
    {
      protected override void Initialize()
      {
        CalculateOnBarClose = false;
      }
      protected override void OnBarUpdate()
      {
        Print("CurrentBar=" + CurrentBar + " TestDS=" + IndicatorA().TestDS + " TestInt=" + IndicatorA().TestInt);
      }
    }
    Output:
    Code:
     
    CurrentBar=11059    TestDS=0   TestInt=11058

    #2
    Someone will respond tomorrow morning.
    RayNinjaTrader Customer Service

    Comment


      #3
      Hi ActiveHawk,

      This is odd. I've taken your code with practically zero changes except adding a new test variant and I get proper results:

      CurrentBar: 4 TestDSInt: 4 TestDS: 4 TestInt: 4
      CurrentBar: 5 TestDSInt: 5 TestDS: 5 TestInt: 5
      CurrentBar: 6 TestDSInt: 6 TestDS: 6 TestInt: 6
      CurrentBar: 7 TestDSInt: 7 TestDS: 7 TestInt: 7

      Please try my attached test references. Also please make sure you are on NT6.5.1000.7.
      Attached Files
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        CalculateOnBarClose Causing Problems

        I'm very happy now - after many hours of trying to figure out what was happening, the mystery has been solved!

        The problem has to do with setting CalculateOnBarClose=true in Initialize() of IndicatorA and I think it's a bug or at least needs better documentation.

        If you take the test code you sent me and change this setting to true then even though it is set to false in the dialog, it will cause the call to the exposed property to fail and return 0 instead of the proper value (in this case CurrentBar). It has nothing to do with CurrentBar - the data series could be set to any value.

        I was lead to believe by the helpfile on CalculateOnBarClose that it's appropriate to have this set to true as the last line in my initialize(). However, it seems that it is best to not set it at all and that way it will default to true in the dialog. If the user sets it to false in the dialog then the code won't interfere with it and all works fine. I guess if you wanted the default to be false then it should be in initialize() but it seems having in set to true in initialize() will only cause problems (and this is how the example in the helpfile shows it)

        Obviously this variable has special implementation under the hood - I always wondered why the helpfile said it must be the last line in initialize(). My experience is that it doesn't matter where it is in initialize() as long as it's not set to true. I wouldn't be surprised to find it's more complicated than this but right now that's my understanding.

        Anyway, I removed the line from all my initialize() functions and now all my indicators work both onbarclose and tick-by-tick - controlling everything from the dialog - with the default being onbarclose.

        Comment


          #5
          Glad you got it resolved. There is a known bug when there is a discrepancy between CalculateOnBarClose settings for a NinjaScript calling another NinjaScript.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Another comment...

            I noticed the "new indicator wizard" generates the CalculateOnBarClose=true line for the initialize(). This seems to encourage the problems I had and it also violates the helpfile's insistance on having it be the last line in initialize().

            It seems something is not right here...

            Comment


              #7
              ActiveHawk,

              The oddity you experience is a bug and the wizard does not account for the bug being there. There will be no changes to the wizard and announcements will be made when appropriate about the status of the bug fix.
              Josh P.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Taddypole, 04-26-2024, 02:47 PM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_Eduardo  
              Started by futtrader, 04-21-2024, 01:50 AM
              6 responses
              58 views
              0 likes
              Last Post futtrader  
              Started by sgordet, Today, 11:48 AM
              0 responses
              4 views
              0 likes
              Last Post sgordet
              by sgordet
               
              Started by Trader146, Today, 11:41 AM
              0 responses
              5 views
              0 likes
              Last Post Trader146  
              Started by jpapa, 04-23-2024, 07:22 AM
              2 responses
              20 views
              0 likes
              Last Post rene69851  
              Working...
              X