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

2 entry orders help!

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

    #16
    Hi Chelsea,

    I downloaded your code for the OCO entries.
    Thank you for posting it.
    Any chance you could upgrade the original code to make the entries and exits inputs?
    Thanks for the help.

    DT_12


    Originally posted by NinjaTrader_ChelseaB View Post
    Hello Andrea,

    To clarify your original question, you are wanting to have opposing orders working at the same time, is this correct?

    I have an example that you may find helpful that uses the unmanaged approach to submit a pair of opposing orders using oco (One Cancels Other) I've made for NinjaTrader 7.

    I've ported this to NT8 for you and I'm attaching both versions so you can compare.


    (Updated July 17th, 2018 - corrected assigning the variables to null when the exit on close occurs. Also corrected preventing orders after the exit on close until the new session)

    Comment


      #17
      Hello DayTrader12,

      This example was created to demonstrate that OCO works as documented in the help guide.

      Below I am providing public links to the help guide on creating inputs.



      I would recommend using the SampleMACrossOver included with NinjaTrader for an example of how to add inputs to your script.

      You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Thanks Chelsea, I will give it a shot.

        Comment


          #19
          Hello ChelseaB, with Unmanaged OCO orders type can I backtest my strategy? Thanks

          Comment


            #20
            Hello davizz,

            Welcome to the NinjaTrader forums!

            Yes, strategies with unmanaged orders that use oco can be backtested.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #21
              Hi, I've been looking at coding OCO brackets... found this thread - great, but I have a question for Chelsea...

              The sample OCO code you provided states explicitly it is designed to work ONLY on live data... not historical. But you say above it can be back-tested (ie, on historical data).
              Can you please clarify?

              And yes, my intended purpose is to have both pending long and short MIT orders active.

              Thanks,
              T.

              Comment


                #22
                Hello T,

                Orders that use OCO can be back-tested.

                The example I have created is not intended to be back-tested.

                The example is designed to show that when one order is filled, the other is cancelled which is not easy to demonstrate in historical data. So I have chosen to have the logic that places orders in OnMarketData() so that the demonstration happens in real-time so anyone testing the script can see it happen.

                Move this logic to OnBarUpdate() if you would like the example script to work historically.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #23
                  Thanks. I also got another script from Jim... will try to implement it today.

                  I have a few challenges... as I am trying to achieve all of the following:
                  a) OCO bracket setup
                  b) ATM-style break-even and thereafer multiple scaled exits
                  c) trailing stop on a final "runner" position, ie with no target.
                  d) backtestable ;-)

                  I've written plenty of substantial code... but not much (any?) previously that needed tick data, And i think to implement a "proper" ATM-style thing, I need tick data. I previously implemented ATM-style trailing stops... but only operated on bar close...It works... but clearly an intra-bar moving of stops would be better..
                  Last edited by tgn55; 06-09-2020, 07:34 PM.

                  Comment


                    #24
                    Hello tgn55,

                    I'm trying to pick a question out of this but I'm having a hard time understanding your inquiry.

                    Can you clarify what you are asking?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #25
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello Andrea,

                      To clarify your original question, you are wanting to have opposing orders working at the same time, is this correct?

                      I have an example that you may find helpful that uses the unmanaged approach to submit a pair of opposing orders using oco (One Cancels Other) I've made for NinjaTrader 7.

                      I've ported this to NT8 for you and I'm attaching both versions so you can compare.


                      (Updated July 17th, 2018 - corrected assigning the variables to null when the exit on close occurs. Also corrected preventing orders after the exit on close until the new session)
                      Hello Chelsea. I'm wondering if there is a simpler way just to get a unique OCO ID for an Indicator script (not an unmanaged strategy one)?

                      I've tested using bitdavid's random Id

                      PHP Code:
                      private static Random MyRand = new Random(DateTime.Now.Millisecond); 

                      but it still throws the error message

                      Time Category Message
                      10/05/2022 18:02:54 Order Failed to submit orders: System.NullReferenceException: Object reference not set to an instance of an object. at NinjaTrader.Cbi.Account.Submit(IEnumerable`1 orders)
                      Time Category Message
                      10/05/2022 18:02:53 Order Sim101, Order 'ff3e8b60dc954bdab38001b0c7b58669' can't be submitted: The OCO ID ‘myOCOSystem.Random’ cannot be reused. Please use a new OCO ID. affected Order: Buy 1 Market
                      PHP Code:
                      buyMktOrder account.CreateOrder(
                                    
                      Instrument,
                                    
                      OrderAction.Buy,
                                    
                      OrderType.Market,
                                    
                      OrderEntry.Manual,
                                    
                      TimeInForce.Day,
                                    
                      quantitySelector.Value,
                                    
                      0,
                                    
                      0,
                                    
                      "myOCO" MyRand,
                                    
                      "buyMktOrder"+DateTime.Now.ToString(),
                                    
                      DateTime.MaxValue,
                                    
                      null); 

                      Why does it still keeps getting the same ID (I suspect it is because somehow buyMktOrder OCO ID wasn't reset)?
                      What it the absolute minimum code required for the solution for an Indicator script?

                      Thanks!
                      Last edited by PaulMohn; 05-10-2022, 10:10 AM.

                      Comment


                        #26
                        Hello PaulMohn,

                        GetAtmStrategyUniqueId() and Guid.NewGuid() can be used to generate unique id strings.


                        Without output from debugging prints, printing the string, I wouldn't be able to give any direction.

                        I can say you are using a static variable which is not making any sense to me. The string should be generated before each new order set, and it looks like you are only generating a string once when the variable is declared and not for each new order pairing.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #27
                          Thanks! I tested with Guid.NewGuid().ToString() as
                          PHP Code:
                                    buyMktOrder account.CreateOrder(
                                        
                          Instrument,
                                        
                          OrderAction.Buy,
                                        
                          OrderType.Market,
                                        
                          OrderEntry.Manual,
                                        
                          TimeInForce.Day,
                                        
                          quantitySelector.Value,
                                        
                          0,
                                        
                          0,
                                        
                          Guid.NewGuid().ToString(),
                                        
                          "buyMktOrder"+DateTime.Now.ToString(),
                                        
                          DateTime.MaxValue,
                                        
                          null); 

                          from this post and the error is not thrown anymore, and the order executes with the the Target order and the Stop Order.

                          But the OCO doesn't work as expected because when either the Stop or the Target order gets filled the other one (Target/Stop order) isn't cancelled.

                          What could be the cause? How to get the remaining OCO order to get cancelled? Thanks!

                          Comment


                            #28
                            Hello PaulMohn,
                            It doesn't look like you are generating the OCOID before placing the orders as advised. It appears this is being generated in order method and only for one order.

                            Are you using the same OCOID for both orders shown on the Orders tab of the Control Center? (OCO is one-cancels-others, for all orders using the same OCOID if one order is cancelled or filled all other orders using the same OCOID will be cancelled)

                            The ProfitChaseStopTrailUnmanagedExample_NT8 and ProfitChaseStopTrailIndicatorExample_NT8 demonstrate.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #29
                              Oh you're right it did place the Target and Stop orders for 2 trades but then it stopped (but no OCO ID error).
                              Here's a screenshot of the Orders tab, the previous trades don't show same ids.

                              Do you have a a sample for regular indicator use? Thanks!
                              Attached Files

                              Comment


                                #30
                                Ah this is the indicator one? I'll check it now thanks!

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Waxavi, 04-19-2024, 02:10 AM
                                2 responses
                                37 views
                                0 likes
                                Last Post poeds
                                by poeds
                                 
                                Started by chbruno, Yesterday, 04:10 PM
                                1 response
                                44 views
                                0 likes
                                Last Post NinjaTrader_Gaby  
                                Started by Max238, Today, 01:28 AM
                                1 response
                                24 views
                                0 likes
                                Last Post CactusMan  
                                Started by giulyko00, Yesterday, 12:03 PM
                                2 responses
                                10 views
                                0 likes
                                Last Post giulyko00  
                                Started by r68cervera, Today, 05:29 AM
                                0 responses
                                4 views
                                0 likes
                                Last Post r68cervera  
                                Working...
                                X