Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CCI Strategy

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

    CCI Strategy

    Hi,

    Having trouble figuring out how to make the proper entry using my strategy. Here is the scenario:
    1. CCI value crosses below 20, then later crosses back above 20. (I will just paint this bar blue since it signifies a valid trigger for a future entry)
    2. At the point CCI crosses back above 20, I would like to enter a long position but only on a bar in which price closes higher than the high from 2 bars before CCI crossed above 20.

    No matter what I do, orders are placed when price closes higher than the close from 2 bars back, but NOT from 2 bars back from that point in which CCI crossed back above 20.

    I am using the WiZard for this but if you prefer I can enter the code manually. Just need help with the code to use please.

    Thanks

    #2
    Hello,

    Would you mind sharing what you've already tried? You can do this by by selecting "View Code" in the strategy wizard and posting the relevent CCI Entry condition in the OnBarUpdate() method.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Hi, Here is the code snippet: I have also attached a picture of what I am trying to accomplish.

      /// <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(RSI(RSIPeriod, 1));
      SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);
      SetStopLoss("", CalculationMode.Ticks, StopLoss, false);

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (CrossAbove(RSI(RSIPeriod, 1), Rsilower, 1))
      {
      Variable0 = 1;
      BarColor = Color.Blue;
      }

      // Condition set 2
      if (Variable0 == 1
      && CrossAbove(RSI(5, 1).Avg, RSIPeriod, 1))
      {
      EnterLong(DefaultQuantity, "");
      Variable0 = 0;
      }



      }

      #region Properties
      [Description("")]
      [GridCategory("Parameters")]
      public int ProfitTarget
      {
      get { return profitTarget; }
      set { profitTarget = Math.Max(1, value); }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int StopLoss
      {
      get { return stopLoss; }
      set { stopLoss = Math.Max(1, value); }
      }

      [Description("RSI Period")]
      [GridCategory("Parameters")]
      public int RSIPeriod
      {
      get { return rSIPeriod; }
      set { rSIPeriod = Math.Max(1, value); }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int BarsAgo
      {
      get { return barsAgo; }
      set { barsAgo = Math.Max(1, value); }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int Rsilower
      {
      get { return rsilower; }
      set { rsilower = Math.Max(1, value); }
      }
      #endregion
      Attached Files

      Comment


        #4
        Hello poipoi11,
        You have only checked one condition, i.e. if there is a crossover. You have not checked for the RSI drops below the 20 level. The below code for example checks fr any cross above and also checks if the RSI dips below 20 in the last 10 bars

        Code:
        if (CrossAbove(RSI(14, 3), 20, 1) && MAX(RSI(14,3), 10)[1] > 10)
        {
        	BarColor = Color.Blue;
        }
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Hi thanks for the response. not sure I completely follow. Are you saying that if i add this check for when RSI crosses below that I will then successfully only trade when the close of the bar after rising above RSI of 20 crossover is higher than the high from 2 bars prior to the rise above RSI 20?

          This was my original issue: "No matter what I do, orders are placed when price closes higher than the close from 2 bars back, but NOT from 2 bars back from that point in which CCI crossed back above 20. "

          Comment


            #6
            Hello poipoi11,
            My apologies for the confusion.

            To clarify further are you trying to code the strategy using the CCI or the RSI. You have wrote CCI in the first post however have coded the strategy using RSI.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Hi, very sorry for the confusion. I am using RSI. Originally I did say CCI but I have changed to RSI.

              Comment


                #8
                Hello poipoi11,
                Thanks for the clarification.

                To clarify further, you are trying to enter a position only on a bar in which price closes higher than the high from 2 bars before CCI crossed above 20.

                Thus say RSI crosses above 20 on Bar X, and the high of Bar X - 2 == $100.

                So you want to enter a trade only when price crosses above $100.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Hi, I pasted your question and added comments for clarity.

                  "To clarify further, you are trying to enter a position only on a bar in which price closes higher than the high from 2 bars before RSI crossed above 20.

                  Thus say RSI crosses above 20 (AND CLOSES ABOVE 20) on Bar X, and the high of Bar X - 2 == $100.

                  So you want to enter a trade only when price crosses (CLOSES) above $100.

                  Also, the stop loss is a close below the low of the bar that closed with RSI of > 20."

                  Comment


                    #10
                    Also, please take a look at the photo I attached as I believe that it accurately reflects what I am trying to accomplish.

                    Comment


                      #11
                      Hello poipoi11,
                      Thanks for the clarification.

                      A sample code will be like:
                      Code:
                      //In Variable region
                      double tradePrice;
                      double trade = false;
                      
                      //in OnBarUpdate
                      if (CrossAbove(RSI(RSIPeriod, 1), Rsilower, 1))
                      {
                      	SetStopLoss(CalculationMode.Price, Low[0]);
                      	tradePrice = High[2];
                      	stopPrice = Low[0];
                      	trade = true;
                      }
                      
                      if (Close[0] > tradePrice && trade)
                      {
                      	EnterLong();
                      	
                      	trade = false;
                      }
                      Please note you have to unlock the code and further edit the code using the NinjaScript Editor.
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        Hi, I have pasted what you have as best I know how but it is generating many errors such as "The name RSI Period doesn't exist" etc. I am not a developer and am only used to the wizard so if I have misaligned something can you spot it?

                        {
                        #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()
                        {
                        CalculateOnBarClose = true;
                        double tradePrice;
                        double trade = false;
                        }

                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                        if (CrossAbove(RSI(RSIPeriod, 1), Rsilower, 1))
                        {
                        SetStopLoss(CalculationMode.Price, Low[0]);
                        tradePrice = High[2];
                        stopPrice = Low[0];
                        trade = true;
                        }

                        if (Close[0] > tradePrice)
                        {
                        EnterLong();
                        SetStopLoss(CalculationMode.Price, stopPrice);
                        trade = false;
                        }
                        }

                        Comment


                          #13
                          Hello poipoi11,
                          RSIPeriod etc are the various input parameters as defined in your original code.

                          Please refer to this post which further demonstrates how to create a custom input parameter
                          JoydeepNinjaTrader Customer Service

                          Comment


                            #14
                            That helped thanks. I know that I am close but as I have it it is generating compile errors such as invalid token and "already contains a definition" messages. Do you spot the issue?

                            #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 Test2 : Strategy
                            {
                            #region Variables
                            private double tradePrice;
                            private double trade = false;
                            private int RSIPeriod;
                            private int SetProfitTarget;
                            private int SetStopLoss;
                            #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(RSI(RSIPeriod, 1));


                            CalculateOnBarClose = true;
                            }

                            /// <summary>
                            /// Called on each bar update event (incoming tick)
                            /// </summary>
                            protected override void OnBarUpdate()
                            {
                            // Condition set 1
                            if (CrossAbove(RSI(RSIPeriod, 1), Rsilower, 1))
                            {
                            SetStopLoss(CalculationMode.Price, Low[0]);
                            tradePrice = High[2];
                            stopPrice = Low[0];
                            trade = true;
                            }

                            // Condition set 2
                            if (Close[0] > tradePrice && trade)
                            {
                            EnterLong();

                            trade = false;
                            }



                            }

                            #region Properties
                            [Description("Price at which there will be a trade")]
                            [GridCategory("Parameters")]
                            public double tradePrice
                            {
                            get { double tradePrice; }
                            set { double tradePrice = Math.Max(1, value); }
                            }

                            [Description("Trade false")]
                            [GridCategory("Parameters")]
                            public double trade
                            {
                            get { double trade = false; }
                            set { double trade = false = Math.Max(1, value); }
                            }

                            [Description("RSI period")]
                            [GridCategory("Parameters")]
                            public int RSIPeriod
                            {
                            get { int RSIPeriod; }
                            set { int RSIPeriod = Math.Max(1, value); }
                            }

                            [Description("Profit")]
                            [GridCategory("Parameters")]
                            public int SetProfitTarget
                            {
                            get { int SetProfitTarget; }
                            set { int SetProfitTarget = Math.Max(1, value); }
                            }

                            [Description("Loss")]
                            [GridCategory("Parameters")]
                            public int SetStopLoss
                            {
                            get { int SetStopLoss; }
                            set { int SetStopLoss = Math.Max(1, value); }
                            }

                            #endregion
                            }
                            }

                            Comment


                              #15
                              Hello poipoi11,
                              You have assigned the same name for the property and the associated variable. Additionally the getter and setter are also wrong. For example take the SetProfitTarget property for example. You have appended it as:

                              Code:
                              [COLOR="Red"]private int SetProfitTarget;
                              
                              [Description("Loss")]
                              [GridCategory("Parameters")]
                              public int SetStopLoss
                              {
                              get { int SetStopLoss; }
                              set { int SetStopLoss = Math.Max(1, value); }
                              }[/COLOR]
                              The correct code will be

                              Code:
                              private int [COLOR="Blue"]s[/COLOR]etProfitTarget;    //note C# is case sensative.
                              
                              [Description("Loss")]
                              [GridCategory("Parameters")]
                              public int SetStopLoss
                              {
                              get {[COLOR="blue"]return[/COLOR] [COLOR="blue"]s[/COLOR]etStopLoss; }
                              set { [COLOR="blue"]s[/COLOR]etStopLoss = Math.Max(1, value); }
                              }
                              You have to correct all the properties.
                              JoydeepNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              630 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
                              566 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