Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsSinceEntry (time-based stop) not working with Position.MarketPosition

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

    BarsSinceEntry (time-based stop) not working with Position.MarketPosition

    I have a BarsSinceEntry condition nested in a Position.MarketPosition condition that does not seem to be working. In particular, I am trying to close the position after 10 bars regardless of the value of the indicator. The exact code goes:

    Code:
    				switch (Position.MarketPosition)
    				{
    					case MarketPosition.Flat:
    						if (15 < indicator && indicator < 40)
    						{
    							EnterShort(1, 1, "sell");
    						}
    						
    						if (-40 < indicator && indicator < -15)
    						{
    							EnterLong(1, 1, "buy");		
    						}
    						break;
    
    					case MarketPosition.Long:
    						if (indicator >= -7.5 ||
    							indicator <= -40 ||
    							BarsSinceEntry(1,"",0) > 10)
    						{
    							ExitLong(1, 1, "", "buy");
    						}
    						break;
    
    					case MarketPosition.Short:
    						if (indicator <= 7.5 ||
    							indicator >= 40 ||
    							BarsSinceEntry(1,"",0) > 10)
    						{
    							ExitShort(1, 1, "", "sell");
    						}
    						break;
    				}
    The positions close correctly when the indicator reach the +/-7.5 and +/-40 values, but the BarsSinceEntry(1,"",0) > 10) condition does not work inside the switch. But if I take out the BarsSinceEntry(1,"",0) > 10) condition and put it outside the switch like this, it works:

    Code:
    				switch (Position.MarketPosition)
    				{
    					case MarketPosition.Flat:
    						if (15 < indicator && indicator < 40)
    						{
    							EnterShort(1, 1, "sell");
    						}
    						
    						if (-40 < indicator && indicator < -15)
    						{
    							EnterLong(1, 1, "buy");		
    						}
    						break;
    
    					case MarketPosition.Long:
    						if (indicator >= -7.5 ||
    							indicator <= -40 )
    						{
    							ExitLong(1, 1, "", "buy");
    						}
    						break;
    
    					case MarketPosition.Short:
    						if (indicator <= 7.5 ||
    							indicator >= 40)
    						{
    							ExitShort(1, 1, "", "sell");
    						}
    						break;
    				}
    				if (BarsSinceEntry(1,"",0) > 10)
    				{
    					ExitLong(1, 1, "", "buy");
    					ExitShort(1, 1, "", "sell");
    				}
    What's wrong with my first piece of code? Thanks!

    #2
    Hello llstelle,
    Thanks for your note.

    To assist you further may I know is the strategy a multi-series strategy?

    I look forward to assisting you further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Yes, it is a multi-series strategy and I am placing the entries on the first added bar.

      Looking forward to your answer and I hope you've had a great Thanksgiving! (:

      Comment


        #4
        Hello,


        Because you are getting the desired results when placing BarsSinceEntry outside of your nested case you would want to use Print() statements to verify values are what you expect - Debugging your NinjaScript code.


        For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - TraceOrders


        It may also help to add drawing objects to your chart for signal and condition confirmation - Drawing Objects.


        Additionally, please keep in mind when working with a multi-series strategy the BarsSinceEntry() will return you the elapsed bars as determined by the first Bars object for the instrument specified by the barsInProgressIndex.


        Please let me know if I can be of further assistance.
        LanceNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        563 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        329 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        547 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        548 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X