Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnBarUpdate() not called

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

    OnBarUpdate() not called

    I've created the custom indicator below which will ultimately be called from a host indicator. The host indicator invokes it from its OnStateChange() event. ie...

    Code:
    if (State == State.Configure){
       rth = RTHEventIndicator(365);
    }
    I found that the RTHEventIndicator below has to be loaded at this specific host state due to the fact the RTHEventIndicator adds a data series. The code below has two print statements. One in OnStateChange() and the other in OnBarUpdate(). The state changes print but OnBarUpdate is never printed.

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Indicators in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.Indicators.Stoneworks
    {
        public class RTHEventIndicator : Indicator
        {
    
            protected override void OnStateChange()
            {
                Print("------ OnStateChange(" + State + ")");        
                if (State == State.SetDefaults)
                {
                    Description = @"Test OnBarUpdate with dataSeries.";
                    Name = "RTHEventIndicator";
                    IsOverlay = true;
                    IsSuspendedWhileInactive = true;
                    Calculate = Calculate.OnEachTick;
                    Period = 365;
    
                  //  AddPlot(new Stroke(Brushes.Red, DashStyleHelper.Dash, 2), PlotStyle.Line, "RTHTest");
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, Period + 1, Instrument.MasterInstrument.TradingHours.Name, true);
                }
                else if (State == State.DataLoaded)
                {
                    sessionIterator = new SessionIterator(BarsArray[1]);
                }
            }
    
    
            protected override void OnBarUpdate()
            {
                Print("----- OnBarUpdate()");
                if (bars.IsFirstBarOfSession)
                {
                    sessionIterator.GetNextSession(Time[0], true);
                }
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Period", Order=1, GroupName="Parameters")]
            public int Period
            { get; set; }
            #endregion
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private Stoneworks.RTHEventIndicator[] cacheRTHEventIndicator;
            public Stoneworks.RTHEventIndicator RTHEventIndicator(int period)
            {
                return RTHEventIndicator(Input, period);
            }
    
            public Stoneworks.RTHEventIndicator RTHEventIndicator(ISeries<double> input, int period)
            {
                if (cacheRTHEventIndicator != null)
                    for (int idx = 0; idx < cacheRTHEventIndicator.Length; idx++)
                        if (cacheRTHEventIndicator[idx] != null && cacheRTHEventIndicator[idx].Period == period && cacheRTHEventIndicator[idx].EqualsInput(input))
                            return cacheRTHEventIndicator[idx];
                return CacheIndicator<Stoneworks.RTHEventIndicator>(new Stoneworks.RTHEventIndicator(){ Period = period }, input, ref cacheRTHEventIndicator);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.Stoneworks.RTHEventIndicator RTHEventIndicator(int period)
            {
                return indicator.RTHEventIndicator(Input, period);
            }
    
            public Indicators.Stoneworks.RTHEventIndicator RTHEventIndicator(ISeries<double> input , int period)
            {
                return indicator.RTHEventIndicator(input, period);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.Stoneworks.RTHEventIndicator RTHEventIndicator(int period)
            {
                return indicator.RTHEventIndicator(Input, period);
            }
    
            public Indicators.Stoneworks.RTHEventIndicator RTHEventIndicator(ISeries<double> input , int period)
            {
                return indicator.RTHEventIndicator(input, period);
            }
        }
    }
    
    #endregion
    Any idea why OnBarUpdate() is never called? Please note the indicator's namespace is one folder deeper than the OOTB indicators. I don't think that should make any difference because the indicator works fine when used unhosted.

    #2
    Hi stoner, thanks for your post.

    The host script must also add the same data series that the child indicator is adding. See all the warnings on this page:


    Also, the child indicator is set to Calculate.OnEachTick, the hosting indicator must also run OnEachTick for this to work. After you add the data series in State.Configure, set up the indicator in State.DataLoaded, so rth = RTHEventIndicator(365); will need to be moved to State.DataLoaded.

    Please let me know if this does not resolve your inquiry.

    Comment


      #3
      Just tried to test your suggestions. I assumed they'd work and moved on to something else expecting success when I came back to it.

      I call the same exact AddDataSeries() within the State.Configure events in both the Host and the Hosted indicators.

      Code:
      AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, Period + 1, Instrument.MasterInstrument.TradingHours.Name, true);
      I set rth = RTHEventIndicator(365) in the Host's State.DataLoaded event.

      I ensured the Indicator's User Interface is configured to use OnEachTick. Both the Host and the Hosted indicators are initializing State.SetDefaults to Calculate = Calculate.OnEachTick

      This is very frustrating especially since the indicator I'm attempting to host primary function is overcoming an NT deficiency.

      Comment


        #4
        Hi, thanks for your reply.

        Try calling "rth.Update();" in the host indicators OnBarUpdate method. This will force the indicator's OBU method to be called, it worked when I tried it.

        Comment


          #5
          Calling rth.Update() from the host does indeed cause the hosted OBU to be called. Is this the solution or a work around? Because other hosted indicators such as SMA have their OBU methods invoked without calling Update(). Granted SMA doesn't call AddDataSeries()

          Comment


            #6
            Hi, stoner thanks for your patience.

            It needs to be there because of the tick-by-tick nature of the script. Calling Update ensures that the indicators are in sync with each other.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            566 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            330 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            547 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            548 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X