Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy to act on positions not opened by the strategy?

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

    Strategy to act on positions not opened by the strategy?

    If I opened a long position manually, can a strategy access it? And can it manage that position (close it,etc)? I would like my strategy to only manage my exits as I open positions manually. Right now my strategy cannot see open positions.

    SOLVED, I had to:
    • Use PositionAccount Instead of Position:
      PositionAccount reflects the actual account position, including manual trades. If your strategy needs to interact with manual trades, check PositionAccount.MarketPosition to reflect the current state.
    • Disable Strategy Position Tracking:
      When setting up a strategy, uncheck the "Start behavior" for Sync Account Position if you're trying to manage positions manually outside the strategy.
    • Use Events Like OnPositionUpdate():
      Use the OnPositionUpdate() method to track any manual changes to positions. This will let you handle any updates that occur manually outside your strategy.
    Last edited by MiCe1999; 09-15-2024, 07:49 PM.

    #2
    Hello MiCe1999,

    Looks like you were able to get it worked out.

    Below is a link to the help guide for future reference.


    Note, manual orders would not update the OnPositionUpdate override.
    It would be necessary instead to add an event handler method to the Account.PositionUpdate event.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello MiCe1999,

      Looks like you were able to get it worked out.

      Below is a link to the help guide for future reference.


      Note, manual orders would not update the OnPositionUpdate override.
      It would be necessary instead to add an event handler method to the Account.PositionUpdate event.
      https://ninjatrader.com/support/help...tionupdate.htm
      Thank you for the information, I am new to this so every bit helps. I think I am confused about few things:

      My goal: I want the strategy to act (trim, close all or add) on all open positions, no matter if opened manually or through strategy

      1. Do I have to use unmanaged approach in my strategy given that entries are a mix of manual and strategy positions?
      2. I am subscribing to the following events, am I missing anything?
      • Account.OrderUpdate - Critical for order tracking
      • Account.ExecutionUpdate - Critical for fills
      • Account.AccountStatusUpdate - For account state awareness

      Comment


        #4
        Hello MiCe1999,

        As you are managing manual orders, I would recommend using the addon approach in an indicator or addon script.

        The Account event handlers are using the addon approach. I would also recommend submitting the automated orders with Account.CreateOrder() and Account.Submit().
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi.

          Thank you for the guidance. I switched from strategy to indicator. I do have an issue, when I create and edit stop loss order on the chart it results in an additional order being created. For example:
          Below, I create stop loss order (ID17001)
          I modify qty of the order on the chart (new order ID17002)
          I modify qty of the order again (new order ID17003)


          Order Update [11/25/2024 6:00:00 PM] - Order 17001:
          State: Initialized
          Order: Sell Stop @ 20971
          Quantity: 1
          Order Update [11/25/2024 6:00:00 PM] - Order 17001:
          State: Submitted
          Order: Sell Stop @ 20971
          Quantity: 1
          Order Update [11/25/2024 6:00:00 PM] - Order 17001:
          State: Accepted
          Order: Sell Stop @ 20971
          Quantity: 1
          Order Update [11/25/2024 6:00:00 PM] - Order 17002:
          State: Initialized
          Order: Sell Stop @ 20971
          Quantity: 1
          Order Update [11/25/2024 6:00:00 PM] - Order 17002:
          State: Submitted
          Order: Sell Stop @ 20971
          Quantity: 1
          Order Update [11/25/2024 6:00:00 PM] - Order 17002:
          State: Accepted
          Order: Sell Stop @ 20971
          Quantity: 1
          Order Update [11/25/2024 6:00:00 PM] - Order 17003:
          State: Initialized
          Order: Sell Stop @ 20971
          Quantity: 3
          Order Update [11/25/2024 6:00:00 PM] - Order 17003:
          State: Submitted
          Order: Sell Stop @ 20971
          Quantity: 3
          Order Update [11/25/2024 6:00:00 PM] - Order 17003:
          State: Accepted
          Order: Sell Stop @ 20971
          Quantity: 3​

          However, that's not always the case: when I submit a new order qty of 5, then I reduce qty to 1, the same order gets modified:
          Order Update [11/25/2024 6:00:00 PM] - Order 17004:
          State: Initialized
          Order: Sell Stop @ 20976.75
          Quantity: 5
          Order Update [11/25/2024 6:00:00 PM] - Order 17004:
          State: Submitted
          Order: Sell Stop @ 20976.75
          Quantity: 5
          Order Update [11/25/2024 6:00:00 PM] - Order 17004:
          State: Accepted
          Order: Sell Stop @ 20976.75
          Quantity: 5
          Order Update [11/25/2024 6:00:00 PM] - Order 17004:
          State: ChangePending
          Order: Sell Stop @ 20976.75
          Quantity: 5
          Order modification in progress
          Order Update [11/25/2024 6:00:00 PM] - Order 17004:
          State: ChangeSubmitted
          Order: Sell Stop @ 20976.75
          Quantity: 1
          Order modification in progress
          Order Update [11/25/2024 6:00:00 PM] - Order 17004:
          State: Accepted
          Order: Sell Stop @ 20976.75
          Quantity: 1​

          My issue is which order should the indicator modify if I needed to modify the order qty programmatically? Currently when indicator modifies one of the orders (that is a single order on the chart) - two or more orders appear on the chart. If I cannot just deal with one order ID, how can the indicator track which new orders are the result or user editing existing order as opposed to user submitted a new order? Is that possible or do I need to assume that if order price is the same it belongs to the same order? Does NinjaTrader provides any way to link these related orders or identify replacements of the original order?

          For reference, I'm using following code to modify the order (not applicable to examples above, which are manual order changes via chart to demonstrate order ID changes):

          Code:
          public void ModifyOrder(Order order, int? newQuantity = null, double? newLimitPrice = null, double? newStopPrice = null)
          {
              if (!isEnabled || isConnectionLost || tradingAccount == null)
              {
                  Print($"Cannot modify order - Status: Enabled={isEnabled}, Connected={!isConnectionLost}, Account={tradingAccount != null}");
                  return;
              }
          
              try
              {
                  if (workingOrders.Contains(order) && order.OrderState == OrderState.Working)
                  {
                      // First, remove the old order from our tracking
                      workingOrders.Remove(order);
                      ClearOrderReference(order);
          
                      // Create the modified order
                      Order modifiedOrder = tradingAccount.CreateOrder(
                          order.Instrument,
                          order.OrderAction,
                          order.OrderType,
                          order.OrderEntry,
                          order.TimeInForce,
                          newQuantity ?? order.Quantity,
                          newLimitPrice ?? order.LimitPrice,
                          newStopPrice ?? order.StopPrice,
                          order.Oco,
                          order.Name,  // Keep the same name to help with tracking
                          order.TimeInForce == TimeInForce.Gtd ? order.Gtd : Core.Globals.MaxDate,
                          null
                      );
          
                      // Submit the change
                      tradingAccount.Change(new[] { modifiedOrder });
          
                      // Add the modified order to our tracking
                      workingOrders.Add(modifiedOrder);
                      UpdateOrderReference(modifiedOrder);
          
                      Print($"Modified order {order.Name}:");
                      if (newQuantity.HasValue)
                          Print($"  Quantity: {order.Quantity} -> {newQuantity.Value}");
                      if (newLimitPrice.HasValue)
                          Print($"  Limit Price: {order.LimitPrice} -> {newLimitPrice.Value}");
                      if (newStopPrice.HasValue)
                          Print($"  Stop Price: {order.StopPrice} -> {newStopPrice.Value}");
                  }
                  else
                  {
                      Print($"Cannot modify order - not found in working orders or not in Working state");
                  }
              }
              catch (Exception ex)
              {
                  Print($"Error modifying order: {ex.Message}");
              }
          }​
          Last edited by MiCe1999; 11-29-2024, 02:17 PM.

          Comment


            #6
            Hello MiCe1999,

            On a chart with chart trader, attempting to modify the order quantity will result in either a new order being submitted when increasing the quantity, or orders being cancelled and replaced.
            However, if you change the order quantity from the Orders tab of the Control Center, this would change the order without making new orders.

            In a script, calling Account.Change() with an new order object that has the order.QuantityChanged property set would update the existing order instead of sending a new order.

            There is an indicator, ProfitCaseStopTrailIndicatorExample, that shows calling Account.Change(), but it's changing the stop and limit prices not the quantity. The setup would be the same, but you would be setting the order.QuantityChanged instead of the StopPriceChanged or LimitPriceChanged properties.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

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