Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnPositionUpdate Issue

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

    OnPositionUpdate Issue

    Hey NT people,

    I am coding a strategy that uses ATM strategies to manage open positions, this strategy has to bracket the market, so naturally I am using the Unmanaged Order approach, and I would like the strategy to cancel the opposing entry order once any of them is hit. For this I thought about using the OnPositionUpdate function to listen for updates and cancel the orders when necessary.

    This is where I ran into issues, if I submit the buy order first, then I submit the sell order, then the OnPositionUpdate method executes only for the sell order, and not for the buy order. If I change the order in which the orders are submitted, then only the last submitted order executes the OnPositionUpdate method.

    My question is, how can I work around this to make it work. Below I have the code for the entry orders and the OnPositionUpdate code.

    Code:
    					if(entry.PH[0] != 0){
    						if(loid.Length > 0 && entry.PH[0] != lP){
    							lP = entry.PH[0];
    							AtmStrategyChangeEntryOrder(0, entry.PH[0] + eT * TickSize, loid);
    						}else if(loid.Length == 0){
    							if(entry.PH[0] + eT * TickSize > Close[0]){
    								lP		= entry.PH[0];
    								loid	= GetAtmStrategyUniqueId();
    								LOID	= GetAtmStrategyUniqueId();
    								AtmStrategyCreate(OrderAction.Buy, OrderType.Stop, 0, entry.PH[0] + eT * TickSize, TimeInForce.Gtc, loid, buATM, LOID);
    							}
    						}
    					}
    					if(entry.PL[0] != 0){
    						if(soid.Length > 0 && entry.PL[0] != sP){
    							sP = entry.PL[0];
    							AtmStrategyChangeEntryOrder(0, entry.PL[0] - eT * TickSize, soid);
    						}else if(soid.Length == 0){
    							if(entry.PL[0] - eT * TickSize < Close[0]){
    								sP 		= entry.PL[0];
    								soid	= GetAtmStrategyUniqueId();
    								SOID	= GetAtmStrategyUniqueId();
    								AtmStrategyCreate(OrderAction.Sell, OrderType.Stop, 0, entry.PL[0] - eT * TickSize, TimeInForce.Gtc, soid, beATM, SOID);
    							}
    						}
    					}
    Code:
    		protected override void OnPositionUpdate(IPosition position){
    			Print("exec all");
    			if(loid.Length != 0 && GetAtmStrategyMarketPosition(LOID) == MarketPosition.Long){
    				Print("exec  L");
    				if(soid.Length != 0 && SOID.Length != 0)
    					CancelOrd(ref SOID, ref soid);
    			}else if(soid.Length != 0 && GetAtmStrategyMarketPosition(SOID) == MarketPosition.Short){
    				Print("exec  S");
    				if(SOID.Length > 0 && soid.Length > 0)
    					CancelOrd(ref LOID, ref loid);
    			}
    		}

    #2
    Hello MazaMaza,

    Thank you for writing in and welcome to the NinjaTrader Support Forum!

    Please note that using the unmanaged approach in your strategy will not change how your ATM Strategies are handled. Order submission with the unmanaged approach is done solely from a single order method: SubmitOrder().

    There is a clear line between a NinjaScript Strategy and an ATM Strategy. The use model for creating an ATM Strategy within a NinjaScript Strategy is when you want to programmatically monitor and generate an entry signal and then manually manage the resulting open position via an ATM Strategy in one of NinjaTrader's order entry windows.

    Please take a look at the Using ATM Strategies page within our help guide for more information about using ATM Strategies from a NinjaScript strategy: http://ninjatrader.com/support/helpG...strategies.htm

    As your strategy is not actually managing any orders, you would not be able to utilize OnPositionUpdate().

    I have tested on my end and do see that the method is called, but it is only called on the first ATM Strategy created and states that the current strategy position is flat.

    Code:
    // test
    protected override void OnPositionUpdate(IPosition p)
    {
         Print(p.ToString());
    }
    The strategy position is flat because the strategy is not managing this order; however, your account position is no longer flat.

    I'm not quite sure why it is called initially, but regardless, you will be unable to utilize OnPositionUpdate() if your strategy is using ATM Strategies as the strategy is actually not managing any orders.

    For more information about the differences between the Strategy Position vs. Account Position, please take a look at this help guide link: http://ninjatrader.com/support/helpG..._account_p.htm

    You will only be able to monitor your ATM Strategies by use of the ATM Strategy methods: http://ninjatrader.com/support/helpG...gy_methods.htm
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hello Zachary G,

      Thanks for your quick and detailed response, I was not aware that I couldn't use any of the methods from the unmanaged approach in the ATM managed strategies.

      I have read through the methods that can be used in ATM strategies but I could not find anything that will execute when the order is executed so I can cancel the opposing order when it enters a trade without having to wait until bar close to execute the cancel order code. I thought about using the calculate on bar close = false but I thought there might have been an easier, cleaner way or getting to this point.

      MazaMaza.

      Comment


        #4
        Hello MazaMaza,

        Please note that your NinjaScript strategy will not manage ATM Strategies.

        If you wish for your strategy to manage your orders, you will need to not use ATM Strategies and utilize standard strategy methods.

        You can find the standard order methods here: http://ninjatrader.com/support/helpG...er_methods.htm
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Hello Zachary G,

          Unfortunately the client I am coding this for wants to strictly use ATM orders due to programming costs and I just wanted to make sure there wasn't any other way I could get the orders to cancel without handling the orders inside the strategy.

          Thanks again for your reply.

          MazaMaza.

          Comment


            #6
            Hello MazaMaza,

            The only way to close out of an ATM Strategy from a NinjaScript strategy is by using the AtmStrategyClose() method: http://ninjatrader.com/support/helpG...ategyclose.htm

            Do note that the strategy will have no idea if the ATM Strategy has closed successfully or not as the strategy is not managing the ATM Strategy.

            If the ATM Strategy has not yet entered a position, you can utilize AtmStrategyCancelEntryOrder() to cancel an entry order: http://ninjatrader.com/support/helpG...entryorder.htm
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              Hello Zachary G,

              Those were the exact methods I used to cancel the ATM entry orders, the only issue was that I had to wait for the next bar to close before executing that bit of code that cancels the orders and I wanted something that executed as soon as a position was entered.

              Thanks for all your responses.

              MazaMaza.

              Comment


                #8
                Hello MazaMaza,

                As OnPositionUpdate() will not function with an ATM Strategy, you will need to allow your strategy to run on each tick by setting the CalculateOnBarClose property to false in Initialize().

                You'll then need to check the position of the ATM Strategy by using the GetAtmStrategyMarketPosition() method: http://ninjatrader.com/support/helpG...etposition.htm

                NOTE: Changes to positions will not be reflected till at least the next OnBarUpdate() event after an order fill. This is why we need OnBarUpdate() to run on each tick rather than at bar close.
                Zachary G.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                647 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                369 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                108 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                572 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