Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why are my positions exiting and re-entering?

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

    Why are my positions exiting and re-entering?

    I'm having a problem with my strategy while running backtests. I'm trying to create a position that scales out, and I understand I need to scale in to do this. So I create two different entry signals. The strategy will enter into the position, but before hitting a profit target or a stop I am exiting out of the position. I feel like I'm missing something pretty important and basic here, but I can't figure it out. I'm new to coding and I know this code isn't optimal..

    Code:
            protected override void Initialize()
            {
    			EntriesPerDirection = 1;
    			EntryHandling 		= EntryHandling.UniqueEntries;
    							
    			CalculateOnBarClose = false;
    								
            }
    
            protected override void OnBarUpdate()
            {
                if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Flat)
    			{
    				if (ToTime(Time[0]) >= 92500 && ToTime(Time[0]) <= 154500)
    				{
    				
    					if (CrossAbove(Close, 100, 1))
    					{
    						EnterLong(position1, "Long100a");
    						EnterLong(position2, "Long100b");
    					}
    
    					
    					if (CrossAbove(Close, 40, 1))
    					{
    						EnterLong(position1, "Long40a");
    						EnterLong(position2, "Long40b");
    					}
    
    					
    					if (CrossBelow(Close, 30, 1))
    					{
    						EnterShort(position1, "Short30a");
    						EnterShort(position2, "Short30b");
    					}
    
    					
    					if (CrossBelow(Close, 90, 1))
    					{
    						EnterShort(position1, "Short90a");
    						EnterShort(position2, "Short90b");
    					}
    				}
    				
    			
    			}
    			
    			//Profit Targets (position1)
    			if (Position.MarketPosition == MarketPosition.Long)
    				{
    					if (Close[0] >= 110)
    						{
    						ExitLong("Long100a", "");
    						}
    				
    					if (Close[0] >= 50)
    						{
    						ExitLong("Long40a", "");
    						}		
    				}
    					
    			if (Position.MarketPosition == MarketPosition.Short)
    				{
    					if (Close[0] <= 20)
    						{
    						ExitShort("Short30a", "");
    						}
    				
    					if (Close[0] <= 80)
    						{
    						ExitShort("Short90a", "");
    						}				
    				}
    				
    			//Stop Losses (position1)
    			if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Long)
    				{
    					if (Close[0] < 90)
    						{
    							ExitLong("Long100a", "");
    						}
    					
    					if (Close[0] < 30)
    						{
    							ExitLong("Long40a", "");
    						}
    				}
    			
    			if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Short)
    				{
    					if (Close[0] > 100)
    						{
    							ExitShort("Short90a", "");
    						}
    					
    					if (Close[0] > 40)
    						{
    							ExitShort("Short30a", "");
    						}
    				}
    		
            }
    The strategy runs without error, but does not produce the results I am looking for. I also attached a screenshot to show some of its entries/exits.
    Attached Files

    #2
    Hello IFT20,

    This is because you are checking the values of the "Close" price to a static value which is causing your strategy conditions to be true on the next bar which is "Exiting" out of your order.

    From your Screenshot I see that the price of the instrument is going to be trading around 130 which is greater than 100 and 40 making your last condition true.

    One thing you may want to do as well is change your signal names in your Exit orders. To define an entry signal to exit out you will want to define it as the second string. For example:

    ExitShort(string signalName, string fromEntrySignal)

    JCNinjaTrader Customer Service

    Comment


      #3
      Ah, the syntax for ExitShort was messed up. I fixed it and seems like its working as intended. Thank you JC!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      62 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X