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

Using OnOrderUpdate()

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

    Using OnOrderUpdate()

    Hey there...

    I have this code in my sys and I want it to run 'perpetually' ... I am using the onorderupdate to manually manage my stops and targets, and found that if I have this following code in 'onbarupdate', it doesn't cancel my stops (most importantly) and targets [not really necessary, but i have it here anyway] until the next bar:

    if (Position.MarketPosition == MarketPosition.Flat && stopOrder != null)
    {
    CancelOrder(targetOrder);
    CancelOrder(stopOrder);
    stopOrder = null;
    targetOrder = null;
    }


    Is this perfect code to have in OnOrderUpdate() to clear out old junk? If so, how 'often' does onorderupdate() actually run? Is it a perpetual loop based on seconds, or only when an Iorder variable gets changed?


    or should I leave it in onBarupdate and put the cancelorder(stopOrder) right in my early exit routines that exist in onBarUpdate immediately after I issue the early sell commands? I figure they are better off in OnOrderUpdate if I elect to use ExitLongLimits that don't immediately filll. As I would not look to be without a stop if I'm in an unexited position.

    I just want to post this to check that my understanding of this mechanism is indeed complete ... I am learning by trial and error, visual confirmation of behavior on paper test, and my Output window debug until then.
    Last edited by scriabinop23; 01-18-2008, 09:41 AM.

    #2
    - OnOrderUpdate() fires anytime a change in any order in your strategy changes state
    - I would only set a variable to null once I have received the Cancelled state back for the order that I cancelled
    RayNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Ray View Post
      - OnOrderUpdate() fires anytime a change in any order in your strategy changes state
      - I would only set a variable to null once I have received the Cancelled state back for the order that I cancelled
      excellent.. so a good solution might be (if I'm reading you correctly):

      1) create a new Iorder order variable just for Cancelorder commands. when i change that variable, onorderupdate() reruns.
      2) put a statement that looks for cancelorder filled conditions, then finally null out whatever was cancelled.

      Question though:

      Can I recursively use Iorder variables? Note that targetOrder will respur OnOrderUpdate.
      ie:

      in variables:

      private IOrder cancel = null; // This variable holds an object representing our entry order

      And in onorderupdate upon the mentioned condition.
      cancel = CancelOrder(targetOrder);
      Last edited by scriabinop23; 01-18-2008, 10:29 AM.

      Comment


        #4
        Close...

        myOrder = ExitLongStop();

        somewhere you will:

        CancelOrder(myOrder);

        then in OnOrderUpdate():

        Code:
        if (myOrder != null && myOrder.Token == order.Token && order.OrderState == OrderState.Cancelled)
            myOrder = null;
        RayNinjaTrader Customer Service

        Comment


          #5
          By the way, does setting an Iorder variable to null spawn OnOrderUpdate?

          ie:

          myOrder = null;

          Comment


            #6
            Originally posted by NinjaTrader_Ray View Post
            Close...

            myOrder = ExitLongStop();

            somewhere you will:

            CancelOrder(myOrder);

            then in OnOrderUpdate():

            Code:
            if (myOrder != null && myOrder.Token == order.Token && order.OrderState == OrderState.Cancelled)
                myOrder = null;
            Is the cancelled state or the filled state what I am looking for when cancelling a stop?

            Comment


              #7
              No it will not since that does not change the state of the underlying order.
              RayNinjaTrader Customer Service

              Comment


                #8
                You probably want to look for all possibilities -

                Cancelled
                Filled
                Rejected

                Since there is not guarantee that an order is cancelled when you cancel it. It may come back filled.

                See this reference sample - http://www.ninjatrader-support.com/v...ead.php?t=3917
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Ray View Post
                  No it will not since that does not change the state of the underlying order.
                  you are referring to the recursive ability, right? (ie cancel = CancelOrder(targetOrder)

                  [hard to follow with the topics changing.]

                  Comment


                    #10
                    CancelOrder() does not return anything. You passed in targetOrder and thats the variable you use to check for in OnOrderUpdate()
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      Oh one last thing.. (please bear with me as my C# is very primitive):

                      If I issue CancelOrder(targetOrder); in OnBarUpdate(), assuming targetOrder is a IOrder variable, does OnOrderUpdate get called?

                      And reminder: would targetOrder = null; being called in OnBarUpdate result in OnOrderUpdate getting called as well (since its null, not with a new order) ?

                      Comment


                        #12
                        Setting a variable to null will not trigger order state changes. Your are only changing a variable and not an order. When cancelling an order you will get state changes for this order in OnOrderUpdate().

                        Trying adding this to OnOrderUpdate()

                        Print(order.OrderState.ToString());

                        and then run some stuff in OnBarUpdate() and see what output you get.
                        RayNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Ray View Post
                          Setting a variable to null will not trigger order state changes. Your are only changing a variable and not an order. When cancelling an order you will get state changes for this order in OnOrderUpdate().

                          Trying adding this to OnOrderUpdate()

                          Print(order.OrderState.ToString());

                          and then run some stuff in OnBarUpdate() and see what output you get.
                          I appreciate your help and teaching.
                          And will use the debug to figure the rest of this out.

                          So basically, if I in onBarUpdate, don't enter an order thru variable manipulation ...
                          ie not entryOrder = EnterLong();
                          But instead use:
                          EnterLong() right away,

                          Does OnOrderUpdate get called?

                          Comment


                            #14
                            Originally posted by NinjaTrader_Ray View Post
                            CancelOrder() does not return anything. You passed in targetOrder and thats the variable you use to check for in OnOrderUpdate()
                            Oh one last thing... (hehe)

                            I am trying to spy on the state of the Iorder variables after an order (ie to see what they are after I issue cancelorder command) like this from onbarupdate():

                            Print(stopOrder.ToString());

                            And this compiles, but the backtest breaks here.. no more trades after this runs. Is this the wrong way to treat this variable?

                            Comment


                              #15
                              Yes that's fine, the breaking is likely a logic issue which you need to debug.
                              RayNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              2 responses
                              29 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by Mindset, 05-06-2023, 09:03 PM
                              10 responses
                              265 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by michi08, 10-05-2018, 09:31 AM
                              5 responses
                              743 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by The_Sec, Today, 02:29 PM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by tsantospinto, 04-12-2024, 07:04 PM
                              4 responses
                              63 views
                              0 likes
                              Last Post aligator  
                              Working...
                              X