Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The Various OrderStates OnExecution() Method tracks?

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

    The Various OrderStates OnExecution() Method tracks?

    Hi

    I would like to know which of the following order states does OnExecution() Method tracks:

    1. OrderState.Accepted
    2. OrderState.Cancelled
    3. OrderState.Filled - This one i know it clearly tracks
    4. OrderState.PartFilled
    5. OrderState.PendingCancel
    6. OrderState.PendingChange
    7. OrderState.PendingSubmit
    8. OrderState.Rejected
    9. OrderState.Working
    10. OrderState.Unknown

    Please mention in front of the Order States mentioned above.

    Thank You

    #2
    Hello Piyushic,

    Thank you for your note.

    Note that OnExecution will only get called when a Fill has occurred.

    PartFilled and Filled are these order states that are only reported when using the IOrder object to get those OrderStates.


    Those other OrderStates are going to be tracked with OnOrderUpdate() as those will get called when the order update has come through.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      The problem gets bigger then.
      Let me explain the issue here, please be very patient.

      Example:
      Variables:
      TQ = Total Quantity = 1000
      SOQ = Single Order Quantity = 100



      OnBarUpdate()

      if Condition
      {
      entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, SOQ, GetCurrentAsk() + 2 * TickSize, 0, "", "EnterLong");
      }

      OnOrderUpdate()
      if order.OrderState == OrderState.Filled
      {
      for(int i = TQ - Position.Quantity; i>0 ; i+=SOQ)
      {
      entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, SOQ, GetCurrentAsk() + 2 * TickSize, 0, "", "EnterLongAgain"+i);

      }
      }

      if order.OrderState == OrderState.Working


      {
      ChangeOrder(entryOrder, SOQ, GetCurrentAsk() + 2 * TickSize, 0);

      }

      ---
      This is just a small snippet.
      There are other OrderStates it gets stuck on, like:
      1. PendingChange
      2. PendingSubmit

      My larger objective here is:
      1. Scale Up the Quantity to be Traded by firing one lot at a time & not expose entire quantity to be traded.
      2. By keeping Slippage under control. That is to trade within a defined % boundary of the Last Traded Price just before the Signal generated. (This part i have not mentioned in the code above but that was extensively discussed in the other thread: Scalability without ATM)

      Comment


        #4
        piyushc, what would happen if you reduced the scale qty then? Let's say for testing do one additional scale in only? What I would suggest is simplifying the code until it works as you expect and only then please add in more complexity as otherwise it will be make next to impossible for you to debug likely.

        Comment


          #5
          Is there a way to code that:
          if the order is in OrderState.Working for more than say 5 minutes, then
          {
          ChangeOrder or CancelOrder
          }

          i know the barNumberOfOrder concept, but here the bars are of 60 minutes so that won't do.
          The time part, how?

          Objective: This should skip many other order states & get me to either Working or Filled, which should be easier to then proceed with.

          Comment


            #6
            Hello piyushc,

            Thanks for your post.

            If your bars are printing at the right times you can check the bar time. You want to record the bar number of the starting bar for this to work.

            We'll say that I've saved the bar number to variable (int) startBar. Also, we'll assume I have an IOrder handle for myOrder.

            For example:

            if (Time[0].AddMinutes(-5) > Time[startBar] && myOrder.OrderState == OrderState.Working)
            {
            // execute code
            }
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              NT freezing

              Thanks Chelsea.

              Code Snippet(NT freezing as soon as the first order is fired):
              i'm quite sure that the problem is there in the code, of course.
              Variables:
              private int tQ = 10; // Total Quantity
              private int sOQ = 1; // Single Order Quantity
              private IOrder entryOrder = null;
              private int startBar = 0; // Bar at which Order is placed



              OnBarUpdate()
              if condition
              {
              entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, SOQ, GetCurrentAsk(), 0, "", "EnterLong");
              startBar = CurrentBar;

              }

              OnOrderUpdate()
              if (order.Name == "EnterLong" && order == entryOrder
              && order.OrderState == OrderState.PendingCancel || order.OrderState == OrderState.PendingChange || order.OrderState == OrderState.PendingSubmit || order.OrderState == OrderState.Unknown || order.OrderState == OrderState.Accepted)
              {
              return;
              }


              if (order.Name == "EnterLong"
              && order == entryOrder && order.OrderState == OrderState.Working
              && Time[0].AddMinutes(-2)>Time[startBar])



              {
              ChangeOrder(entryOrder, SOQ, GetCurrentAsk(), 0);
              startBar = CurrentBar;
              }


              OnExecution()

              if (execution.Order.Name == "EnterLong"
              && execution.Order == entryOrder && execution.Order.OrderState == OrderState.Filled
              && Position.Quantity < TQ)
              for(int i = TQ - Position.Quantity; i>0 ; i+=SOQ)
              {
              entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, SOQ, GetCurrentAsk(), 0, "", "EnterLongAgain"+i);
              startBar = CurrentBar;
              }
              --------------

              Trace file attached. Pls chk the entries at 9:20:00.
              Moreover, NT freezes completely after that & i have to exit via Task Manager.

              My guess is that the first entry via OnBarUpdate() is running fine but as soon as it enters OnOrderUpdate() confusion occurs. What's your take?

              Thank You
              Attached Files

              Comment


                #8
                Hi piyushc,

                May I have an export of the code? This just makes things easier for testing. (getting errors when I compile this).

                To export your script do the following:
                1. Click File -> Utilities -> Export NinjaScript
                2. Enter a unique name for the file in the value for 'File name:'
                3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
                4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


                By default your exported file will be in the following location:
                • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


                Below is a link to the help guide on Exporting NinjaScripts.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  text file of the code attached.
                  Attached Files
                  Last edited by piyushc; 03-18-2014, 10:06 AM.

                  Comment


                    #10
                    Hi piyushc,

                    The issue I am seeing is with the for loop that you have in multiple places.

                    The code is:
                    for(int i = TQ - Position.Quantity; i>0 ; i+=SOQ)

                    Prints can be extremely helpful when debugging your code. They can show you the values of the variables so that you can see how they evaluate.

                    I printed the values of these variables as the first line of OnExecution.
                    The values printed as (TQ-Position.Quantity) - 9, SOQ - 1.

                    If we plug these values into the loop it looks like:
                    for (int i=9; i>0; i+=SOQ)

                    This evaluates to start at 9. If i is greater than 0 then add 1 and run the loop again.

                    This loop will never end. If you add 1 to a number greater than 0. It will always be greater than 0.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      May be you're right but my logic is:
                      i is a function of TQ-Position.Quantity.
                      So when i=9, it is 10-1.
                      So it enters 1 more, i would then be 10-2=8, reducing sequentially.
                      Once TQ=Position.Quantity, i=0. Loop Exits.

                      Comment


                        #12
                        Hi piyushc,

                        I am printing (TQ-Position.Quantity). This means that if the quantity is 1 then TQ is 10.

                        Regardless of the initial setting, the variable i is going to increase by SOQ (1) on each iteration.

                        So if it starts at 9 then the next run it will be 10. Then 11. Then 12.. etc...

                        It will never decrease from the code you have.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Bull's Eye.
                          i bifurcated this part of the code, formed a new strategy, checked the trace file. You're absolutely right.
                          Can you please help me correct it & make it more what i intend to?

                          i think it has to be i-=SOQ.

                          Comment


                            #14
                            Hi piyushc,

                            Using i-=SOQ would cause the loop to decrease by 1 on each iteration and would eventually become 0.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              The for loop got completely sorted. Thanks.
                              Now, OnBarUpdate() & OnExecution() are working perfectly well.
                              i had segregated them & formed a strategy with first OnBarUpdate() & then added OnExecution(), there were no errors.
                              Then as soon as i added OnOrderUpdate(), problem resurfaced.
                              So this is the hole right now.
                              i'm attaching the latest trace file, please check it at 4:08:00 AM.
                              Re-attaching the modified cs code file.

                              Thank You
                              Attached Files
                              Last edited by piyushc; 03-19-2014, 04:49 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              600 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              346 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              558 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              558 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X