Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simultaneously Entering two Long positions with OCO

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

    Simultaneously Entering two Long positions with OCO

    Simultaneously Entering two Long positions with OCO

    Yesterday, 08:19 AM
    I want to know if someone in NinjaTrader Forum faced a similar issue with implementing the 2 bracket strategy.
    Narrative: I'm implementing a simple strategy for a 2 Long Limit order to trigger two OCO orders.
    There are two entries LongEntryOrde1= LongEntryOrder2. Each order entry is linked to its own OCO.
    I used SetProfitTarget() and SetStopLoss() methods to set OCO1 and OCO2 before filling the long orders.
    I cancel LongEntryOrder1 and LongEntryOrder2 after they are filled.
    Issue: After LongEntryOrde1 and LongEntryOrde2 were filled, the OCO order quantities exceeded the quantity of Long Entries.
    For instance: LongEntryOrde1 has 3 contracts and LongEntryOrde2 has 4 contracts. so I'm anticipating 3 contracts for OCO1 pair and 4 contracts for OCO2 pair to be placed, totaling 7 contracts.
    Instead, I'm getting placed 14 OCO contracts.
    The attached files may clarify the issue.
    Appreciate help.​​
    Attached Files

    #2
    Hello AlexAOM,

    Thank you for your post.

    To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.

    I am happy to assist you with analyzing the output from the output window.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

    https://support.ninjatrader.com/s/ar...nd-TraceOrders

    Comment


      #3
      Gaby,
      Appreciate your prompt response. I have attached a few documents Code Snippet and the copy of Output Window.
      Can you review those files? Can I send you a private message? I don't won't to overload system. Instead, I'll post solution after the issue get resolved.
      Thanks,
      AlexAOM

      Comment


        #4
        Hello AlexAOM,

        There aren't any files attached to your post. Please try attaching the output again.

        Comment


          #5
          Gaby,
          Sorry for confusion. 3 files are attached to my first post. Just in case I repost them again.
          Attached Files

          Comment


            #6
            Hello AlexAOM,

            Thank you for the output.

            Unfortunately, these prints from the output aren't descriptive enough for us to understand what the strategy is doing. Additionally, it doesn't look like TraceOrders was enabled.

            This support article contains an example of a descriptive print extremely similar to one of the conditions in your script:



            If you need assistance creating a print, please let me know and I can provide an example of a descriptive print for your script.

            I also recommend printing out the Position. Quantity within OnBarUpdate, so we can keep track of how the quantity is increasing.



            The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

            The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

            Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

            Comment


              #7
              Gaby,
              I'll do that.
              On a side note: I did enable Trace but did not see any output data. Any suggestion?
              In the meantime, Can you provide me an example of placing an order with two brackets? I don't have any problem with a single bracket tough.
              Alex

              Comment


                #8
                Hello AlexAOM,

                Did you remove the strategy instance and add a new instance to the chart or Strategies tab after enabling TraceOrders? This is necessary in order to load the new defaults.

                When you say "place an order with two brackets" do you mean a single order with a TP and an SL? Or two orders, both with a TP and an SL?

                We have an existing sample script that demonstrates submitting protective orders (stop loss and profit target): https://ninjatrader.com/support/helpGuides/nt8/using_onorderupdate_and_onexec.htm

                Comment


                  #9
                  Hi Gaby,
                  First of all, I want to reply to your previous email:
                  Q1: Did you remove the strategy instance and add a new instance to the chart or Strategies tab after enabling TraceOrders? This is necessary in order to load the new defaults.Did you remove the strategy instance and add a new instance to the chart or Strategies tab after enabling TraceOrders? This is necessary in order to load the new defaults.
                  A1: Yes, I did. I don't have an issue with Trace.

                  Q2: When you say "place an order with two brackets" do you mean a single order with a TP and an SL? Or two orders, both with a TP and an SL
                  A2: I mean TwoOrders L1 and L2. PT1 and SL1 associated L1 order and PT2 and SL2 associated L2 order.
                  Since I'm trying to simulate One order trigger two Brackets(OCO), L1 = L2.
                  In my experiment, I used L1 != L2 but it did not help.

                  Q3: We have an existing sample script that demonstrates submitting protective orders (stop loss and profit target):
                  https://ninjatrader.com/support/helpGuides/nt8/using_onorderupdate_and_onexec.htm

                  A3: Appreciate your reference. Unfortunately, This example is not representative of what I want to accomplish.
                  - The motods SetStoppLoss and SetProfitTarget have not been used.
                  - This example explains only a single OCO concept.

                  Secondly:
                  I followed the document, which explained how to troubleshoot strategy.
                  Please find attached three files:

                  ​1. Source code. You may want to run this code to see the issue. It's very consistent. I used NinzaRenko and TimeBased charts, the result was the same.
                  2024-08-11_MySimplifiedStrategyTest.cs

                  2. Raw Out window data. You'll see the trace data along with my Print
                  NinjaScript Output 8_11_2024 4_49 PM.txt

                  3. Spreadsheet. I decided to analyze data using a spreadsheet. I used only data after SW transitioned to the RUN state.
                  "//" indicated my comments.
                  2024-08-11_AOM_TwoBrac****rder.xlsx

                  Alex
                  NOTE:
                  Due to the file size, I have provided links to the files.

                  Comment


                    #10
                    Hello AlexAOM,

                    Unfortunately, these links say I do not have access. You need to edit the permissions on these Google Drive links, or alternatively you could .zip the output and post the .zip file.

                    Comment


                      #11
                      Gaby,
                      I'll send you a personal message with the attachments
                      Alex

                      Comment


                        #12
                        Hello Alex,

                        Our support team cannot receive personal messages on the forum. If you would like, you can email us at scriptingsupport[AT]ninjatrader.com

                        Comment


                          #13
                          Gaby,
                          I could not send you a private message.
                          Here are the links to unlocked files.


                          https://docs.google.com/spreadsheets...6763728&rtpof= true&sd=true
                          ​Let me know if you can open them
                          Thanks,
                          Alex

                          Comment


                            #14
                            Hello Alex,

                            I am seeing the info from TraceOrders, however unfortunately these prints aren't very descriptive. For example:

                            6/26/2024 4:48:43 PM | OnBarUpdate | Condition To Cancell LongEntryOrder1: if (CrossAbove(SMA(10), SMA(20), 1) && Close[0] > Open[0] && LongEntryOrder1 != null )

                            This doesn't tell us anything about how this condition is evaluating or what these values are.

                            A better print would be:

                            ​Print(string.Format( "{0} | SMA(10)[1]: {1} < SMA(20)[1]: {2} && SMA(10)[0]: {3} > SMA(20)[0]: {4} && Close[0]: {5} > Open[0]: {6} && LongEnterOrder1: {7} != null", Time[0], SMA(7)[1], SMA(14)[1], SMA(7)[0], SMA(14)[0], Close[0], Open[0], LongEntryOrder1 ));​

                            You need to create similarly descriptive prints for all conditions in the strategy.

                            Additionally, I'm not seeing any prints for the Position.Quantity we are tracking? Make sure to also include a print statement within OnBarUpdate that prints this value out.

                            If you would like someone to debug this script on your behalf, you can search our extensive library of NinjaScript consultants through the link below. Simply enter a consultant name or search by using our filter categories. Once you have identified your consultants of choice, please visit each consultant's site for more information or contact them directly to learn more:

                            https://ninjatraderecosystem.com/sea...mming-services

                            You can locate the contact information for the consultants on their direct websites for any additional questions you may have. Since these consultants are third-party services for NinjaTrader, all pricing and support information will need to be obtained through the consultant.

                            The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem, LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

                            Let me know if I may be of further assistance.

                            Comment


                              #15
                              Gaby,
                              I'm not sure if you have had a chance to review the info in the spreadsheet where I have a detailed analysis of the trace file. I found the issue and just want to review it with you.
                              It looks like NT blocking my XLS file. Is it possible to have a quick meeting, so I can confirm my findings?
                              Appreciate your help,
                              Alex

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              24 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              120 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              63 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              41 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              45 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X