Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adjusting Order Quantity of Stops/Take Profits After ScaleIn

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

    Adjusting Order Quantity of Stops/Take Profits After ScaleIn

    Morning, I've tried everything I know to try and I can't get this to work. My strategy scales in if the price goes against it. It then needs to update the order quantity for the stop loss and take profit (the take profit price is also changed). I've been doing this by cancelling the existing exit orders and adding new ones. Everything seems to work fine, but when it scales in the order quantity of the new stop and take profit are 1. I checked to make sure Order.Quantity was correctly 2 and it was, so I explicitly set the order quantity of the new orders to 2 and that also failed. I thought that perhaps it was the ATM strategy order quantity getting in the way so I set that to 2 and that also didn't help. Here's the problematic code: what am I doing wrong?

    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    // Phase 1: Cancel Existing Orders
    if (execution.Order == longScaleInOrder)
    {
    Print($"Scale-in executed for long position at {price}. Initiating cancellation phase.");

    // Cancel any existing long exit orders
    if (longLimitOrder != null)
    {
    CancelOrder(longLimitOrder);
    longLimitOrder = null;
    Print("Cancelled existing long limit order.");
    }
    if (longStopOrder != null)
    {
    CancelOrder(longStopOrder);
    longStopOrder = null;
    Print("Cancelled existing long stop order.");
    }

    // Set a flag indicating new orders need to be placed
    isLongOrdersPending = true;
    }
    else if (execution.Order == shortScaleInOrder)
    {
    Print($"Scale-in executed for short position at {price}. Initiating cancellation phase.");

    // Cancel any existing short exit orders
    if (shortLimitOrder != null)
    {
    CancelOrder(shortLimitOrder);
    shortLimitOrder = null;
    Print("Cancelled existing short limit order.");
    }
    if (shortStopOrder != null)
    {
    CancelOrder(shortStopOrder);
    shortStopOrder = null;
    Print("Cancelled existing short stop order.");
    }

    // Set a flag indicating new orders need to be placed
    isShortOrdersPending = true;
    }

    // Phase 2: Place New Orders
    if (isLongOrdersPending)
    {
    Print($"Placing new long exit orders for position size: {Position.Quantity}");

    longLimitOrder = ExitLongLimit(0, true, Position.Quantity, originalEntryPrice, "NewExitLongLimit", "ScaleInLong");
    Print($"New long limit order placed at {originalEntryPrice} with quantity {Position.Quantity}");

    longStopOrder = ExitLongStopMarket(0, true, Position.Quantity, stopPrice, "NewExitLongStop", "ScaleInLong");
    Print($"New long stop order placed at {stopPrice} with quantity {Position.Quantity}");

    isLongOrdersPending = false; // Reset the flag
    }
    else if (isShortOrdersPending)
    {
    Print($"Placing new short exit orders for position size: {Position.Quantity}");

    shortLimitOrder = ExitShortLimit(0, true, Position.Quantity, originalEntryPrice, "NewExitShortLimit", "ScaleInShort");
    Print($"New short limit order placed at {originalEntryPrice} with quantity {Position.Quantity}");

    shortStopOrder = ExitShortStopMarket(0, true, Position.Quantity, stopPrice, "NewExitShortStop", "ScaleInShort");
    Print($"New short stop order placed at {stopPrice} with quantity {Position.Quantity}");

    isShortOrdersPending = false; // Reset the flag
    }
    // Original entrys and stop and take profit placement
    if (execution.Name == "BullishGapEntry")
    {
    // Set stop loss and profit target for long position with isLiveUntilCancelled
    longStopOrder = ExitLongStopMarket(0, true, quantity, stopPrice, "ExitLongStop", "BullishGapEntry");
    longLimitOrder = ExitLongLimit(0, true, quantity, targetPrice, "ExitLongLimit", "BullishGapEntry");
    longScaleInOrder = EnterLongLimit(0, true, 1, High[2], "ScaleInLong");
    Print($"longScaleInOrder: {scaleInPriceLong}");

    }
    else if (execution.Name == "BearishGapEntry")
    {
    // Set stop loss and profit target for short position with isLiveUntilCancelled
    shortStopOrder = ExitShortStopMarket(0, true, quantity, stopPrice, "ExitShortStop", "BearishGapEntry");
    shortLimitOrder = ExitShortLimit(0, true, quantity, targetPrice, "ExitShortLimit", "BearishGapEntry");
    Print($" ExitShortLimit: {targetPrice}, ExitShortLimit");
    shortScaleInOrder = EnterShortLimit(0, true, 1, Low[2], "ScaleInShort");
    Print($" shortScaleInOrder price: {scaleInPriceShort}");
    }
    }​

    Click image for larger version

Name:	image.png
Views:	139
Size:	261.3 KB
ID:	1330472

    #2
    Jk, I figured it out. I used ChangeOrder instead and that fixed it.

    Comment


      #3
      Hello Draikon7276,

      Assign orders to variables from the order parameter of OnOrderUpdate() only.

      From the Desktop SDK:
      "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."
      Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


      Then to change the order price or quantity, supply the variable (assigned from OnOrderUpdate()) to the ChangeOrder() method.
      Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


      Below is a link to examples that modify orders.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      52 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      70 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      48 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X