Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy for stock pairs

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

    #16
    { myDataSeries.set(close[1]/close[0]); }

    You should not be { } bracketing this.

    Likewise you may or may not have an extra } at the end of condition set 4, before Properties. You need to match them up yourself. Each { needs a }.
    Josh P.NinjaTrader Customer Service

    Comment


      #17
      Originally posted by intuit2k2 View Post
      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("", "");
      }
      }
      }
      try replacing your OnBarUpdate with this:

      Code:
      protected override void OnBarUpdate()
      {
      myDataSeries.set([B]closes[1][0]/closes[0][0][/B]);
      
      /// 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("", "");
      }
      }

      Comment


        #18
        Thanks for your posts.

        THe root of the error i am getting comes from the fact that the code generated my ninja strategy wizard does not compile, this is the code with no changes.. and its giving me error: expected class in line 80 which just has a "}" in it.

        Here is the code:

        #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 CefPairTest : 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);
        CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
        //myDataSeries.Set(closes[1][0]/close[0][0]
        // Condition set 1
        if (CrossBelow(Bollinger(2, 20).Upper, GetCurrentAsk(), 1))
        {
        EnterShort(DefaultQuantity, "");
        }

        // Condition set 2
        if (CrossAbove(Bollinger(2, 20).Upper, GetCurrentAsk(), 1))
        {
        ExitShort("", "");
        }

        // Condition set 3
        if (CrossAbove(Bollinger(2, 20).Lower, GetCurrentAsk(), 1))
        {
        EnterLong(DefaultQuantity, "");
        }

        // Condition set 4
        if (CrossBelow(Bollinger(2, 20).Lower, GetCurrentAsk(), 1))
        {
        ExitLong("", "");
        }
        }

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

        Comment


          #19
          intuit2k2, are you sure the error is located in 'this' strategy code? Please keep in mind all errors in all your NinjaScript files will be listed at this bottom of the NinjaScript editor when compiling. Just doubleclick on the error shown and the editor will show exactly in which file and line it is located.

          Comment


            #20
            So i got the code to compile, now when i need to figure how to change my order entry so that that i am long one stock short the other in my pair system. The enterlong function doesn't have an input where ou can specifiy which indexed stock you're referring to. how would I go about doing this. Also when backtesting the strategy didnt' generate any trades. DOes anyone know why?


            #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 CefPairTest : 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);
            CalculateOnBarClose = true;
            }

            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
            myDataSeries.Set(Close[1]/Close[0]);
            // Condition set 1
            if (CrossBelow(Bollinger(myDataSeries,2, 20).Upper, GetCurrentAsk(), 1))
            {
            EnterShort(DefaultQuantity, "");
            }

            // Condition set 2
            if (CrossAbove(Bollinger(myDataSeries,2, 20).Upper, GetCurrentAsk(), 1))
            {
            ExitShort("", "");
            }

            // Condition set 3
            if (CrossAbove(Bollinger(myDataSeries,2, 20).Lower, GetCurrentAsk(), 1))
            {
            EnterLong(DefaultQuantity, "");
            }

            // Condition set 4
            if (CrossBelow(Bollinger(myDataSeries,2, 20).Lower, GetCurrentAsk(), 1))
            {
            ExitLong("", "");
            }
            }

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

            Comment


              #21
              You need to specify which bars series to submit to and that in turn will determine which instrument the order is for.


              Please see the last listed syntax.
              Josh P.NinjaTrader Customer Service

              Comment


                #22
                I have specified the stock through the bars in progress input, but the strategy still doesn't generated trades when backtested. Could it be that the crossover isn't the appropriate trigger to use, but instead use bollbands being greater than the current ask... Do you think this is causing the problem.

                #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 CefPairTest : 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);
                CalculateOnBarClose = true;
                }

                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                myDataSeries.Set(Close[1]/Close[0]);
                // Condition set 1
                if (CrossBelow(Bollinger(myDataSeries,2, 20).Upper, GetCurrentAsk(), 1))
                {
                EnterShort(0,100, "");
                EnterLong (1,100, "");
                }

                // Condition set 2
                if (CrossAbove(Bollinger(myDataSeries,2, 20).Upper, GetCurrentAsk(), 1))
                {
                ExitShort("", "");
                ExitLong ("", "");
                }

                // Condition set 3
                if (CrossAbove(Bollinger(myDataSeries,2, 20).Lower, GetCurrentAsk(), 1))
                {
                EnterLong(0,DefaultQuantity, "");
                EnterShort(1,DefaultQuantity, "");
                }

                // Condition set 4
                if (CrossBelow(Bollinger(myDataSeries,2, 20).Lower, GetCurrentAsk(), 1))
                {
                ExitLong("", "");
                ExitShort("", "");
                }

                }


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

                Comment


                  #23
                  Hello,

                  I will have Josh reply on Monday.
                  DenNinjaTrader Customer Service

                  Comment


                    #24
                    Add the following at the start of OnBarUpdate:

                    Code:
                    if (CurrentBar < 1) return;
                    This is because your strategy references Close[1] and it's not defined for bar 0.

                    Comment


                      #25
                      I would suggest trying to reverse the order of your series in the CrossAbove / CrossBelow statements...you may also simplify it by only submitting to one bars object for execution first, just to ensure the trigger is correct, then proceed by adding the other bars object.

                      Comment


                        #26
                        Below is the code that i have. I can't figure out whether i can use myDataSeries throughout the code if i have declared and assigned a value. The compilation errors that i have been getting is that DataSeries.this is an overloaded method and that it can't convert DataSeries.this into an 'int'. and that close is used in a wrong context. The compilation errors are in lines 50, 57, 64 and 71. Thanks for your help



                        #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 CefPairTest : 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);
                        CalculateOnBarClose = true;
                        }

                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                        myDataSeries.Set(Close[1]/Close[0]);

                        // Condition set 1
                        if (CrossAbove(Bollinger(2, 20).Upper[myDataSeries],close[myDataSeries],1));
                        {
                        EnterShort(1,100, "");
                        EnterLong(0,100, "");
                        }

                        // Condition set 2
                        if (CrossBelow(Bollinger(2, 20).Upper[myDataSeries],close[myDataSeries],1));
                        {
                        ExitShort("", "");
                        ExitLong("", "");
                        }

                        // Condition set 3
                        if (CrossAbove(Bollinger(2, 20).Lower[myDataSeries],close[myDataSeries],1));
                        {
                        EnterLong(0,100, "");
                        EnterShort(1,100,"");
                        }

                        // Condition set 4
                        if (CrossBelow(Bollinger(2, 20).Lower[myDataSeries],close[myDataSeries],1));
                        {
                        ExitLong("", "");
                        ExitShort("", "");
                        }


                        // Condition set 1
                        // if (CrossBelow(Bollinger(myDataSeries,2, 20).Upper, GetCurrentAsk(), 1))
                        // {
                        // EnterShort(0,100, "");
                        // EnterLong (1,100, "");
                        // }

                        // Condition set 2
                        // if (CrossAbove(Bollinger(myDataSeries,2, 20).Upper, GetCurrentAsk(), 1))
                        // {
                        // ExitShort("", "");
                        // ExitLong ("", "");
                        // }

                        // Condition set 3
                        // if (CrossAbove(Bollinger(myDataSeries,2, 20).Lower, GetCurrentAsk(), 1))
                        // {
                        // EnterLong(0,DefaultQuantity, "");
                        // EnterShort(1,DefaultQuantity, "");
                        // }

                        // Condition set 4
                        // if (CrossBelow(Bollinger(myDataSeries,2, 20).Lower, GetCurrentAsk(), 1))
                        // {
                        // ExitLong("", "");
                        // ExitShort("", "");
                        // }

                        }


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

                        Comment


                          #27
                          Interesting idea.

                          I've changed it so that it compiles (see attached).

                          And who knows, that might be what you wanted it to do.

                          I also added the fix that I mentioned earlier, as now that it will compile, that would have been the first run-time error that you would have encountered.

                          Good luck.
                          Attached Files

                          Comment


                            #28
                            Thanks KBJ,

                            Code does compile, but does not execute any trades. Maybe its my connectivity... Maybe we are not coding the signals correctly.

                            Comment


                              #29
                              "if (CrossAbove(Bollinger(2, 20).Lower,myDataSeries,1));"

                              In this line for example, we have to indicate that its the bollinger band of the price ratio, the program assumes that its the stock being backtested, i think we need to use the following bollinger syntax:

                              bollinger(input data, st dev, # of periods)

                              What do you think?

                              Comment


                                #30
                                Hi intuit2k2,

                                did your changes fix the problem as I don't get any trades either?

                                Thanks

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by NullPointStrategies, Yesterday, 05:17 AM
                                0 responses
                                56 views
                                0 likes
                                Last Post NullPointStrategies  
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                133 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                73 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                45 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                49 views
                                0 likes
                                Last Post TheRealMorford  
                                Working...
                                X