Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator OnBarUpdate not called from strategy

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

    Indicator OnBarUpdate not called from strategy

    Hello, when accessing a custom indicator from a strategy, the indicator's OnBarUpdate() method is not being called. I have enabled the strategy, and set both to calculate OnBarClose.

    Strategy:
    Code:
    // Strategy code
    // These defined above OnStateChange();
    private AdvancedSRZones ASRZ; // defined above OnStateChange();
    private SMA sma1; // defined above OnStateChange();
    
    else if (State == State.DataLoaded) // inside OnStateChange()
    {
    
        ASRZ = AdvancedSRZones(AreaStrengthMultiplier, TimeThreshold, ProxyStrengthMultiplier, NewZoneStrength, ZoneTimeoutStrength, NewZoneTopMultiplier, NewZoneBottomMultiplier, ResZoneColor, SupZoneColor);
        AddChartIndicator(ASRZ);
    
    }
    When replacing this indicator load with a standard one, like SMA, the standard indicator SMA appears correctly. My custom indicator call is not even running OnBarUpdate() How can I fix this?
    I have already tried calling the indicator in the strategy OnBarUpdate(), it just throws an error that the index is out of bounds, like so: (assume ... is args)
    Code:
    protected override void OnBarUpdate() // Strategy code
    {
        var temp = AdvancedSRZones( ... )[0]; // Out of bounds error
    }
    Both the indicator and strategy use identical State.SetDefault parameters, so they should be synced...

    Thank you

    edit: I should mention the indicator appears perfectly fine on its own, when not hosted by a strategy.
    Last edited by eoinf; 04-05-2021, 11:18 PM.

    #2
    Originally posted by eoinf View Post
    Code:
    ASRZ = [COLOR=#e74c3c]AdvancedSRZones[/COLOR](AreaStrengthMultiplier, TimeThreshold, ProxyStrengthMultiplier,
                           NewZoneStrength, ZoneTimeoutStrength, NewZoneTopMultiplier,
                           NewZoneBottomMultiplier, ResZoneColor, SupZoneColor);
    edit: I should mention the indicator appears perfectly fine on its own, when not hosted by a strategy.
    Do you have the source code for this custom indicator?

    If yes, we can certainly help.

    If no, then you'll have to ask the vendor from whence this indicator
    came why you can't use it in a strategy.

    [Some vendors have been known to release free indicators as closed
    source that they specifically state cannot be used in a strategy. Why?
    Because they sell a "Pro" version of said free indicator, and one of
    the features of the Pro version is: it works with strategies.]

    Where did you get this indicator?

    Comment


      #3
      Small amend in your first code example you provide.

      Instead of :
      Code:
      private AdvancedSRZones ASRZ; // defined above OnStateChange();
      please try :
      Code:
      private NinjaTrader.NinjaScript.Indicators.AdvancedSRZones ASRZ;

      Secondly, How about you take out the code :
      Code:
      var temp = AdvancedSRZones( ... )[0];
      will it be still error ??

      Thirdly, can you copy paste the whole error sentence ???


      Lastly, after tried my way, you still got error, likes david mentioned, contact back the people who do this default indicator for you.

      Comment


        #4
        Hi eoinf, thanks for posting.

        If AdvancedSRZones has a period that it calculates from make sure to wait until that many bars before accessing it in OnBarUpdate e.g.

        if(CurrentBar < 10) return; //period of 10 bars to calculate the indicator

        var temp = AdvancedSRZones( ... )[0];

        If this does not fix the issue, we would need to either see a copy of the script or you would need to ask the developer how to properly access the indicator in a strategy.

        Best regards.

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hi eoinf, thanks for posting.

          If AdvancedSRZones has a period that it calculates from make sure to wait until that many bars before accessing it in OnBarUpdate e.g.

          if(CurrentBar < 10) return; //period of 10 bars to calculate the indicator

          var temp = AdvancedSRZones( ... )[0];

          If this does not fix the issue, we would need to either see a copy of the script or you would need to ask the developer how to properly access the indicator in a strategy.

          Best regards.
          Hello, I have tried this fix and a combination of others, and got different issues for each. I have many debugging Print lines setup. When I use [0] at the end of the indicator call, I get an out of bounds error. When I don't use that, OnBarUpdate() is not called inside the indicator.
          Each time a bar closes, the strategy should call the indicator. When the indicator is called, the indicator loops through all bars in the dataset and displays zones on the chart.

          Now what I have realized is, the indicator only calculates OnBarUpdate ... it doesn't seem to loop through any historical bars. I wonder if this is why it works on its own, but not when called from a strategy.

          Thanks for the help, the code is attached for both. (I have written both pieces) The indicator is quite long, I recommend collapsing the unimportant parts from a text editor.
          Attached Files
          Last edited by eoinf; 04-06-2021, 11:33 AM.

          Comment


            #6
            Hi, thanks for posting that.

            I got it to work by calling Update() on the object in the Strategies OnBarUpdate:

            protected override void OnBarUpdate()
            {
            ASRZ.Update();
            ...

            The indicator is showing up after adding that.

            Please let me know if you have any other questions.

            Comment


              #7
              Originally posted by NinjaTrader_ChrisL View Post
              Hi, thanks for posting that.

              I got it to work by calling Update() on the object in the Strategies OnBarUpdate:

              protected override void OnBarUpdate()
              {
              ASRZ.Update();
              ...

              The indicator is showing up after adding that.

              Please let me know if you have any other questions.
              Wow, it was really that simple. Thank you!

              Comment


                #8
                Thanks Chris for sharing this solution! I can't believe it was that simple

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                22 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                120 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                63 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                41 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                45 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X