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 OCO Behavior

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

    Unexpected OCO Behavior

    When I initially get an order fill in a strategy, the stop and targets generated by the code below do not have OCO values. If I reload the strategy with an active position, the stop and target orders are assigned OCO values. The desired behavior I want is to not have OCO values on the orders. Any idea what is going on?

    PHP Code:
    #region OnExecution
      
    protected override void OnExecution(IExecution execution)
      {
       
    // Only used in Print statements
       
    string TimeStamp Time[0].ToString()+" "+Instrument.MasterInstrument.Name+" "+BarsPeriod.Value.ToString();
       Print(
    TimeStamp+" In OnExecution, Entry Order="+entryOrder.OrderId+", Entry Order="+execution.Order.OrderId+", Current Order="+execution.Order.ToString());
       
    #region Entry Order Filled
       
    if (entryOrder != null &&
        
    entryOrder.OrderId == execution.Order.OrderId && 
        
    execution.Order.OrderState == OrderState.Filled)
       {
        
    double fillPrice entryOrder.AvgFillPrice;
        
    int fillQty entryOrder.Quantity;
        if (
    bEntryLong == true)
        {
         Print(
    TimeStamp+"\tLong Entry Fill at "+fillPrice);
         if (
    Stop0Ticks != 0)
         {
          
    exitS0Order ExitLongStop(0trueEntryQtyfillPrice+Stop0Ticks*TickSize"Stop0""Long");
          Print(
    TimeStamp+"\tStop S0 "+EntryQty+" Set at "+(fillPrice+Stop0Ticks*TickSize));
         }
         if (
    Target1Ticks != 0)
         {
          
    exitT1Order ExitLongLimit(0trueTarget1QtyfillPrice+Target1Ticks*TickSize"Target1""Long");
          Print(
    TimeStamp+"\tTarget T1 "+Target1Qty+" Set at "+(fillPrice+Target1Ticks*TickSize));
             }
         if (
    Target2Ticks != 0)
         {
          
    exitT2Order ExitLongLimit(0trueTarget2QtyfillPrice+Target2Ticks*TickSize"Target2""Long");
          Print(
    TimeStamp+"\tTarget T2 "+Target2Qty+" Set at "+(fillPrice+Target2Ticks*TickSize));
         }
         if (
    Target3Ticks != 0)
         {
          
    exitT3Order ExitLongLimit(0trueTarget3QtyfillPrice+Target3Ticks*TickSize"Target3""Long");
          Print(
    TimeStamp+"\tTarget T3 "+Target3Qty+" Set at "+(fillPrice+Target3Ticks*TickSize));
         }
         if (
    Target4Ticks != 0)
         {
          
    exitT4Order ExitLongLimit(0trueTarget4QtyfillPrice+Target4Ticks*TickSize"Target4""Long");
          Print(
    TimeStamp+"\tTarget T4 "+Target4Qty+" Set at "+(fillPrice+Target4Ticks*TickSize));
         }
        }
        if (
    bEntryShort == true)
        {
         Print(
    TimeStamp+"\tShort Entry Fill at "+fillPrice);
         if (
    Stop0Ticks != 0)
         {
          
    exitS0Order ExitShortStop(0trueEntryQtyfillPrice-Stop0Ticks*TickSize"Stop0""Short");
          Print(
    TimeStamp+"\tStop S0 "+EntryQty+" Set at "+(fillPrice-Stop0Ticks*TickSize));
         }
         if (
    Target1Ticks != 0)
         {
          
    exitT1Order ExitShortLimit(0trueTarget1QtyfillPrice-Target1Ticks*TickSize"Target1""Short");
          Print(
    TimeStamp+"\tTarget T1 "+Target1Qty+" Set at "+(fillPrice-Target1Ticks*TickSize));
         }
         if (
    Target2Ticks != 0)
         {
          
    exitT2Order ExitShortLimit(0trueTarget2QtyfillPrice-Target2Ticks*TickSize"Target2""Short");
          Print(
    TimeStamp+"\tTarget T2 "+Target2Qty+" Set at "+(fillPrice-Target2Ticks*TickSize));
         }
         if (
    Target3Ticks != 0)
         {
          
    exitT3Order ExitShortLimit(0trueTarget3QtyfillPrice-Target3Ticks*TickSize"Target3""Short");
          Print(
    TimeStamp+"\tTarget T3 "+Target3Qty+" Set at "+(fillPrice-Target3Ticks*TickSize));
         }
         if (
    Target4Ticks != 0)
         {
          
    exitT4Order ExitShortLimit(0trueTarget4QtyfillPrice-Target4Ticks*TickSize"Target4""Short");
          Print(
    TimeStamp+"\tTarget T4 "+Target4Qty+" Set at "+(fillPrice-Target4Ticks*TickSize));
         }
        }
       }
       
    #endregion
      
    }
      
    #endregion 
    The Trading Mantis
    NinjaTrader Ecosystem Vendor - The Trading Mantis

    #2
    Hello gregschr,

    Can you clarify where you are checking OCO values for these orders? These order methods don't allow you to specify OCO values, although you may see this behavior as part of internal order handling rules.

    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      I am seeing the OCO values on the Orders tab of the Control Panel and also in IB Trader Workstation.

      I'm not trying to set or use the OCO values. Everything functions fine unless I reload the strategy with an active position. After I reload the script, T2, T3 and T4 get cancelled after T1 is hit. I don't want this behavior

      Thanks.
      The Trading Mantis
      NinjaTrader Ecosystem Vendor - The Trading Mantis

      Comment


        #4
        gregschr,

        Make sure you have Stop and Target handling here set to PerEntryExecution.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Ryan, I checked and handling is set to PerEntryExecution. But I am not sure it is applicable since I am not using SetStopLoss(), SetTrailStop() or SetProfitTarget(). What does this setting do with orders such as ExitLongStop and ExitLongLimit?
          The Trading Mantis
          NinjaTrader Ecosystem Vendor - The Trading Mantis

          Comment


            #6
            This property will apply to Set statements or the Exit methods. Can you let us know which version you're using?
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              I using Version 7 Beta 18
              The Trading Mantis
              NinjaTrader Ecosystem Vendor - The Trading Mantis

              Comment


                #8
                Can you attach the complete strategy file you're using or send to [email protected]

                To confirm the steps needed to see this:
                1) strategy enters position
                2) Reload strategy

                Issue is that targets at other levels are canceled when the first is hit?
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  In the process of trying to understand what was going on, I modified my test code. I now can't reproduce the problem I was having, but get another, even more bizarre problem. When a target fills, OnExecution and OnOrderUpdate do not appear to get called.

                  I've attached the strategy file that is doing this.

                  I'll keep playing with this to see if I can also reproduce the orginal problem I was having.
                  Attached Files
                  The Trading Mantis
                  NinjaTrader Ecosystem Vendor - The Trading Mantis

                  Comment


                    #10
                    I've been able to duplicate the original problem.

                    In Control Center Options/Strategies tab/NinjaScript tab, select the "Immediately submit live working historical orders" radio button and uncheck "Cancel exit orders when a strategy is disabled".

                    I also re-enabled the historical process in the prior strategy I posted. The revised strategy with historical processing enabled is attached to this post.

                    You don't need to wait for an exit fill. Once you have an entry fill and the targets and stop orders are placed, reload the strategy and the target and stop orders are then all linked by the same OCO group.

                    Also if one of the orders fills and then you try to reload the strategy again. All the orders are cancelled by the broker because the OCO value matches the value of a group that was already filled.

                    I'm using IB.
                    Attached Files
                    The Trading Mantis
                    NinjaTrader Ecosystem Vendor - The Trading Mantis

                    Comment


                      #11
                      On Execution Problem

                      Back to the OnExecution Problem. I have attached 2 stategies.

                      SimpleEntryTest65 works correctly in both 6.5 and 7.0. Calls to OnExecution happen as expected.

                      SimpleEntryTest only runs on 7.0 because of GridCategory used in properties. Calls to OnExecution do not happen in this version.

                      I believe the only diff is the use of GridCategory.
                      Attached Files
                      The Trading Mantis
                      NinjaTrader Ecosystem Vendor - The Trading Mantis

                      Comment


                        #12
                        Thanks - I'll take a look at these items and get back to you later today.
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          This is really bizarre.... I saved the file that was not calling OnExecution with a different name (all the code is the same) and the new one works correctly.
                          The Trading Mantis
                          NinjaTrader Ecosystem Vendor - The Trading Mantis

                          Comment


                            #14
                            Thanks for the update. I'm using the original name SimpleEntryTest and am seeing calls to OnExecution() here.
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #15
                              Before saving the file under a new name, I installed Beta 19 over 18 and had the same problem. I also tried deleting the NinjaTrader.Custom.dll and NinjaTrader.Custom.xml files, and recompling.

                              I don't know if it makes any difference, but are you running the SimpleEntryTest with IB?

                              It almost seems like part of the strategy is cached somewhere and the cached portion is being called. This may be one of those unresolved problems that goes away and hopefully never comes up again.
                              The Trading Mantis
                              NinjaTrader Ecosystem Vendor - The Trading Mantis

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ETFVoyageur, Yesterday, 10:13 PM
                              3 responses
                              25 views
                              0 likes
                              Last Post ETFVoyageur  
                              Started by ETFVoyageur, Yesterday, 12:52 AM
                              3 responses
                              33 views
                              0 likes
                              Last Post ETFVoyageur  
                              Started by ETFVoyageur, Today, 02:10 AM
                              2 responses
                              27 views
                              0 likes
                              Last Post ETFVoyageur  
                              Started by ryan_21, Today, 08:46 PM
                              1 response
                              2 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by ESHunter, Today, 08:06 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X