Announcement

Collapse
No announcement yet.

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 Playdc, Today, 05:47 PM
          0 responses
          3 views
          0 likes
          Last Post Playdc
          by Playdc
           
          Started by 2inthebush, Today, 04:22 PM
          1 response
          13 views
          0 likes
          Last Post 2inthebush  
          Started by Touch-Ups, Today, 08:42 AM
          4 responses
          23 views
          0 likes
          Last Post Touch-Ups  
          Started by Chromatix, 02-07-2024, 09:22 PM
          4 responses
          825 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by Browniver, 01-16-2024, 01:11 PM
          18 responses
          224 views
          0 likes
          Last Post 2inthebush  
          Working...
          X