Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel order at a specific time

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

    Cancel order at a specific time

    Hi, I have a strategy where I want orders to be placed only within a given time range. If orders are not filled within that time period they have to be canceled.
    if (BarsInProgress == 0)
    {
    entryValue = High[0] + 5*TickSize;
    if (Position.MarketPosition == MarketPosition.Flat && entryOrder == null && TradingTime(Time[0]))
    {
    EnterLongStopMarket(1, true, DefaultQuantity, entryValue, "long stop entry");
    }
    }
    }
    where
    private bool TradingTime(DateTime time)
    {
    if (ToTime(time)>=StartTime && ToTime(time)<=StopTime)
    {
    return true;
    }
    return false;
    }
    The isssue is the following: if I put TradingTime between 110000 and 112500 (using NQ 10 min bars and secondary DataSeries 1 Tick), I'd expect he EntryLongStop order to be placed at 11:00:00. What instead of that is happening, is that the oreder is placed at 10:59:59 and therefore immediately canceled as 10:59:59 is out of the 110000 - 112500 range:
    if (BarsInProgress == 1)
    if (entryOrder != null && TradingTime(Time[0]) == false)
    {
    CancelOrder(entryOrder);
    Print( TradingTime(Time[0])+ " CancelOrder Trading time is: " + ToTime(Time[0]));
    }

    I am checking what time the order has been submitted (backest) with OnOrderUpdate()
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    // Checks for all updates to entryOrder.
    if (entryOrder == null && order.Name == "long stop entry")
    {
    entryOrder = order;
    Print("Order accepted at " + time.ToString());
    }

    The printout is the following:
    Click image for larger version  Name:	image.png Views:	0 Size:	2.5 KB ID:	1303960
    Could you please help me to understand why the order is sumbitted before the TradingTime() condition, starting at 110000, is true?

    Thank you

    Martin


    PS
    attached the strategy

    I am using NT8, please move my reply if needed. Just did not wat to open a new topic as the subject is similar
    Last edited by martin70; 05-17-2024, 05:39 AM.

    #2
    Hello martin70,

    In the future please post any NT8 questions under the NinjaTrader desktop subforum, I have moved your post. Just as a heads up if you find information in the NT7 forum that is similar to your question just make a new post in the NT8 forums and provide a link. We could check if that is at all relevant to your question or not, most NT7 threads will not likely be helpful or relate to what you see happening in NT8 due to a lot being changed in NT8.

    Regarding the time check, since you are looking for a specific time and not a range of times do you still see this happen if you change to only checking if the time is exactly 11?

    Comment


      #3
      Hi,
      the code is (see also previous message)
      if (ToTime(time)>=StartTime && ToTime(time)<=StopTime)
      therefore when StartTime is set 110000, with OnBarUpdate the order is placed at 105959 what should not be the case.

      Comment


        #4
        Hello martin70,

        Do you see any change if you use

        Code:
        if (ToTime(time) == StartTime)

        Comment


          #5
          Hello Jesse,
          I tried to put ​
          if (ToTime(time) == StartTime)
          The printout I get is exactly the same:
          Click image for larger version

Name:	image.png
Views:	77
Size:	2.5 KB
ID:	1304030
          Attached Files

          Comment


            #6
            Hello martin70,

            Please try exiting NinjaTrader and then sync your PC clock. After doing that re open NinjaTrader and reload all historical data for the instrument you are trying to use. Do you still see the same?

            Comment


              #7
              Hi, I hoped that could be the issue, but no: same output:

              Click image for larger version  Name:	image.png Views:	0 Size:	3.1 KB ID:	1304041
              For any test I attach the strategy script​

              Comment


                #8
                Hello martin70,

                Thank you for providing a sample, I will review this and check what may be causing that with this sample. When I have more details I will reply back here.

                Comment


                  #9
                  Thank you. For the test I used NQ 10 min bars

                  Comment


                    #10
                    Hi, may I ask if you have any news? I'd like to know if this is an error and how to fix it, or if I have to program a way around this strange oucome. Thank you, Martin

                    Comment


                      #11
                      Hello martin70,

                      At this time I don't have anything additional that I can relay, development is currently reviewing the case and once they provide notes I will post that information back here.

                      Comment


                        #12
                        Hi, it's 1 month ago.. may I ask if you please have at least a fist update? Thank you very much.
                        Best regards
                        Martin

                        Comment


                          #13
                          Hello martin70,

                          I checked the issue and it looks like development rejected the issue because this is expected as per how multiseries NinjaScript is processed historical. This was the information that was provided.



                          'Understanding the sequence in which bars series process and the granularity provided by market data vendors is essential for efficient multi-series development. Let’s assume we have two series (primary and secondary) in our script, which is representing the same instrument, yet different intervals. During historical data processing, NinjaTrader updates the two series strictly according to their timestamps, calling the primary bar series of the corresponding timestamps first, and then calling the secondary series'
                          • example (to make it easier I go with 1 second = 1 tick data setup):

                          Times[0][0]=2024-05-31 10:45:00.000 Times[1][0]=2024-05-31 10:59:57.052 BarsInProgress=1
                          Times[0][0]=2024-05-31 10:45:00.000 Times[1][0]=2024-05-31 10:59:58.072 BarsInProgress=1
                          Times[0][0]=2024-05-31 10:45:00.000 Times[1][0]=2024-05-31 10:59:59.048 BarsInProgress=1
                          Times[0][0]=2024-05-31 11:00:00.000 Times[1][0]=2024-05-31 10:59:59.048 BarsInProgress=0 <-- this is call to OnBarUpdate where order is submitted
                          Times[0][0]=2024-05-31 11:00:00.000 Times[1][0]=2024-05-31 11:00:00.020 BarsInProgress=1
                          Times[0][0]=2024-05-31 11:00:00.000 Times[1][0]=2024-05-31 11:00:01.000 BarsInProgress=1
                          Times[0][0]=2024-05-31 11:00:00.000 Times[1][0]=2024-05-31 11:00:02.116 BarsInProgress=1
                          • EnterLong is triggered on BarsInProgress == 0
                          • so when we have Time[0] == 11:00:00 AM, Time[1] is processed up to 10:59:59.048 → expected

                          pls. note that this is the historical case. in case of realtime processing order would be submitted when the first tick >= 10:45:00.000 AM. this tick (e.g. 10:45:00.456 AM) would build 11:00:00 AM bar and submit order on next OnBarUpdate. this is also expected

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by NullPointStrategies, Today, 05:17 AM
                          0 responses
                          44 views
                          0 likes
                          Last Post NullPointStrategies  
                          Started by argusthome, 03-08-2026, 10:06 AM
                          0 responses
                          126 views
                          0 likes
                          Last Post argusthome  
                          Started by NabilKhattabi, 03-06-2026, 11:18 AM
                          0 responses
                          65 views
                          0 likes
                          Last Post NabilKhattabi  
                          Started by Deep42, 03-06-2026, 12:28 AM
                          0 responses
                          42 views
                          0 likes
                          Last Post Deep42
                          by Deep42
                           
                          Started by TheRealMorford, 03-05-2026, 06:15 PM
                          0 responses
                          46 views
                          0 likes
                          Last Post TheRealMorford  
                          Working...
                          X