Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

on/off switch

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

    on/off switch

    Hi There,

    I have a question on how to make a switch for entry logic. My code has multiple entry conditions like on below example:

    if (CrossAbove(Close, EMA1, 1))
    if (TEMA1[0] > TEMA1[1])
    if ((HMA1[0] > HMA1[1])
    && (RSI1.Avg[0] < 20)
    && (TEMA1[0] > TEMA1[1])
    && (CCi[0] < 20))
    {
    EnterLong(1, "MyEntry1");
    }


    When i am building a strategy i lock different entry conditions with //

    //if (CrossAbove(Close, EMA1, 1))
    //if (TEMA1[0] > TEMA1[1])
    if ((HMA1[0] > HMA1[1])
    && (RSI1.Avg[0] < 20)
    && (TEMA1[0] > TEMA1[1]))
    //
    && (CCi[0] < 20))
    {
    EnterLong(1, "MyEntry1");
    }


    Could you suggest how can i make a switch that so that i could turn on and off different combinations of entry conditions in Strategy properties on chart. Instead of going to the code and lock it with //.

    Thanks

    #2
    Hello baluga123,

    Thank you for your post.

    The simplest way would be to make a few bools as user inputs. I've put together a little reduced example of how you might separate that logic out and use bools to control which set(s) get executed:

    Code:
             #region Variables
            // Wizard generated variables
            private bool useEMA = false; // Default setting for UseEMA
            private bool useTEMA = false; // Default setting for UseTEMA
            private bool useHMA = false; // Default setting for UseHMA
            // 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;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                if (UseEMA 
                && CrossAbove(Close, EMA1, 1)
                && (RSI1.Avg[0] < 20)
                && (TEMA1[0] > TEMA1[1])
                && (CCi[0] < 20))
                {
                    EnterLong(1, "MyEntry1");
                }
                else if (UseTEMA
                && TEMA1[0] > TEMA1[1]
                && (RSI1.Avg[0] < 20)
                && (TEMA1[0] > TEMA1[1])
                && (CCi[0] < 20))
                {
                    EnterLong(1, "MyEntry1");
                }
                if (UseHMA
                && (HMA1[0] > HMA1[1])
                && (RSI1.Avg[0] < 20)
                && (TEMA1[0] > TEMA1[1])
                && (CCi[0] < 20))
                {
                    EnterLong(1, "MyEntry1");
                }
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public bool UseEMA
            {
                get { return useEMA; }
                set { useEMA = value; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public bool UseTEMA
            {
                get { return useTEMA; }
                set { useTEMA = value; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public bool UseHMA
            {
                get { return useHMA; }
                set { useHMA = value; }
            }
            #endregion
    This way, you can choose which sets will initially be active when you set the strategy on the chart. You could also add logic to change those bools within the code if you like.

    This is only one way you could approach this, but looking at what you're wanting to do, likely one of the simplest.

    Please let us know if we may be of further assistance to you.
    Last edited by NinjaTrader_Kate; 04-03-2020, 12:53 PM. Reason: Updated with NT7 code
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hey Kate,

      Thank you for your answer.

      Sorry i'm kind of new in coding so this might be some clumsy questions.

      If i understand correctly this way i could lock and unlock different predefined sets. If i would have a small number of entry logic possibilities that would be good solution. But i have a lot of them. what i would like to achieve is to have a pool of different entry conditions and then unlock them one by one. And all that i choose to unlock would need to be combined in one to get buy signal. Not each one separately. Like so:


      Let's say i have 10 different conditions

      ---> && CrossAbove(Close, EMA1, 1) ..//1
      && (RSI1.Avg[0] < 20) ..........................//2
      && (TEMA1[0] > TEMA1[1]) .................//3
      && (CCi[0] < 20)) ..................................//4
      && (HMA1[0] > HMA1[1]) .....................//5
      && CrossBelove(Close, EMA1, 1) ........//6
      && (RSI1.Avg[0] > 20) ..........................//7
      ---> && (TEMA1[0] < TEMA1[1]) ..........//8
      && (CCi[0] > 20)) ..................................//9
      && (HMA1[2] > HMA1[3]) .....................//10
      .....

      So if i choose/unlock 2 of them like this UseEMA and UseTEMA with bool i get two separate conditions and two signals to buy.


      if (UseEMA
      && CrossAbove(Close, EMA1, 1) .....//1
      {
      EnterLong(1, "MyEntry1"));
      }

      if (UseTEMA
      && (TEMA1[0] < TEMA1[1]) .............//8
      {
      EnterLong(1, "MyEntry1"));
      }
      .....

      When i choose/unlock them i would like to get combination of them like this to get one entry signal from combination of two conditions.


      if (CrossAbove(Close, EMA1, 1) .....//1
      && (TEMA1[0] < TEMA[0])) ............//8
      {
      EnterLong(1, "MyEntry1")
      }


      Is this achievable with bools? Or i need to do it some other way? I would really appreciate if you could give me some short example on how to do it.

      Thanks for your time
      Last edited by baluga123; 04-04-2020, 11:07 AM.

      Comment


        #4
        Hello baluga123,

        Thank you for your reply.

        Short of commenting the lines out each time as you were previously doing there wouldn't be a good way to do what you're asking other than separate condition sets. Lines cannot be commented out dynamically in C# (the basis for NinjaScript) as by run time the script has already been compiled.

        Please let us know if we may be of further assistance to you.
        ​​​​​
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by baluga123 View Post
          Hey Kate,

          Thank you for your answer.

          Sorry i'm kind of new in coding so this might be some clumsy questions.

          If i understand correctly this way i could lock and unlock different predefined sets. If i would have a small number of entry logic possibilities that would be good solution. But i have a lot of them. what i would like to achieve is to have a pool of different entry conditions and then unlock them one by one. And all that i choose to unlock would need to be combined in one to get buy signal. Not each one separately. Like so:


          Let's say i have 10 different conditions

          ---> && CrossAbove(Close, EMA1, 1) ..//1
          && (RSI1.Avg[0] < 20) ..........................//2
          && (TEMA1[0] > TEMA1[1]) .................//3
          && (CCi[0] < 20)) ..................................//4
          && (HMA1[0] > HMA1[1]) .....................//5
          && CrossBelove(Close, EMA1, 1) ........//6
          && (RSI1.Avg[0] > 20) ..........................//7
          ---> && (TEMA1[0] < TEMA1[1]) ..........//8
          && (CCi[0] > 20)) ..................................//9
          && (HMA1[2] > HMA1[3]) .....................//10
          .....

          So if i choose/unlock 2 of them like this UseEMA and UseTEMA with bool i get two separate conditions and two signals to buy.


          if (UseEMA
          && CrossAbove(Close, EMA1, 1) .....//1
          {
          EnterLong(1, "MyEntry1"));
          }

          if (UseTEMA
          && (TEMA1[0] < TEMA1[1]) .............//8
          {
          EnterLong(1, "MyEntry1"));
          }
          .....

          When i choose/unlock them i would like to get combination of them like this to get one entry signal from combination of two conditions.


          if (CrossAbove(Close, EMA1, 1) .....//1
          && (TEMA1[0] < TEMA[0])) ............//8
          {
          EnterLong(1, "MyEntry1")
          }


          Is this achievable with bools? Or i need to do it some other way? I would really appreciate if you could give me some short example on how to do it.

          Thanks for your time
          You should evaluate your conditions conditionally individually, in a gated fashion, then evaluate the combined condition to make your entry. Here is an example using 4 conditions. You should be able to expand it to any number of conditions, simply by expanding it all using the same code structure and constructs.

          Code:
                  #region Variables
                  private bool useEMA = false; // Default setting for UseEMA
                  private bool useRSI = false; // Default setting for UseRSI
                  private bool useTEMA = false; // Default setting for UseTEMA
                  private bool useCCI = false; // Default setting for Usecci
          
                  private bool CondEMA = false; // Default setting for CondEMA
                  private bool CondRSI = false; // Default setting for CondRSI
                  private bool CondTEMA = false; // Default setting for CondTEMA
                  private bool CondCCI = false; // Default setting for CondCCI
          
                  #endregion
          
                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any bar data is loaded.
                  /// </summary>
                  protected override void Initialize()
                  {
                      CalculateOnBarClose = true;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                      CondEMA = UseEMA ? CrossAbove(Close, EMA1, 1) : true;
                      CondRSI = UseRSI ? (RSI1.Avg[0] < 20) : true;
                      CondTEMA = UseTEMA ? (TEMA1[0] > TEMA1[1]) : true;
                      CondCCI = UseCCI ? (CCI1[0] < 20) : true;
          
                      if (CondEMA
                          && CondRSI    
                          && CondTEMA
                          && CondCCI
                          )
                      {
                          EnterLong(1, "MyEntry1");
                      }
                  }
          
                  #region Properties
                  [Description("")]
                  [GridCategory("Parameters")]
                  public bool UseEMA
                  {
                      get { return useEMA; }
                      set { useEMA = value; }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public bool UseRSI
                  {
                      get { return useRSI; }
                      set { useRSI = value; }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public bool UseTEMA
                  {
                      get { return useTEMA; }
                      set { useTEMA = value; }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public bool UseCCI
                  {
                      get { return useCCI; }
                      set { useCCI = value; }
                  }
                  #endregion
          You left out the definitions of EMA1, RSI1, TEMA1, and CCI1, and, therefore, so did I. That means that the posted code will not compile, but I am sure that you knew that already.

          Comment


            #6
            Koganam THANK YOU!! Exactly what i was looking for. Just changed GridCategory for Display and works like i wanted. Cheers

            Comment


              #7
              Originally posted by baluga123 View Post
              Koganam THANK YOU!! Exactly what i was looking for. Just changed GridCategory for Display and works like i wanted. Cheers
              Don't mention it. I am glad that I could help.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DJ888, 04-16-2024, 06:09 PM
              4 responses
              12 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by terofs, Today, 04:18 PM
              0 responses
              8 views
              0 likes
              Last Post terofs
              by terofs
               
              Started by nandhumca, Today, 03:41 PM
              0 responses
              6 views
              0 likes
              Last Post nandhumca  
              Started by The_Sec, Today, 03:37 PM
              0 responses
              3 views
              0 likes
              Last Post The_Sec
              by The_Sec
               
              Started by GwFutures1988, Today, 02:48 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Working...
              X