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

Questions on SampleOnOrderUpdate

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

    Questions on SampleOnOrderUpdate

    Hi, I've been reading codes in SampleOnOrderUpdate. 1 thing I don't get is how the entryOrder variable gets assigned to an actual order.

    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    	// Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
    	if (entryOrder != null && entryOrder == order)
    	{
            // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
            // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting
            entryOrder = order;
    
            // Reset the entryOrder object to null if order was cancelled without any fill
            if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    		{
    			entryOrder = null;
    		}
    	}
    }
    The if block checks if entryOrder is not null and entryOrder equals to order then assign entryOrder to order. Why do we need to assign entryOrder to order if we've checked that they are equal?

    And this entryOrder = order assignment only appears once in the whole script, which will never run when entryOrder is null initially.

    I'm scratching my head and trying to figure out this bit. Could anyone shed some light on this? Thanks!

    #2
    Hello josephktcheung, and thank you for your query.

    You are indeed correct that assigning order to entryOrder is redundant, and can be removed as an optimization if you would like. This is a good catch and I thank you for bringing it to our attention.

    I would like to reformat the code block you provided in pseudo-code to make it easier to go over what it is doing.

    Code:
    [FONT=Courier New]if (there is an existing entryOrder which matches the passed in order)
    {
        set entryOrder to the passed in order;
        if (the order was cancelled)
        {
            unset entryOrder, there is now no existing entryOrder;
        }
    }
    [/FONT]
    This code is a "no-op", which means that the compiler will simply erase it harmlessly. It is possible, given the comment, that this code was added in as a bug fix, which indicates that the code surrounding it needed to be modified at one point in time, rendering this block of code a "no-op".
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the answer! Another question is about the first assignment of entryOrder. When is entryOrder assigned to order in SampleOnOrderUpdate? I can't find any part that does this, which means entryOrder is always null.

      I guess 1 way to do this is there should be something like using order.Name to find the entry order and assign this order to entryOrder in onOrderUpdate().

      Code:
      if (entryOrder == null && order.Name == "MyEntry")
      {
              entryOrder = order;
      }
      Is this the right way? Thanks again!
      Last edited by josephktcheung; 04-18-2016, 07:20 PM.

      Comment


        #4
        Hello again,

        Your example is an excellent way of doing this. However, this would mean that entryOrder could only be set once for the life of the strategy. The names of the variables suggest that entryOrder should always be set to the current existing entry order. Given this, I would recommend removing the null check, and simply using

        Code:
        [FONT=Courier New]if (order.Name == "MyEntry")
        {
            entryOrder = order;
        }[/FONT]
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Cool! Thanks for the answer!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Vietanhnguyen2hotmailcom, Yesterday, 10:29 AM
          4 responses
          21 views
          0 likes
          Last Post Vietanhnguyen2hotmailcom  
          Started by PhillT, 04-19-2024, 02:16 PM
          4 responses
          35 views
          0 likes
          Last Post PhillT
          by PhillT
           
          Started by ageeholdings, 05-01-2024, 05:22 AM
          5 responses
          37 views
          0 likes
          Last Post ageeholdings  
          Started by reynoldsn, Today, 02:34 PM
          0 responses
          13 views
          0 likes
          Last Post reynoldsn  
          Started by nightstalker, Today, 02:05 PM
          0 responses
          20 views
          0 likes
          Last Post nightstalker  
          Working...
          X