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

Canceling limit orders at end of session

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

    Canceling limit orders at end of session

    Hi,

    I have a strategy that I'm working on that places a limit order. The problem that I'm having is that when I initiate the strategy, it will place the limit order from the previous session and if the market is trading in that range after hours, the order will be filled. I only want the strategy to trade the US Day session.

    I'm also using calculate on bar close = true, so the last bar of the session isn't updated until the first bar of the next session.

    Thanks,

    Rich
    tradethepulse.com

    #2
    richp6,

    What is your setting in Tools > Options > Strategies > NinjaScript > "On starting a real-time strategy" section?
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      "Wait until flat before executing live".

      The other thought I had though is that even if I have the strategy running and the market closes, if the strategy doesn't cancel all open orders, then it could still be triggered after hours.

      Is there a simple way to find the next to last bar in a session? Then I could just cancel any open orders when that bar closes. Since the strategy could be used in different session templates, there is no way for me to know ahead of time what time of day is the next to last session bar.

      Thanks,

      Rich

      Comment


        #4
        rich,

        There is no supported way to do this currently. Some people have had success keeping track of their own session endings as well as holiday days.

        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Adam,

          This is what I ended up doing...

          if (Bars.FirstBarOfSession)
          {
          Bars.Session.GetNextBeginEnd(BarsArray[0], 0, out sessionBegin, out sessionEnd);
          }
          s_end = ToTime(Time[0]) + 4100; //this will effectively add 1 minute to the current 1 minute bar chart
          if (s_end == ToTime(sessionEnd))
          {
          if (myOrder.OrderState != OrderState.Filled)
          CancelOrder(myOrder);
          }

          Seems to work fine in real-time and backtesting through strategy analyzer.

          Comment


            #6
            CancelOrder() still not working on Limit Orders.

            Originally posted by richp6 View Post
            Adam,

            This is what I ended up doing...

            if (Bars.FirstBarOfSession)
            {
            Bars.Session.GetNextBeginEnd(BarsArray[0], 0, out sessionBegin, out sessionEnd);
            }
            s_end = ToTime(Time[0]) + 4100; //this will effectively add 1 minute to the current 1 minute bar chart
            if (s_end == ToTime(sessionEnd))
            {
            if (myOrder.OrderState != OrderState.Filled)
            CancelOrder(myOrder);
            }

            Seems to work fine in real-time and backtesting through strategy analyzer.
            richP6,

            CancelOrder() of LimitOrders does not seem to be working. My strategy stops trading when I "reset" (CancelOrder()) the orders on Bars.FirstBarOfSession.

            Even after I make them null (myOrder = null) in OnOrderUpdate or OnExecution.

            Not sure what's going on.

            Comment


              #7
              carlosavellan,

              What is your syntax for this?

              You say after setting them to null and cancelling doesn't work. Is this what you are doing, nulling them first then cancelling?
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                Cal,
                I think I solved it, a few of the corrections without going into much detail:

                First, I make sure all orders, including target and stop, are null at the first bar of session. If not, I cancel them:

                Code:
                if (Bars.FirstBarOfSession)
                {
                  if (myOrder!=null)
                    CancelOrder(myOrder);
                
                  if (stopOrder!=null)
                	CancelOrder(stopOrder);
                				
                  if (targetOrder!=null)
                	CancelOrder(targetOrder);
                }
                Then, in OnOrderUpdate if Cancel is the orderstate I assign the "null".

                Also, before executing the LimitOrders (Target and Stop) I checked they were both "null" as a condition.

                And finally, I reset in OnExecution once the position was closed

                Code:
                if ( (stopOrder != null && stopOrder == execution.Order) ||  (targetOrder != null && targetOrder == execution.Order) )
                {
                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                {
                	stopOrder = null;
                	targetOrder = null;
                }
                }

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Option Whisperer, Today, 09:55 AM
                0 responses
                4 views
                0 likes
                Last Post Option Whisperer  
                Started by geddyisodin, 04-25-2024, 05:20 AM
                8 responses
                58 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by halgo_boulder, 04-20-2024, 08:44 AM
                2 responses
                22 views
                0 likes
                Last Post halgo_boulder  
                Started by mishhh, 05-25-2010, 08:54 AM
                19 responses
                6,189 views
                0 likes
                Last Post rene69851  
                Started by gwenael, Today, 09:29 AM
                0 responses
                5 views
                0 likes
                Last Post gwenael
                by gwenael
                 
                Working...
                X