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

ExitLong()/ExitShort() fires order twice

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

    ExitLong()/ExitShort() fires order twice

    Hello,

    I have a strategy that when meeting a specific condition it will call ExitLong()/ExitShort() to close the current position, and then open a new reverse position by calling EnterShort()/EnterLong().

    but it seems that the ExitLong() or ExitShort() tries to fire 2 orders to close the current position. Adding to the new position which I intend to open, it results in more quantity traded than I want.

    For example, currently I have a long position called "Long0", after meeting the required condition, ExitLong("ExitLong0","Long0") is called in my script to close the current position, and I intentionally want to take a reverse position "Short0". However, NT fires 2 orders to close the position. Adding to my EnterShort("Short0") call, it fired 3 orders in total.

    In Log, total 3 orders were fired (lower the newer in timeline) :-

    11/1/2019 0:05:00,Order,Order='632966e1a4544ec9b05e854aff7fd 7a0/Sim101' Name='ExitLong0' New state=PendingSubmit Instrument='BABA' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',

    11/1/2019 0:05:00,Order,Order='a960840f15cd4c39992d648f41631 608/Sim101' Name='Close position' New state=PendingSubmit Instrument='BABA' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',

    11/1/2019 0:05:00,Order,Order='781de5123a054e319f4cd4abe17ef 790/Sim101' Name='Short0' New state=PendingSubmit Instrument='BABA' Action=SellShort Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',


    I also added TraceOrders = true to my script, and the output shows that only 2 PlaceOrder() is called.

    11/1/2019 0:05:00 Entered internal PlaceOrder() method at 11/1/2019 0:05:00: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='ExitLong0' FromEntrySignal='Long0'

    11/1/2019 0:05:00 Entered internal PlaceOrder() method at 11/1/2019 0:05:00: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Short0' FromEntrySignal=''

    So my conclusion is that ExitLong() fired two sell orders, namely 'ExitLong0' and 'Close position' to close my current 'Long0' position.

    I have no idea why it happens. I will be very grateful if anyone can shed some light on the problem!


    Thank you very much!

    Cheers,
    Hiromichi

    #2
    By the way, it functions normally when I backtest using the script in Strategy Analyzer. The problem only occurs when I run the strategy using Sim101 using realtime data.

    Comment


      #3
      Hello Hiromichi,

      Thanks for your post.

      A trainee would like to use this inquiry as a training opportunity.

      We appreciate your patience and we will be with you shortly.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hello Hiromichi,

        Thank you for your post.

        When using the managed approach, NinjaTrader will automatically flip the position from long to short or vice versa.

        Calling ExitLong() and EnterShort() on the the same bar is guaranteed to cause undesired behavior.

        When calling an entry method in the opposite direction of your position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.

        If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not yet know that your position is closed. This will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

        The result is that either the script will double the quantity when it reverses or it will cause an overfill and stop the script.

        In your case:
        11/1/2019 0:05:00,Order,Order='632966e1a4544ec9b05e854aff7fd 7a0/Sim101' Name='ExitLong0' New state=PendingSubmit Instrument='BABA' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='', - This is the exit order submitted by your ExitLong() method

        11/1/2019 0:05:00,Order,Order='a960840f15cd4c39992d648f41631 608/Sim101' Name='Close position' New state=PendingSubmit Instrument='BABA' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='', - This is the automatic order NinjaTrader places to close your existing position

        11/1/2019 0:05:00,Order,Order='781de5123a054e319f4cd4abe17ef 790/Sim101' Name='Short0' New state=PendingSubmit Instrument='BABA' Action=SellShort Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='', - This is the entry to the opposite position placed with your EnterShort() method

        If you remove the exit method, you should see the behavior you expect.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Dear Kate,

          Thanks for your information on the mechanism behind!
          It works as I expect now by removing the exit method.

          Phil

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Brevo, Today, 01:45 AM
          0 responses
          3 views
          0 likes
          Last Post Brevo
          by Brevo
           
          Started by aussugardefender, Today, 01:07 AM
          0 responses
          3 views
          0 likes
          Last Post aussugardefender  
          Started by pvincent, 06-23-2022, 12:53 PM
          14 responses
          239 views
          0 likes
          Last Post Nyman
          by Nyman
           
          Started by TraderG23, 12-08-2023, 07:56 AM
          9 responses
          384 views
          1 like
          Last Post Gavini
          by Gavini
           
          Started by oviejo, Today, 12:28 AM
          0 responses
          6 views
          0 likes
          Last Post oviejo
          by oviejo
           
          Working...
          X