Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Leftover orders from optimization iterations

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

    Leftover orders from optimization iterations

    OK, I was trying to optimize a strategy using my simulated ATM backtesting framework. It works fine when running a strategy script once on an instrument.

    But weird things were happening when I tried to optimize a strategy. So I keyed my order names to the strategy parameters, to be sure that the order names were consistent with the iteration being run.

    I discovered that each new iteration of the optimization was polluted by filled positions that were active from prior iterations.

    For example, I have two parameters, EMAlength and TargetMultiple. For EMAlength=12 and TargetMultiple=3.5, all order names will have "123.5" appended to them. OnStartUp() prints "123.5" to the Output window to indicate which iteration is running.

    But then my order management framework generates internal error messages to the Output window with strings like "125" and "124.5" appended to the order name, saying it does not recognize these orders that just got filled. And it shouldn't. These fill events shouldn't even be happening because those orders were never placed in that iteration.

    I'm using unmanaged orders.

    How do I make sure that everything is FLAT at the end of each optimization iteration, so no remnants of previous positions or orders pollute the new optimization iterations?

    #2
    anachronist,

    Thank you for your post.

    Can you provide a broken down script where I would be able to test this on my end?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Cal View Post
      Can you provide a broken down script where I would be able to test this on my end?
      Working on it -- I wrote a simple unmanaged order strategy to demonstrate the problem (not using my ATM framework) but the chart blanks out and freezes up after the first execution. Putting Print() everywhere shows that it completes OnExecution() but doesn't even make it into the next OnBarUpdate(). It seems to be freezing somewhere not in my code. I'm installing Visual Studio now to investigate further.
      -A

      Comment


        #4
        OK, I made a simple Unmanaged order strategy to demonstrate this problem but for some reason the chart goes blank and locks up when the first order is executed. Debugging in Visual Studio didn't enlighten the problem.

        Attached is the test strategy (not written to be profitable but to demonstrate a point). It's simple:
        • Chart weekly bars on a stock. Strategy will load daily bars internally for trading.
        • Trend is established when EMA goes outside a Keltner channel
        • If trend is up, buy limit at lower bound of Keltner channel. If trend is down, SellShort limit at upper bound.
        • Target profit at some multiple of ATR. Stoploss at some multiple of ATR.

        See the comments at the top of the code for setting it up.

        What might be wrong that causes it to crash?

        After that is figured out, I can get on with demonstrating the bug that started this thread.

        -A
        Attached Files
        Last edited by anachronist; 11-10-2014, 04:26 PM.

        Comment


          #5
          Update: New version of the strategy attached.

          The point here is to distill an unmanaged order strategy down to its bare essence: Identify a trend, submit a limit order to enter, and once entered, submit target and stop orders to exit.

          Once that is done, I can demonstrate the bug I found with optimization and testing against collections.

          There are six separate order references being maintained: long and short entry, long and short target exit, long and short stoploss.

          The chart locks up right after the first call to OnExecution() occurs. It locks up even if you comment out the exit orders that are submitted within OnExecution(). Visual Studio reports no exceptions. The chart simply blanks and locks up.

          -Alex
          Attached Files
          Last edited by anachronist; 11-12-2014, 05:17 PM.

          Comment


            #6
            anachronist,

            Thanks for that. I have the updated script and will take a look at this one.
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Hello anachronist,

              Thank you for your patience.

              I am seeing the same issue on my end. Have you tried implementing Try-Catch blocks within the OnExecution method to try to isolate the root cause?

              You can find information on Try-Catch at the following link: http://www.ninjatrader.com/support/f...ead.php?t=9825

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                I am seeing the same issue on my end. Have you tried implementing Try-Catch blocks within the OnExecution method to try to isolate the root cause?
                I'll try that, but I'm skeptical it will reveal anything because, when stepping through the code in Visual Studio, the lockup occurs after OnExecution() is all finished executing.
                -Alex

                Comment


                  #9
                  Yup, I was right. No exception is generated in OnExecution or OnBarUpdate. I enclosed the code in each method in separate try..catch blocks.

                  If nothing is amiss with the way I wrote this simple strategy, it appears the problem is in the NinjaTrader source. Would it be possible to enclose the code that calls the strategy in try..catch blocks to see what's going on?

                  -Alex

                  Comment


                    #10
                    Hello anachronist,

                    Thank you for your response.

                    Using Try-Catch in the NinjaTrader 7 proprietary code is not an option. What needs to be done is to break down the code further than you have already. Instead of just running Try-Catch blocks or only commenting out code you think is the issue, try other sections of the code to comment out. Take it step by step.

                    Debugging is generally not the most pleasant nor easiest task. We will look into this further on our end, but please continue on your end as well.

                    Comment


                      #11
                      Originally posted by NinjaTrader_PatrickH View Post
                      Using Try-Catch in the NinjaTrader 7 proprietary code is not an option.
                      It isn't an option for me, but it is for you.
                      What needs to be done is to break down the code further than you have already. Instead of just running Try-Catch blocks or only commenting out code you think is the issue, try other sections of the code to comment out. Take it step by step.
                      Patrick, I'm not a newbie here, been programming NT for years before you joined, own a lifetime license, and have contributed to the upcoming NT8 codebase. Believe me, I know how to debug. If I weren't stuck, I wouldn't have started this thread.

                      I have commented the code down to the point where all it does is submit an unmanaged limit order. AFTER the limit order is executed, the chart locks up, perhaps on the last bar of the chart (see my next post below).

                      Stripped down version attached. If you follow the instructions on setting it up in the comments at the top of the script, and run the strategy, you should get the following in the output window, showing the strategy submit and then cancel a couple of limit orders before a limit order gets executed:
                      Code:
                      **NT** Enabling NinjaScript strategy 'UnmanagedTest/cac985028fc44d2b9fc7b1a78e1068d2' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=UniqueEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositions ExitOnClose=False Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=True MaxRestarts=4 in 5 minutes
                      Parameters: 14/3/3
                      20 Placed Buy entry order L20-14/3/3: Order='NT-00000/Sim101' Name='L20-14/3/3' State=PendingSubmit Instrument='BA' Action=Buy Limit price=54.59 Stop price=0 Quantity=100 Strategy='UnmanagedTest' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='6289ea98142d4d22aede199b1fcad5d7' Gtd='12/01/2099 0:00:00'
                      161 Placed SellShort entry order S161-14/3/3: Order='NT-00001/Sim101' Name='S161-14/3/3' State=PendingSubmit Instrument='BA' Action=SellShort Limit price=95.62 Stop price=0 Quantity=100 Strategy='UnmanagedTest' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='c12d7b1945944149a121f91687692588' Gtd='12/01/2099 0:00:00'
                      161,781 cancelled long entry order L20-14/3/3
                      238 Placed Buy entry order L238-14/3/3: Order='NT-00002/Sim101' Name='L238-14/3/3' State=PendingSubmit Instrument='BA' Action=Buy Limit price=40.68 Stop price=0 Quantity=100 Strategy='UnmanagedTest' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='7e6b6f833e3d4888a77ee4276fc7d91e' Gtd='12/01/2099 0:00:00'
                      238,1151 cancelled short entry order S161-14/3/3
                      240,1164 executed L238-14/3/3
                      All OnExecution() does is Print that last line. Stepping through in VS, I can verfiy that OnExecution completes without problem, and then the chart crashes.

                      -Alex
                      Attached Files
                      Last edited by anachronist; 11-18-2014, 08:46 AM. Reason: copyedit for clarity

                      Comment


                        #12
                        Perhaps this might shed a clue. Here is the simplest possible scenario, placing one unmanaged buy limit order on a specific date and doing nothing else:
                        1. Load a WEEKLY chart for stock BA, using 1850 days up to 11/1/2014.
                        2. The strategy adds its own daily series internally.
                        3. Limit order is placed on 6/5/2009 against the daily series, and filled a few days later
                        4. The error "Object reference not set to an instance of an object" happens on last bar.

                        The entire strategy is just 4 lines of code. Should that error be happening? It may be related to the mysterious crash in the script posted in my previous message.

                        Update 11/18/14: The error is caused by OnExecution() being passed an IExecution object with a null Order object. Testing for this fixes the error. Unfortunately, the same test does not fix the problem in the script attached to the previous post.,

                        Code:
                        namespace NinjaTrader.Strategy {
                            [Description("Unmanaged limit order test")]
                            public class UnmanagedLimit : Strategy {
                                #region Variables
                                private IOrder entryorder = null;
                                #endregion
                        
                                protected override void Initialize() {
                                    Add(PeriodType.Day, 1);
                                    Unmanaged = true;
                                    TimeInForce = Cbi.TimeInForce.Gtc;
                                    ExitOnClose = false;
                                    BarsRequired = 20;
                                    CalculateOnBarClose = true;
                                }
                        
                                protected override void OnExecution(IExecution ex) {
                                    Print("Executed order "+ex.Order.Name);
                                }
                        
                                protected override void OnBarUpdate() {
                                    if (BarsInProgress != 0) return;
                                    if (ToDay(Time[0]) == 20090605) {
                                        entryorder = SubmitOrder(1, OrderAction.Buy, OrderType.Limit, 100, 40.68, 0, "", "MyEntry");
                                        Print("Submitted order "+entryorder.ToString());
                                    }
                                }
                        
                                #region Properties
                                #endregion
                            }
                        }
                        Last edited by anachronist; 11-18-2014, 11:31 AM. Reason: update with fix

                        Comment


                          #13
                          Hello anachronist,

                          For the Unmanaged Test you need to set the IOrder objects to private. For example:
                          Code:
                          		private IOrder longentryorder, shortentryorder, // entry orders
                          			longtargetorder, longstoporder,     // exit orders for long entries
                          			shorttargetorder, shortstoporder;   // exit orders for short entries
                          For the Unmanaged Limit, I am not seeing the same issues. Can you provide the .cs file for the Unmanaged Limit?

                          Comment


                            #14
                            Originally posted by NinjaTrader_PatrickH View Post
                            For the Unmanaged Test you need to set the IOrder objects to private.
                            Thanks, I was hoping the problem would be something so simple, but unfortunately that didn't fix it. The script still locks up.
                            For the Unmanaged Limit, I am not seeing the same issues. Can you provide the .cs file for the Unmanaged Limit?
                            Please disregard -- I now suspect that's unrelated. I updated the post above - the problem was a null Order object being passed by NT in the last bar. Putting a check for this in the UnmanagedTest script didn't fix it though. It still locks up.
                            -Alex
                            Last edited by anachronist; 11-18-2014, 12:40 PM.

                            Comment


                              #15
                              Hello anachronist,

                              Thank you for your response.

                              Setting the IOrder objects to private resolved the lock up on my end. Can you provide the script that is still locking up after you have set the IOrder objects to private?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              647 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              368 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              571 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X