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 Skifree, Today, 03:41 AM
      3 responses
      12 views
      0 likes
      Last Post Skifree
      by Skifree
       
      Started by traderqz, Yesterday, 09:06 AM
      5 responses
      32 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by guillembm, Today, 11:25 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by owensd, 04-21-2024, 11:34 PM
      9 responses
      34 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by trilliantrader, 04-10-2024, 09:33 PM
      7 responses
      25 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X