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

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 algospoke, 04-17-2024, 06:40 PM
        3 responses
        26 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by bmartz, 03-12-2024, 06:12 AM
        3 responses
        28 views
        0 likes
        Last Post NinjaTrader_Zachary  
        Started by Aviram Y, Today, 05:29 AM
        2 responses
        9 views
        0 likes
        Last Post Aviram Y  
        Started by gentlebenthebear, Today, 01:30 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by cls71, Today, 04:45 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X