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

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 guillembm, Yesterday, 11:25 AM
                2 responses
                9 views
                0 likes
                Last Post guillembm  
                Started by junkone, 04-21-2024, 07:17 AM
                9 responses
                68 views
                0 likes
                Last Post jeronymite  
                Started by trilliantrader, 04-18-2024, 08:16 AM
                4 responses
                20 views
                0 likes
                Last Post trilliantrader  
                Started by mgco4you, Yesterday, 09:46 PM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by wzgy0920, Yesterday, 09:53 PM
                0 responses
                10 views
                0 likes
                Last Post wzgy0920  
                Working...
                X