Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CreateOrder intermittent failures

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

    CreateOrder intermittent failures

    Howdy,

    I have a strategy which is submitting unmanaged orders to multiple accounts simultaneously. Every now and then it fails to submit to my rithmic accounts but never fails to submit to my sim accounts, when it doesn't work there's no log or error generated, my print statement says it's submitting the order but there's nothing returned in order trace or in logs. The trace shows the other order submissions that did work, which always seem to be on my sim accounts, my rithmic account sometimes gets the order and sometimes doesn't. I'm using this code to add the account objects to an array which works every time:

    Code:
                        foreach(Account acct in Account.All){
                            if(acct.Name.Contains(copy_trade_name) || acct.Name=="Sim101" || acct.Name=="Sim102"){
                                Print(copy_trade_name.ToString() + " ACCOUNT FOUND, ADDING TO LIST: " + acct.Name);                            
                                acct.ExecutionUpdate += OnExecutionUpdate;                  
                                copy_accounts[acct_num]=acct;
                                acct_num++;
                            }
                        }​
                        Print("FOUND " + acct_num.ToString() + " " + copy_trade_name.ToString() + " ACCOUNTS");
    ​
    Log showing this working:
    Click image for larger version

Name:	image.png
Views:	111
Size:	3.0 KB
ID:	1303613

    And I have this code to submit the initial entry order which always prints the print() statements to the output window:

    Code:
                                    for(int a=0;a<acct_num;a++){
                                        Print("Entering Long Order at " + Close[0].ToString() + " on " + copy_accounts[a].Name.ToString());    
                                        buy_orders[a]=copy_accounts[a].CreateOrder(Instrument, OrderAction.Buy, OrderType.Market, TimeInForce.Day, Convert.ToInt32(DefaultQuantity), 0, 0, "", "Long Order", null);                
                                        copy_accounts[0].Submit(new[] {buy_orders[a]});
                                    }    ​
    Log showing order submission prints:
    ​​Click image for larger version

Name:	image.png
Views:	70
Size:	2.9 KB
ID:	1303614

    I'm using OnExecutionUpdate to create the stop and limit orders, when the order submission fails for the rithmic account nothing in this is printed and no trace or log or error is created:
    Code:
    private void OnExecutionUpdate(object sender, ExecutionEventArgs e)
            {
                int a = Array.IndexOf(copy_accounts, e.Execution.Account);
                
                if (buy_orders[a] != null && buy_orders[a] == e.Execution.Order){
                       if (e.Execution.Order.OrderState == OrderState.Filled || e.Execution.Order.OrderState == OrderState.PartFilled || (e.Execution.Order.OrderState == OrderState.Cancelled && e.Execution.Order.Filled > 0)){
                           sums_filled[a] += e.Execution.Quantity;
                           if (e.Execution.Order.OrderState == OrderState.Filled && sums_filled[a] == e.Execution.Order.Filled && e.Execution.Order.OrderAction==OrderAction.Buy && e.Execution.Order.OrderType==OrderType.Market){
                            String ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
                          
                               Print("Submitting Long Stop Order on " + e.Execution.Account.Name.ToString() + " at " + (e.Execution.Order.AverageFillPrice - stop_loss).ToString());
                               stop_orders[a]=e.Execution.Account.CreateOrder(Instrument, OrderAction.Sell, OrderType.StopMarket, TimeInForce.Day, Convert.ToInt32(DefaultQuantity), 0, e.Execution.Order.AverageFillPrice - stop_loss, ocoString, "Long Stop Loss", null);                                
                              
                               Print("Submitting Long Limit Order on " + e.Execution.Account.Name.ToString() + " at " + (e.Execution.Order.AverageFillPrice + profit_target).ToString());
                               limit_orders[a]=e.Execution.Account.CreateOrder(Instrument, OrderAction.Sell, OrderType.Limit, TimeInForce.Day, Convert.ToInt32(DefaultQuantity), e.Execution.Order.AverageFillPrice + profit_target, 0, ocoString, "Long Take Profit", null);                                        
                            e.Execution.Account.Submit(new[] {stop_orders[a], limit_orders[a]});    
                          
                            Print("OCO String: " + ocoString);
                        }
                      
                           if (e.Execution.Order.OrderState == OrderState.Filled && sums_filled[a] == e.Execution.Order.Filled && e.Execution.Order.OrderAction==OrderAction.Sell && e.Execution.Order.OrderType==OrderType.Market){
                            String ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
                          
                               Print("Submitting Short Stop Order on " + e.Execution.Account.Name.ToString() + " at " + (e.Execution.Order.AverageFillPrice + stop_loss).ToString());
                               stop_orders[a]=e.Execution.Account.CreateOrder(Instrument, OrderAction.Buy, OrderType.StopMarket, TimeInForce.Day, Convert.ToInt32(DefaultQuantity), 0, e.Execution.Order.AverageFillPrice + stop_loss, ocoString, "Short Stop Loss", null);                                
                              
                               Print("Submitting Short Limit Order on " + e.Execution.Account.Name.ToString() + " at " + (e.Execution.Order.AverageFillPrice - profit_target).ToString());
                               limit_orders[a]=e.Execution.Account.CreateOrder(Instrument, OrderAction.Buy, OrderType.Limit, TimeInForce.Day, Convert.ToInt32(DefaultQuantity), e.Execution.Order.AverageFillPrice - profit_target, 0, ocoString, "Short Take Profit", null);                                        
                            e.Execution.Account.Submit(new[] {stop_orders[a], limit_orders[a]});    
                          
                            Print("OCO String: " + ocoString);
                        }
                      
                           if (e.Execution.Order.OrderState != OrderState.PartFilled && sums_filled[a] == e.Execution.Order.Filled){
                               buy_orders[a] = null;
                               sums_filled[a] = 0;
                               open_orders[a] = true;
                               open_position_num++;
                           }
                       }
                   }
              
                if ((stop_orders[a] != null && stop_orders[a] == e.Execution.Order) || (limit_orders[a] != null && limit_orders[a] == e.Execution.Order)){
                    if (e.Execution.Order.OrderState == OrderState.Filled || e.Execution.Order.OrderState == OrderState.PartFilled){
                        Print("FLAT on " + e.Execution.Account.Name.ToString());
                           stop_orders[a] = null;
                           limit_orders[a] = null;        
                        open_orders[a] = false;
                        open_position_num--;
                    }
                    if (e.Execution.Order.OrderState == OrderState.Rejected){
                        // Stop loss order was rejected !!!!
                        Print("!!!!====---- Stop Loss or Limit Order Rejected, Flattening Account " + copy_accounts[a].Name.ToString() + " ----====!!!!");
                        copy_accounts[a].CancelAllOrders(Instrument);
                    }
                }
            }    ​
    Log showing only the sim accounts OnExecutionUpdate:
    Click image for larger version

