Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get SP 500 index in a script?

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

    How to get SP 500 index in a script?

    I wanna get a index in my strategy to check the general trend of the market. Is it possible to get it in the script?

    #2
    hoyje,

    Yes, you can use a multi-instrument strategy.

    A sample comes with NinjaTrader. You can see it by going to Edit NinjaScript > Strategy > SampleMultiinstrument.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      private string indexName = @"^SP500"; // Default setting for IndexName

      protected override void Initialize()
      {
      Add(IndexName, PeriodType.Day, 5000);
      My strategy stop working after adding extra instrument even though I don't use it in the script?

      Comment


        #4
        Hi hoyje,

        Are there any error messages in log tab of control center when you run the strategy? What is not working about it exactly?
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_RyanM View Post
          Hi hoyje,

          Are there any error messages in log tab of control center when you run the strategy? What is not working about it exactly?
          When using Strategy Analyser. I try backtest the strategy on DOW30 list with periodType.Day. First I try run the backtest without Add() and I get results. But when I adding the line Add() I don't get any results even though I don't even use the added index anywhere in the code. I have compile it and I don't get any errors. Code example, when uncomment Add() the script will stop working:

          Code:
                  
          protected override void Initialize()
          {
              //Add("^SP500", PeriodType.Day, 5000); 	
              SetStopLoss("", CalculationMode.Percent, 10, false);
              CalculateOnBarClose = true;
          }
          
          protected override void OnBarUpdate()
           {
               if (IsHighHigherThenHighestHigh)
                  EnterLong();
          			
               if (IsLowLowerThenLowestLow)
          	ExitLong();
          }
          
          private bool IsHighHigherThenHighestHigh
          {
             get 
             { 
                return High[0] > MAX(High, 50)[1]; 
             }
          }
          		
          private bool IsLowLowerThenLowestLow
          {
             get 
             { 
                return Low[0] < MIN(Low, 20)[1]; 
             }
          }

          Comment


            #6
            You're adding a 5000-Day series. Minimum bars required must be satisfied for all series. With the default setting of 20, your strategy would require at least 270 years of historical daily data before it would start taking orders.

            If you want to add a 1 day series, this is done with:
            Add(PeriodType.Day, 1);
            Last edited by NinjaTrader_RyanM1; 12-06-2011, 03:00 PM.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_RyanM View Post
              You're adding a 5000-Day series. Minimum bars required must be satisfied for all series. With the default setting of 20, your strategy would require at least 27 years of historical daily data before it would start taking orders.

              If you want to add a 1 day series, this is done with:
              Add(PeriodType.Day, 1);
              Seems to work now. Thanks for the help Ryan!

              Comment


                #8
                Glad that took care of it. Thanks for following up here.
                Ryan M.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                633 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                364 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                105 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                567 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                568 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X