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

Entry Orders cancelled after 5 minutes...

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

    Entry Orders cancelled after 5 minutes...

    Hi

    i run my strategy in a 5 minutes basis, i enter long or short with a limit order, calculateonbarclose=true

    i write the code to say, "if the order is not filled during the 5 minutes, keeps it but cancel it after the 3 bar if it is not filled (15 minutes in total)"

    but if the entry order is not filled after the 1st 5 minute, it is cancelled...

    here my code (as shown on the guide NT7), can you help to tell me what's wrong ?

    // Condition buy
    if ( entryOrderAchat == null && buycondition)

    {
    entryOrderAchat = EnterLongLimit(
    0, true, 1, Close[0], "Buy");
    barNumberOfOrderAchat = CurrentBar;
    }

    // If entryOrder has not been filled within 3 bars, cancel the order.
    elseif (entryOrderAchat != null && CurrentBar > barNumberOfOrderAchat + 3)
    {
    // When entryOrder gets cancelled below in OnOrderUpdate(), it gets replaced with a Market Order via EnterLong()
    CancelOrder(entryOrderAchat);
    }

    #2
    Hi Thomas79,

    Code looks good from here for what you're looking to do. Is there any reason provided for cancelling this order in TraceOrders output? Enable it in Initialize() section and then check output in Tools > Output Window

    Try adding something like below to check if it's cancelling due to your custom logic or if it's something else:

    Code:
    else if (entryOrderAchat != null && CurrentBar > barNumberOfOrderAchat + 3)
    {
    Print("CB: " + CurrentBar);
    Print("BNO: " + barNumberOfOrderAchat);
    // When entryOrder gets cancelled below in OnOrderUpdate(), it gets replaced with a Market Order via
    EnterLong()
    CancelOrder(entryOrderAchat);
    }
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      i recheck the code example... and i this i forget this part of code

      protectedoverridevoid OnOrderUpdate(IOrder order)
      {
      // Checks for all updates to entryOrder.
      if (entryOrder != null && entryOrder == order)
      {
      // Check if entryOrder is cancelled.
      if (order.OrderState == OrderState.Cancelled)
      {
      // Reset entryOrder back to null
      entryOrder = null;

      // Replace entry limit order with a market order.
      mar****rder = EnterLong(1, "market order");
      }
      }
      }

      Comment


        #4
        Hi, can you please help with programming "Cancel order" in NT Strategy? I have several condition sets in a Strategy and only for 1 of them I need to program Cancel order for a limit order (the other sets have market orders). So far I have:

        EnterLongLimit(0, true, DefaultQuantity, Close[0] + -2 * TickSize, "Long2v");

        Now the order is active till it is filled. I need to cancel this order after:
        1/ 20 minutes or 5 bars

        Can you please advise where and how to place the script for cancelling?

        Thanks a lot

        Comment


          #5
          Hi paatech,

          You can see a sample on CancelOrder() here:
          When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until


          This sample cancels the order if not filled within 3 bars, so you should be able to adapt to your needs.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_RyanM View Post
            Hi paatech,

            You can see a sample on CancelOrder() here:
            When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until


            This sample cancels the order if not filled within 3 bars, so you should be able to adapt to your needs.
            Thanks a lot, I trtied hard, but still have not managed to complete the set. Would you mind and completed the script for me? This would be great, many thanks. I need to cancel the order after 5 bars or 20 minutes. The condition set is:

            // Condition set 6 "Short 2v"
            if (CCI(14)[0] < 0
            && CCI(14)[1] < 0
            && CCI(50)[1] < 0
            && CCI(14)[1] > -100
            && CCI(50)[1] > -100
            && CCI(14)[2] < CCI(14)[1]
            && CCI(14)[0] < CCI(14)[1] + -5
            && CCI(50)[2] < CCI(50)[1]
            && CCI(50)[0] < CCI(50)[1] + -5
            && ToTime(Time[0]) >= ToTime(15, 40, 0)
            && ToTime(Time[0]) <= ToTime(18, 0, 0))
            {
            EnterShortLimit(0, true, DefaultQuantity, Close[0] + -2 * TickSize, "Short2v");
            }
            Last edited by paatech; 08-16-2012, 01:23 PM.

            Comment


              #7
              Unfortunately we are not able to custom code as it would limit our ability to provide support to everyone needing it. We're happy to help with any specific questions you have and if you would like professional custom coding development you can look into our 3rd party NinjaScript consultants here:

              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Cancelling IOrder with multiple instruments

                I have a functioning multi instrument strategy that I need to cancel any unfilled limit entry orders when the price has moved a set distance away from it. This is your standard IOrder script.... how do I incorporate the barsinprogress index to the private IOrder "myEntryOrder"?




                private IOrder myEntryOrder = null;
                private int barNumberOfOrder = 0;

                protected override void OnBarUpdate()
                {
                // Submit an entry order at the low of a bar
                if (myEntryOrder == null)
                {
                myEntryOrder = EnterLongLimit(0, true, 1, Low[0], "Long Entry");
                barNumberOfOrder = CurrentBar;
                }

                // If more than 5 bars has elapsed, cancel the entry order
                if (CurrentBar > barNumberOfOrder + 5)
                CancelOrder(myEntryOrder);
                }


                Regards to all and thx
                Last edited by elliot5; 07-14-2015, 12:32 PM.

                Comment


                  #9
                  Hello everington_f,

                  How you would apply a BarsInProgress check depends on what you are trying to do. Could you please be more specific as to what you are trying to accomplish so we can better assist?

                  If you are submitting and checking the order against the primary data series it might look something like this:
                  Code:
                  private IOrder myEntryOrder = null;
                  private int barNumberOfOrder = 0;
                  
                  protected override void OnBarUpdate()
                  {
                      if(BarsInProgress==0) //If the primary data series is calling OnBarUpdate
                      {
                          // Submit an entry order at the low of a bar
                          if (myEntryOrder == null)
                          {
                              myEntryOrder = EnterLongLimit(0, true, 1, Low[0], "Long Entry");
                              barNumberOfOrder = CurrentBar;
                          }
                          // If more than 5 bars has elapsed, cancel the entry order
                          if (CurrentBar > barNumberOfOrder + 5)
                              CancelOrder(myEntryOrder);
                      }
                  }
                  More information on using BarsInProgress can be found in our help guide here: http://ninjatrader.com/support/helpG...inprogress.htm

                  Thank you in advance.
                  Michael M.NinjaTrader Quality Assurance

                  Comment


                    #10
                    I have an array of 50 stocks and by entering longlimit get current bid I might find that the market moves away from my limit order and therefore needs to be cancelled. any working entry orders from that array need to be cancelled when the market moves 10 ticks away from the enterlimit level. Thats why i need barsinprogress incorporated into the IOrder to create the loop .

                    Comment


                      #11
                      Hello everington_f,

                      If you keep track of which orders are in which data series, then you know which orders to cancel when the condition is met. Here is an example using a List containing another List with IOrders. The first List is indexed by the BarsInProgress so we know which data series we are working with. The second List is indexed by the number of orders that data series contains:
                      Code:
                      #region Using declarations
                      //...
                      using System.Collections.Generic;
                      //...
                      #endregion
                      //...
                      #region Variables
                      	private List<List<IOrder>> orderList = new List<List<IOrder>>();
                      	private IOrder myEntryOrder = null;
                      	private int[] barNumberOfOrder;
                      #endregion
                      protected override void OnBarUpdate()
                      {
                      	if (orderList[BarsInProgress].Count == 0) //Only have one order per stock
                      	{
                      		orderList[BarsInProgress].Add(EnterLongLimit(0, true, 1, Low[0], "Long Entry"));
                      		barNumberOfOrder[BarsInProgress] = CurrentBar;
                      	}
                      	// If more than 5 bars has elapsed, cancel the entry order
                      	if (barNumberOfOrder[BarsInProgress] != null && CurrentBar > barNumberOfOrder[BarsInProgress] + 5)
                                  CancelOrder(orderList[BarsInProgress][0]); //Cancel the entry order
                      }
                      Please provide an example from your actual code if you would like a more specific example of what you are looking for.
                      Thank you in advance.
                      Michael M.NinjaTrader Quality Assurance

                      Comment


                        #12
                        I have taken your suggestion and have built it into the strategy. Many thanks for you help

                        Comment


                          #13
                          Hello everington_f,

                          Thank you for the update. I am glad to hear I could be of assistance.

                          Please let me know if you have any questions anytime.
                          Michael M.NinjaTrader Quality Assurance

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Barry Milan, Yesterday, 10:35 PM
                          5 responses
                          16 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by DanielSanMartin, Yesterday, 02:37 PM
                          2 responses
                          13 views
                          0 likes
                          Last Post DanielSanMartin  
                          Started by DJ888, 04-16-2024, 06:09 PM
                          4 responses
                          12 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Started by terofs, Today, 04:18 PM
                          0 responses
                          11 views
                          0 likes
                          Last Post terofs
                          by terofs
                           
                          Started by nandhumca, Today, 03:41 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post nandhumca  
                          Working...
                          X