Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OCO limit orders (Buy Limit order and Sell Limit Order) on specific time (09:30 EST)

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

    OCO limit orders (Buy Limit order and Sell Limit Order) on specific time (09:30 EST)

    Hello Hello!

    I just need guidance with the strategy I'm currently working on. I need an OCO limit orders, (1 Buy Limit Order 20ticks above an indicator, and 1 Sell Limit Order 20ticks below the same Indicator) that cancels the other and place target and stop loss as either of it gets filled. But I need it to initiate only on the market open and stop the strategy once 1 trade is complete. I could not find any sample strategies that uses specific time and uses OCO limit orders of different direction. Hope someone can guide me.


    #2
    Hello jhudas88,

    Thank you for your post.

    Is your strategy using the Managed or Unmanaged approach? In the Managed approach, the only orders that use OCO are stops and targets placed via the Set() methods, such as SetStopLoss() and SetProfitTarget(). Otherwise, the Managed approach follows the "Internal Order Handling Rules that Reduce Unwanted Positions" found on this page:


    The order handling rules would prevent you from having both a buy limit and sell limit order in place; that page mentions one of the scenarios where methods that generate orders to enter a position will be ignored if, "The strategy position is flat and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction​."

    If you are using the Unmanaged approach, then you could use OCO for two potential entry orders. SubmitOrderUnmanaged() allows for an OCO string:Please let us know if we may be of further assistance.

    Comment


      #3
      I'm looking for an Unmanaged OCO. I found a sample from Chelsea B. Now, I need help placing the limit orders at exactly 9:30 EST. (RTH). So, place the order only when the RTH session opens and cancel what was not filled. I found a way to set targets and stops too.

      Comment


        #4
        hi i am interested in this aswell. Can you perhaps share a sample script?

        Comment


          #5
          Hello jhudas88,

          Below is a link to an example of OCO for entry orders using the unmanaged approach.


          As well as an example of placing the exit stop and limit with OCO when the entry fills in OnExecutionUpdate() in the unmanaged approach.


          Use time conditions to submit an order at a specific time.


          Save the order to a variable in OnOrderUpdate(), and then supply the variable to CancelOrder() to cancel an order.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi, thanks alot for the scripts. I tried to implement it to my trade idea, and i have come quite far, but there is one piece i do not quite understand.

            i replaced the "int quantity" in the "SubmitOrderUnmanaged" commands (entry order, take profit order and stop loss order) from "1" to an int variable "Q1" which i allow the user to change in the properties section.

            Now, if i choose "1", the code works just fine, but if if choose more than "1", for example "2", or "3", the code enters the right amount of entry orders, but doubles the amount of take profit and stop loss orders.

            Do you perhaps have a solution for this?


            Comment


              #7
              Hello konganda,

              It may be a mistake in the logic.

              Are you using Q1 as the quantity for the SubmitOrderUnmanaged calls for the stop and limit order in OnExecutionUpdate()?

              Have you enabled TraceOrders and printed the order.ToString() object in OnOrderUpdate() to see if multiple stop and limit orders are being submitted?


              Please save this output to a text file (right-click the output window > select Save as) and attach the .txt file to your next post.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                yes, exactly, this is the respective script section:

                protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                Cbi.MarketPosition marketPosition, string orderId, DateTime time)
                {
                // if the long entry filled, place a profit target and stop loss to protect the order
                if (longStopEntry != null && execution.Order == longStopEntry)
                {
                // generate a new oco string for the protective stop and target

                ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
                // submit a protective profit target order
                SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, Q_one, (execution.Order.AverageFillPrice + Target * TickSize), 0, ocoString, "longProfitTarget");
                // submit a protective stop loss order
                SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, Q_one, 0, (execution.Order.AverageFillPrice - StopLoss * TickSize), ocoString, "longStopLoss");
                trade_was_activated = true;

                }

                // reverse the order types and prices for a short
                else if (shortStopEntry != null && execution.Order == shortStopEntry)
                {
                ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
                //SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.Limit, Q_one, (Low[0] - 20 * TickSize), 0, ocoString, "shortProfitTarget");
                //SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.StopMarket, Q_one, 0, (High[0] + 10 * TickSize), ocoString, "shortStopLoss");
                //Print("Execution.Order: " + execution.Order);
                SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.Limit, Q_one, (execution.Order.AverageFillPrice - Target * TickSize), 0, ocoString, "shortProfitTarget");
                SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.StopMarket, Q_one, 0, (execution.Order.AverageFillPrice + StopLoss * TickSize), ocoString, "shortStopLoss");
                trade_was_activated = true;
                }

                // I didn't use Order variables to track the stop loss and profit target, but I could have
                // Instead, I detect the orders when the fill by their signalName
                // (the execution.Name is the signalName provided with the order)

                // when the long profit or stop fills, set the long entry to null to allow a new entry
                else if (execution.Name == "longProfitTarget" || execution.Name == "longStopLoss" || execution.Name == "shortProfitTarget" || execution.Name == "shortStopLoss")
                {
                longStopEntry = null;
                shortStopEntry = null;
                }
                }


                i have enabled traceorders now and printed it.. please find attached.
                Attached Files

                Comment


                  #9
                  although i believe this might be due to the market data... when there is high volatility, it seems to have difficulties to place the right take profit orders after initial order fill. on some days it works fine, on other days, it does not

                  Comment


                    #10
                    Hello konganda,

                    Please enable TraceOrders in State.Configure and reload the script.

                    Reproduce the behavior, and then provide the output text file.

                    Let me know at what time the behavior occurred in the output file.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello konganda,

                      In the output you have provided, the shortStopEntry order part filled with a quantity of 1, and then part filled again with a quantity of 1.

                      orderId='267092b69c9441d8b3228673bde77c8a' account='Playback101' name='shortStopEntry' orderState=PartFilled instrument='NQ JUN24' orderAction=SellShort orderType='Stop Market' limitPrice=0 stopPrice=18320.5 quantity=2 tif=Gtc oco='unmanagedentryoco1036058241' filled=1 averageFillPrice=18320.25 onBehalfOf='' id=89450 time='2024-04-08 15:30:18' gtd='2099-12-01' statementDate='2024-04-08'

                      orderId='267092b69c9441d8b3228673bde77c8a' account='Playback101' name='shortStopEntry' orderState=Filled instrument='NQ JUN24' orderAction=SellShort orderType='Stop Market' limitPrice=0 stopPrice=18320.5 quantity=2 tif=Gtc oco='unmanagedentryoco1036058241' filled=2 averageFillPrice=18320.125 onBehalfOf='' id=89450 time='2024-04-08 15:30:18' gtd='2099-12-01' statementDate='2024-04-08'

                      The shortProfitTarget order was submitted twice, each time a part fill occurred, with each order having a quantity of 2, for a total quantity of 4.

                      orderId='c3d98e2639394b53992fd5356791eb5e' account='Playback101' name='shortProfitTarget' orderState=Submitted instrument='NQ JUN24' orderAction=BuyToCover orderType='Limit' limitPrice=18315.25 stopPrice=0 quantity=2 tif=Gtc oco='unmanageexitdoco1036079905' filled=0 averageFillPrice=0 onBehalfOf='' id=89453 time='2024-04-08 15:30:18' gtd='2099-12-01' statementDate='2024-04-08'

                      orderId='125bd17397414fb5960cebb66c0a9321' account='Playback101' name='shortStopLoss' orderState=Submitted instrument='NQ JUN24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=18420.25 quantity=2 tif=Gtc oco='unmanageexitdoco1036079905' filled=0 averageFillPrice=0 onBehalfOf='' id=89454 time='2024-04-08 15:30:18' gtd='2099-12-01' statementDate='2024-04-08'

                      Is this what you are intending for a part filled order?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        not really, i intended it to fill a shortStopEntry of quantity = 2, and also the ShortProfitTarget to fill at quantity = 2.

                        Comment


                          #13
                          I believe it might have something to do with high volatility events. Sometimes it works, sometimes it does not. I intend to compensate for this by adding a script add-on, that flattens everything as soon as my dailypnl (unrealized and realized) reaches my max pnl (which i will set equal to the intended profit i would get if the ProfitTarget is hit).

                          Do you perhaps know of a sample script, that reliably flattens all orders in unmanaged mode?

                          Comment


                            #14
                            Hello konganda,

                            If the logic is not prepared for handling partial fills of the entry order properly, I would recommend you wait until the order is fully filled before submitting the exit orders.

                            The better approach would be to correct the logic to submit the exit orders on the entry part fill, then check if the orders are working and do a changeorder to change the quantity for each additional part fill of the entry, instead of sending a new order for each part fill of the entry.

                            I am not aware of a sample script that flattens all orders in unmanaged mode. However, this wouldn't help the script place the correct amount of orders or quantity for part filled entries.
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by NullPointStrategies, Yesterday, 05:17 AM
                            0 responses
                            56 views
                            0 likes
                            Last Post NullPointStrategies  
                            Started by argusthome, 03-08-2026, 10:06 AM
                            0 responses
                            132 views
                            0 likes
                            Last Post argusthome  
                            Started by NabilKhattabi, 03-06-2026, 11:18 AM
                            0 responses
                            73 views
                            0 likes
                            Last Post NabilKhattabi  
                            Started by Deep42, 03-06-2026, 12:28 AM
                            0 responses
                            45 views
                            0 likes
                            Last Post Deep42
                            by Deep42
                             
                            Started by TheRealMorford, 03-05-2026, 06:15 PM
                            0 responses
                            49 views
                            0 likes
                            Last Post TheRealMorford  
                            Working...
                            X