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

How can I get the EMA of an added data series?

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

    How can I get the EMA of an added data series?

    Hello,

    If I AddDataSeries("^VIX", BarsPeriodType.Day, 1) in my strategy, how can I compute the EMA (period 10) of the added data in the (State == State.DataLoaded)? Thanks.

    Billy

    #2
    Hello billythekid72,

    Thank you for your post.

    We have some information about working with bars objects as input for indicator methods in the help guide here:


    If the ^VIX is the only added data series, here is the syntax you could use to get the EMA with a period of 10 based on the added series:
    EMA(BarsArray[1], 10)[0]

    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hello Emily,

      Thank you for your reply. ^VIX is the second data series I added, see below:

      else if (State == State.Configure)
      {
      AddDataSeries(Data.BarsPeriodType.Tick, 1);

      AddDataSeries("^VIX", BarsPeriodType.Day, 1);
      ​}


      I did exactly what you suggested and called it in the (State == State.DataLoaded) of the OnStateChange() callback.

      else if (State == State.DataLoaded)
      {
      Print("=========================================== ===========");
      Print("^VIX 10 days moving average" + EMA(BarsArray[1], 10)[0]);
      Print("=========================================== ===========");​
      }


      but I got the following error message:

      Strategy 'AGG6BarDailyMonthlyVIX': Error on calling the 'OnStateChange' method: Object reference not set to an instance of an object.

      Your assistance is much appreciated, thanks.

      Billy

      Comment


        #4
        Hello Emily,

        I want to further clarify that I was running the strategy in the StrategyAnalyzer. Thanks.

        Billy

        Comment


          #5
          Hello Billy,

          Thank you for your reply.

          I apologize for any confusion regarding State.DataLoaded. I added the bar index [0] to get the value for the current bar, although that is something that should be used in OnBarUpdate. For more information about each state in OnStateChange():


          Additionally, since the ^VIX is the second added data series, you would want to access BarsArray[2] instead of BarsArray[1], which would be the 1-tick data series. You could move the prints into OnBarUpdate() while also adding some logic to make sure you have enough bars on your chart and also ensure you are only calling the prints when the added ^VIX daily series is updated. This would look like the following snippet:
          Code:
          protected override void OnStateChange()
          {
          else if (State == State.Configure)
          {
          AddDataSeries(Data.BarsPeriodType.Tick, 1);
          AddDataSeries("^VIX", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
          }
          }
          
          protected override void OnBarUpdate()
          {
          // checks to make sure the ^VIX series has at least 10 bars, since the period of the EMA we are using is 10
          if (CurrentBars[2] < 10)
          return;
          // this will make sure the block of code only runs when OnBarUpdate() is called when BarsInProgress is 2, which is the ^VIX series
          if (BarsInProgress == 2)
          {
          Print("=========================================== ===========");
          Print("^VIX 10 days moving average" + EMA(BarsArray[2], 10)[0]);
          Print("=========================================== ===========");​
          }
          }
          ​I hope this has been some helpful information. Please let me know if I may be of further assistance.
          Emily C.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by traderzoso, 12-27-2020, 05:19 PM
          6 responses
          227 views
          0 likes
          Last Post lekor
          by lekor
           
          Started by ETFVoyageur, 05-18-2024, 12:45 AM
          4 responses
          26 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by Skifree, Today, 11:47 AM
          4 responses
          16 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by molecool, Today, 01:48 PM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by ETFVoyageur, Today, 12:26 PM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X