Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

2 Conditions with different Bars.BarsSinceSession

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

    2 Conditions with different Bars.BarsSinceSession

    Hey Ninjatrader team

    How can I tell ninjatrader to allow two different conditions with different Bars.BarsSinceSession rules. to add more info I want to be able to create two booleans and have different Bars.BarsSinceSession inside the code. (for strategy)

    This is what I have right now. please explain how/why this does not work and how to fix it.

    Code:
     #region Variables
            // Wizard generated variables
            private int myInput0 = 1; // Default setting for MyInput0
    		private bool enterLongStrat1 = false;
    		private bool enterLongStrat2 = false;
            // 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()
            {
                SetProfitTarget("", CalculationMode.Ticks, 4);
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			enterLongStrat1 = false;
    			enterLongStrat2 = false;
    		
                // Condition set 1
    	               [B][COLOR="Blue"] if (Bars.BarsSinceSession >=50)
    			if (Bars.BarsSinceSession <=84)[/COLOR][/B]
                            if (Close[0] >= EMA(20)[0]
                                && Close[0] >= EMA(50)[0])	
                {
              	    //  enterLongStrat2 = true;
             	  	enterLongStrat1 = true;
                }
    		
    			//  Condition set 2
    			[B][COLOR="Blue"]if (Bars.BarsSinceSession == 1)[/COLOR][/B]
    			if (Close[0] >= Open[0] + 4 * TickSize)
    			 {
    				enterLongStrat2 = true;
           		             // enterLongStrat1 = false;
    			}
    
    			if (enterLongStrat2 == true && enterLongStrat1 == true)
    			{    
      				EnterLong(DefaultQuantity, "");
    			}	
            }

    #2
    Hello wallsteetking,

    Thank you for your note.

    I believe you are trying to check a condition on the first bar of the session and then a second condition in-between a specified number of bars. If this is correct, the reason it does not work is the that the two bools are reset to false on each bar update:
    Code:
            protected override void OnBarUpdate()
            {
    			enterLongStrat1 = false;
    			enterLongStrat2 = false;
    Please comment out these two lines and advise if this resolves the matter.

    Comment


      #3
      Patrick, you are correct I am trying to check a condition on the first bar of the session and then a second condition in-between a specified number of bars. However, removing
      Code:
        protected override void OnBarUpdate()
              {
      			enterLongStrat1 = false;
      			enterLongStrat2 = false;
      Does not solve the problem. Here is proof. I changed the code just little bit where on, bar 1 close needs to be bigger then open by 4 ticks and on bar 7 the close needs to be also bigger than the open by 4 ticks. ( SEE BLUE CODE)
      Code:
       #region Variables
              // Wizard generated variables
              private int myInput0 = 1; // Default setting for MyInput0
      		private bool enterLongStrat1 = false;
      		private bool enterLongStrat2 = false;
              // 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()
              {
                  SetProfitTarget("", CalculationMode.Ticks, 4);
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Condition set 1
      	   [B][COLOR="Blue"]  if (Bars.BarsSinceSession == 1)[/COLOR][/B]
                  if (Close[0] >= Open[0] + 4 * TickSize)
                     {
               	 enterLongStrat1 = true;
      		}
      		
      		//  Condition set 2
      		[B][COLOR="blue"]if (Bars.BarsSinceSession == 7)[/COLOR][/B]
      		if (Close[0] >= Open[0] + 4 * TickSize)
      		 {
      			enterLongStrat2 = true;
      		}
      
                     // tally up 
      if (Bars.BarsSinceSession == 10 && enterLongStrat1 == true && enterLongStrat2 == true)
      			{    
        				EnterLong(DefaultQuantity, "");
      			}	
              }
      however, it just takes a long on the 10th bar Always
      I also tried,

      Code:
      #region Variables
              // Wizard generated variables
              private int myInput0 = 1; // Default setting for MyInput0
      		private bool enterLongStrat1 = false;
      		private bool enterLongStrat2 = false;
              // 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()
              {
                  SetProfitTarget("", CalculationMode.Ticks, 4);
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Condition set 1
      	   [B][COLOR="Blue"]  if (Bars.BarsSinceSession == 1)[/COLOR][/B]
                  if (Close[0] >= Open[0] + 4 * TickSize)
                     {
               	 enterLongStrat1 = true;
                      [B][COLOR="red"] enterLongStrat2 = false;[/COLOR][/B]
      		}
      		
      		//  Condition set 2
      		[B][COLOR="blue"]if (Bars.BarsSinceSession == 7)[/COLOR][/B]
      		if (Close[0] >= Open[0] + 4 * TickSize)
      		 {
      			enterLongStrat2 = true;
                              [B][COLOR="Red"]enterLongStrat1 = false;[/COLOR][/B]
      		}
      
                     // tally up 
      if (Bars.BarsSinceSession == 10 && enterLongStrat1 == true && enterLongStrat2 == true)
      			{    
        				EnterLong(DefaultQuantity, "");
      			}
      Attached Files

      Comment


        #4
        Hello wallsteetking,

        Thank you for your response.

        Are you looking for the session's open or the current bar's open? Open[0] would be the current bar's open.

        Setting the parameters to false will not resolve this matter. Can you provide a screenshot of your chart where the entries are made that should not be with your first example?

        I look forward to your response.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NRITV, Today, 01:15 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by maybeimnotrader, Yesterday, 05:46 PM
        5 responses
        24 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by quantismo, Yesterday, 05:13 PM
        2 responses
        16 views
        0 likes
        Last Post quantismo  
        Started by frankthearm, Today, 09:08 AM
        7 responses
        27 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by adeelshahzad, Today, 03:54 AM
        5 responses
        33 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X