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

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.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NRITV, Today, 01:15 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by maybeimnotrader, Yesterday, 05:46 PM
    5 responses
    24 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by quantismo, Yesterday, 05:13 PM
    2 responses
    16 views
    0 likes
    Last Post quantismo  
    Started by frankthearm, Today, 09:08 AM
    6 responses
    27 views
    0 likes
    Last Post frankthearm  
    Started by adeelshahzad, Today, 03:54 AM
    5 responses
    33 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Working...
    X