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

Market Exit order OCO ID

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

    Market Exit order OCO ID

    I've been testing a Strategy on a live CQG Demo account today for the first time off-simulator. I've repeatedly encountered an issue where a Stop Loss order was hit; and also an Exit Signal triggered simultaneously.

    Rather than making the Strategy Flat, this opened an unprotected position in the opposite direction, and the strategy stopped making any further executions.

    This is basically what I'm doing:

    Code:
    if(entryConditionTrue) {
          EnterLong(1, "LONGSIGNAL");
          SetStopLoss("LONGSIGNAL", CalculationMode.Price, stopPrice, false);
          
    }

    then later....

    Code:
    if(exitConditionTrue) {
           ExitLong("LONGSIGNAL");
    }
    The problem appears to be that the Market Order for the "ExitLong" call doesn't have an OCO ID. I can see that this is the case in the orders tab in the control center. So the market order is accepted even though the StopLoss Order has been filled.

    I saw this issue happen 3 times within the first 10 minutes of the session today. How can I ensure that exit Market orders have an OCO ID so they don't fill if the Stop has already filled?

    #2
    Hello kevinenergy,

    Thanks for your post.

    ExitLong should not submit an order if the strategy is not in a long position. However, there is still the occurrence of an in-flight execution which sounds to be the case you are referring to.

    The Managed Approach will only OCO exit orders placed with the Set methods. If you would like full control over using OCO id's I suggest using the Unmanaged Approach. I have attached an example Unmanaged strategy and I have included a link to our Unmanaged documentation. This example is loosely based off of the SampleOnOrderUpdate strategy.

    Unamanged Approach - https://ninjatrader.com/support/help...d_approach.htm

    SampleOnOrderUpdate (Useful for understanding advanced order handling methods) - https://ninjatrader.com/support/help...and_onexec.htm

    Also to note with the Managed Approach, we suggest calling the Set methods when the strategy is in a flat position to reset stops/targets so when your next Enter method comes back as filled, the appropriate levels are used. You may see the example below for reference.

    SamplePriceModification - https://ninjatrader.com/support/help...of_stop_lo.htm

    Please let us know if we can be of further help.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hey Jim,

      thanks for the response. This is a huge setback. I will need to work out whether it will be easier to rework the whole thing to use Unmanaged Orders or to rework it to not use Market Orders. I despair.

      Isn't there a secret under-the-hood way you know of to Force the OCO ID on the Exit Order using the Managed Approach? Feel free to PM me the details.

      Thanks for the links and the samples.

      Comment


        #4
        Hello kevinenergy,

        There is not a supported way to apply OCO id's to orders of the Managed Approach, and I'm not aware of another way this could be done. I'll leave the thread open for any community members who have attempted this and would like to share their input.

        I've also submitted a feature request on your behalf to consider adding OCO functionality to the Managed Approach. The ticket ID is SFT-3564. We cannot offer an ETA or promise of fulfillment, but your interest is tracked.

        If there is anything else we can do to assist, please let us know.
        JimNinjaTrader Customer Service

        Comment


          #5
          I created an extension method to allow managed order submission with an OCO Id. I have not tested it with a real broker account, only the NT sim account so far.

          public static class StrategyBaseExtensions
          {
          private const string MethodNameSubmitOrderManaged = "SubmitOrderManaged";
          private static MethodInfo _methodInfoSubmitOrderManaged;

          public static Order SubmitOrderManaged(this StrategyBase strategy, int barsInProgress, bool isLiveUntilCancelled, OrderAction orderAction,
          OrderType orderType, int quantity, double limitPrice = 0.0, double stopPrice = 0.0, string signalName = null, string fromEntrySignal = null,
          string oco = null, bool isSimulatedStop = false)
          {
          if (_methodInfoSubmitOrderManaged == null)
          {
          _methodInfoSubmitOrderManaged = typeof(StrategyBase).GetMethods(BindingFlags.NonPu blic | BindingFlags.Instance)
          .FirstOrDefault(x => x.Name.Equals(MethodNameSubmitOrderManaged));

          if (_methodInfoSubmitOrderManaged == null)
          throw new InvalidOperationException($"Reflection of {MethodNameSubmitOrderManaged} method failed");
          }

          return _methodInfoSubmitOrderManaged.Invoke(strategy,
          new object[]
          {
          barsInProgress, isLiveUntilCancelled, orderAction, orderType, quantity,
          limitPrice, stopPrice, signalName, fromEntrySignal, oco, isSimulatedStop
          }) as Order;
          }
          }


          Comment


            #6
            This is a neat solution using reflection. In the end I went down the unmanaged approach and have a system that works for me. But I wish I had thought of this back then. Writing an unmanaged order system for Ninjatrader is a LOT of work.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, Yesterday, 06:40 PM
            2 responses
            19 views
            0 likes
            Last Post algospoke  
            Started by ghoul, Today, 06:02 PM
            3 responses
            14 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by jeronymite, 04-12-2024, 04:26 PM
            3 responses
            45 views
            0 likes
            Last Post jeronymite  
            Started by Barry Milan, Yesterday, 10:35 PM
            7 responses
            21 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by AttiM, 02-14-2024, 05:20 PM
            10 responses
            181 views
            0 likes
            Last Post jeronymite  
            Working...
            X