Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy orders not triggering correctly

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

    Strategy orders not triggering correctly

    I've developed a few strategies for NQ and CL. These strategies take 1 trade per day, per instrument, and there are four of them that operate at separate time frames. I've attached each strategy to a separate chart.

    At first, these worked properly and would execute trades correctly. But after a few days they stopped executing trades. Instead of executing trades, I get error messages. Sometimes it will execute a trade but not execute the take profit or stop loss. It gives the errors, 'order rejected' or OCO ID cannot be reused. Once it gives the error message, the Control Center disables the strategy entirely

    How do I solve this?
    Attached Files

    #2
    Hello mattlaguardia,

    Once an order that is using an OCOId has been filled, rejected, or cancelled, no other orders can use that OCO ID.

    Ensure that you are using a new unique OCOID for each new pairing, and that you are not attempting to use an OCO ID that has been used for any other order that is filled, rejected, or cancelled.


    Below is a link to an example of using OCO with the unmanaged approach.


    If your inquiry is about a rejected order (which would cause any paired order to also be cancelled due to OCO), be sure the order is being submitted at a valid price, during market hours. Buy stop orders must be above the current ask, sell stop orders must be below the current bid.
    Hi I have a sellshort StopLimit order that i change if unfilled. I tick below the last bar's low. I keep getting the stop price can't be changed above the market but the price value it quotes ( the one I want) is below the market. Currently I am on the simulated feed. I appreciate in real market situations this could happen
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the reply, Chelsea.

      Is it possible to combine all four strategies into one strategy file that names each order individually, and place them into the OnBarUpdate() block?

      Using this as a crude example:

      protected override void OnBarUpdate()
      {
      // Strategy 1: Buy at 3:30AM
      if (StrategyName == "Strategy 1" && Time[0].TimeOfDay == new TimeSpan(3, 30, 0))
      {
      IOrder entryOrder = EnterLong(2, "Strategy1Entry");
      entryOrder.SetStopLoss(CalculationMode.Ticks, 10);
      entryOrder.SetProfitTarget(CalculationMode.Ticks, 20);
      }

      // Strategy 2: Buy at 8:34AM
      if (StrategyName == "Strategy 2" && Time[0].TimeOfDay == new TimeSpan(8, 34, 0))
      {
      IOrder entryOrder = EnterLong(4, "Strategy2Entry");
      entryOrder.SetStopLoss(CalculationMode.Ticks, 3);
      entryOrder.SetProfitTarget(CalculationMode.Ticks, 15);
      }

      // Strategy 3: Sell at 9:30AM
      if (StrategyName == "Strategy 3" && Time[0].TimeOfDay == new TimeSpan(9, 30, 0))
      {
      IOrder exitOrder = ExitLong(4, "Strategy3Exit", "Strategy2Entry");
      exitOrder.SetStopLoss(CalculationMode.Ticks, 3);
      exitOrder.SetProfitTarget(CalculationMode.Ticks, 15);
      }

      // Strategy 4: Sell at 10:36AM
      if (StrategyName == "Strategy 4" && Time[0].TimeOfDay == new TimeSpan(10, 36, 0))
      {
      IOrder exitOrder = ExitLong(4, "Strategy4Exit", "Strategy2Entry");
      exitOrder.SetStopLoss(CalculationMode.Ticks, 3);
      exitOrder.SetProfitTarget(CalculationMode.Ticks, 15);
      }
      }

      Comment


        #4
        Hello mattlaguardia,

        Yes, you could combine the logic into one strategy.

        Is StrategyName a public string the user types in strategy parameters window?

        IOrder is not the right order type. Are you making a script for NinjaTrader 7, or is this for NinjaTrader 8?

        NinjaTrader 8 order variables use the object type 'Order'.


        These objects must be assigned to variables in OnOrderUpdate().
        "Notes
        OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated."



        entryOrder.SetStopLoss is not correct syntax.

        If you are trying to use Set methods with the managed approach, just call SetStopLoss() or SetProfitTarget(). These methods to the Strategy class, and are not sub-methods of Order objects.




        Below I am providing a link to a forum post with helpful resources, including training videos, on getting started with C# and NinjaScript.


        As well as examples of order management.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you for the response Chelsea. This is for NT 8.

          Like this example?

          protected override void OnBarUpdate()

          {

          // Strategy 1: Buy at 3:30AM

          if (StrategyName == "Strategy 1" && Time[0].TimeOfDay == new TimeSpan(3, 30, 0))

          SetProfitTarget(CalculationMode.Ticks, 20);

          SetStopLoss(CalculationMode.Ticks, 10);




          // Place the order at 8:34AM

          if (Time[0].TimeOfDay == new TimeSpan(3, 33, 0))

          {

          entryOrder = EnterLong(2);

          }




          // Check if the entry order has been filled

          if (entryOrder != null && entryOrder.OrderState == OrderState.Filled)

          {

          // Reset the entry order to null

          entryOrder = null;

          }




          // Strategy 2: Buy at 8:34AM

          if (StrategyName == "Strategy 2" && Time[0].TimeOfDay == new TimeSpan(8, 34, 0))

          {

          SetProfitTarget(CalculationMode.Ticks, 15);

          SetStopLoss(CalculationMode.Ticks, 3);




          // Place the order at 8:34AM

          if (Time[0].TimeOfDay == new TimeSpan(8, 34, 0))

          {

          entryOrder = EnterLong(4);

          }




          // Check if the entry order has been filled

          if (entryOrder != null && entryOrder.OrderState == OrderState.Filled)

          {

          // Reset the entry order to null

          entryOrder = null;

          }

          }




          // Strategy 3: Sell at 9:30AM

          if (StrategyName == "Strategy 3" && Time[0].TimeOfDay == new TimeSpan(9, 30, 0))

          SetProfitTarget(CalculationMode.Ticks, 15);

          SetStopLoss(CalculationMode.Ticks, 3);




          // Place the order at 8:34AM

          if (Time[0].TimeOfDay == new TimeSpan(9, 30, 0))

          {

          entryOrder = EnterLong(4);

          }




          // Check if the entry order has been filled

          if (entryOrder != null && entryOrder.OrderState == OrderState.Filled)

          {

          // Reset the entry order to null

          entryOrder = null;

          }




          // Strategy 4: Sell at 10:36AM

          if (StrategyName == "Strategy 4" && Time[0].TimeOfDay == new TimeSpan(10, 36, 0))

          SetProfitTarget(CalculationMode.Ticks, 15);

          SetStopLoss(CalculationMode.Ticks, 3);




          // Place the order at 8:34AM

          if (Time[0].TimeOfDay == new TimeSpan(10, 36, 0))

          {

          entryOrder = EnterLong(4);

          }




          // Check if the entry order has been filled

          if (entryOrder != null && entryOrder.OrderState == OrderState.Filled)

          {

          // Reset the entry order to null

          entryOrder = null;

          }

          }

          }}

          Comment


            #6
            Hello mattlaguardia,

            In the suggested code, you are attempting to assign an Order object to a variable from the order method call.

            entryOrder = EnterLong(2);

            This is what I am advising you will not work.

            The entryOrder variable, must be assigned from OnOrderUpdate().
            These objects must be assigned to variables in OnOrderUpdate().
            "Notes
            OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated."
            https://ninjatrader.com/support/help...rderupdate.htm
            If you are checking an order is filled and setting the variable to null, that should also be in OnOrderUpdate when the order fills and not in OnBarUpdate() when the bar is updating.​
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            75 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            146 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            79 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            50 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            54 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X