• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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
    	                if (Bars.BarsSinceSession >=50)
    			if (Bars.BarsSinceSession <=84)
                            if (Close[0] >= EMA(20)[0]
                                && Close[0] >= EMA(50)[0])	
                {
              	    //  enterLongStrat2 = true;
             	  	enterLongStrat1 = true;
                }
    		
    			//  Condition set 2
    			if (Bars.BarsSinceSession == 1)
    			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.
    Patrick H.NinjaTrader Customer Service

    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
      	     if (Bars.BarsSinceSession == 1)
                  if (Close[0] >= Open[0] + 4 * TickSize)
                     {
               	 enterLongStrat1 = true;
      		}
      		
      		//  Condition set 2
      		if (Bars.BarsSinceSession == 7)
      		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
      	     if (Bars.BarsSinceSession == 1)
                  if (Close[0] >= Open[0] + 4 * TickSize)
                     {
               	 enterLongStrat1 = true;
                       enterLongStrat2 = false;
      		}
      		
      		//  Condition set 2
      		if (Bars.BarsSinceSession == 7)
      		if (Close[0] >= Open[0] + 4 * TickSize)
      		 {
      			enterLongStrat2 = true;
                              enterLongStrat1 = false;
      		}
      
                     // 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.
        Patrick H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by 7robert, Today, 08:11 AM
        3 responses
        47 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by madmen, Yesterday, 09:18 PM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by stoner, Today, 09:01 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by Conall, Yesterday, 04:00 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by neoikon, Today, 08:43 AM
        2 responses
        12 views
        0 likes
        Last Post neoikon
        by neoikon
         
        Working...
        X