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

When do I need to call Update()

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

    When do I need to call Update()

    I've got a problem with a Strategy that's Freezing sometimes when I add it to a chart. I'm presently digging through my code trying to work out what the problem is.

    I'm wondering if I'm calling Update() in too many places.

    The guide at: https://ninjatrader.com/support/help...us/?update.htm

    Covers the requirement for Update() when accessing internal simple variables.

    I'm currently also calling it when accessing internal Series<> object and plots.

    e.g.

    Code:
    private Series<double> _lowSeries;
    
            [NinjaScriptProperty]
            [Browsable(false)]
            [XmlIgnore]
            public ISeries<double> LowSeries
            {
                get
                {
                    Update();
                    return _lowSeries;
                }
            }
    
            [NinjaScriptProperty]
            [Browsable(false)]
            [XmlIgnore]
            public ISeries<double> PlotSeries
            {
                get
                {
                    Update();
                    return Values[0];
                }
            }
    I intend to grab values from these externally as follows:

    _myIndicator.LowSeries[0];
    _myIndicator.PlotSeries[0];

    Do I need to call Update() if I'm accessing the properties in this way?

    #2
    Also.... If I have a public method that makes use of the CurrentBar property, do I need to call Update() to ensure that CurrentBar is the correct value? e.g:

    Code:
            public bool PublicMethod()
            {
                Update();
    
                if (CurrentBar == -1) return false;
                else return ......
    
    
            }

    Comment


      #3
      Hello kevinenergy,

      Thanks for your questions.

      I'm not aware of a context where you could run into freezes calling Update(). I would suggest reducing the code until the issue is clear. If you find that there is a situation where freezes can be encountered with Update() we would appreciate it if you can provide us a small example that shows how this can be reproduced.

      Series objects will be synchronized to the data series they are built against, however if you are accessing the Series from another event (for example, a timer) you may wish to ensure that OnBarUpdate() is called before accessing the value. This is when it would be good use to use Update(). If you are simply accessing those values from OnBarUpdate(), this would not be necessary since the hosting NinjaScript adds all needed data series. These same rules would apply for exposing public variables as well public series/plots. Update() should really be reserved for specific situations where OnBarUpdate() might not be called.

      For the public method, I would recommend adding Update() if the method would be used in a context outside of OnBarUpdate().

      Please let us know if you have any additional questions.
      JimNinjaTrader Customer Service

      Comment


        #4
        Thanks Jim,

        I have ripped my code to bits. Your right it had nothing to do with calls to Update(). I'm really not sure what the problem is, but I think it has something to do with the Indicator cache, and how I'm passing Series between indicators.

        My strategy has an Indicator which processes the Open High Low Close prices into a smoother version. I'm then passing these Series to other indicators via NinjascriptProperty like this:

        Code:
        [NinjaScriptProperty]
        [Browsable(false)]
        [XmlIgnore]
        public ISeries<double> HighSeries
        {
              set { _highSeries = value; }
              get
              {
                  return _highSeries ?? High;
              }
        }
        And these indicators pass the series to other indicators too. My strategy seems to work once, but as soon as I disable it and re-enable it it crashes the chart and I need to force-quit the whole platform. The fact that it works first time but not second time makes me think it might be a cache issue.

        Questions:

        What is the context of the indicator cache (via the partial class at the bottom of the indicator classes). i.e. Is it a global cache that all Charts and strategies can use. Or does every strategy have its own cache?

        Is there any way to bypass the cache. i.e to instantiate an indicator directly without putting it in the cache?


        Thanks for the excellent support!

        Comment


          #5
          Hello kevinenergy,

          The NinjaScript generated code at the bottom of the script are simply overloads for the indicator so they can be called by other NinjaScripts.

          Any issue seen with cached properties would mostly be encountered when optimizing a strategy with IsInstantiatedOnEachOptimizationIteration set to false, and not properly resetting class level variables. With that said, I would not expect freezes from here either. You can read more about this property and other best practices in the link below.

          NinjaScript best practices - https://ninjatrader.com/support/help..._practices.htm

          You could also add prints through out your script after you have reduced it the best you can. If you see prints before accessing or setting one of these properties, that would tip you off for an issue related there.

          I could not really provide too much input on your code being an issue unless I had more context for how it was being used and how you were running into the issue. I tested using Series<double>'s and I did not have any issue in a simple test. In addition to adding prints throughout the script, I suggest breaking down the indicator to basic terms of functionality. An example like the below would confirm if a specific implementation you are trying is the issue.

          Code:
          private Series<double> _highSeries;
          
          protected override void OnStateChange()
          {
              if (State == State.SetDefaults)
              {
                  Description                                    = @"Enter the description for your new custom Indicator here.";
                  Name                                        = "MyCustomIndicator";
              }
              else if (State == State.DataLoaded)
              {                
                  _highSeries = new Series<double>(this);
              }
          }
          
          protected override void OnBarUpdate()
          {
              HighSeries[0] = Close[0];
              Print(HighSeries[0]);
          }
          
          [NinjaScriptProperty]
          [Browsable(false)]
          [XmlIgnore]
          public Series<double> HighSeries
          {
                set { _highSeries = value; }
                get
                {
                    return _highSeries ?? High as Series<double>;
                }
          }
          Please let us know if we can be of further assistance.
          JimNinjaTrader Customer Service

          Comment


            #6
            Hey Jim,

            I think I got to the bottom of the issue. I opened a new thread with a sample strategy and instructions at the following link:



            Comment


              #7
              Thanks keveinenergy,

              I'll follow up with you on that thread after reviewing.
              JimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Aviram Y, Today, 05:29 AM
              0 responses
              1 view
              0 likes
              Last Post Aviram Y  
              Started by quantismo, 04-17-2024, 05:13 PM
              3 responses
              25 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by ScottWalsh, 04-16-2024, 04:29 PM
              7 responses
              34 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by cls71, Today, 04:45 AM
              0 responses
              6 views
              0 likes
              Last Post cls71
              by cls71
               
              Started by mjairg, 07-20-2023, 11:57 PM
              3 responses
              216 views
              1 like
              Last Post PaulMohn  
              Working...
              X