Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EntriesPerDirection=1 but still executes 2

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

    EntriesPerDirection=1 but still executes 2

    Hi,

    I have a bit of a problem with my strategy and cannot figure out why. Even though I set the strategy rule EntriesPerDirection=1, on LIVE testing sometimes it opens 2 positions and I don't understand why. During the back tests everything seems fine but on live testing every now and then it open 2 positions. Here below is my code, maybe the execution order is wrong, any suggestion to improve the code would be appreciated.

    Code:
    			 if (Close[0] <= sell_price)
    			 	{
    				 if (Position.MarketPosition == MarketPosition.Long)
    			 		{
    					ExitLong(0, 1, "reversing","buy");
    					EnterShort(0, 1, "sell");
    					}	
    				 if (Position.MarketPosition == MarketPosition.Flat)
    					{
    					EnterShort(0, 1, "sell");
    					}
    			 	 }

    #2
    Hello mntemel,

    Thank you for your note.

    If you are long and you call EnterShort, it will sell you 1 unit to flatten you up and another to put you short.

    So calling ExitLong before EnterShort is not necessary and the reason you are getting double orders. ExitLong will sell 1, then EnterShort not having received the fill from the first ExitLong call, will sell you two.

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,

      Last February I posted an order error issue

      Support for the development of custom automated trading strategies using NinjaScript.


      Basically like you suggested when system was flatting the open position every now and then I would have an OCO ID issue during the process and I was suggested to use ExitLong in my code to flatten the position myself and then let the system open a new position. This is why I added the code other wise I get those OCO ID issues from time to time.

      I think the problem that I have right now is, the first piece in my code is flattening and opening the new position and second piece is also opening the second order maybe because of some timing issue.

      Comment


        #4
        Hello mntemel,

        An ExitLong call has to send an order to the broker/exchange and receive notice back of the fill before your market position is updated. This amount of time is greater than the amount of time it takes to make that call and move on to the EnterShort call, which is going to still see you as in a long position it needs to exit first. This is why you are seeing a doubling of the position.

        I would suggest adding the print below which should return Long even after the ExitLong call was made, in those situations you are seeing double the quantity taken.

        Code:
        ExitLong(0, 1, "reversing","buy");
        Print(Position.MarketPosition.ToString());
        EnterShort(0, 1, "sell");
        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        78 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        40 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        63 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        63 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        54 views
        0 likes
        Last Post CarlTrading  
        Working...
        X