Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calling SMA from State.Dataloaded

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

    Calling SMA from State.Dataloaded

    I am trying to calculate an SMA value in State.DataLoaded so that I don't have to call it over and over in OnBarUpdate since I only need the value once. Here is what I am trying but the value being printed does not match the SMA Indicator when added to a Daily chart:

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class MyCustomIndicator : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "MyCustomIndicator";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries("AAPL", new BarsPeriod {BarsPeriodType = BarsPeriodType.Day, Value = 1}, 30, "US Equities RTH", true);
                }
                else if (State == State.DataLoaded)
                {
                    //Print the 22 period SMA for the last bar in this bars array.
                    Print(SMA(BarsArray[1], 22)[0]);
                }            
            }
    
            protected override void OnBarUpdate()
            {
            }
        }
    }

    #2
    Hello swooke,

    Thanks for your message.

    Indicator values cannot be checked in State.DataLoaded because the script has not yet started processing data. There will not be any data processed for the indicator to create a value.

    I would suggest checking for the indicator value in OnBarUpdate for the BarsInProgress index of the data series that the indicator is based on. This will keep the value current to whenever it will be updated. If you are trying to find the current value only at the time you enable the strategy, you could consider checking this value in State.Realtime instead.

    More information on using BarsInProgress checks in OnBarUpdate is included in our Multi Time frame and Instruments guide (See True Event Driven OnBarUpdate Method) - https://ninjatrader.com/support/help...nstruments.htm

    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    80 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    63 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X