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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    601 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    347 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    103 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    559 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    558 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X