Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

2 entry orders help!

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

    Hello bobperez,

    In the UnmanagedOCOBracketExample_NT8 note I've used OrderType.StopMarket orders.
    A buy stop order would be placed above the ask, a sell stop order would be placed below the bid.

    In this code you have chosen to use OrderType.Limit which are placed on the reverse side of the market.
    A buy limit would be placed below the ask, a sell limit would be placed above the bid.

    I see in your code you've submitted the buy limit at the High of the most recently closed bar and the sell limit at the Low of the most recently closed bar. These orders will most likely be on the wrong side of the market when the new bar opens.
    With some brokerages that order will be immediately filled (including NinjaTrader), with other brokerages that order would be rejected (such as Forex brokerages).

    Also, orders should be assigned to variables in OnOrderUpdate() and not directly from the order method.

    I've changed is and tested the suggested code and I am finding this is working.

    Below is a link to a video of the test.

    BobperezTest_NT8.zip

    There are no exit orders or setting the variables back to null so it only places orders once, but this works as expected.
    Chelsea B.NinjaTrader Customer Service

    Comment


      Originally posted by NinjaTrader_ChelseaB View Post
      Hello bobperez,

      In the UnmanagedOCOBracketExample_NT8 note I've used OrderType.StopMarket orders.
      A buy stop order would be placed above the ask, a sell stop order would be placed below the bid.

      In this code you have chosen to use OrderType.Limit which are placed on the reverse side of the market.
      A buy limit would be placed below the ask, a sell limit would be placed above the bid.

      I see in your code you've submitted the buy limit at the High of the most recently closed bar and the sell limit at the Low of the most recently closed bar. These orders will most likely be on the wrong side of the market when the new bar opens.
      With some brokerages that order will be immediately filled (including NinjaTrader), with other brokerages that order would be rejected (such as Forex brokerages).

      Also, orders should be assigned to variables in OnOrderUpdate() and not directly from the order method.

      I've changed is and tested the suggested code and I am finding this is working.

      Below is a link to a video of the test.

      [ATTACH]n1295472[/ATTACH]

      There are no exit orders or setting the variables back to null so it only places orders once, but this works as expected.
      Hi Chelsa,

      Thank you so much for your continued support. I'll check the video and your suggestions and will let you know later my results.

      I've implemented your suggestions and they work perfectly. I've also changed the entry Limit orders for Stop Orders.

      Cheers!

      Bob Perez
      Last edited by bobperez; 03-13-2024, 11:41 AM.

      Comment


        Originally posted by NinjaTrader_ChelseaB View Post
        Hello bobperez,

        In the UnmanagedOCOBracketExample_NT8 note I've used OrderType.StopMarket orders.
        A buy stop order would be placed above the ask, a sell stop order would be placed below the bid.

        In this code you have chosen to use OrderType.Limit which are placed on the reverse side of the market.
        A buy limit would be placed below the ask, a sell limit would be placed above the bid.

        I see in your code you've submitted the buy limit at the High of the most recently closed bar and the sell limit at the Low of the most recently closed bar. These orders will most likely be on the wrong side of the market when the new bar opens.
        With some brokerages that order will be immediately filled (including NinjaTrader), with other brokerages that order would be rejected (such as Forex brokerages).

        Also, orders should be assigned to variables in OnOrderUpdate() and not directly from the order method.

        I've changed is and tested the suggested code and I am finding this is working.

        Below is a link to a video of the test.

        [ATTACH]n1295472[/ATTACH]

        There are no exit orders or setting the variables back to null so it only places orders once, but this works as expected.
        Hi Chelsea,

        If I want to enter the oco trades with 2 contracts instead of 1, and each contract with a different target, will I need to send one pair of orders for each contract separately?
        Last edited by bobperez; 03-15-2024, 10:37 AM.

        Comment


          Hello bobperez,

          Yes, this would be necessary to have independent OCO pairing. Use two entry orders with unique signal names and two exit orders with the from entry signals of those entries.
          Chelsea B.NinjaTrader Customer Service

          Comment


            Originally posted by NinjaTrader_ChelseaB View Post
            Hello bobperez,

            Yes, this would be necessary to have independent OCO pairing. Use two entry orders with unique signal names and two exit orders with the from entry signals of those entries.
            Ok, thank you, Chelsea.

            Comment


              Originally posted by NinjaTrader_ChelseaB View Post
              Hello bobperez,

              Yes, this would be necessary to have independent OCO pairing. Use two entry orders with unique signal names and two exit orders with the from entry signals of those entries.
              Hi Chelsea,

              I am getting the attached errors after a while Click image for larger version  Name:	F4.jpg Views:	19 Size:	107.7 KB ID:	1296087 .



              Is there anything missing?

              BobPerez
              Last edited by bobperez; 03-18-2024, 01:34 PM.

              Comment


                Hello bobperez,

                The message is stating there is no real-time data.

                Who are you connected to for data? (Do you see changing ask and bid prices in Chart Trader?)

                Are these orders being submitted during market hours?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello bobperez,

                  The message is stating there is no real-time data.

                  Who are you connected to for data? (Do you see changing ask and bid prices in Chart Trader?)

                  Are these orders being submitted during market hours?
                  HI Chelsea, During the weekend I was connected to Rithmic, as well as today. Today it worked fine.

                  Bob

                  Comment


                    Hello Bob,

                    During the weekend the markets are closed and there is no real-time data.

                    However the Trading hours template should have prevented any trades.
                    Do you have Trading hours set to 'use instrument settings' in the Data Series window?
                    What Start behavior is selected in the Strategies window?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello Bob,

                      During the weekend the markets are closed and there is no real-time data.

                      However the Trading hours template should have prevented any trades.
                      Do you have Trading hours set to 'use instrument settings' in the Data Series window?
                      What Start behavior is selected in the Strategies window?
                      'use instrument settings' I : Yes
                      Start behavior I: Waiting UntilFlat

                      Bob

                      Comment


                        Hello Bob,

                        With 'Wait until flat' and not 'Wait until flat synchronize account' and the exchange trading hours, I would not expect an order to be submitted while the market is closed.

                        If you would like me to investigate further, please send an email to scriptingsupport[at]ninjatrader[dot]com so that I may request your log and trace files.
                        In the email, please include a link to this forum thread.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hello bobperez,

                          In the UnmanagedOCOBracketExample_NT8 note I've used OrderType.StopMarket orders.
                          A buy stop order would be placed above the ask, a sell stop order would be placed below the bid.

                          In this code you have chosen to use OrderType.Limit which are placed on the reverse side of the market.
                          A buy limit would be placed below the ask, a sell limit would be placed above the bid.

                          I see in your code you've submitted the buy limit at the High of the most recently closed bar and the sell limit at the Low of the most recently closed bar. These orders will most likely be on the wrong side of the market when the new bar opens.
                          With some brokerages that order will be immediately filled (including NinjaTrader), with other brokerages that order would be rejected (such as Forex brokerages).

                          Also, orders should be assigned to variables in OnOrderUpdate() and not directly from the order method.

                          I've changed is and tested the suggested code and I am finding this is working.

                          Below is a link to a video of the test.

                          [ATTACH]n1295472[/ATTACH]

                          There are no exit orders or setting the variables back to null so it only places orders once, but this works as expected.
                          Hi Chelsea,

                          Regarding this topic, if two OCO orders are simultaneously filled at say 3:55 PM, each with different targets and stop-losses, and I want to enter again at 3:58 PM, do the 3:55 orders need to be closed before submitting the new ones at 3:58? If so, how do I close the open position?

                          Also, how do I close any open OCO orders at session end?

                          Thanks,

                          Bob Perez

                          Comment


                            Hello Bob,

                            "Regarding this topic, if two OCO orders are simultaneously filled at say 3:55 PM, each with different targets and stop-losses, and I want to enter again at 3:58 PM, do the 3:55 orders need to be closed before submitting the new ones at 3:58? If so, how do I close the open position?"

                            You can continue scaling in with new entries in the same direction if you would like. With the unmanaged approach you can submit new orders without the position being closed or previous orders cancelled.

                            If you want to exit the position, call SubmitOrderUnmanaged() with a market order in the opposite direction of the position. (For example if the position is Long 5, submit a sell market order with a quantity of 5).

                            "Also, how do I close any open OCO orders at session end?"

                            If you are not wanting to rely on the Exit on session close behavior to cancel working orders and close open positions, you can write custom logic and use a SessionIterator to call CancelOrder() on working orders some time before the sesssion ends.


                            The PreventEntryAfterExitOnCloseExample example below has sample logic of using a sessionIterator.
                            NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on


                            Instead of setting a bool to prevent new entries when the time is reached, you could instead call CancelOrder() and supply the variable holding the order.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              Originally posted by NinjaTrader_ChelseaB View Post
                              Hello Bob,

                              "Regarding this topic, if two OCO orders are simultaneously filled at say 3:55 PM, each with different targets and stop-losses, and I want to enter again at 3:58 PM, do the 3:55 orders need to be closed before submitting the new ones at 3:58? If so, how do I close the open position?"

                              You can continue scaling in with new entries in the same direction if you would like. With the unmanaged approach, you can submit new orders without the position being closed or previous orders cancelled.

                              If you want to exit the position, call SubmitOrderUnmanaged() with a market order in the opposite direction of the position. (For example if the position is Long 5, submit a sell market order with a quantity of 5).

                              "Also, how do I close any open OCO orders at session end?"

                              If you are not wanting to rely on the Exit on session close behavior to cancel working orders and close open positions, you can write custom logic and use a SessionIterator to call CancelOrder() on working orders some time before the sesssion ends.


                              The PreventEntryAfterExitOnCloseExample example below has sample logic of using a sessionIterator.
                              NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on


                              Instead of setting a bool to prevent new entries when the time is reached, you could instead call CancelOrder() and supply the variable holding the order.
                              Thank you Chelsea,

                              Scale-In

                              - The issue with continuing to scale in is that the new OCO order may trigger a Short entry while the previous open order is in a Long position. This would generate an error, correct?

                              Custom SessionIterator

                              I do prefer to rely on the Exit on session close behavior to cancel working orders and close open positions, but it does not seem to be working. So I tried using your custom iterator:
                              Code:
                                      // prevents entry orders after the exit on close until the start of the new session
                                      private bool ExitOnCloseWait(DateTime QuickTime)
                                      {
                                          // the sessionIterator only needs to be updated when the session changes (after its first update)
                                          if (Bars.IsFirstBarOfSession)
                                              sessionIterator.GetNextSession(Time[0], true);
                              
                                          // if after the exit on close, prevent new orders until the new session
                                          if (tickTime >= sessionIterator.ActualSessionEnd.AddSeconds(-ExitOnSessionCloseSeconds) && tickTime <= sessionIterator.ActualSessionEnd)
                                              exitOnCloseWait = true;
                              
                                          // an exit on close occurred in the previous session, reset for a new entry on the first bar of a new session
                                          if (exitOnCloseWait && Bars.IsFirstBarOfSession)
                                              exitOnCloseWait = false;
                              
                                          return exitOnCloseWait;
                                      }
                              ​
                              I want to see Historical results, so I placed this command on the OnBarUpdate() method, instead of OnMarketData()
                              Code:
                                              if (ExitOnCloseWait(Time[0]))
                                              {
                                                  return;
                                              }            
                                              
                                          sessionIterator.GetNextSession(Time[0], true);
                              ​
                              Is there something I did incorrectly because it does not work either?

                              Bob Perez

                              Comment


                                Hello Bob,

                                "The issue with continuing to scale in is that the new OCO order may trigger a Short entry while the previous open order is in a Long position. This would generate an error, correct?"

                                This would depend on the broker and if they are sensitive to the OrderAction being correct in relation to the position. However, I would recommend that you submit an exit order using OrderAction.Sell before calling an entry with OrderAction.SellShort to ensure this works with all brokerages.

                                "I do prefer to rely on the Exit on session close behavior to cancel working orders and close open positions, but it does not seem to be working."

                                The forum post I've linked does have a video demonstrating the Exit on session close behavior does work, but does not prevent new entries after the event and before the end of the session.

                                The code you suggested does not appear to calling CancelOrder() to cancel any orders. Further there are no entry orders below the return that would not be reached.
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                627 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                359 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                105 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                562 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                568 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X