Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy for stock pairs

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

    Strategy for stock pairs

    How could you make a strategy for a stock pair. I am looking to implement a strategy using bollinger bands on the ratio of two stock prices. How would I be able to do this and then backtest on that pair. It seems like strategy analyzer only allows you to backtest one stock at a time. Any Ideas?
    Thanks.

    #2
    Hello,

    Thank you for your post.

    This requires custom NinjaScript programming.
    Please see the multi-instrument instructions here: http://www.ninjatrader-support.com/H...struments.html
    Ray S.NinjaTrader Customer Service

    Comment


      #3
      Ok it seems like what I have to do is use the Add method to add the two stocks in the script, index them. Then reference that in the bollinger indicator. But since i am explicitly stating the dataset i am using the script, I would have to run the backtest from it too. (because i wouldn't be able to select stocks in the strategy analyzer window). Is there a way to run the backtest in ninja script if so, can you link to the help article.

      Comment


        #4
        intuit2k2,

        I am not sure I follow you exactly. What you want to do is add the series to make it a multi-series strategy and from there you can pass it to your Bollinger Bands.

        In the Strategy Analyzer you can choose the first instrument in your backtest. All subsequent instruments have to be chosen by the code. Unfortunately you cannot trigger a backtest per se from the code. It has to be either done in the Strategy Analyzer or in a chart.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          What I want to do is have a strategy that buys sells/based on the price ratio of 2 stocks relative to the price ratio's bollinger bands. Then i want to backtest this strategy on that exact pair.

          What is the best way to go about this?

          Comment


            #6
            intuit2k2,

            You can do this in the Strategy Analyzer. You select the first instrument and the second is chosen by the code.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Is there a way reference which stock you're referrign to by its index number. So lets say the stock that will be chosen by strategy analyzer is given index number 1, and the stock you add using the Add () method is index number 2. Is there a way to write something like the following.

              Index (1)/Index (2).Bollinger(2,20) ... trying to say thats its price ratios bollinger band... ?

              Comment


                #8
                intuit2k2,

                Yes. The selected one is 0. The added one is 1. If you add a third it is 2. All of this information is in the link RJStein provided earlier. Please review the "Using Bars Objects as Input to Indicator Methods" section of it.

                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  My question was specifically on the syntax of the bollinger method..
                  How would you be able to do:

                  Index (1)/Index (2).Bollinger(2,20) ... trying to refer to the price ratios bollinger band... ?

                  The alternative of just computing the quotient of the two bollinger bands wouldn't wokr because they wouldn't be equivalent.

                  Does this make sense?

                  Comment


                    #10
                    intuit2k2, not sure I fully follow - you can compute your ratio as dataseries first and then use this as input data for your BollingerBands method - http://www.ninjatrader-support.com/H...Bollinger.html
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      This is what I have so far. I tried to do what you suggested to make a new dataSeries and followed the example in the link you posted. Do you see anything wrong in the following code...

                      I got an expected class error:

                      #region Using declarations
                      using System;
                      using System.ComponentModel;
                      using System.Diagnostics;
                      using System.Drawing;
                      using System.Drawing.Drawing2D;
                      using System.Xml.Serialization;
                      using NinjaTrader.Cbi;
                      using NinjaTrader.Data;
                      using NinjaTrader.Indicator;
                      using NinjaTrader.Gui.Chart;
                      using NinjaTrader.Strategy;
                      #endregion

                      // This namespace holds all strategies and is required. Do not change it.
                      namespace NinjaTrader.Strategy
                      {
                      /// <summary>
                      /// Enter the description of your strategy here
                      /// </summary>
                      [Description("Enter the description of your strategy here")]
                      public class BolBands : Strategy
                      {
                      #region Variables
                      // Wizard generated variables
                      private int myInput0 = 1; // Default setting for MyInput0
                      // User defined variables (add any user defined variables below)
                      #endregion

                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize()
                      {

                      Add("MSFT",PeriodType.Day, 200);
                      CalculateOnBarClose = true;
                      }
                      #region Variables
                      private DataSeries myDataSeries;
                      #endregion
                      }

                      protected override void Initialize()
                      { myDataSeries = new DataSeries(this); }

                      protected override void OnBarUpdate()
                      { myDataSeries.set(close[1]/close[0]); }


                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {

                      // Condition set 1
                      if (Bollinger(2, 20).Upper[0] < GetCurrentAsk())
                      {
                      EnterShort(DefaultQuantity, "");
                      }

                      // Condition set 2
                      if (Bollinger(2, 20).Upper[0] > GetCurrentAsk())
                      {
                      ExitShort("", "");
                      }

                      // Condition set 3
                      if (Bollinger(2, 20).Lower[0] > GetCurrentAsk())
                      {
                      EnterLong(DefaultQuantity, "");
                      }

                      // Condition set 4
                      if (Bollinger(2, 20).Lower[0] < GetCurrentAsk())
                      {
                      ExitLong("", "");
                      }
                      }

                      #region Properties
                      [Description("")]
                      [Category("Parameters")]
                      public int MyInput0
                      {
                      get { return myInput0; }
                      set { myInput0 = Math.Max(1, value); }
                      }
                      #endregion
                      }
                      }

                      Comment


                        #12
                        You did this:

                        protected override void Initialize()
                        {

                        Add("MSFT",PeriodType.Day, 200);
                        CalculateOnBarClose = true;
                        }
                        #region Variables
                        private DataSeries myDataSeries;
                        #endregion
                        }

                        protected override void Initialize()
                        { myDataSeries = new DataSeries(this); }

                        You should not create Initialize() twice. Just move that myDataSeries line into the original Initialize() as a new line. You should also put the private DataSeries line in the original Variables region.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Ok This is what I have..

                          // This namespace holds all strategies and is required. Do not change it.
                          #region Using declarations
                          using System;
                          using System.ComponentModel;
                          using System.Diagnostics;
                          using System.Drawing;
                          using System.Drawing.Drawing2D;
                          using System.Xml.Serialization;
                          using NinjaTrader.Cbi;
                          using NinjaTrader.Data;
                          using NinjaTrader.Indicator;
                          using NinjaTrader.Gui.Chart;
                          using NinjaTrader.Strategy;
                          #endregion

                          // This namespace holds all strategies and is required. Do not change it.
                          namespace NinjaTrader.Strategy
                          {
                          /// <summary>
                          /// Enter the description of your strategy here
                          /// </summary>
                          [Description("Enter the description of your strategy here")]
                          public class BolBands : Strategy
                          {
                          #region Variables
                          // Wizard generated variables
                          private int myInput0 = 1; // Default setting for MyInput0
                          private DataSeries myDataSeries;
                          // User defined variables (add any user defined variables below)
                          #endregion

                          /// <summary>
                          /// This method is used to configure the strategy and is called once before any strategy method is called.
                          /// </summary>
                          protected override void Initialize()
                          {

                          Add("MSFT",PeriodType.Day, 200);
                          CalculateOnBarClose = true;
                          myDataSeries = new DataSeries(this); }

                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>

                          protected override void OnBarUpdate()
                          { myDataSeries.set(close[1]/close[0]); }

                          /// Condition set 1
                          if (Bollinger(myDataSeries,2, 20).Upper[myDataSeries] < CurrentBar() )
                          { EnterShort(DefaultQuantity, ""); }

                          /// Condition set 2
                          if (Bollinger(myDataSeries,2, 20).Upper[myDataSeries] > CurrentBar() )
                          {
                          ExitShort("", "");
                          }

                          /// Condition set 3
                          if (Bollinger(myDataSeries,2, 20).Lower[myDataSeries] > CurrentBar() )
                          {
                          EnterLong(DefaultQuantity, "");
                          }

                          /// Condition set 4
                          if (Bollinger(myDataSeries,2, 20).Lower[0] < CurrentBar ())
                          {
                          ExitLong("", "");
                          }
                          }

                          #region Properties
                          [Description("")]
                          [Category("Parameters")]
                          public int MyInput0
                          {
                          get { return myInput0; }
                          set { myInput0 = Math.Max(1, value); }
                          }
                          #endregion
                          }
                          }

                          These are the errors:
                          Mostly in lines 49-59 including identifier expected (doesnt' recognize myDataSeries), Class member decleration expectation and other other errors "] expected"

                          Thanks.

                          Comment


                            #14
                            protected override void OnBarUpdate()
                            { myDataSeries.set(close[1]/close[0]); }

                            You should not be closing the OnBarUpdate() method until after all your logic is inside there.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              I only have two errors left. Code is below.
                              error in line 80: Error "} expected"
                              error in line 83: Error "Expected class, delegate, enum, interface, or struct"



                              // This namespace holds all strategies and is required. Do not change it.
                              #region Using declarations
                              using System;
                              using System.ComponentModel;
                              using System.Diagnostics;
                              using System.Drawing;
                              using System.Drawing.Drawing2D;
                              using System.Xml.Serialization;
                              using NinjaTrader.Cbi;
                              using NinjaTrader.Data;
                              using NinjaTrader.Indicator;
                              using NinjaTrader.Gui.Chart;
                              using NinjaTrader.Strategy;
                              #endregion

                              // This namespace holds all strategies and is required. Do not change it.
                              namespace NinjaTrader.Strategy
                              {
                              /// <summary>
                              /// Enter the description of your strategy here
                              /// </summary>
                              [Description("Enter the description of your strategy here")]
                              public class BolBands : Strategy
                              {
                              #region Variables
                              // Wizard generated variables
                              private int myInput0 = 1; // Default setting for MyInput0
                              private DataSeries myDataSeries;
                              // User defined variables (add any user defined variables below)
                              #endregion

                              /// <summary>
                              /// This method is used to configure the strategy and is called once before any strategy method is called.
                              /// </summary>
                              protected override void Initialize()
                              {

                              Add("MSFT",PeriodType.Day, 200);
                              CalculateOnBarClose = true;
                              myDataSeries = new DataSeries(this); }

                              /// <summary>
                              /// Called on each bar update event (incoming tick)
                              /// </summary>

                              protected override void OnBarUpdate()
                              {
                              { myDataSeries.set(close[1]/close[0]); }

                              /// Condition set 1
                              if (Bollinger(myDataSeries,2, 20).Upper[myDataSeries] < CurrentBar() );
                              { EnterShort(DefaultQuantity, ""); }

                              /// Condition set 2
                              if (Bollinger(myDataSeries,2, 20).Upper[myDataSeries] > CurrentBar() );
                              {
                              ExitShort("", "");
                              }

                              /// Condition set 3
                              if (Bollinger(myDataSeries,2, 20).Lower[myDataSeries] > CurrentBar() );
                              {
                              EnterLong(DefaultQuantity, "");
                              }

                              /// Condition set 4
                              if (Bollinger(myDataSeries,2, 20).Lower[0] < CurrentBar ());
                              {
                              ExitLong("", "");
                              }
                              }
                              }
                              #region Properties
                              [Description("")]
                              [Category("Parameters")]
                              public int MyInput0
                              {
                              get { return myInput0; }
                              set { myInput0 = Math.Max(1, value); } }
                              #endregion
                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by inanazsocial, Today, 01:15 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post inanazsocial  
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              5 responses
                              22 views
                              0 likes
                              Last Post trilliantrader  
                              Started by Davidtowleii, Today, 12:15 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Davidtowleii  
                              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
                              71 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X