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.

    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.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          53 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          130 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          70 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          44 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          49 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X