Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need algo help on orders linked to fills

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

    Need algo help on orders linked to fills

    Guys-

    I have a decent order management algo here for my unmanaged system which takes fills and issues stops/targets. However, once in a blue moon I encounter an issue with my "unique lableling" methodology which results in having a position that doesn't get matched stops/targets.

    To start, entry orders are issued a "unique identifier" based on the instrument and time entered. I believe the problem stems from near-simultaneous executions which receive identical entry names:

    Code:
    if (BarsInProgress == 0 && !noTrading)
                    {
                        // First, cancel any outstanding entry orders:
                        if (CurrentBar > barNumberOfOrder && entryOrder != null)
                        {
                            CancelOrder(entryOrder);
                            // Need to reset entryOrder to null in the order update:
                            entryOrder = null;
                        }
    
                        if (Signal == 1)
                        {
                                newPosition = Instrument.FullName + "_" + "Long_" + Time[0];
    
                            entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, positionSize, Instrument.MasterInstrument.Round2TickSize(CRG.LongLimitEntryPrice), 0, entryOrders.Count.ToString(), newPosition);
                        }
    
                        if (Signal == -1)
                        {
    newPosition = Instrument.FullName + "_" + "Short_" + Time[0];
    
                            entryOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Limit, positionSize, Instrument.MasterInstrument.Round2TickSize(CRG.ShortLimitEntryPrice), 0, entryOrders.Count.ToString(), newPosition);
                        }
                        
                        if (entryOrder != null)
                        {
                            // Add the entry order's token to the token array:
                            entryOrderTokens.Add(entryOrder.Token);
                            
                            // Add the entry order to the entryOrder array:
                            entryOrders.Add(entryOrder);
                            entryOrder = null;    
                            
                            // Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
                            barNumberOfOrder = CurrentBar;
                        }
                    }
    Subsequently, target orders and stops are issued on a per-execution basis:

    Code:
            protected override void OnExecution(IExecution execution)
            {
                try
                {    
                    // First, loop through the entry orders to see if this execution is linked to an entry order:
                    if (entryOrders.Count !=0)
                    {
                        for (int i = entryOrders.Count - 1; i >= 0; i --)
                        {
                            // Create a temporary entry order container:
                            IOrder localEntryOrder = entryOrders[i];
                            
                            // If the execution token matches the token to the entry order we were looking for:
                            if (localEntryOrder.Token == execution.Order.Token)
                            {                    
                                //Print(execution.ToString());
                        
                                // Is the order filled/partially filled OR has the order been cancelled & filled?
                                //if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                                if (execution.Order.OrderState == OrderState.Filled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                                //if (execution.Order.OrderState == OrderState.Filled)
                                {
                                    if (Position.MarketPosition == MarketPosition.Long)
                                    {
                                        targetExitPrice = Position.AvgPrice + CRG.TargetExit;
                                        trailingStopPrice = Position.AvgPrice - CRG.StopExit;
                                        
                                        stopOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, execution.Order.Quantity, 0, trailingStopPrice, execution.Order.Name, "Stop Order");
                                        targetOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, execution.Order.Quantity, targetExitPrice, 0, execution.Order.Name, "Target Order");
                                    }
                                    
                                    if (Position.MarketPosition == MarketPosition.Short)
                                    {
                                        
                                        targetExitPrice = Position.AvgPrice - CRG.TargetExit;
                                        trailingStopPrice = Position.AvgPrice + CRG.StopExit;
                                        
                                        stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, execution.Order.Quantity, 0, trailingStopPrice, execution.Order.Name, "Stop Order");
                                        targetOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Limit, execution.Order.Quantity, targetExitPrice, 0, execution.Order.Name, "Target Order");
                                    }
                                    
                                    // Add stop order to the stop order list
                                    if (stopOrder != null)
                                    {
                                           stopOrders.Add(stopOrder);
                                        stopOrder = null;
                                    }
    
                                    // Add target order to the target order list.
                                    if (targetOrder != null)
                                    {
                                        targetOrders.Add(targetOrder);
                                        targetOrder = null;
                                    }
                            
                                    // Resets the entryOrder object to null after the order has been filled or partially filled
                                    //if (execution.Order.OrderState != OrderState.PartFilled)
                                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.Cancelled)
                                    {
                                        entryOrders.RemoveAt(i);
                                        entryOrder = null;
                                
                                    }
                                }
                            }
                        }    
                    }
                    
                    // Second, loop through the target orders to see if this execution is linked to a target order:
                    if (targetOrders.Count != 0)
                    {
                        for (int i = targetOrders.Count - 1; i >= 0; i --)
                        {
                            IOrder localTargetOrder = targetOrders[i];
                            
                            // If the execution token matches the token to the entry order we were looking for:
                            if (localTargetOrder.Token == execution.Order.Token)
                            {
                                // Is the order filled/partially filled OR has the order been cancelled & filled?
                                //if (execution.Order.OrderState == OrderState.Filled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.Cancelled)
                                {
                                    //CancelOrder(targetOrders[i]);
                                    targetOrders.RemoveAt(i);    
                                }
                            }
                        }
                    }
                    
                    // Third, loop through the stop orders to see if this execution is linked to a stop order:
                    if (stopOrders.Count != 0)
                    {
                        for (int i = stopOrders.Count - 1; i >= 0; i --)
                        {
                            IOrder localStopOrder = stopOrders[i];
                            
                            // If the execution token matches the token to the entry order we were looking for:
                            if (localStopOrder.Token == execution.Order.Token)
                            {
                                // Is the order filled/partially filled OR has the order been cancelled & filled?
                                //if (execution.Order.OrderState == OrderState.Filled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.Cancelled)
                                {
                                    //CancelOrder(stopOrders[i]);
                                    stopOrders.RemoveAt(i);    
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);    
                }
            }
    So I guess my question is here - is there a better unique ID to link executions to that will ensure that every execution is directly linked to a target/stop order?

    Thanks-
    CG

    PS I have left just a few details out in terms of the code but everything needed for the positioning algo is there -

    #2
    Thanks for sharing this, CG. Time[0] is only accurate to the second. You may be running into conditions where trades are happening within the same second. Maybe you could add a random number or another loop here. Unfortunately this is beyond our scope of support - hopefully another community member can offer input in this area.
    Last edited by NinjaTrader_RyanM1; 08-02-2010, 09:06 AM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      Thanks for sharing this, CG. Time[0] is only accurate to the second. You may be running into conditions where trades are happening within the same second. Maybe you could add a random number or another loop here. Unfortunately this is beyond our scope of support - hopefully another community member can off input in this area.
      I believe that's exactly the issue I'm running into.

      Are there no other unique identifiers that I could use that are supported within NT, such as some unique execution ID, fill ID, etc?

      Hopefully, as you said, another member might help -

      CG

      Comment


        #4
        You may be able to work with the Execution ID for this.
        http://www.ninjatrader.com/support/h...iexecution.htm
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          I just wanted to let people know that this code doesn't really work - linking stops/targets to executions is much more difficult than linking them to orders, as the orders themselves (using OrderState) are much easier to track and manage.

          I wouldn't suggest anybody use this code other than as a guide for how to proceed in a general fashion.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ETFVoyageur, Today, 04:00 PM
          0 responses
          5 views
          0 likes
          Last Post ETFVoyageur  
          Started by AaronKTradingForum, Today, 03:44 PM
          1 response
          6 views
          0 likes
          Last Post AaronKTradingForum  
          Started by Felix Reichert, 04-26-2024, 02:12 PM
          11 responses
          77 views
          0 likes
          Last Post Felix Reichert  
          Started by junkone, 04-28-2024, 02:19 PM
          7 responses
          82 views
          1 like
          Last Post junkone
          by junkone
           
          Started by pechtri, 06-22-2023, 02:31 AM
          11 responses
          136 views
          0 likes
          Last Post Nyman
          by Nyman
           
          Working...
          X