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

Exit order size doesn't match with entry order size

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

    Exit order size doesn't match with entry order size

    Hi team,

    I have one strategy working OnEachTick that has been working pretty well when i create entry/exit limit orders of 1 size.

    Today i tried to increase the quantity to 3 contracts per order and for the entry it works well. However, the limit/stoploss exit orders only appear to create orders for 1 contract (instead of 3).

    Below you can see the orders generated and how for the entry order the quantity is 3 but the exit orders are for quantity 1 only:Click image for larger version

Name:	image.png
Views:	122
Size:	15.0 KB
ID:	1278163
    Below the screenshot of the code, the orders i have them in OnExecutionUpdate like that:
    Click image for larger version

Name:	image.png
Views:	98
Size:	6.2 KB
ID:	1278164
    Then if one bar has elapsed, the exit limit order price is updated OnBarUpdate as per below:
    Click image for larger version

Name:	image.png
Views:	98
Size:	14.2 KB
ID:	1278165

    Any idea why it only creates 1 contract when exiting? i assume it has to be with the execution.Order.Filled but would love some guidance on how to fix this.

    THanks!

    #2
    Hello linkcou,

    Thanks for your post.

    In OnExecutionUpdate(), are you checking to ensure the entryOrder is not null and the entryOrder == execution.Order?

    Then are you checking if execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0?

    Followed by summing the quantities of each execution making up the entry order and assigning it to a variable?

    For example:

    // We sum the quantities of each execution making up the entry order
    sumFilled += execution.Quantity;

    And, are checking if the execution.Order.OrderState is OrderState.PartFilled or OrderState.Filled and then calling your Exit order method to place the order?

    See this reference sample that demonstrates submitting profit target and stop loss exit orders in OnExecutionUpdate():
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon, thanks for the quick reply!

      Yes, i am doing the first 2 steps you mentioned. See below screenshot.

      However, not sure i am doing the sumFilled variable. Where should I add this command? right before creating the variable ExitLimitL?

      Could you please resend the link? it says "Page Not Found"
      Click image for larger version

Name:	image.png
Views:	89
Size:	36.3 KB
ID:	1278233

      Comment


        #4
        Hello linkcou,

        I corrected the link in post # 2, however I am including a better example that actually uses the sumFilled amount for the exit orders.
        Hello, I see in the documentation you have fixed: 15048 Added Adapter, Rithmic Updated Rithmic API to version 11.3.0.0 Can you elaborate on that please? Will OnOrderUpdate() OnExecutionUpdate() and OnPositionUpdate() be called in the expected order?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,
          Thanks for your reply. The examples you shared were a bit confusing so i am not 100% sure if the code as per below works well. Could you please let me know if you see anything wrong with it?

          Click image for larger version

Name:	image.png
Views:	90
Size:	49.3 KB
ID:	1278394

          Comment


            #6
            Hello linkcou,

            You would want to separate the exits for longs into a condition checking the long entry order is filling, and the exits for shorts into a condition checking the short entry is filling, and not submit these at the same time.

            However, the use of sumFilledLong / sumFilledShort as the quantity appears correct.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,
              Thanks for your comments! I implemented the changes (see below). Do you think this is correct now?
              I will try to implement it today to see if it works well and keep you posted.
              Thanks for your help!

              Click image for larger version

Name:	image.png
Views:	86
Size:	57.1 KB
ID:	1278522

              Comment


                #8
                Hello linkcou,

                Yes, this looks like something that would work.

                It's not shown here, but hopefully you have entryOrder being assigned an object in OnOrderUpdate().

                Let me know if you run into any issues.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Chelsea,

                  I am experiencing issues with this topic again. In the strategy I have an entry limit order with 3 contracts. However, as you can see in the screenshot below, the "ExitLimitShort" order generates an order for 2 contracts (instead of 3) forcing me to close the position manually.

                  I am doing the SumFilledLong and SumFilledShort trick that we disucssed above but not sure why it only pick up 2 contracts only. Any idea how to solve it?

                  Click image for larger version

Name:	image.png
Views:	68
Size:	6.2 KB
ID:	1280240

                  Sharing below the piece of code in OnExecutionUpdate:
                  Click image for larger version

