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.
Comment