Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

sample/example oco bracket

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

    sample/example oco bracket

    Hi,
    Has anyone developed this unmanaged approach strategy type which can be backtested?
    thanks,
    David

    #2
    Hello David,

    Thank you for your post.

    There are UnmanagedOCOBracketExample scripts in the following post:
    https://forum.ninjatrader.com/forum/...579#post770579

    As stated in that post, "These examples are designed to place orders in real-time so that you can see the orders appear and try and cancel one to see the behavior.
    Unmanaged orders can also be submitted in historical data and be backtested by removing the check for real-time.​"

    The check for real-time is found in the OnMarketData() method as follows:
    Code:
                // only places orders in real time
                if (State != State.Realtime || ExitOnCloseWait(marketDataUpdate.Time))
                    return;    ​
    You could modify that portion of logic to allow historical orders:
    Code:
                // only places orders in real time
                //if (State != State.Realtime || ExitOnCloseWait(marketDataUpdate.Time))
                //    return;            
                // allows for historical orders
                if (ExitOnCloseWait(marketDataUpdate.Time))
                    return;​
    Additionally, as OnMarketData() is a real-time data stream, the logic should be moved to OnBarUpdate() to see orders processed historically. This is also noted in that same thread in the following post:


    Please let us know if we may be of further assistance.
    Last edited by NinjaTrader_Emily; 12-14-2023, 01:42 PM.

    Comment


      #3
      Thank-you!

      Comment


        #4
        I'm getting an error. Please see screen shot: Click image for larger version

Name:	error.png
Views:	193
Size:	382.3 KB
ID:	1281792

        Comment


          #5
          Originally posted by trader3000a View Post
          I'm getting an error. Please see screen shot: Click image for larger version

Name:	error.png
Views:	193
Size:	382.3 KB
ID:	1281792
          If you press F5 to trigger a compile, do you still receive an error? The screenshot indicates the error is in line 132, though your screenshot cuts off the line numbers. If you delete and re-write out the logic from line 132, does the script compile successfully? Please try these suggestions to identify what part of the code is causing the error and resolve it.

          It appears to be AssignOrderToVariable(ref order); - you may need to delete this line and type it out again without copy/pasting into the editor to ensure no unknown characters are added into the script.

          Thank you for your time and patience.

          Comment


            #6
            Hi,
            Typing by hand resolved the error. I've left the rest of the script untouched but it doesn't place any orders when I attempt to backtest. I attached my copy.
            thanks,
            David
            Attached Files

            Comment


              #7
              Originally posted by trader3000a View Post
              Hi,
              Typing by hand resolved the error. I've left the rest of the script untouched but it doesn't place any orders when I attempt to backtest. I attached my copy.
              thanks,
              David
              So I may assist you more accurately, please answer all of the following questions:
              • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
              • Do you see similar results (no orders placed) when running the same test on the SampleMaCrossOver strategy in NinjaTrader with the same settings as your strategy?
              • Who are you connected to? This is displayed in green in the lower-left corner of the Control Center window.
              • Are you connected to your data feed provider when running this test?
              • What instrument(s) (and expiry if applicable) have you selected?
              • What Data Series Type have you selected? Example: Tick, Minute, Day
              • What From and To date is selected?
              • If you open a chart for the same instrument(s) and the same date range, then right-click the chart and select 'Reload all historical data' is the historical data showing on the chart?
              • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
              Thanks in advance; I look forward to assisting you further.​

              Comment


                #8
                Originally posted by NinjaTrader_Emily View Post
                Hello David,

                Thank you for your post.

                There are UnmanagedOCOBracketExample scripts in the following post:
                https://forum.ninjatrader.com/forum/...579#post770579

                As stated in that post, "These examples are designed to place orders in real-time so that you can see the orders appear and try and cancel one to see the behavior.
                Unmanaged orders can also be submitted in historical data and be backtested by removing the check for real-time.​"

                The check for real-time is found in the OnMarketData() method as follows:
                Code:
                 // only places orders in real time
                if (State != State.Realtime || ExitOnCloseWait(marketDataUpdate.Time))
                return; ​
                You could modify that portion of logic to allow historical orders:
                Code:
                 // only places orders in real time
                //if (State != State.Realtime || ExitOnCloseWait(marketDataUpdate.Time))
                // return;
                // allows for historical orders
                if (ExitOnCloseWait(marketDataUpdate.Time))
                return;​
                Please let us know if we may be of further assistance.
                I tried the change you suggested but I'm still not getting any historical orders. Is there another place that needs to be modified? I attached the .cs file I'm using
                Attached Files

                Comment


                  #9
                  Originally posted by mlarocco View Post

                  I tried the change you suggested but I'm still not getting any historical orders. Is there another place that needs to be modified? I attached the .cs file I'm using
                  Hello,

                  Thank you for your note.

                  I also realize that the logic in OnMarketData() needs to be moved to OnBarUpdate() or you need to enable Tick Replay in order for OnMarketData() to be called historically. This is noted in the thread I linked previously as well:


                  For more information regarding OnMarketData():


                  I will amend my previous post to also mention moving the logic to OnBarUpdate().

                  Please let us know if we may be of further assistance.

                  Comment


                    #10
                    Originally posted by NinjaTrader_Emily View Post

                    Hello,

                    Thank you for your note.

                    I also realize that the logic in OnMarketData() needs to be moved to OnBarUpdate() or you need to enable Tick Replay in order for OnMarketData() to be called historically. This is noted in the thread I linked previously as well:


                    For more information regarding OnMarketData():


                    I will amend my previous post to also mention moving the logic to OnBarUpdate().

                    Please let us know if we may be of further assistance.
                    Thank you... that did the trick!

                    Comment


                      #11
                      Hi,
                      Do we move the entirety of code in OnMarketData into OnBarUpdate? Will it still work live?
                      thanks,
                      David

                      Comment


                        #12
                        Originally posted by trader3000a View Post
                        Hi,
                        Do we move the entirety of code in OnMarketData into OnBarUpdate? Will it still work live?
                        thanks,
                        David
                        Yes, you could move everything in OnMarketData() into OnBarUpdate() and it will also still work live. As my colleague mentioned in his post, "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."

                        The point of the examples he created was to show orders canceling each other as OCO in real-time as that was the easiest way to demonstrate that OCO was working. It is hard to observe that OCO behavior in historical backtests, though may certainly move the logic into OnBarUpdate() and it will work historically and live.

                        Please let us know if we may be of further assistance.

                        Comment


                          #13
                          ok, so I moved everything to OnBarUpdate. I now have the error "The name 'marketDataUpdate' does not exist in the current context. It's in this section:

                          // only places orders in real time
                          //if (State != State.Realtime || ExitOnCloseWait(marketDataUpdate.Time))
                          // return;
                          // allows for historical orders
                          if (ExitOnCloseWait(marketDataUpdate.Time))
                          return;​

                          Comment


                            #14
                            maybe you could post a version that works historically and live?

                            Comment


                              #15
                              Originally posted by trader3000a View Post
                              maybe you could post a version that works historically and live?
                              Please see the attached example. Thank you for your patience and please let me know if I may be of further assistance!
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              54 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              130 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              70 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              44 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