Name:	image.png
Views:	61
Size:	24.7 KB
ID:	1280241

                  Comment


                    #10
                    Hello linkcou,

                    In the screenshot it looks like the entry part filled with a quantity of 1 and a part fill with a quantity of 2.

                    Are you adding any debugging prints to see if quantity is the expected amount on the second part filled? (Print the order object from OnOrderUpdate() print the execution object from OnExecutionUpdate())

                    Is TraceOrders enabled?

                    Please provide the output from the prints and TraceOrders.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Chelsea, thanks for the quick reply!

                      I didnt have the TraceOrders enabled, I just did. Could this be the issue?

                      I'm adding the below line of code. Is this what you are referring to? I am waiting to see if there is a trade executed to see if it works now.

                      Print ("Quantity executed : " + execution.Quantity);

                      Best,

                      Comment


                        #12
                        I was just thinking, what if I add quantity = 3 when creating the exit order? It seems that i am alway filling the quantity so it should work right?

                        Comment


                          #13
                          Hello again Chelsea,

                          I am not sure I understood 100% your original message with the prints. Could you please provide more guidance on this debugging thing?.

                          Let me share with you the full OnOrderUpdate and OnExecutionUpdate sections in case you see anything wrong. Let me know if easier to copy-paste directly instead of a screenshot:

                          Click image for larger version

Name:	image.png
Views:	60
Size:	69.6 KB
ID:	1280304Click image for larger version

Name:	image.png
Views:	56
Size:	123.5 KB
ID:	1280305Click image for larger version

Name:	image.png
Views:	55
Size:	69.2 KB
ID:	1280306

                          Comment


                            #14
                            Hello linkcou,

                            Thanks for your notes.

                            "Are you adding any debugging prints to see if quantity is the expected amount on the second part filled? (Print the order object from OnOrderUpdate() print the execution object from OnExecutionUpdate())"

                            What Chelsea meant by the above comment is to print out the order object in OnOrderUpdate(). And, print the execution object from OnExecutionUpdate().

                            In the code you shared I do not see where you are printing out the order object in OnOrderUpdate(). The order object should be printed in OnOrderUpdate(). The print would look something like this.

                            Print("order object: " + order);

                            The execution object print in OnExecutionUpdate() might look something like this.

                            Print("execution object: " + execution);

                            Enabling TraceOrders will mean that traces are generated and displayed in the NinjaScript Output window for each call of an order method providing confirmation that the method is entered and the quantity and providing information if order methods are ignored and why. This is valuable for debugging if you are not seeing expected behavior when calling an order method.​

                            Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                            https://ninjatrader.com/support/foru...121#post791121
                            Brandon H.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi Brandon,

                              Thanks for your help on the print details. I have implemented these prints in the code and this are the results when executing an order:

                              Execution object: execution='1164452|126356274|126356274' instrument='NQ DEC23' account='PRACTICENov302663245' exchange=Globex price=15847.25 quantity=1 marketPosition=Long orderId='126356274' time='2023-12-04 13:09:04' sod=False statementDate='2023-12-04'
                              SumFilledLong : 1
                              04/12/2023 13:09:04 Strategy 'NQDonchianMMv8_MultipleContracts/310224931': Entered internal SubmitOrderManaged() method at 04/12/2023 13:09:04: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=15852,25 StopPrice=0 SignalName='ExitLimitLong' FromEntrySignal='MMDL1'
                              order object: orderId='126356274' account='PRACTICENov302663245' name='MMDL1' orderState=PartFilled instrument='NQ DEC23' orderAction=Buy orderType='Limit' limitPrice=15847.25 stopPrice=0 quantity=3 tif=Gtc oco='' filled=2 averageFillPrice=15847.25 onBehalfOf='' id=10010 time='2023-12-04 13:09:05' gtd='2099-12-01' statementDate='2023-12-04'
                              order object: orderId='126356274' account='PRACTICENov302663245' name='MMDL1' orderState=Filled instrument='NQ DEC23' orderAction=Buy orderType='Limit' limitPrice=15847.25 stopPrice=0 quantity=3 tif=Gtc oco='' filled=3 averageFillPrice=15847.25 onBehalfOf='' id=10010 time='2023-12-04 13:09:05' gtd='2099-12-01' statementDate='2023-12-04'
                              04/12/2023 13:10:00 Strategy 'NQDonchianMMv8_MultipleContracts/310224931': Entered internal SubmitOrderManaged() method at 04/12/2023 13:10:00: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=15849,00 StopPrice=0 SignalName='ExitLimitLong' FromEntrySignal='MMDL1'
                              04/12/2023 13:10:00 Strategy 'NQDonchianMMv8_MultipleContracts/310224931': Amended matching order at 04/12/2023 13:10:00: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=15849,00 StopPrice=0 SignalName='ExitLimitLong' FromEntrySignal='MMDL1'​

                              For reference, the orders in the chart look like the below (i had to close manually 2 contracts because the exit order only contains 1 contract):
                              Click image for larger version

Name:	image.png
Views:	60
Size:	5.5 KB
ID:	1280693

                              Do you know what am I missing in the code?

                              Let me know if you need more details. ​

                              Thanks!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by poplagelu, Today, 05:00 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post poplagelu  
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,407 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              98 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              160 views
                              0 likes
                              Last Post loganjarosz123  
                              Working...
                              X