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

Issues With Multi Contract Order Fills

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

    Issues With Multi Contract Order Fills

    Hello staff,

    I have observed that when trading on a Live account, and when filling a submitted Stop-Market order of 2 contracts, I am getting two different order ID's and two different entry prices.
    This results in the OnExecution() update happening twice while when trading the exact same strategy on a Demo account it happens only once. The problem is that I am submitting SL/TP targets when the execution happens, but they only appear as 1 contract on each side while there should be 2 on the SL and TP.

    What can I do to solve this from happening? Why are the order ID's different to begin with?

    I'm looking forward to your reply.

    Attached Files

    #2
    Hello GLFX005,

    Do you have two instances of the strategy running on the same account?

    When you say demo account are you talking about the Sim101, or are you talking about a live demo with an account name like DEMONT4567?

    NinjaTrader will have an internal ID for an order as it is generated and submitted to the broker, the broker is going to make their own ID for the order once its created on their backend and send this ID back to NinjaTrader which will change the internal ID to the ID received from the broker.

    This change of ID would not result in multiple orders and would not cause the position to change for multiple orders, as this would just be a single order.

    I would look for multiple instances of the script running. Or look for order methods being called multiple times in the script.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea, thank you for your reply.

      Yes, I am talking about the Sim101 account, which is using a live data feed from a live account. The issue I am observing does not occur on the Sim101 accounts.

      What happens is that one strategy that is running on the live account on one particular market is submitting 2 trades, accordingly, but when filled it shows two execution events. Normally speaking the strategy should submit a TP/SL order to match the Position.Quantity of the newly filled order, but because the order is executed twice, the submitted SL/TP are only happening once. This does not happen every time, but only sometimes.

      Is it possible that running the same strategy, on two different markets, yet on the same Live account, can result in errors because they use the same order submission names?

      I'm looking forward to your reply.

      Comment


        #4
        Hello GLFX005,

        I would suspect that the logic is allowing multiple orders to be placed.

        Use prints and TraceOrders to confirm that the order submission method is not being called twice.


        Below is a link to an example of modifying stop and target orders.


        Are you finding that this strategy is having the same behavior?

        May I test your script?

        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


          #5
          Hello Chelsea,

          I have confirmed that my strategy is submitting one trade with 2 contracts at once, but is showing two single contract fills in the execution events when the order is filled. This happens only on a Live account, and not on the Sim101 demo. My prior message wasn't clear enough about the scenario I am observing.

          Comment


            #6
            Hello GLFX005,

            One order is being submitted and shows in OnOrderUpdate()?

            This order has a quantity of 2?

            Are these part fills?
            If they are part fills, what is the quantity of each fill?

            NinjaTrader sees live demos and funded brokerage accounts the same. If there is an issue on the platform side, we can test using a live demo (not the Sim101). If my example script cannot reproduce, we are likely looking at an issue in your custom logic. If my example script can reproduce then we are likely looking at an issue on the brokerage side.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello Chelsea,

              One order is being submitted and shows in OnOrderUpdate() as two contracts. See Attachment1 for that, and Attachment2 for the OnExecutionUpdate().

              The logic to submit is this

              if (BTP2 == null
              && intBTP2 == 0
              && Position.MarketPosition == MarketPosition.Flat
              && Close[0] < KyoriBands3(TwentyFive, Fifty, SeventyFive, Hundred, HundredTwentyFive).High5[0] - 5 * TickSize
              && Close[0] > KyoriBands3(TwentyFive, Fifty, SeventyFive, Hundred, HundredTwentyFive).High3[0]
              && High[0] > KyoriBands3(TwentyFive, Fifty, SeventyFive, Hundred, HundredTwentyFive).High2[0])
              {
              intBTP2 = 1;
              SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.MIT, Contracts, 0, KyoriBands3(TwentyFive, Fifty, SeventyFive, Hundred, HundredTwentyFive).High5[0], "", "BTP2");
              }


              And the logic once BTP2 is filled looks like this.

              if (BTP2 != null && BTP2 == execution.Order)
              {
              if (execution.Order.OrderState == OrderState.Filled)
              {
              CancelOrder(TP78);
              CancelOrder(SL78);
              CancelOrder(BS3);
              CancelOrder(BS4);
              TP78 = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.MIT, Position.Quantity, 0, Open[0] - Trend * TickSize, (ToDay(Time[0]) + Instrument.FullName + Account.Name + "Q3" + CurrentBar + SystemPerformance.AllTrades.Count), "TP78");
              SL78 = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.StopMarket, Position.Quantity, 0, Close[1] + Reverse * TickSize, (ToDay(Time[0]) + Instrument.FullName + Account.Name + "Q3" + CurrentBar + SystemPerformance.AllTrades.Count), "SL78");
              BTP2 = null;
              intBTP2 = 0;
              }
              }


              Please let me know if there's anything else I can provide to make things clearer.
              Attached Files

              Comment


                #8
                Hello GLFX005,

                Your screenshot shows a quantity of 1 each. Likely you are looking at part fills. If the quantity is 2, then this appears correct.

                Print the order object in OnOrderUpdate() to understand the behavior of the orders.

                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Chelsea,

                  I've managed to resolve the prior issue. I do have some additional questions though.

                  1) When an order is filled, does the order become null, or does it still exist?
                  The reason why I am asking is because I have adapted the practice to nullify an order when it has been filled, but this may cause unwanted results since I'm not aware of what Ninjatrader does or doesn't do by itself under the hood. (Unmanaged Approach, by the way)

                  2) When running two of the same strategies, on the same account, but each on a different market, with similar OCO strings, are they able to interfere with each other? And what if the two strategies ran on two different accounts but the same market?

                  I am looking forward to your reply.

                  Comment


                    #10
                    Hello GLFX005,

                    When an order is filled it remains an order and will have an OrderState of OrderState.Filled.

                    Yes, setting an Order variable to null is a good way to know the order is done. An example I have does the same.


                    Yes, re-using OCO IDs will cause issues with other orders either manual, or from other scripts, or from other orders within the same script.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Chelsea, thank you for providing me those examples.

                      1) I have a question about the "ProfitChaseStopTrailUnmanagedExample" and a different example you've given me before, called "SampleOnOrderUpdate".
                      I notice that in the former example, you are nullifying an entry order once the relevant SL/TP targets have been filled, but in the latter example you are nullifying the entry order once the entry order itself has been filled. Which approach is best, or does it make no difference?

                      2) Can I use a User Defined Variable (set when installing the strategy on a chart) in my Private Order names to make the order names unique for each strategy?

                      3) What is the difference between using...

                      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)

                      vs.

                      protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)

                      4) Is it better to check for Position changes in OnPositionUpdate(), such as "MarketPosition.Long", compared to checking it in OnBarUpdate()?

                      I'm looking forward to your reply.
                      Last edited by GLFX005; 07-31-2020, 09:29 AM.

                      Comment


                        #12
                        Hello GLFX005, thanks for your questions.

                        1. An order should be set to null once it has been canceled or filled (in a terminal state). So in any case, if an entry order is filled or canceled, the order's corresponding object should be nullified.

                        2. Yes, a String user variable can be used to specify order names.

                        3. Those two overrides are the same, the nativeError and comment are just names for a string parameter.

                        4. It depends on if you just need to know the current position at any time, or if you need to know exactly when the position changes. The OnPositionUpdate will notify the script at the exact time the position changes.

                        Please let me know if I can assist any further.
                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello Chris, thank you for your informative replies.

                          I have another question considering this Help Guide article --> https://ninjatrader.com/support/help...-threading.htm

                          On the bottom it says:

                          "Considering the concept above, if you are unsure if you should be using the core order object value vs the updating method parameter value value, ask your self if you are truly looking for the most current order state, or the sequence of order states:
                          For the most current order state, use the core "order" object property (e.g., order.OrderState, order.LimitPrice, order.StopPrice, etc)
                          For the sequence of order states, use the updating method parameter value (e.g., orderState, limitPrice, stopPrice, etc)"


                          How can I ensure that my script is using the correct sequence of order states? The example provided is unclear to me in terms of how that would look like in a real piece of code.
                          Is this somehow related to my prior 3rd question regarding the difference between the two override methods?

                          Since in this Help Guide article about OnOrderUpdate() --> https://ninjatrader.com/support/helpGuides/nt8/?onorderupdate.htm it is shown to be using the "Cbi" override version in the Multithreading example.

                          I am looking forward to your reply.

                          Comment


                            #14
                            Hi GLFX005, thanks for your reply.

                            The Cbi.Order order and Order order are the same types. The fully qualified type is NinjaTrader.Cbi.Order. That note is saying if you want the most current state of the order, get it directly from the order object. e.g. myorder.OrderState. To know the exact sequence of the order, you can determine that through the call sequence of OnOrderUpdate.

                            Please let me know if any further questions come up.
                            Chris L.NinjaTrader Customer Service

                            Comment


                              #15
                              Hello Chris,

                              What I understand from that is, in the following examples, to get the most current state of the order I should do the following.

                              OnExecutionUpdate()

                              if (AS1 != null && AS1 == execution.Order)
                              {
                              if (execution.AS1.OrderState == OrderState.Filled)
                              <--- Will this logic trigger twice if a 2 contract market order is giving two execution updates because the order was filled at 2 different price levels as a result of slippage?
                              {
                              CancelOrder(TP12);
                              SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.MIT, Position.Quantity, 0, Close[1] - Reverse * TickSize, "", "TP12");
                              AS1 = null;
                              }
                              }


                              and

                              OnOrderUpdate()

                              if (SL12 != null && SL12 == order)
                              {
                              if (SL12.OrderState == OrderState.Cancelled)
                              {
                              SL12 = null;
                              }
                              }


                              I'm looking forward to your reply.

                              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