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 Brevo, Today, 01:45 AM
      0 responses
      3 views
      0 likes
      Last Post Brevo
      by Brevo
       
      Started by aussugardefender, Today, 01:07 AM
      0 responses
      3 views
      0 likes
      Last Post aussugardefender  
      Started by pvincent, 06-23-2022, 12:53 PM
      14 responses
      238 views
      0 likes
      Last Post Nyman
      by Nyman
       
      Started by TraderG23, 12-08-2023, 07:56 AM
      9 responses
      384 views
      1 like
      Last Post Gavini
      by Gavini
       
      Started by oviejo, Today, 12:28 AM
      0 responses
      6 views
      0 likes
      Last Post oviejo
      by oviejo
       
      Working...
      X