Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to hook into order reversals

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

    How to hook into order reversals

    I have an automated trading strategy which is fully managed, however I do hook into OnOrderUpdate() and OnExecution() to send myself email notifications. Works just fine and I get emails when an entry is taken, a stop is hit, a stop needs to be moved, or a target has been hit. However with a new strategy I've run into an issue as it reverses direction when an inverse entry condition is encountered.

    As you know when already long and a short entry is triggered NT will automatically close out the long position, and cancel the stop. Then it will take the short entry and set the stop as defined in the strategy.

    The problem I'm having is that I am not receiving an email to close the old position when a position is being flipped. I do receive an email about the new entry and to set a new stop as the former is handled in my OnOrderUpdate method and the latter in my OnExecution() method.

    However, I am not sure how I can catch the 'flip position' event. I would like to know when positions are being flipped so that I can email myself a 'close short' email before I get my 'go long' email. In other words I'm looking for the best method/way to hook into that.

    i hope the above makes sense - if not please ask away. Thanks for any help in advance.
    Last edited by molecool; 07-10-2013, 12:06 PM.

    #2
    Hi molecool,

    Thanks for your note.

    In the OnExecution the order to close the position is sent before the order to enter the opposite condition and can be found.

    This order will have a name of "Close position".

    For example:

    if (execution.Name == "Close position")
    // send email


    Please let me know if you would like further assistance with this.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Awesome - that's what I was looking for. Thanks for the quick response :-)

      Comment


        #4
        Quick follow up for clarification

        What I'm doing in OnExecution is the following:

        Code:
        protected override void OnExecution(IExecution execution) {
           try {
              System.Text.StringBuilder buffer = new System.Text.StringBuilder();
              
              if (exitOrder != null && exitOrder == execution.Order) {
                 // do and log stuff
              }
        
              if (stopOrder != null && stopOrder == execution.Order) {
        	// do and log stuff
              }
        
              if (entryOrder != null && (entryOrder == execution.Order)) {
        					
                 if (StopFlag && (execution.Order.OrderState == OrderState.Filled ||
                    execution.Order.OrderState == OrderState.PartFilled 
        	    || (execution.Order.OrderState == OrderState.Cancelled 
                    && execution.Order.Filled > 0))) {
        	
                    if(execution.MarketPosition == MarketPosition.Long) {
        		 // do and log stuff
                     }
        						
        	     if(execution.MarketPosition == MarketPosition.Short) {
                         // log and do stuff
        	     }
                  }
              }
        }
        So should I just put this outside of all that? Perhaps right after the try { statement? Or would this best in the context of the entryOrder condition?

        Thanks!
        Last edited by molecool; 07-10-2013, 12:23 PM.

        Comment


          #5
          Hi molecool,

          I would think you would just make an if statement to check this inside of the try the same as your other checks.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hi molecool,

            I would think you would just make an if statement to check this inside of the try the same as your other checks.
            Okay, I'll proceed as suggested. Thanks Chelsea.

            Comment


              #7
              Actually...

              Hi again - I just noticed that I have this in my OnOrderUpdate method:

              Code:
              // If we reverse positions (e.g. long to short) within one session:
              if (order.Name == "Close position") {					
              	buffer.Append("CLOSE OPEN POSITION: ").Append(order.OrderAction)
                      .Append(" at ").Append(order.AvgFillPrice).Append(" and clear all stops")
                      .Append(".\n");
              	if (enableLog) Print("Buffer inside OnOrderUpdate:\n" + buffer);
              	stopOrder = null;
              }
              Obviously it was never called - is it because of it being in OnOrderUpdate? Sorry I didn't mention this earlier but I honestly wrote the base code of this four years ago so i totally forgot it was in there.

              Comment


                #8
                Hello molecool,

                Yes, this would be because it is in the OnOrderUpdate. Try moving that to OnExecution and changing to using the IExecution object instead of IOrder object. (The IExecution object will actually contain the IOrder object within it).
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  The journey continues

                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello molecool,

                  Yes, this would be because it is in the OnOrderUpdate. Try moving that to OnExecution and changing to using the IExecution object instead of IOrder object. (The IExecution object will actually contain the IOrder object within it).
                  I just looked into my mail history and back in June I found some occasions when this routine was actually hit on a position reversal.:



                  So I'm a bit confused. The context appears to be the same - a position reversal from a long to a short trade. But despite what you're saying this routine was executed properly. Then there are times when it is not. How would you explain that?

                  Sorry, I'm not trying to make your job more complicated but software cannot be intermittent - when it is then there's usually a special condition or a bug

                  Thanks again for your help.
                  Attached Files

                  Comment


                    #10
                    Hello molecool,

                    You are correct, the Close position does trigger OnBarUpdate...

                    Can you show me an example where this is not working?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello molecool,

                      You are correct, the Close position does trigger OnBarUpdate...

                      Can you show me an example where this is not working?
                      My current code is sending emails and only during live signals. Let me put in some log statements and see if I can replicate it either in historical or live mode. I'm going to respond here when I find such an occasion.

                      Comment

                      Latest Posts

                      Collapse

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