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 CarlTrading, Yesterday, 11:51 AM
      0 responses
      20 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, Yesterday, 11:48 AM
      0 responses
      23 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-25-2026, 09:53 PM
      0 responses
      30 views
      0 likes
      Last Post CaptainJack  
      Started by CaptainJack, 03-25-2026, 09:51 PM
      0 responses
      18 views
      0 likes
      Last Post CaptainJack  
      Started by Mindset, 03-23-2026, 11:13 AM
      0 responses
      24 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X