Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT 6.5-Stop not triggering on multiple orders

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

    NT 6.5-Stop not triggering on multiple orders

    Hi, I'm using the example on
    When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until

    as basis for this code.

    What I'm trying to do is:
    - Enter long 2 contracts at limit
    - Have the same initial stop loss for both contracts
    - Have different targets for each contract
    - Be able to trail my stop on 2nd contract (Long 1b)

    The problem I'm having is the the 1st profit target order below (targetOrder1) triggers but not the stop orders (stopOrder1)...

    stopOrder1 = ExitLongStop(0, true, 1, execution.Price - 8 * TickSize, "stop", "Long Entry");
    targetOrder1 = ExitLongLimit(0, true, 1, execution.Price + 4 * TickSize, "target1a", "Long Entry");
    stopOrder2 = ExitLongStop(0, true, 1, execution.Price - 8 * TickSize, "stop1b", "Long Entry");
    targetOrder2 = ExitLongLimit(0, true, 1, execution.Price + 20 * TickSize, "target1b", "Long Entry");

    Can anyone please point me in the right direction?



    Code:
            protected override void OnBarUpdate()
            {
                // Return if there aren't enough loaded bars
                if (CurrentBar < 20)
                    return;
                
                // First, we need a simple entry. Then entryOrder == null checks to make sure entryOrder does not contain an order yet.
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    bLongSignal = Close[1] > SMA(20)[1];
                    bShortSignal = Close[1] < SMA(20)[1];
    
                    // Check IOrder objects for null to ensure there are no working entry orders before submitting new entry order
                    if (entryOrder == null && bLongSignal)
                    {
                        /* Our IOrder object, entryOrder, is assigned an entry order.
                        It is offset 5 ticks below the low to try and make it not execute to demonstrate the CancelOrder() method. */
                        entryOrder = EnterLongLimit(0, true, 2, Median[0], "Long Entry");
                        
                        // Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
                        barNumberOfOrder = CurrentBar;
                    }                
                    
                    // If entryOrder has not been filled within 3 bars, cancel the order.
                    else if (entryOrder != null && CurrentBar > barNumberOfOrder + 3)
                    {
                        // When entryOrder gets cancelled below in OnOrderUpdate(), it gets replaced with a Market Order via EnterLong()
                        CancelOrder(entryOrder);
                    }
                }
            }
            
            protected override void OnOrderUpdate(IOrder order)
            {
                // Checks for all updates to entryOrder.
                if (entryOrder != null && entryOrder.Token == order.Token)
                {    
                    // Check if entryOrder is cancelled.
                    if (order.OrderState == OrderState.Cancelled)
                    {
                        // Reset entryOrder back to null
                        entryOrder = null;
                        
                        // Replace entry limit order with a market order.
                        //mar****rder = EnterLong(1, "market order");
                    }
                }
            }
            
            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.
                
                This first if-statement is in place to deal only with the long limit entry. */
                if (entryOrder != null && entryOrder.Token == execution.Order.Token)
                {
                    // This second if-statement is meant to only let fills and cancellations filter through.
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                    {
                        // Simple stop and target
                        stopOrder1 = ExitLongStop(0, true, 1, execution.Price - 8 * TickSize, "stop", "Long Entry");
                        targetOrder1 = ExitLongLimit(0, true, 1, execution.Price + 4 * TickSize, "target1a", "Long Entry");
                        stopOrder2 = ExitLongStop(0, true, 1, execution.Price - 8 * TickSize, "stop1b", "Long Entry");
                        targetOrder2 = ExitLongLimit(0, true, 1, execution.Price + 20 * TickSize, "target1b", "Long Entry");
                        
                        // Resets the entryOrder object to null after the order has been filled
                        if (execution.Order.OrderState != OrderState.PartFilled)
                        {
                            entryOrder     = null;
                        }
                    }
                }
                
            }

    #2
    trasdinghumble, you would need to scale in with two entryorders to be able to scale out later the way you have in mind - http://www.ninjatrader-support2.com/...ead.php?t=3751

    Comment


      #3
      Bertrand, I did try to scale in using :

      entryOrder1 = EnterLongLimit(0, true, 1, Median[0], "Long 1a");
      entryOrder2 = EnterLongLimit(0, true, 1, Median[0], "Long 1b");



      but the 2nd order never triggers... entryOrder1 <> null but entryOrder2 == null...

      Originally posted by NinjaTrader_Bertrand View Post
      trasdinghumble, you would need to scale in with two entryorders to be able to scale out later the way you have in mind - http://www.ninjatrader-support2.com/...ead.php?t=3751
      Last edited by tradinghumble; 10-21-2009, 09:16 AM.

      Comment


        #4
        Then please enable TraceOrders in the Initialize() and rerun the strategy with the output windows open to debug why this occurs - http://www.ninjatrader-support2.com/...ead.php?t=3627

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        666 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        376 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        110 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        580 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X