Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,221 views
        0 likes
        Last Post xiinteractive  
        Started by andrewtrades, Today, 04:57 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by chbruno, Today, 04:10 PM
        0 responses
        6 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        436 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        9 views
        0 likes
        Last Post FAQtrader  
        Working...
        X