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 TraderBCL, Today, 04:38 AM
      2 responses
      16 views
      0 likes
      Last Post TraderBCL  
      Started by martin70, 03-24-2023, 04:58 AM
      14 responses
      106 views
      0 likes
      Last Post martin70  
      Started by Radano, 06-10-2021, 01:40 AM
      19 responses
      609 views
      0 likes
      Last Post Radano
      by Radano
       
      Started by KenneGaray, Today, 03:48 AM
      0 responses
      5 views
      0 likes
      Last Post KenneGaray  
      Started by thanajo, 05-04-2021, 02:11 AM
      4 responses
      471 views
      0 likes
      Last Post tradingnasdaqprueba  
      Working...
      X