Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using a for loop instead of straight command

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

    Using a for loop instead of straight command

    I am testing an exit strategy where I exit a long position after x consecutive down bars or a short position after x consecutive up bars. Here is the code I am using:

    Code:
    			// Close Position if order was filled on a retrace and retrace becomes a new trend.
    				if(Position.MarketPosition != MarketPosition.Flat) // && Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) < 0)
    				{
    					if(Position.MarketPosition == MarketPosition.Long && Close[0] < Close[1] && Close[1] < Close[2] && Close[2] < Close[3])
    					{
    					ExitLong("Bars_X", "");	
    					}
    					
    					if(Position.MarketPosition == MarketPosition.Short && Close[0] > Close[1] )
    					{
    					ExitShort("Bars_X", "");
    					}
    				}
    Clearly this particular code segment exits a position after three down or up bars. I would like to be able to more easily test the effect of exiting after a varying number of bars. Do you have some code example that would show me how to use a looping command to do this so when back-testing all I would have to do is vary an integer to count the number of loops to execute the test?
    Thanks
    DaveN

    #2
    Hello DaveN,

    You may use NBarsDown() or NBarsUp().
    JCNinjaTrader Customer Service

    Comment


      #3
      Worked great, thanks so much for your help. Have a great weekend.
      Daven

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      77 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      45 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      27 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      32 views
      0 likes
      Last Post TheRealMorford  
      Started by Mindset, 02-28-2026, 06:16 AM
      0 responses
      63 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X