Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Unexpected behaviour caused by the Internal Order Handling

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

    Unexpected behaviour caused by the Internal Order Handling

    When running my strategy, I encoutered a pretty weird and potentially bad situation that is probably caused by NT's Managed Approach.
    The strategy happened to issue the closing order for a short position and the opening market order for a long position at the same time, because the conditions for both were met. However, instead of one long, this resulted in a position of two long contracts, one of them uncontrolled by the strategy and dangerously "on the loose". On investigating the log I found that the NT engine issued a "Close Position" order before my two orders were issued, hence three long orders.
    Now, the only reason known to me why the NT engine would do something not unauthorized by a strategy is the Managed Approach. However, the only thing that the internal order handling would do, at least per the documentation, is preventing an order to be issued in certain cases, like for instance the one that occured with my strategy, which basically was an order to reverse the position. On the other hand, my opening order was a market order, and I did not get the usual Internal OrderHandling Violation message in the log. I hope someone can explain that "mystery" to me, and more importantly, how to prevent it from happening again.
    Last edited by Loki45; 10-23-2015, 08:20 PM.

    #2
    Hello Loki45,

    Thank you for writing in. To make sure I am fully understanding:
    1) You are in a short position
    2) Your strategy conditions cause an order to close the short position and an order to enter a long position at the same time
    3) You end up with an extra long position that is not tied to the rest of the strategy

    If this is the case, what I believe is happening is that when using the EnterLong (or EnterLongStop, etc) you are still in a position of 1 short so it is submitting the order with a quantity of 2 to reverse the position (this is part of the managed order handling method) plus you have an order with a quantity of 1 submitted to close the position at the same time. So you end up with 3 orders, one of which is not tied to anything because it was supposed to bring you flat.

    To prevent this, I recommend adding a simple bool into your strategy so that if one condition is true, the other condition cannot also be true. Then reset the bool at the end of OnBarUpdate().

    For example:
    Code:
    private bool onlyOneCondition = false;
    if(ClosePositionIsTrue)
    {
    onlyOneCondition = true;
    ExitShort();
    }
    if(ReversePositionIsTrue && !onlyOneCondition)
    {
    EnterLong();
    }
    This would prevent both exitshort and enterlong from getting called on the same bar.

    If you require further assistance, please provide the snippet of your code which controls this behavior in your strategy so I can provide a more specific example.

    Thank you in advance.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      Thanks for your reply. While it does not explain why an order with the name "Close Position" was issued - which can not be from the strategy, because there is no order with this name -, I think adding some boolean logic like you suggested makes sense.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Barry Milan, Yesterday, 10:35 PM
      7 responses
      19 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by AttiM, 02-14-2024, 05:20 PM
      10 responses
      179 views
      0 likes
      Last Post jeronymite  
      Started by ghoul, Today, 06:02 PM
      0 responses
      9 views
      0 likes
      Last Post ghoul
      by ghoul
       
      Started by DanielSanMartin, Yesterday, 02:37 PM
      2 responses
      13 views
      0 likes
      Last Post DanielSanMartin  
      Started by DJ888, 04-16-2024, 06:09 PM
      4 responses
      13 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Working...
      X