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

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 judysamnt7, 03-13-2023, 09:11 AM
        4 responses
        59 views
        0 likes
        Last Post DynamicTest  
        Started by ScottWalsh, Today, 06:52 PM
        4 responses
        36 views
        0 likes
        Last Post ScottWalsh  
        Started by olisav57, Today, 07:39 PM
        0 responses
        7 views
        0 likes
        Last Post olisav57  
        Started by trilliantrader, Today, 03:01 PM
        2 responses
        21 views
        0 likes
        Last Post helpwanted  
        Started by cre8able, Today, 07:24 PM
        0 responses
        10 views
        0 likes
        Last Post cre8able  
        Working...
        X