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

How to cancel submitted orders if not filled in certain time?

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

    How to cancel submitted orders if not filled in certain time?

    Hey all,

    Is anyone familiar with how to programmatically cancel a submitted order if it isn't filled in a certain time? By default, orders are canceled when the next bar is drawn, but I'm looking to cancel an order if it isn't filled in 30 seconds.

    I presume we'd open the position with an isLiveUntilCancelled parameter, such as EnterLongLimit(), and leverage the Time property in the Order class, but I'm stuck after that. Any examples or help that anybody could point me to would be greatly appreciated.

    Thank you!

    #2
    Hello nchingro,

    Welcome to the NinjaTrader support forums.

    We have a sample of using Cancel order to cancel an order later in the following link: https://ninjatrader.com/support/help...thod_to_ca.htm

    How you determine when to cancel the order would really be the part you need to work on. You could do that in a variety of ways but really depends on what settings you are going to run the script with. If you were using a 30 second period for the chart you could just use the OnBarUpdate events and a variable. If you are using other timeframes you could still accomplish 30 seconds so long as you have enough granularity to do that, for example 1 second series or 10 second series both would land on 30 second intervals so that could be found.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello nchingro,

      Welcome to the NinjaTrader support forums.

      We have a sample of using Cancel order to cancel an order later in the following link: https://ninjatrader.com/support/help...thod_to_ca.htm

      How you determine when to cancel the order would really be the part you need to work on. You could do that in a variety of ways but really depends on what settings you are going to run the script with. If you were using a 30 second period for the chart you could just use the OnBarUpdate events and a variable. If you are using other timeframes you could still accomplish 30 seconds so long as you have enough granularity to do that, for example 1 second series or 10 second series both would land on 30 second intervals so that could be found.


      I look forward to being of further assistance.
      Thank you, Jesse. What if I'm using a higher time frame, like 15 or 30 mins? My initial thought was to get the DateTime from a non-filled, open order and calculate the difference between its submission time and the local machine's current time (assuming an order's submission date is programmatically exposed).

      From your response, however, if I'm using a higher time frame, would I have to add a 30-sec data series (via AddDataSeries()) and mirror the barNumberOfOrder / CurrentBar method detailed in the example you referenced? Something like the following (using example from SampleCancelOrder.cs):

      Code:
      OnStateChange()
      {[INDENT][B]// Add 15-minute and 30-second bars[/B][/INDENT][INDENT]else if (State == State.DataLoaded)
      {[/INDENT][INDENT=2]AddDataSeries(Data.BarsPeriodType.Minute, 15);
      AddDataSeries(Data.BarsPeriodType.Second, 30);[/INDENT][INDENT]}[/INDENT]
        }
      
      OnBarUpdate()
      {[INDENT]// Check IOrder objects for null to ensure there are no working entry orders before submitting new entry order
      if (entryOrder == null && mar****rder == null && Close[0] > Close[1])
      {[/INDENT][INDENT=2]/* Offset 5 ticks below the low to try and make the order not execute to demonstrate the CancelOrder() method. */
      EnterLongLimit(0, true, 1, Low[0] - 5 * TickSize, "long limit entry");
      
      // Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
      barNumberOfOrder = CurrentBars[2]; [B]//use 2 as the index because the primary data from UI is index 0 and the 15-min series is index 1[/B][/INDENT][INDENT]}
      
      // If entryOrder has not been filled within 1 30-sec bar, cancel the order.
      else if (entryOrder != null && CurrentBars[2] > barNumberOfOrder)
      {[/INDENT][INDENT=2]CancelOrder(entryOrder);[/INDENT][INDENT]}[/INDENT]
       }
      Or would another approach be better? Please let me know if you need any clarification. Thanks, Jesse!
      Last edited by nchingro; 08-24-2020, 04:56 PM.

      Comment


        #4
        Literally doing the same thing - this is the best way to do it - gives me no issues:

        if (CurrentBars[2] > barNumberOfOrder + 1)
        CancelOrder(entryOrder);


        As long as you have entry order defined properly as the actual Order element within this (below), you should be good:

        protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
        double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)

        if (order.Name == "long limit entry")
        entryOrder= order;


        I'm not sure you have the order defined, or you didn't include the text, and so CancelOrder would do nothing since "entryOrder" has null value still.

        Comment


          #5
          Thank you, Jesse. What if I'm using a higher time frame, like 15 or 30 mins? My initial thought was to get the DateTime from a non-filled, open order and calculate the difference between its submission time and the local machine's current time (assuming an order's submission date is programmatically exposed).

          From your response, however, if I'm using a higher time frame, would I have to add a 30-sec data series (via AddDataSeries()) and mirror the barNumberOfOrder / CurrentBar method detailed in the example you referenced? Something like the following (using example from SampleCancelOrder.cs):
          You would still need a way to execute the logic quicker than 15 or 30 minutes if the intention is to cancel after 30 seconds. You would have to be running OnEachTick or use a 30 second series in addition to the 15 and 30 minute series. With adding a 30 second series that type of logic would work for the purpose but because you are storing the secondary series bar and then checking if the new secondary series bar is greater than a pre defined amount.






          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Originally posted by lmatiukas View Post
            Literally doing the same thing - this is the best way to do it - gives me no issues:

            if (CurrentBars[2] > barNumberOfOrder + 1)
            CancelOrder(entryOrder);
            Originally posted by NinjaTrader_Jesse View Post

            You would have to be running OnEachTick or use a 30 second series in addition to the 15 and 30 minute series. With adding a 30 second series that type of logic would work for the purpose but because you are storing the secondary series bar and then checking if the new secondary series bar is greater than a pre defined amount.
            Thanks for your help, guys! Verified this after knocking out some additional bugs.

            Comment


              #7
              How can we use Time[0] instead of bar reference for more precision?

              Could we adjust nchingro example with:
              timeOfOrder = Time[0];

              or

              timeOfOrder = Time[0].TimeOfDay;


              and
              else if (entryOrder != null && Time[0] >= timeOfOrder.AddSeconds(30) )
              {
              CancelOrder(entryOrder);
              }


              Code:
              OnStateChange()
              {[INDENT]// Add 15-minute and 30-second bars[/INDENT][INDENT]else if (State == State.DataLoaded)[/INDENT][INDENT]{[/INDENT][INDENT]AddDataSeries(Data.BarsPeriodType.Minute, 15);[/INDENT][INDENT]AddDataSeries(Data.BarsPeriodType.Second, 30);[/INDENT][INDENT]}[/INDENT]
               
                }
              
              OnBarUpdate()
              {[INDENT]// Check IOrder objects for null to ensure there are no working entry orders before submitting new entry order[/INDENT][INDENT]if (entryOrder == null && mar****rder == null && Close[0] > Close[1])[/INDENT][INDENT]{[/INDENT][INDENT=2]/* Offset 5 ticks below the low to try and make the order not execute to demonstrate the CancelOrder() method. */[/INDENT][INDENT=2]EnterLongLimit(0, true, 1, Low[0] - 5 * TickSize, "long limit entry");[/INDENT]
               [INDENT=2]
              // get the timestamp of time of Order Entry[/INDENT][INDENT=2]timeOfOrder = Time[0];[/INDENT][INDENT]}[/INDENT]
               [INDENT]
              // If entryOrder has not been filled within 30 seconds from entry time, cancel the order.[/INDENT][INDENT]else if (entryOrder != null && Time[0] >= timeOfOrder.AddSeconds(30) )[/INDENT][INDENT]{[/INDENT][INDENT=2]CancelOrder(entryOrder);[/INDENT][INDENT]}[/INDENT]
               
                }
              Can we use the AddSeconds() method or another method with milliseconds?

              Else how to achieve the same result with time reference?

              Comment


                #8
                Hello Cormick,

                I am not entirely sure I understand the question, are you asking how to use the orders fill time instead of the bar time for more accuracy?

                You can do all of the items you asked with C# DateTime objects, you can add seconds to the time or milliseconds.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jesse,

                  Thanks a lot for the answer!

                  I'll test it later. And be back asap with results.

                  Be well!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by bortz, 11-06-2023, 08:04 AM
                  47 responses
                  1,603 views
                  0 likes
                  Last Post aligator  
                  Started by jaybedreamin, Today, 05:56 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post jaybedreamin  
                  Started by DJ888, 04-16-2024, 06:09 PM
                  6 responses
                  18 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by Jon17, Today, 04:33 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post Jon17
                  by Jon17
                   
                  Started by Javierw.ok, Today, 04:12 PM
                  0 responses
                  12 views
                  0 likes
                  Last Post Javierw.ok  
                  Working...
                  X