Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting Order to Null

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

    Setting Order to Null

    I am having troubles with the proper way of resetting an order to null which is then causing improper trades.

    The code that is causing the issue is in OnBarUpdate and it is when I am trying to cancel any unfilled Entry orders and then reset to null in order for it to prepare for the next trading day.

    Here is the code:

    Code:
    				if (ToTime(Time[0]) >= ToTime(removeOrderHr, removeOrderMin,0)
    					&& LongEntry != null)
    				{
    					CancelOrder(LongEntry);
    					LongEntry = null;
                                    }

    The problem that I am running into is that LongEntry is also being reset to null when a LongEntry trade is currently active.

    Here is the goal:
    If LongEntry has a value of null and other criteria are met, then place an order to go Long @ XXXX.XX. If LongEntry triggers and is filled, then place ExitLongStop @ XXXX.XX & ExitLongLimit @ XXXX.XX. Reset LongEntry to null only AFTER ExitLongStop or ExitLongLimit are triggered and filled.

    BUT, if LongEntry does not trigger and get filled before a certain time of day, then cancel LongEntry and reset it to null.
    The problem with the above code is that it is resetting LongEntry to null every single day at the required time of day, regardless if there is a current active trade. If the trade is currently active, then it should not be resetting LongEntry back to null until the stop loss or profit target are hit.

    I do have code in OnExecution to say to reset it to null when the LongStop and/or LongLimit are executed. Here is that code:

    Code:
    				if ((LongStop != null
    					|| LongTarget != null)
    					&& (LongStop == execution.Order
    					|| LongTarget == execution.Order))
    				{
    					LongEntry = null;
    				}

    How can I reset LongEntry to null when the order is cancelled without resetting it every single day if the trade is still in play? So far, I am not able to come up with a workable idea.

    Thank you for your help
    Last edited by jg123; 07-13-2014, 04:02 AM.

    #2
    Originally posted by jg123 View Post
    I am having troubles with the proper way of resetting an order to null which is then causing improper trades.

    The code that is causing the issue is in OnBarUpdate and it is when I am trying to cancel any unfilled Entry orders and then reset to null in order for it to prepare for the next trading day.

    Here is the code:

    Code:
                    if ([COLOR=Blue][B]Position.MarketPosition != MarketPosition.Long &&[/B] [/COLOR]ToTime(Time[0]) >= ToTime(removeOrderHr, removeOrderMin,0)
                        && LongEntry != null)
                    {
                        CancelOrder(LongEntry);
                        LongEntry = null;
                                    }
    The problem that I am running into is that LongEntry is also being reset to null when a LongEntry trade is currently active.

    Here is the goal:


    The problem with the above code is that it is resetting LongEntry to null every single day at the required time of day, regardless if there is a current active trade. If the trade is currently active, then it should not be resetting LongEntry back to null until the stop loss or profit target are hit.

    I do have code in OnExecution to say to reset it to null when the LongStop and/or LongLimit are executed. Here is that code:

    Code:
                    if ((LongStop != null
                        || LongTarget != null)
                        && (LongStop == execution.Order
                        || LongTarget == execution.Order))
                    {
                        LongEntry = null;
                    }
    How can I reset LongEntry to null when the order is cancelled without resetting it every single day if the trade is still in play? So far, I am not able to come up with a workable idea.

    Thank you for your help
    Check the position in the market first. Code added, in blue, into your original code.
    Last edited by koganam; 07-13-2014, 02:34 PM.

    Comment


      #3
      Thank you very much! That really helps a lot!

      This does lead me to another question about how Position.MarketPosition works. First, though, I need to give a brief bit more about the strategy.

      LongEntry is the name of the order when I want to go long. But, if LongEntry is currently in a trade and the market sets up for another trade before LongEntry closes, then it is supposed to use the variable LongEntry1. If LongEntry & LongEntry1 are both in trades, then it is supposed to use LongEntry2, etc, etc, etc.

      It is highly likely that I will be in LongEntry1 but not in LongEntry due to LongEntry closing after it opened LongEntry1.

      Therefore, when I am testing for the criteria of: Position.MarketPosition != MarketPosition.Long, will this effect both LongEntry & LongEntry1?

      For example:

      Code:
                      if (Position.MarketPosition != MarketPosition.Long 
                          && ToTime(Time[0]) >= ToTime(removeOrderHr, removeOrderMin,0)
                          && LongEntry != null)
                      {
                          CancelOrder(LongEntry);
                          LongEntry = null;
                      }
      What would happen in this case if LongEntry's MarketPosition is not long, but LongEntry1's MarketPosition is currently long? Will this code still cancel LongEntry and set it back to null?

      Thank you very much for your help.

      Comment


        #4
        Originally posted by jg123 View Post
        Thank you very much! That really helps a lot!

        This does lead me to another question about how Position.MarketPosition works. First, though, I need to give a brief bit more about the strategy.

        LongEntry is the name of the order when I want to go long. But, if LongEntry is currently in a trade and the market sets up for another trade before LongEntry closes, then it is supposed to use the variable LongEntry1. If LongEntry & LongEntry1 are both in trades, then it is supposed to use LongEntry2, etc, etc, etc.

        It is highly likely that I will be in LongEntry1 but not in LongEntry due to LongEntry closing after it opened LongEntry1.

        Therefore, when I am testing for the criteria of: Position.MarketPosition != MarketPosition.Long, will this effect both LongEntry & LongEntry1?

        For example:

        Code:
                        if (Position.MarketPosition != MarketPosition.Long 
                            && ToTime(Time[0]) >= ToTime(removeOrderHr, removeOrderMin,0)
                            && LongEntry != null)
                        {
                            CancelOrder(LongEntry);
                            LongEntry = null;
                        }
        What would happen in this case if LongEntry's MarketPosition is not long, but LongEntry1's MarketPosition is currently long? Will this code still cancel LongEntry and set it back to null?

        Thank you very much for your help.
        The MarketPosition is for the entire strategy, not for particular orders. If you want to know the state of particular orders, you will need to track things much more closely using OnExecution() to set and reset boolean flags specific to each entry.

        Comment


          #5
          Hello jg123,

          Thank you for your posts.

          Koganam's assessments are correct here. If you want more info on Position.MarketPosition you can find it in our help guide at the following link: http://www.ninjatrader.com/support/h...7/position.htm

          Comment


            #6
            Thank you both for this information.

            I have been working on this a while in order to come up with a solution but I have not been able to yet.

            I will give a scenario first which should make much more clear my current question.

            I have 3 IOrders
            IOrder Entry1
            IOrder Entry2
            IOrder Entry3.

            If Entry1 is currently in an active trade, then use Entry2. If Entry1 & Entry2 are in active trades, then use Entry3.

            Let's assume that Entry1 is in an active trade. Therefore Entry2 has placed orders; but, because the market conditions were not right, those orders were not triggered and now, at the end of the day, it is time to cancel the orders and reset Entry2 to null.

            As brought to my attention earlier (thank you!), it should be tracked in OnExecution

            The question is, how do I track if Entry2 is in a currently active trade? Essentially i want to do this:

            OnExectution......
            If (Entry2 is not currently in an active trade && It is time to remove the orders)
            {
            CancelOrder(Entry2);
            Entry2 = null;
            }

            Thank you very much for your help. Please let me know if you need more information.

            Comment


              #7
              Hello jg123,

              Thank you for your post.

              The condition to check not filled would be the following:
              Code:
              if(entry2.OrderState != OrderState.Filled)

              Comment


                #8
                Thank you, Patrick. I really appreciate that.

                I am still running into an issue.

                I have placed the code into OnExecution and expect it now to cancel the order at a particular time, but it is not. Here is the code that I am using.

                Code:
                				if (LongEntry != null
                					&& ToTime(Time[0]) >= RemoveOrderHr
                					&& LongEntry.OrderState != OrderState.Filled)
                				{
                					CancelOrder(LongEntry);
                					LongEntry = null;
                				}
                I am also attaching a screenshot of the Output window and you will be able to see that the order is there well past the RemoveOrderHr (which is currently set to 19:00)

                Can you please let me know what I am doing wrong here and point me in the right direction to fix it?

                Thank you!!
                Attached Files

                Comment


                  #9
                  Hello jg123,

                  Thank you for your response.

                  I would recommend adding Print() statements when the condition returns true to see if it is calling the CancelOrder() at all. If it is not, please attach your .cs file for the strategy to your response so I may investigate this matter further.

                  For information on Print() please visit the following link: http://www.ninjatrader.com/support/h.../nt7/print.htm

                  Comment


                    #10
                    Hi Patrick

                    Thank you very much. I have just tried adding the Print statements and it appears that it is not being called; therefore, as you said, I am attaching the code.

                    the code is somewhat long.

                    I have an enum in this and currently I am working on TrendDirection.Up which is found on lines 142 - 173. The exit orders for this are found on lines 279 - 340 and the cancel order code is found in 342 - 366.

                    There is plenty of more code and naturally that could be causing the problem, but above are the areas that I am actively working on right now and hopefully that will help narrow it down for you.
                    Attached Files

                    Comment


                      #11
                      Hello jg123,

                      Thank you for your response.

                      Testing only the LongEntry of your strategy I cannot get trades to be produced in backtesting.

                      Can you provide details on the test you run? Such as instrument, From and To Dates, Period type and Interval?

                      Comment


                        #12
                        Oh okay.

                        Here is a screenshot of my chart that shows that trades are being placed on the code that i sent. I am also attaching a screenshot of the paramters that I am using. I hope that this helps

                        The chart range that I am using is from May 27 through current day
                        Attached Files

                        Comment


                          #13
                          Hello jg123,

                          Thank you for that information.

                          I was able to see orders placed, but the entries are failing. In your log do you see any messages similar to the following:
                          7/23/2014 1:34:10 PM|3|128|A Buy order placed at '6/2/2014 10:00:00 AM' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.

                          Comment


                            #14
                            That is interesting. I am actually not getting that error in my log. Here is a screen print for it. It is in German, but at the very least you can see that there are no yellow error mesasges. The only error was many hours ago and dealt with a connection loss and has nothing to do with the strategy. The 9:09 pm log messages are the ones currently dealing with this issue.
                            Attached Files

                            Comment


                              #15
                              Hi jg123,

                              OnExecution() is called when an order has filled.

                              If you are wanting to cancel an order when a time is reached, this will need to be done in OnBarUpdate(). This is because there isn't an order that has filled that will trigger OnExecution().

                              Therefore, lines 342 through 366 should be moved to OnBarUpdate().

                              If the order does fill, then you will want to set the IOrder handle for that order to null inside of OnExecution().
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

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