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

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
    BertrandNinjaTrader Customer Service

    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 -

        BertrandNinjaTrader Customer Service

        Comment


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

          Dan

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,404 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          95 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          8 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          159 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          8 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X