Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Losing IOrder object after placing stop loss and target orders

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

    Losing IOrder object after placing stop loss and target orders

    Hello,
    My strategy enters a limit order, then sets the stop loss and profit target in OnExecution(). I essentially used the code provided in SampleOnOrderUpdate. My strategy wants to move my stop when the order goes into profit. The problem is the IOrder object for my stop order is null, even though the stop order exists. Here is OnExecution code when I place the stop & target. Below that is the code used to attempt to move the stop, which is in a method called from OnBarUpdate().

    protected override void OnExecution(IExecution execution)
    {
    /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
    which ensures your strategy has received the execution which is used for internal signal tracking. */
    if (entryOrder != null && entryOrder == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    if (execution.MarketPosition==MarketPosition.Long)
    {
    stopOrder = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - StopLoss * TickSize, "MyStop", "Monkey Long");
    targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + ProfitTarget * TickSize, "MyTarget", "Monkey Long");
    }

    if (execution.MarketPosition==MarketPosition.Short)
    {
    stopOrder = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + StopLoss * TickSize, "MyTarget", "Monkey Short");
    targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - ProfitTarget * TickSize, "MyStop", "Monkey Short");
    }
    // Resets the entryOrder object to null after the order has been filled
    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    entryOrder = null;
    }
    }
    }

    // Reset our stop order and target orders' IOrder objects after our position is closed.
    if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrder = null;
    targetOrder = null;
    }
    }
    }

    Here is my code used to try and move my stop. I've printed all values to Output for debugging, including "my condition", and stopOrder. It shows stopOrder is null, when there is actually a live stop order, so the code never triggers.
    if (stopOrder != null && my condition exists)
    {
    stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice - newStop * TickSize, "MyStop", "Monkey Long");
    }
    Any ideas what is going on here?
    Thanks

    #2
    Hello TheWolf,
    The code snippet you provided is working fine.

    Can it be the case that the stop/target orders are being nullifed from another part of the code.

    Also please append TraceOrders in the initialize section of the strategy to track the orders
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      I currently use TraceOrders. The only place the stop orders are nullified, is in the OnExecution code that I provided, and originally set to null when declared in Variables. When my condition is met to move the stop I print out the variables in my condition to the output, and also do the following to check the stop:
      if (stopOrder != null)
      Print("stop price = "+stopOrder.StopPrice);
      else
      Print("stop order is null");
      In the output, it prints: "stop order is null", so my code to move the stop is never triggered.
      When my stop are target are entered, the output is the following:
      8/28/2012 10:42:12 AM Entered internal PlaceOrder() method at 8/28/2012 10:42:12 AM: BarsInProgress=0 Action=Sell OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1406.75 SignalName='MyStop' FromEntrySignal='Monkey Long'
      8/28/2012 10:42:12 AM Entered internal PlaceOrder() method at 8/28/2012 10:42:12 AM: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=1411.25 StopPrice=0 SignalName='MyTarget' FromEntrySignal='Monkey Long'

      The stops & target are placed, and they execute correctly when hit, but the IOrder objects are null when I check them.

      Comment


        #4
        Hello TheWolf,
        To assist you further can you please send a toy NinjaScript code* replicating the behavior to support[AT]ninjatrader[DOT]com

        Please append Attn:Joydeep in the subject line of the email and give a reference of this thread in the body of the email.

        I look forward to assisting you further.

        *The "toy" just means something that is a stripped down version that isn't necessarily the whole logic. It makes things easier to rout out.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          OK, I figured it out. Before I started placing the stop & target in OnExecution, I had the following in Initialize():
          SetStopLoss(CalculationMode.Ticks, StopLoss);
          SetProfitTarget(CalculationMode.Ticks, ProfitTarget);
          Once I commented those out,everything worked fine. Imagine that. Duh!

          Thanks for your help.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          646 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          367 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          107 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          569 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          573 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X