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

When Can I Use IOrder?

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

    When Can I Use IOrder?

    I have a strategy that implements a somewhat complex logic for updating a stop. Furthermore, the strategy logic is written to update the stop for any number of active trades. To this end, I have a custom object for each trade that contains the information I need. One of the fields of this object is the IOrder output from the ExitLongStop/ExitShortStop call.

    I was using this IOrder output as a unique marker for each trade. Perhaps this was a mistake.

    As I step through the debugger, I notice that often IOrder returns null. Such as in OnOrderUpdate (or the same is in OnExecution):

    Code:
    IOrder myOrder = ExitLongStop(0,true,quantityInt,stopPrice,exitName,enterName);
    often returns a null myOrder IOrder the next line in the debugger.

    Looking through other forum help about the subject, it seems that the explanation is that filling the IOrder to a non-null value may happen in a different thread, and thus, myOrder may become non-null later.

    The problem is that I want to know myOrder so that I can place it in my custom order object and move on.

    The question is: If the IOrder cannot be guaranteed to be written at the time the order is called, how can I use it? Must I declare IOrder myOrder as a global in the strategy? If the IOrder output is not written immediately afterward, then it makes no sense to declare IOrder myOrder locally in OnOrderUpdate or OnExecution. Is this correct? Am I going to need to create my own order fingerprint, and forget using an IOrder object?

    Has anyone run into similar problems? Suggestions? Thanks.

    #2
    Hi Serac,

    Using IOrders is preferred. It is possible an IOrder can be null and you should add checks for null within your code.

    Typically, in examples I make, I set an IOrder to null after the trade has been exited. By checking that its null, I know that the position is flat simply because the IOrder will not be set to null until the trade is exited.

    Attached I have added an example script to demonstrate.
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply.

      I'm not sure if you understood my question.

      My question is that an IOrder object may be null soon after a call to an order. For example:

      Code:
      IOrder myOrder = ExitLongStop(...); 
      
      if (myOrder == null)
      {
          Print("IOrder is null following call to order!");
      }
      This will sometimes go to the print statement.

      Thus, if you use IOrder for tracking (as you suggest), the IOrder may be null while the ExitLongStop may be processing on another thread.

      If this is the case, then I do not understand how IOrder may be used for tracking.

      Comment


        #4
        Originally posted by Serac View Post
        I have a strategy that implements a somewhat complex logic for updating a stop. Furthermore, the strategy logic is written to update the stop for any number of active trades. To this end, I have a custom object for each trade that contains the information I need. One of the fields of this object is the IOrder output from the ExitLongStop/ExitShortStop call.

        I was using this IOrder output as a unique marker for each trade. Perhaps this was a mistake.

        As I step through the debugger, I notice that often IOrder returns null. Such as in OnOrderUpdate (or the same is in OnExecution):

        Code:
        IOrder myOrder = ExitLongStop(0,true,quantityInt,stopPrice,exitName,enterName);
        often returns a null myOrder IOrder the next line in the debugger.

        Looking through other forum help about the subject, it seems that the explanation is that filling the IOrder to a non-null value may happen in a different thread, and thus, myOrder may become non-null later.

        The problem is that I want to know myOrder so that I can place it in my custom order object and move on.

        The question is: If the IOrder cannot be guaranteed to be written at the time the order is called, how can I use it? Must I declare IOrder myOrder as a global in the strategy? If the IOrder output is not written immediately afterward, then it makes no sense to declare IOrder myOrder locally in OnOrderUpdate or OnExecution. Is this correct? Am I going to need to create my own order fingerprint, and forget using an IOrder object?

        Has anyone run into similar problems? Suggestions? Thanks.
        Looks like a standard scope issue to me. That declaration makes the IOrder local to the method in which it was declared. You may want to do as you suggest and declare the IOrders with class scope.

        Comment


          #5
          Hello Serac,

          koganam is correct, if the variable is declared within a method, that variable is reset each time the method is called.

          Take a look at the example I have provided in post #2.

          This example declares the variables outside of the method within the scope of the class. This means that the variable is not re-created each time a method runs.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by judysamnt7, 03-13-2023, 09:11 AM
          4 responses
          59 views
          0 likes
          Last Post DynamicTest  
          Started by ScottWalsh, Today, 06:52 PM
          4 responses
          36 views
          0 likes
          Last Post ScottWalsh  
          Started by olisav57, Today, 07:39 PM
          0 responses
          7 views
          0 likes
          Last Post olisav57  
          Started by trilliantrader, Today, 03:01 PM
          2 responses
          21 views
          0 likes
          Last Post helpwanted  
          Started by cre8able, Today, 07:24 PM
          0 responses
          10 views
          0 likes
          Last Post cre8able  
          Working...
          X