Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with different results using VS debugger

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

    Problem with different results using VS debugger

    I have developed many strategies, but now happened something new, that I cannot explain right now.

    PROBLEM:
    I run the strategy from NT -> and get backtest, that has wrong results, because some orders are magically CANCELLED.

    When I run the same strategy with visual studio debugger and stop at some breakpoints, it runs completely right - because some orders, that were previously cancelled are not cancelled now -> and they are correctly filled.

    MAIN QUESTION:
    Do you have any idea, what/where should I look for, to find out - what mechanisms in NT can cause this type "magic behind the scene" like cancellations of the orders ?


    I know, that such "magic" cancellation can occur ,when :
    • strategy has setup ExitOnClose = true
    • RealtimeErrorHandling - can cause some "magical" cancellations
    • ... maybe something else, I am not aware of...


    PS
    It is easily reproducible always, but I can't understand causes of the "magical" cancellations. Completely right backtest from debugger in Visual Studio, wrong backtest (because of some orders are magically cancelled) when I run backtest without VS debugger.

    I don't call CancelOrder anywhere in my strategy - so I expect, the cancellation process is initiated by some NT API code - for example like in case of automatic error handling, when all position are closed and pending orders cancelled.
    Last edited by misova; 05-16-2015, 07:02 AM.

    #2
    To visualize the problem - I found these cancellations in Output window log:
    Something caused cancellation = orders changed to PendingCancel -> and then Cancelled state.

    Bar: 0/240 (2010-01-06 14:15:00) ; Order update: Order='NT-00011/Backtest' Name='Buy to cover' State=PendingCancel Instrument='6E ##-##' Action=BuyToCover Limit price=1,4347 Stop price=0 Quantity=1 Strategy='TrendLinesStrategy' Type=Limit Tif=Gtc Oco='3nAfnmo0MJ' Filled=0 Fill price=0 Token='030e11dff3d9413d99caa3e5d1a33a57' Gtd='1. 12. 2099 0:00:00'

    Bar: 0/240 (2010-01-06 14:15:00) ; Order update: Order='NT-00011/Backtest' Name='Buy to cover' State=Cancelled Instrument='6E ##-##' Action=BuyToCover Limit price=1,4347 Stop price=0 Quantity=1 Strategy='TrendLinesStrategy' Type=Limit Tif=Gtc Oco='3nAfnmo0MJ' Filled=0 Fill price=0 Token='030e11dff3d9413d99caa3e5d1a33a57' Gtd='1. 12. 2099 0:00:00'


    There are many others similar cancellations, but it is not clear, what causes them.
    These orders should be my profit/stoploss, but they are "magically" cancelled.

    Problem is, that I didn't initiate these cancellations directly from my custom code.

    These cancellations occur in standard backtest, but they DO NOT occur when running the same backtest with breakpoints in Visual Studio -> then these orders are not cancelled and correctly filled.
    Last edited by misova; 05-16-2015, 07:01 AM.

    Comment


      #3
      Originally posted by misova View Post
      To visualize the problem - I found these cancellations in Output window log:
      Something caused cancellation = orders changed to PendingCancel -> and then Cancelled state.

      Bar: 0/240 (2010-01-06 14:15:00) ; Order update: Order='NT-00011/Backtest' Name='Buy to cover' State=PendingCancel Instrument='6E ##-##' Action=BuyToCover Limit price=1,4347 Stop price=0 Quantity=1 Strategy='TrendLinesStrategy' Type=Limit Tif=Gtc Oco='3nAfnmo0MJ' Filled=0 Fill price=0 Token='030e11dff3d9413d99caa3e5d1a33a57' Gtd='1. 12. 2099 0:00:00'

      Bar: 0/240 (2010-01-06 14:15:00) ; Order update: Order='NT-00011/Backtest' Name='Buy to cover' State=Cancelled Instrument='6E ##-##' Action=BuyToCover Limit price=1,4347 Stop price=0 Quantity=1 Strategy='TrendLinesStrategy' Type=Limit Tif=Gtc Oco='3nAfnmo0MJ' Filled=0 Fill price=0 Token='030e11dff3d9413d99caa3e5d1a33a57' Gtd='1. 12. 2099 0:00:00'


      There are many others similar cancellations, but it is not clear, what causes them.
      These orders should be my profit/stoploss, but they are "magically" cancelled.

      Problem is, that I didn't initiate these cancellations directly from my custom code.

      These cancellations occur in standard backtest, but they DO NOT occur when running the same backtest with breakpoints in Visual Studio -> then these orders are not cancelled and correctly filled.
      Your debugger is an external mechanism, interfering with the order processing, and so probably affecting normal processing.

      You want to look in the log to track the complete order of events causing the cancelled orders when running without the debugger. That complete order of events will probably pin-point the cause of the problem.

      Comment


        #4
        Thankx, I checked more deeply and I found the CAUSE of "magical" cancellations
        that I could not explain previously. It is the OCO relation in orders, that caused this.
        One order was filled and many others were cancelled automatically, because of the same OCO.

        So the technical perspective of the problem is SOLVED now.

        I have to check my code, what assigns same OCO strings to unrelated orders,
        but that's different story, that will be solved easily now...

        Misova

        Comment


          #5
          I also found the exact technical problem, why the OCO were generated the same.

          Reason is, that OCO was generated with 2 instances new Random(), that had the same seed (because they were created very fast one after another. The faster computer, the bigger chance this happens), and so they generated same OCO string.

          Solution is very easy - to reuse the same Random() instance instead of creating new one.


          This explains, why the strategy worked fine in debug mode, when I stopped and debugged strategy after each execution.

          This is a lesson learnt for the future, so we have more intuition, where can be root of the problem in similar cases...
          Last edited by misova; 05-18-2015, 01:00 PM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          558 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          324 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          545 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          547 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X