Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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

    #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.
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by fx.practic, 10-15-2013, 12:53 AM
    5 responses
    5,403 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Started by Shai Samuel, 07-02-2022, 02:46 PM
    4 responses
    95 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Started by DJ888, Yesterday, 10:57 PM
    0 responses
    7 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by MacDad, 02-25-2024, 11:48 PM
    7 responses
    159 views
    0 likes
    Last Post loganjarosz123  
    Started by Belfortbucks, Yesterday, 09:29 PM
    0 responses
    8 views
    0 likes
    Last Post Belfortbucks  
    Working...
    X