Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Sample code for unmanaged approach handling IgnoreAllErrors and HasOverfill

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

    Sample code for unmanaged approach handling IgnoreAllErrors and HasOverfill

    Since it appears that StopCancelCloseIgnoreRejects is not working properly in my strategy, I would like to know how exactly to handle errors and overfills if I use IgnoreAllErrors instead. Specifically, I want to do the following:

    1. If an overfill was detected on any order, I want to close out my entire position.
    2. If any error other than order rejection occurs, I also want to close out my entire position.

    To do these, of course I need accurate information for what my position is when I try to close it. What I experienced today was that StopCancelCloseIgnoreRejects gave the wrong information about my position and ended up reversing my position before I noticed and had to manually close.

    So, please provide some sample code for how to handle any orders and overfills. I want to make sure that no error conditions fall through the cracks and leave me with an unintended open position.

    #2
    Hello westofpluto,

    Could you please elaborate on what did not appear to work properly with StopCancelCloseIgnoreRejects? You mentioned that, "gave the wrong information about my position and ended up reversing my position before I noticed and had to manually close." What was the correct information vs. the information that you got? How did you gather the position information in this event? Who is your broker? That may be the root of what needs to be addressed and we would be glad to take a look at the unexpected behavior if that is the case.

    I have addressed your concerns 1 and 2below:
    1. If an overfill was detected on any order, I want to close out my entire position.
    When using IgnoreOverfill, the bool HasOverfill will be true on the Order object if an overfill is detected. You could add logic to close your position if HasOverfill is true. You should be able to use position update information to understand your position and size to submit an order to close the position out. You could test this with prints of Position.MarketPosition and Position.Quantity.
    2. If any error other than order rejection occurs, I also want to close out my entire position.
    You could add a check that if the error value from OnOrderUpdate() does not equal ErrorCode.NoError, then add logic that will close out your position.

    As for sample code, there are snippets in the help guide sections linked that might offer some insight. Ultimately, the unmanaged approach is reserved for very experienced programmers as mentioned in the help guide. There are some important considerations and even any sample code our support team might offer may not be fully inclusive to cover every possible scenario that might end up in an unwanted position. An unintended open position is always possible as a risk of electronic trading. It is up to the developer to also thoroughly test out the code to make sure it works in various scenarios and to add logic and error handling that will cover potential scenarios to try and prevent unwanted positions. If you could explain what happened with StopCancelCloseIgnoreRejects, we might be able to narrow down on the potential cause and offer a solution to prevent it from happening in the future.

    Thank you for your patience and understanding. I look forward to your reply.​

    Comment


      #3
      Emily
      Today I provided Jesse with all the information you asked about, including trace and log files. Apparently I am using an old version of NT8 from 2020 (version 8.0.24) and Jesse said they cannot test any problems in this "older" version, which comes across to me as a complete copout.
      My broker is Ninjatrader. My strategy used StopCancelCloseIgnoreRejects, which is supposed to close all my positions if there is an error. There was indeed an overfill error on one of my orders. Upon that error (which has me 2 extra long contracts), the strategy tried to close all positions and disable. In doing so, it not only failed to close the 2 longs, it added 3 more longs to my position and left that open. My strategy does not do any error handling, so all the order handling is done internally by the NT platform, and it obviously screwed up. I don't know how to say it more plainly and explicitly than this. You have my order and trace logs. Please look into them and see why your automatic close resulted in more than doubling my erroneous position size and leaving it open.​

      Comment


        #4
        See my answer here.

        Comment


          #5
          Originally posted by westofpluto View Post
          Emily
          Today I provided Jesse with all the information you asked about, including trace and log files. Apparently I am using an old version of NT8 from 2020 (version 8.0.24) and Jesse said they cannot test any problems in this "older" version, which comes across to me as a complete copout.
          My broker is Ninjatrader. My strategy used StopCancelCloseIgnoreRejects, which is supposed to close all my positions if there is an error. There was indeed an overfill error on one of my orders. Upon that error (which has me 2 extra long contracts), the strategy tried to close all positions and disable. In doing so, it not only failed to close the 2 longs, it added 3 more longs to my position and left that open. My strategy does not do any error handling, so all the order handling is done internally by the NT platform, and it obviously screwed up. I don't know how to say it more plainly and explicitly than this. You have my order and trace logs. Please look into them and see why your automatic close resulted in more than doubling my erroneous position size and leaving it open.​
          Hello westofpluto,

          Thank you for that information; I did not realize that Jesse was already on the case and working with you on this.

          As you already have a separate open thread about this as well as an open support case with your diagnostic files, I will let Jesse continue to assist you so as not to duplicate efforts. I sincerely appreciate your time and patience with this situation. I would like to add that version 8.0.24.0 was released on March 10, 2021 and there have been several updates and issue fixes since then. If you would like to review the release notes for each version that include release dates and details about issues that were fixed and features that were added, please see the notes in our help guide here:


          Thank you again for your time and patience.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, Yesterday, 11:51 AM
          0 responses
          12 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, Yesterday, 11:48 AM
          0 responses
          19 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-25-2026, 09:53 PM
          0 responses
          16 views
          0 likes
          Last Post CaptainJack  
          Started by CaptainJack, 03-25-2026, 09:51 PM
          0 responses
          13 views
          0 likes
          Last Post CaptainJack  
          Started by Mindset, 03-23-2026, 11:13 AM
          0 responses
          18 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Working...
          X