Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue with Strategy and Account Position Synchronization When Using Rithmic

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

    Issue with Strategy and Account Position Synchronization When Using Rithmic

    NinjaTrader Version 8.1.2.1 64-bit

    Hello NinjaTrader Support and Community,

    I am encountering a persistent issue with my NinjaTrader strategy where the strategy's position and the account position become out of sync, despite following best practices for order management(I think). I am using the unmanaged approach with Rithmic as my data feed.

    Here is a brief overview of my strategy's workflow:
    • Order Entry: I enter long positions in OnBarUpdate.
      • Code:
        		Order entryOrder = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, quantity, 0, 0, "", signalName);
    • Order Exit: I handle submitting exit orders in OnExecutionUpdate after the entry order is filled, i only enter exit orders after the execution invokes the filled entry order execution, i check this by comparing the custom name for the entry order, then use my custom order manager class to determine which of the scenarios in my strategy is the one to be applied then calculate profit target and stop loss prices, and submit the exit orders, this is the relevant part of the code:
      • Code:
         if (orderDetails != null && orderDetails.EntryOrder == execution.Order && execution.Order.OrderState == OrderState.Filled )
        		{
        		if( orderDetails.EntryOrder == execution.Order )
        		{
        		if ( printDebug ) Print("Found order details for order: " + execution.Order.Name);///diagnostic
        		if ( printDebug ) Print("Calculating order targets for order: " + execution.Order.Name);///diagnostic
        
        		OrderTargets3 orderTargets = CalculateTradeParameters(orderDetails.Pattern);
        		double entryOrderProfitTarget = orderTargets.ProfitTarget;
        		double entryOrderStopPrice = orderTargets.StopLoss;
        		int profitTargetType = orderTargets.ProfitTargetType;
        
        		for (int orderQuantity = 1; orderQuantity <= orderDetails.Quantity; orderQuantity++)
        		{
        		var stopLossOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, entryOrderStopPrice, orderManager.GenerateUniqueExitOrderOcoByDate(Acco unt.DisplayName, Name, orderDetails.Pattern, orderQuantity, Time[0], orderDetails.UniqueIdentifier), orderManager.GenerateUniqueExitOrderSignalName(ord erDetails.Pattern, "L", orderQuantity));
        		var profitTargetOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, 1, entryOrderProfitTarget, 0, orderManager.GenerateUniqueExitOrderOcoByDate(Acco unt.DisplayName, Name, orderDetails.Pattern, orderQuantity, Time[0], orderDetails.UniqueIdentifier), orderManager.GenerateUniqueExitOrderSignalName(ord erDetails.Pattern, "T", orderQuantity));
        		orderManager.AddExitOrder(orderDetails.EntryOrder. Name, stopLossOrder, 1);
        		orderManager.AddExitOrder(orderDetails.EntryOrder. Name, profitTargetOrder, 1);
        		orderManager.AddProfitTargetType(orderDetails.Entr yOrder.Name, profitTargetType);
        		}​
    • Order Management: I have a custom order manager class that tracks all entry and exit orders and their relationships via OCO and handles their references internally via a dictionary that holds the current active entry orders and their "child" exit orders, it also handles removing all references to an active order once an exit order is filled, cancelled, rejected, and so on.
    • Issue Detection: In OnOrderUpdate, I check if an order is cancelled and then remove the exit orders through my order manager class. Despite this, the strategy and account positions become out of sync as seen in the control center.
      • Code:
        		if (orderState == OrderState.Cancelled)
        		{
        
        		orderManager.RemoveExitOrderByOrderObject(order);
        		}​

    Specific Issue: After my strategy closes an open position, the control center shows that the position is not flat(See attached image). This suggests a discrepancy between the strategy’s internal position state and the actual market position on the account.

    Could you please provide guidance or suggestions on how to resolve this synchronization issue? Are there specific aspects of handling Rithmic data feeds or order management in unmanaged strategies that I might be missing?

    Any insights or advice would be greatly appreciated.

    Thank you for your assistance.

    Best regards, Aviram Y.
    Attached Files
    Aviram Y
    NinjaTrader Ecosystem Vendor - Aviram Y

    #2
    Hello Aviram Y,

    Thanks for your post.

    Rithmic still uses separate feeds for Order/Execution/Position events and these events still do not have a deterministic order in NinjaTrader.

    From the help guide: "Rithmic and Interactive Brokers Users: When using a NinjaScript strategy it is best practice to only work with passed by value data from OnExecution. Instances of multiple fills at the same time for the same instrument might result in an incorrect OnPositionUpdate, as sequence of events are not guaranteed due to provider API design."

    OnExecutionUpdate(): https://ninjatrader.com/support/help...tionupdate.htm

    You could view the UnmanagedRithmicIBFriendlyExample script and the UnmanagedRithmicIBFriendlyMultipleEntriesExample script located on this forum post for more information: https://forum.ninjatrader.com/forum/...72#post1206372

    The aim of the sample script is still to keep order/execution/position information separate and use information from the Execution (not Execution.Order) to calculate the target/stop submission and distance in OnExecutuionUpdate.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 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
    72 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
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X