Name:	image.png
Views:	69
Size:	5.2 KB
ID:	1303615

    When it does work I see the same prints for the rithmic accounts and the orders work the same way as they do on the sim accounts.

    If it were failing all the time I would know it was definitely my code but since it's intermittent I'm looking for confirmation if there's anything obviously incorrect with my code or if this could be a rithmic/connection issue, or if there are any other known issues related to any of this. I'll also mention when I enable the strategy, I enable it on a Sim account and it finds the other rithmic accounts which I am connected through using the built-in RIthmic for NinjaTrader connection which connects succesfully and works fine to submit orders to when this strategy is only working with that account or with any other strategies that are not submitting orders to multiple accounts.

    In the Orders tab the failed order still shows 'Initialized' with the ability to cancel it, the other orders all show Filled/Cancelled. In the executions tab I only see the orders which worked on the sim accounts in this case.

    Orders:
    Click image for larger version

Name:	image.png
Views:	67
Size:	157.7 KB
ID:	1303617

    Log/order trace:
    Click image for larger version

Name:	image.png
Views:	74
Size:	823.2 KB
ID:	1303616

    #2
    Hello drnoggin,

    From the given information I would not be sure where the problem may lie as what you are creating is very complex. Adding prints would be the first step at addressing the problem so you can see how your logic is working, beyond that you would need to find a way to reliably see the problem so we could look into why that is happening.

    I can say that order events are not guaranteed when working with rithmic so it is possible that some of your logic is failing based on the connection being used. You can take a look at the sample in the following link which outlines the requirements to do advanced order handling on rithmic based connections.

    Hello, I see in the documentation you have fixed: 15048 Added Adapter, Rithmic Updated Rithmic API to version 11.3.0.0 Can you elaborate on that please? Will OnOrderUpdate() OnExecutionUpdate() and OnPositionUpdate() be called in the expected order?


    To proceed with this issue I would likely suggest to remove some of the layers of complexity, for example find a single account which this happens on and remove all of the other accounts from the strategy. You may need to create an entirely new test strategy to do this type of test, once the logic which is failing is isolated and you have a specific situation where you see that happen we could likely find out why based on what logic is used and the log information.

    Comment


      #3
      I added more print statements and found that when the order was part filled my logic wasn't adding stops, even once the order is later fully filled. I updated it to always add stops with a quantity of Execution.Order.Filled and that seems to have fixed it. Thanks

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      65 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      139 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X