Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop and reverse with Unmanaged Approach leads to infinite loop

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

    Stop and reverse with Unmanaged Approach leads to infinite loop

    Hi,

    I'm having trouble with a strategy I'm working on where it keeps getting stuck in an infinite loop. It works properly until it gets to that specific bar (11/8/2013 8:00:00 AM), then it blows up and overflows my log. I've included a snippet of the log and the relevant code.

    Here is the log:
    Code:
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3433 OcoId='' Name='MyShortOrderStop'
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=Buy OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3433 OcoId='' Name='MyLongOrder'
    11/8/2013 8:00:00 AM: ooh, an execution.  It's name is: MyShortOrderStop
    11/8/2013 8:00:00 AM: ooh, an execution.  It's name is: MyLongOrder
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=Sell OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3390 OcoId='' Name='MyLongOrderStop'
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=SellShort OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3390 OcoId='' Name='MyShortOrder'
    11/8/2013 8:00:00 AM: ooh, an execution.  It's name is: MyLongOrderStop
    11/8/2013 8:00:00 AM: ooh, an execution.  It's name is: MyShortOrder
    11/8/2013 8:00:00 AM: Creating stop on short order... 1.3433
    11/8/2013 8:00:00 AM: Current price is: 1.3415
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3433 OcoId='' Name='MyShortOrderStop'
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=Buy OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3433 OcoId='' Name='MyLongOrder'
    11/8/2013 8:00:00 AM: ooh, an execution.  It's name is: MyShortOrderStop
    11/8/2013 8:00:00 AM: ooh, an execution.  It's name is: MyLongOrder
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=Sell OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3390 OcoId='' Name='MyLongOrderStop'
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=SellShort OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3390 OcoId='' Name='MyShortOrder'
    11/8/2013 8:00:00 AM: ooh, an execution.  It's name is: MyLongOrderStop
    11/8/2013 8:00:00 AM: ooh, an execution.  It's name is: MyShortOrder
    11/8/2013 8:00:00 AM: Creating stop on short order... 1.3433
    11/8/2013 8:00:00 AM: Current price is: 1.3415
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3433 OcoId='' Name='MyShortOrderStop'
    11/8/2013 8:00:00 AM Entered internal SubmitOrder() method at 11/8/2013 8:00:00 AM: Action=Buy OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.3433 OcoId='' Name='MyLongOrder'
    **NT** Error on calling 'OnExecution' method for strategy 'SwingDonchian/f37ea7df53534f26bfdd70f879e6aa0b': Object reference not set to an instance of an object.
    Here is the code in question:
    Code:
    		protected override void OnExecution(IExecution execution)
    		{
    			DonchianChannel dc = DonchianChannel(period);
    			
    			myPrint ("ooh, an execution.  It's name is: "+execution.Name);
    			if (execution.Name == "MyLongOrder") {
    				//myPrint("Creating stop on long order...");
    				longOrderStop = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, dc.Lower[0], "", "MyLongOrderStop");
    				//longOrder = null;
    				shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, 1, 0, dc.Lower[0], "", "MyShortOrder");
    			}
    			else if (execution.Name == "MyShortOrder") {
    				myPrint("Creating stop on short order... "+dc.Upper[0]);
    				myPrint("Current price is: "+Close[0]);
    				shortOrderStop = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, 1, 0, dc.Upper[0], "", "MyShortOrderStop");
    				//shortOrder = null;
    				longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 1, 0, dc.Upper[0], "", "MyLongOrder");
    			}
    The variables are properly declared in the scope of the class. What is really driving me crazy is that it works for several iterations, then it gets stuck in a loop. When I finally get Strategy Analyzer closed, it gives the final error at the bottom of the log -- "Object reference not set to an instance of an object." Uh, what? Which object?

    What is going on? I thought I was following the rules and doing what I am supposed to be doing. I must use the Unmanaged Approach because my initial entry is an OCO bracket.

    Please help me solve this bug.

    -Matt

    #2
    Matt, your approach via unmanaged would be correct for this scenario - I would suggest further debugging of the code via C# try/catch to get a finer return in which exact area to look for this issue. Could there be a check for null for your dc object needed?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Matt,

      Infinite loop will happen if a piece of code is calling itself.
      Is it happening in some execution instance, that your MyLongOrder is sending a possible MyShortOrder, which gets filled and inturn calls OnExecution function again. Hence creating a loop?

      Comment


        #4
        Originally posted by StockFundoo View Post
        Infinite loop will happen if a piece of code is calling itself.
        Is it happening in some execution instance, that your MyLongOrder is sending a possible MyShortOrder, which gets filled and inturn calls OnExecution function again. Hence creating a loop?
        Yes, I agree that is how infinite loops are created.

        I am unsure how it is being called again as it seems to be the OnExecution handler which is being called repeatedly, but the handler is placing stop orders above or below the highs or lows, respectively, which are outside of the current bar and (in this case) the next bar, so there is no way it could be legitimately looping. In the code sample I included above, it shows that the orders are stop orders. I've triple checked that those prices are outside the price action of the current bar and the next bar. Actually, I now placed them one tick extra outside of the Donchian Channel, just to be safe.

        Perhaps it is a bug in the Strategy Analyzer.
        Last edited by MattR; 11-15-2013, 07:52 AM.

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          Matt, your approach via unmanaged would be correct for this scenario
          Bertrand, thanks. It is good to have confirmation.

          Originally posted by NinjaTrader_Bertrand View Post
          I would suggest further debugging of the code via C# try/catch to get a finer return in which exact area to look for this issue.
          Argh. I have some doubts about it being a C# issue, but then again, I am not a specialist in C#. I will add some try/catch logic to try to narrow down the problem.

          Originally posted by NinjaTrader_Bertrand View Post
          Could there be a check for null for your dc object needed?
          Actually, regarding the dc object, that is more or less a copy to be sure that I did not have a scoping issue. Originally I defined the dc object inside the scope of the entire class and then assigned the object to the variable in the Initialize method. I use that same call elsewhere, and it seems to be bulletproof, so I have a hard time imagining that it is related, especially considering that it displays the same value in the log which is used in the apparently looping SubmitOrder method...

          Comment


            #6
            Perhaps those two issues are not related, but it would be first of all good where you are running into accessing an empty object exactly here. For that standard C# try/catch can be very helpful to help pin down.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              fix

              I was a bit worried when I saw this title, but I think the basic problem is that you are setting stop orders without checking the actual price. This results (depending on how price is moving) in cases where your stops are being filled immediately. If your stops are filled immediately then calls to OnExecution will go into what seems like an infinite loop (unless perhaps price can move in a favorable direction faster than the execution loop you have started). I assume that NT uses some sort of queue to keep track of unprocessed async events, and this queue probably has some limit, which might be exceeded in your case.

              You need to check that your Sell Stop orders are below market price, and that your Buy Stop orders are above market price.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Harry, 05-02-2018, 01:54 PM
              10 responses
              3,202 views
              0 likes
              Last Post tharton3  
              Started by cre8able, Yesterday, 01:16 PM
              3 responses
              11 views
              0 likes
              Last Post cre8able  
              Started by ChartTourist, Today, 08:22 AM
              0 responses
              6 views
              0 likes
              Last Post ChartTourist  
              Started by LiamTwine, Today, 08:10 AM
              0 responses
              2 views
              0 likes
              Last Post LiamTwine  
              Started by Balage0922, Today, 07:38 AM
              0 responses
              5 views
              0 likes
              Last Post Balage0922  
              Working...
              X