Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Beginner's question:How to get the price of an index?

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

    Beginner's question:How to get the price of an index?

    Hi,

    On my strategy I'm trying to calculate a price ratio between SP500 instruments and ^SPX(or SPY). Do i need to insert Add("SPY",PeriodType.Day,1) on Initialize() in order to get the price of SPY?

    Thank you in advance

    Dan

    #2
    Hi Dan, welcome to our forums - yes that would be correct, if you wanted to add / access the daily price series of the SPY ETF. A detailed into and reference for our MultiSeries framework could be reviewed here : http://www.ninjatrader.com/support/h...nstruments.htm

    Comment


      #3
      Bertrand,

      First of all thank you for your quick response,
      Here's the code in my strategy:
      Code:
      protected override void Initialize()
              {
      						
                              CalculateOnBarClose = false;
      			Add("SPY",PeriodType.Day,1);
      			myDataSeries = new DataSeries(this); 
      			
      			
              }
      protected override void OnBarUpdate()
              {
                              double instClose=Close[0];
      			double spyClose=Closes[1][0];
      			myDataSeries.Set(Close[0]/Closes[1][0]);
      			PriceRatio=instClose /spyClose;
                              Print("The date is ="+Time[0].Date.ToString()+ "The stock is "+Instrument.FullName+" the price is "+Close[0]);
      			Print("Priceratio is "+PriceRatio);
      }
      And this is my output window:
      The date is =5/24/2013 12:00:00 AM The stock is BSX the price is 9.08
      Priceratio is 0.0549271066481157
      The date is =5/24/2013 12:00:00 AM The stock is SPY the price is 165.31
      Priceratio is 1
      The date is =5/24/2013 12:00:00 AM The stock is BTU the price is 20.44
      Priceratio is 0.123646482366463
      The date is =5/24/2013 12:00:00 AM The stock is SPY the price is 165.31
      Priceratio is 1
      By looking at the output you can see that I get two Priceratio for the same date and I only need one, which is the first one.
      What am I doing wrong?

      Thank you,

      Dan

      Comment


        #4
        Dan, you're welcome - you're not filtering for any Bars update calling OnBarUpdate() here, so essentially your code is then run twice as both the primary and added series would be calling the calcs.

        You can filter this via BarsInProgress -

        Comment


          #5
          Thank you, I'll give it a try.

          Dan

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          330 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          548 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          549 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X