Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error in strategy - incorrect entries and exits on next bar

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

    Error in strategy - incorrect entries and exits on next bar

    Hello everyone,

    - Hoping someone can help me figure out why my "multiple bollinger fade" strategy is not entering or exiting properly...

    Positions should be entered once as price closes above each bollinger band (i.e. max 3 contracts long or short at any time)

    The positions (1, 2 or 3 contracts, either all long or all short) should then hold until one of three events takes place:

    1. price closes above/below the mid line of 1st bollinger band (close 1, 2 or 3 contracts accordingly)
    2. stop loss in $$ is hit (close 1, 2 or 3 contracts accordingly)
    3. profit target in $$ is hit (close 1, 2 or 3 contracts accordingly)

    It's probably something simple but I can't seem to figure it out.

    Complete code below:


    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 MultiFadeAtEachBollingerZiggy2 : Strategy
        {
            #region Variables
     
            private int boLength1a = 100; // Default length for first bolinger band 
            private int boLength2a = 100; // Default length for second bollinger band
            private int boLength3a = 100; // Default length for third bollinger band
            private double boDev1a = 1.500; // Default Std Dev for first bolinger band
            private double boDev2a = 2.000; // Default Std Dev for second bolinger band
            private double boDev3a = 1.000; // Default Std Dev for third bolinger band
            private int trgtInDollars = 2500; // Default setting for profit target in $$
            private int stpInDollars = 5000; // Default setting for stop loss in $$
     
            #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(Bollinger(BoDev1a, BoLength1a));
                Add(Bollinger(BoDev2a, BoLength2a));
                Add(Bollinger(BoDev3a, BoLength3a));
                Add(Bollinger(BoDev1a, BoLength1a));
                Add(Bollinger(BoDev2a, BoLength2a));
                Add(Bollinger(BoDev3a, BoLength3a));
                Add(Bollinger(BoDev1a, BoLength1a));
                Add(Bollinger(BoDev1a, BoLength1a));
                SetProfitTarget(TrgtInDollars);
                SetStopLoss(StpInDollars, false);
                CalculateOnBarClose = true;
            }
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (Close[0] > Bollinger(BoDev1a, BoLength1a).Upper[0]
                    && Variable0 == 0)
                {
                    EnterShort(DefaultQuantity, "Short1");
                    Variable0 = 1;
                }
                // Condition set 2
                if (Close[0] > Bollinger(BoDev2a, BoLength2a).Upper[0]
                    && Variable0 == 1)
                {
                    EnterShort(DefaultQuantity, "Short2");
                    Variable0 = 2;
                }
                // Condition set 3
                if (Close[0] > Bollinger(BoDev3a, BoLength3a).Upper[0]
                    && Variable0 == 2)
                {
                    EnterShort(DefaultQuantity, "Short3");
                    Variable0 = 3;
                }
                // Condition set 4
                if (Close[0] < Bollinger(BoDev1a, BoLength1a).Lower[0]
                    && Variable0 == 0)
                {
                    EnterLong(DefaultQuantity, "Long1");
                    Variable0 = 1;
                }
                // Condition set 5
                if (Close[0] < Bollinger(BoDev2a, BoLength2a).Lower[0]
                    && Variable0 == 1)
                {
                    EnterLong(DefaultQuantity, "Long2");
                    Variable0 = 2;
                }
                // Condition set 6
                if (Close[0] < Bollinger(BoDev3a, BoLength3a).Lower[0]
                    && Variable0 == 2)
                {
                    EnterLong(DefaultQuantity, "Long3");
                    Variable0 = 3;
                }
                // Condition set 7
                if (Close[0] < Bollinger(BoDev1a, BoLength1a).Middle[0])
             //       && Position.MarketPosition == MarketPosition.Long)
                {
                    ExitLong("", "Long1");
                    ExitLong("", "Long2");
                    ExitLong("", "Long3");
                    Variable0 = 0;
                }
                // Condition set 8
                if (Close[0] > Bollinger(BoDev1a, BoLength1a).Middle[0])
             //       && Position.MarketPosition == MarketPosition.Short)
                {
                    ExitShort("", "Short1");
                    ExitShort("", "Short2");
                    ExitShort("", "short3");
                    Variable0 = 0;
                }
            }
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int BoLength1a
            {
                get { return boLength1a; }
                set { boLength1a = Math.Max(1, value); }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public int BoLength2a
            {
                get { return boLength2a; }
                set { boLength2a = Math.Max(1, value); }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public int BoLength3a
            {
                get { return boLength3a; }
                set { boLength3a = Math.Max(1, value); }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public double BoDev1a
            {
                get { return boDev1a; }
                set { boDev1a = Math.Max(0.00006, value); }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public double BoDev2a
            {
                get { return boDev2a; }
                set { boDev2a = Math.Max(0.00006, value); }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public double BoDev3a
            {
                get { return boDev3a; }
                set { boDev3a = Math.Max(0.00006, value); }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public int TrgtInDollars
            {
                get { return trgtInDollars; }
                set { trgtInDollars = Math.Max(1, value); }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public int StpInDollars
            {
                get { return stpInDollars; }
                set { stpInDollars = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Last edited by Zigman; 03-30-2012, 09:35 AM.

    #2
    Hello Zigman,
    Thanks for your post and I am happy to assist you.

    Since you have used the variables as a conditions too, your strategy will depend on the Bollinger order too. Again at the end of the code Variable0 will be reset-ed to zero if the conditions met. You need to filter it further with MarketPosition (which you have commented out).

    Can you tell me what result you are getting and what result you expect.

    I look forward to assist you further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Still trying

      Hi Joydeep,

      I tried to modify the code w the martket position commands (see below) but I guess I'm not sure wht you mean exactly. (i.e. still exiting on the next bar every time)
      >????

      PLEASE SEE ATTACHED SCREEN CAPTURE OF WHAT I'M TRYING TO ACCOMPLISH - HELPS TO CLARIFY A LOT.


      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
       
       
       
      // posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
      // posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
      // posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
      // posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
      // posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
       
       
       
      // 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 MultiFadeAtEachBollingerZiggy3 : Strategy
      {
      #region Variables
       
      private int boLength1a = 100; // Default length for first bolinger band 
      private int boLength2a = 100; // Default length for second bollinger band
      private int boLength3a = 100; // Default length for third bollinger band
      private double boDev1a = 1.500; // Default Std Dev for first bolinger band
      private double boDev2a = 2.000; // Default Std Dev for second bolinger band
      private double boDev3a = 1.000; // Default Std Dev for third bolinger band
      private int trgtInDollars = 2500; // Default setting for profit target in $$
      private int stpInDollars = 5000; // Default setting for stop loss in $$
       
      #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(Bollinger(BoDev1a, BoLength1a));
      Add(Bollinger(BoDev2a, BoLength2a));
      Add(Bollinger(BoDev3a, BoLength3a));
      Add(Bollinger(BoDev1a, BoLength1a));
      Add(Bollinger(BoDev2a, BoLength2a));
      Add(Bollinger(BoDev3a, BoLength3a));
      Add(Bollinger(BoDev1a, BoLength1a));
      Add(Bollinger(BoDev1a, BoLength1a));
      SetProfitTarget(TrgtInDollars);
      SetStopLoss(StpInDollars, false);
      CalculateOnBarClose = true;
      }
      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (Close[0] > Bollinger(BoDev1a, BoLength1a).Upper[0]
      && Variable0 == 0 && Position.MarketPosition == MarketPosition.Flat)
      {
      EnterShort(DefaultQuantity, "Short1");
      Variable0 = 1;
      // Position.MarketPosition == MarketPosition.Short;
      }
      // Condition set 2
      if (Close[0] > Bollinger(BoDev2a, BoLength2a).Upper[0]
      && Variable0 == 1 && Position.MarketPosition == MarketPosition.Short)
      {
      EnterShort(DefaultQuantity, "Short2");
      Variable0 = 2;
      // Position.MarketPosition == MarketPosition.Short;
      }
      // Condition set 3
      if (Close[0] > Bollinger(BoDev3a, BoLength3a).Upper[0]
      && Variable0 == 2 && Position.MarketPosition == MarketPosition.Short)
      {
      EnterShort(DefaultQuantity, "Short3");
      Variable0 = 3;
      // Position.MarketPosition == MarketPosition.Short;
      }
      // Condition set 4
      if (Close[0] < Bollinger(BoDev1a, BoLength1a).Lower[0]
      && Variable0 == 0 && Position.MarketPosition == MarketPosition.Flat)
      {
      EnterLong(DefaultQuantity, "Long1");
      Variable0 = 1;
      // Position.MarketPosition == MarketPosition.Long;
      }
      // Condition set 5
      if (Close[0] < Bollinger(BoDev2a, BoLength2a).Lower[0]
      && Variable0 == 1 && Position.MarketPosition == MarketPosition.Long)
      {
      EnterLong(DefaultQuantity, "Long2");
      Variable0 = 2;
      // Position.MarketPosition == MarketPosition.Long;
      }
      // Condition set 6
      if (Close[0] < Bollinger(BoDev3a, BoLength3a).Lower[0]
      && Variable0 == 2 && Position.MarketPosition == MarketPosition.Long)
      {
      EnterLong(DefaultQuantity, "Long3");
      Variable0 = 3;
      // Position.MarketPosition == MarketPosition.Long;
      }
      // Condition set 7
      if (Close[0] < Bollinger(BoDev1a, BoLength1a).Middle[0]
      && Position.MarketPosition == MarketPosition.Long)
      {
      ExitLong("", "Long1");
      ExitLong("", "Long2");
      ExitLong("", "Long3");
      Variable0 = 0;
      // Position.MarketPosition == MarketPosition.Flat;
      }
      // Condition set 8
      if (Close[0] > Bollinger(BoDev1a, BoLength1a).Middle[0]
      && Position.MarketPosition == MarketPosition.Long)
      {
      ExitShort("", "Short1");
      ExitShort("", "Short2");
      ExitShort("", "short3");
      Variable0 = 0;
      // Position.MarketPosition == MarketPosition.Flat;
      }
      }
      #region Properties
      [Description("")]
      [GridCategory("Parameters")]
      public int BoLength1a
      {
      get { return boLength1a; }
      set { boLength1a = Math.Max(1, value); }
      }
      [Description("")]
      [GridCategory("Parameters")]
      public int BoLength2a
      {
      get { return boLength2a; }
      set { boLength2a = Math.Max(1, value); }
      }
      [Description("")]
      [GridCategory("Parameters")]
      public int BoLength3a
      {
      get { return boLength3a; }
      set { boLength3a = Math.Max(1, value); }
      }
      [Description("")]
      [GridCategory("Parameters")]
      public double BoDev1a
      {
      get { return boDev1a; }
      set { boDev1a = Math.Max(0.00006, value); }
      }
      [Description("")]
      [GridCategory("Parameters")]
      public double BoDev2a
      {
      get { return boDev2a; }
      set { boDev2a = Math.Max(0.00006, value); }
      }
      [Description("")]
      [GridCategory("Parameters")]
      public double BoDev3a
      {
      get { return boDev3a; }
      set { boDev3a = Math.Max(0.00006, value); }
      }
      [Description("")]
      [GridCategory("Parameters")]
      public int TrgtInDollars
      {
      get { return trgtInDollars; }
      set { trgtInDollars = Math.Max(1, value); }
      }
      [Description("")]
      [GridCategory("Parameters")]
      public int StpInDollars
      {
      get { return stpInDollars; }
      set { stpInDollars = Math.Max(1, value); }
      }
      #endregion
      }
      }
      Attached Files
      Last edited by Zigman; 03-30-2012, 12:33 PM. Reason: more info

      Comment


        #4
        Hello Zigman,
        In condition 3, say price crosses above the lowest upper band (BoDev3a = 1). Buy you wont have any order triggered since Variable0 is not equal to 2.

        Unless price crosses over the middle and the highest upper band variable0 wont be assigned to 2. You got to rectify it. Unless you set the Bollinger order appropriately you wont get the code to work as per your desire.

        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Thanks!! - I figured it was something simple

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          646 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          367 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          107 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          569 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          573 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X