Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

N Bar Up/Down

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

    N Bar Up/Down

    Hello,

    I'm new to strategy building. I've got a strategy built with an issue that i'm stuck on...

    Using N Bar Up/Down, I want to enter a position in the opposite direction once a certain number of consecutive bars are reached. (Example: buy after the 4th consecutive bar). The indicator triggers no problem with a 1 value that I can include in the strategy, but the problem is that it buys the 4th, 5th ,6th, 7th, etc consecutive bars also. So if im wrong and it goes past 4 to 8 consecutive bars, im wrong 4 times. How do I get the strategy to only buy the first bar after the N Bar (4) is triggered?? Screen shot attached.

    Any help is appreciated!

    JB
    Attached Files

    #2
    Hello justinbourque81,
    Thanks for your post.

    You could implement a counter or a switch to essentially turn on/off your conditions for entry. Something similar to the following snippet would count 4 'signal bars', place an entry on the following bar, and then reset the counter to zero.

    Code:
    private int counter = 0;
    protected override void OnBarUpdate()
    {
    	if (counter == 4)
    	{
    		//order entry here
    		counter = 0;
    	}
    	
    	if (counter < 4 && [COLOR="Green"]//your signal condition here//[/COLOR])
    	{
    		counter++;		
    	}
    
    }
    Please let me know if you have any questions.
    Last edited by NinjaTrader_JoshG; 04-02-2018, 09:00 AM.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Thanks for getting back to me. I am really new to this as I mentioned. Im sure this is painful for you to explain such easy stuff to a rookie haha. So what you provided for me is intended to be an addition to the N up/down bar one I have already built or the counter will replace the N up/down bar indicator i am using?? Thanks, JB

      Comment


        #4
        Hello justinbourque81,

        I see now that my sample was not as clear as it could have been. For most indicators my previous sample would be the correct way to accomplish this. I edited the original snippet to be more clear where your signal would go. After testing a little bit more; for the n Bars up/down indicators specifically it would be best to use the "switch" method though.
        This is because the nBars up/down indicator is already doing the counting for us.

        Code:
        private bool mySwitch = true;
        protected override void OnBarUpdate()
        {
        	double value = NBarsUp(4,true,true,true)[0];
        	if (value==0 && mySwitch == false) 
        	{
        		mySwitch = true;
        	}
        	
        	if (value==1 && mySwitch == true)
        	{
        		mySwitch = false;
        		Value[0] = value;
        	}
        }
        This snippet would only allow the first occurrence of NBarsUp after four consecutive up bars.

        Please let me know if you have any questions.
        Josh G.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Mindset, 04-21-2026, 06:46 AM
        0 responses
        93 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        138 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        68 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        123 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        73 views
        0 likes
        Last Post PaulMohn  
        Working...
        X