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

OCO Orders cancelling at bar close

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

    OCO Orders cancelling at bar close

    Hi NinjaTraders,

    I am using Managed approach for order submission - Submitting EnterLongLimit order on 15 minute dataseries so that order is cancelled if not filled before the close of the bar on which it was submitted. This is working very well - as expected.

    However, along with EnterLongLimit order, strategy is sending Stop Loss and Profit Target OCO orders (Using SetStoploss and SetProfitTarget). The problem lies here - when either of them is partially filled, it gets cancelled sometimes. I suspect it gets cancelled on 15 minute bar close, adopting Entry order behavior. This is obviously not what I am expecting - I want these orders to be active until one or another is fully filled.

    Stoploss and ProfitTarget are calculated in State.Configure. It should not make any difference whether they are calculated there, or in OnBarUpdate section, per what I understood from documentation - and I could not figure out any other way to program the behavior in a way that these orders (OCO Stop/Profit) would stay alive until fully filled (ignoring bar close) AND EnterLongLimit order would still cancel at bar close if not filled.

    Trace Order says "Cancelled Expired Order" for Stop/Profit order - again, this only happens on partial fills.
    I am assuming this also impacts EntriesPerDirection - as soon as partially filled order (stop/profit) is cancelled, program ignores any other orders that should be submitted with message saying that EntriesPerDirection have been exceeded, as there is position still open.

    I hope what I tried to explain here is understandable.
    Any feedback is greatly appreciated.
    Thank you!



    #2
    Hello Paco0,

    The entry is part filled correct?

    Is the remaining portion of the entry cancelled?

    Please print the order object from OnOrderUpdate() and re-run the script.

    Then please save the output from the output window (right-click > Save As) and attach the output text file with your reply.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB,

      Thanks for quick reply.

      Entry was filled in parts yes, however, entry order was fully filled before bar close, so no portion was cancelled.
      I am not able to re-run the script to reproduce the issue as I am testing the strategy real-time with my broker demo account.

      Stoploss order total quantity matches the total entry quantity, however gets cancelled after partial fill.
      Is there a way to resolve this without the print or do I need to update the code with Print and wait for next occurrence?

      Thanks again.

      Comment


        #4
        Hello Paco0,

        Leave that instance of the script running if you want, and add another instance on the Sim101 account.

        A stop loss being cancelled after the entry is part filled would be a bug that needs to be reported.
        I've tested with a test script I have created that places large quantities so I get part fills and has a stop and target set in OnStateChange() when State is State.Configure and I am not able to reproduce this behavior so I am wanting to see evidence of this from you.
        Then possibly I will want your code to test if I am seeing that evidence.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi ChelseaB,

          Unfortunately (for me) I am not familiar with OnOrderUpdate() as I have not used it previously, or with creating another instance on a different account.. It is going to take some time for me to research what exactly I need to do to provide evidence you are asking for.

          Just to make sure we are on the same page, it is stop loss order being cancelled after (stop loss order) partial fill. I do understand it might be a consequence of entry order partial fills but it doesn't make much sense to me as the order sizes match.

          If you could provide more details on how I can get what you need for further analysis - perfect, if not totally understandable.

          Regards,

          Comment


            #6
            Hello Paco0,

            Within the scope of the class add:
            Code:
            protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
            {
             Print(order.ToString();
            }
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi ChelseaB,

              Thank you for helping me with the code.
              I did not get the same example today with cancelled stop loss so far but there has been one which I think might help to understand issue described above.

              Entry order was submitted for 25948 shares, filled 21900 shares before entry order was cancelled on bar close.
              Stop loss and profit target order quantity were being updated with partial fills for entry order until some point, where it stopped updating and quantity remained at 13100 shares. I would expect correct quantity to be 21900 based on entry order fill.
              Attached is the output with Order object print.

              I will continue to keep watching for an example with stop loss cancelled and post it here as soon as I have one.
              Regards,
              Last edited by NinjaTrader_ChelseaB; 11-13-2019, 10:09 AM.

              Comment


                #8
                Hello Paco0,

                At what time did the quantity not update correctly?

                Or what is the order id of the entry?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I think I understand what is going on in this case - There was a partial fill of entry order while it was already in Cancelled Pending status, that is why the stop loss and profit target did not update. If my understanding is correct, then my question would be if this is a standard behavior of NT and if yes, how to avoid it? Do I have to dip into unmanaged approach?

                  Still waiting for an example of cancelled stop loss, no luck so far.
                  Thank you

                  Comment


                    #10
                    Hi ChelseaB,

                    This is what I am talking about:

                    Comment


                      #11
                      I finally have an example of a cancelled stop loss too - please see attached output. Stop loss and profit target are cancelled as expired at the bottom of the file (13. 11. 2019 17:19:57).
                      Filled quantity at the time stop loss order was cancelled was 900 out of total stop loss order quantity which was 6491.

                      Thanks for looking into this.
                      Last edited by Paco0; 11-13-2019, 11:04 AM. Reason: skipped word

                      Comment


                        #12
                        Hello Paco0,

                        Thanks for providing the files. (I removed these from your post when I realized your account number is in these, but I do have them.)

                        In the output I did see that order 1085540024 after the last part fill was filled to 13100.

                        This is what the stop and target were last updated to.

                        Then there is a missing part fill and instead we see the order is cancelled and the total filled amount is 21900.

                        The stop 1085540025 and target 1085540026 are never updated from the last part fill because the last part fill was not processed before the order was cancelled. These remain working with a quantity of 13100.


                        The entry 1085540084 appears to have a position open and there is no exit when stop 1085540087 and target 1085540088 are cancelled. This means there likely wasn't an exit on close event and the position would still be opened.


                        Are you able to reproduce this behavior on the Sim101?

                        May I test your strategy?
                        If so, please export this and attach the export to your reply.

                        To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
                        1. Click Tools -> Export -> NinjaScript...
                        2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
                        3. Click the 'Export' button
                        4. Enter a unique name for the file in the value for 'File name:'
                        5. Choose a save location -> click Save
                        6. Click OK to clear the export location message
                        By default your exported file will be in the following location:
                        • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
                        Below is a link to the help guide on Exporting NinjaScripts.

                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello ChelseaB,

                          While I cannot share the full code, please find attached a reference code created by removing some of the condition logic from original strategy, while keeping order submission mechanism and Stop loss and Profit orders untouched so the order behavior should be the same - please find attached the file.

                          I am not familiar with how to test real-time with Sim101 account but I will definitely test the behavior during market hours next week with my account - I just wanted to post it here in case you would be able to do some testing on Sim101 account outside market hours.

                          Thank you,
                          Attached Files

                          Comment


                            #14
                            Hello Paco0,

                            To confirm, you have reproduced using this script you have provided?

                            To test a strategy on the Sim101 account
                            Right-click a chart -> select Strategies -> select the strategy -> click add
                            On the right set Account to Sim101 -> check the Enabled checkbox -> click OK

                            It looks like the strategy you have provided requires the code to be changed on line 109 to the account name.

                            Are you able to reproduce using the Playback connection outside of market hours?

                            Below is a link to a video on Playback.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi ChelseaB,

                              I am trying to reproduce the scenario with PublicStrategy and I just realized that my Start Behavior until now for the stretagy has always been "Immediately submit". Could this be the cause of order quantities discrepancy?

                              To describe my situation:
                              - Every day before market open I am not holding any open positions in my account
                              - I load approx. 150 Days of historical data in the strategy.

                              I am not an expert on strategy vs. account position but I am assuming that if the strategy position is not in sync with account position (which can be the case with "immediately submit" start behavior) , the strategy position might be assuming that I am holding a position in my account based on historical orders that would have been submitted/filled if the strategy was active at that historical time?

                              As for playback connection, I enabled market replay recording just today, as data for instruments I am trading was not available in standard.

                              Thank you for the support.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              10 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by elirion, Today, 01:36 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Started by gentlebenthebear, Today, 01:30 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post gentlebenthebear  
                              Working...
                              X