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

Trace order error

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

    Trace order error

    I use exitorderlimit and i'm getting this error which seems to contradict itself:

    10/17/2023 9:36:00 PM Strategy 'ScalpClass/-1': Ignored SubmitOrderManaged() method at 10/17/2023 9:36:00 PM: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=4 LimitPrice=87.54 StopPrice=0 SignalName='20231025145708071786f645d151@ExitLongT P' FromEntrySignal='20231025145708071786f645d151' Reason='SignalName does not have a matching FromEntrySignal to exit'


    Here is where I call the sale to exit
    Code:
    //////////////////////////////////
                                ///LONG
                                else if (order.OrderAction == OrderAction.Buy && (order.OrderState == OrderState.PartFilled || order.OrderState == OrderState.Filled) && order.OrderState != OrderState.Working && order.OrderState != OrderState.Cancelled)// && Position.MarketPosition == MarketPosition.Short)
                                {
                                    if((GetCurrentAsk(0) < orderRecord.StopPrice)|| GetCurrentAsk(0) > orderRecord.TakeProfitPrice)
                                    {
                                        if ( GetCurrentAsk(0) < orderRecord.StopPrice)/// Did price CROSS BELOW stop
                                        {
    
                                            exitReason = "@ExitLongStop";
                                            exitOrderIdStop = signal+exitReason;
                                            if(!(exitToEntryOrderMap.ContainsKey(exitOrderIdStop)))
                                            {
                                                exitToEntryOrderMap[exitOrderIdStop] = signal;
                                            }
                                            ExitLongLimit(orderRecord.OrderSize, GetCurrentAsk(0), exitOrderIdStop, signal);
                                        }
    
                                        else if (GetCurrentAsk(0) > orderRecord.TakeProfitPrice)/// If increment isn't triggered, use standard take profit
                                        {
                                            exitReason = "@ExitLongTP";
                                            exitOrderIdStop = signal+exitReason;
                                            if(!(exitToEntryOrderMap.ContainsKey(exitOrderIdStop)))
                                            {
                                                exitToEntryOrderMap[exitOrderIdStop] = signal;
                                            }
                                            ExitLongLimit(orderRecord.OrderSize, GetCurrentAsk(0), exitOrderIdStop, signal);
                                        }
    
                                    }
    
                                }​



    Why is it telling me that the matching name doesn't exit, when it is right there?

    #2
    Hello Skifree,

    When are you calling this logic? The error says that the entry position does not exist meaning this was submitted too soon before the entry had a chance to fill or the signal name used was wrong in some way.
    JesseNinjaTrader Customer Service

    Comment


      #3
      it's called within OnBarUpdate(), prior to new orders being created. I manage orders manually, and this is an exit condition. I use order.OrderState != OrderState.Working to ensure I don't create an order twice (even though it shouldn't be possible), but what condition would create this? Do i need to check for a different state?

      of note, this is specifically during a backtest

      Comment


        #4
        Hello Skifree,

        I am not sure what you mean by you manage orders manually, are you submitting the entry manually? The code you are using can only be used for strategy submitted orders, the strategy would need to submit an entry with that signal name for the exit to be used.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I enter limits and exit limits with a custom class to store properties, rather than use trailing stops etc

          Code:
          protected void EntryLimitFunction(int accountEntryQuantity,double entryPrice, double stopLossPrice, double takeProfitPrice,double d,bool reversal)
                  {
          
                          //entryPriceOffset = Math.Abs(High[1]-Low[10]);
          
          
                          double direction = d;
                          //SHORT
                          if(direction < 0)
                              {
          
          
                                  string OrderId = GenerateSignalId();
                                  entrySignalId = OrderId + orderCounter.ToString();
          
                                  EnterShortLimit(0,true,accountEntryQuantity,entryPrice, entrySignalId);
                              //    EnterShort(accountEntryQuantity,entrySignalId);
                                  OrderRecord order = new OrderRecord
                                  {
                                      OrderId = entrySignalId,
                                      LimitPrice = entryPrice,
                                      StopPrice = stopLossPrice,
                                      TakeProfitPrice = takeProfitPrice,
                                      EntryPrice = entryPrice,
                                      OrderSize = accountEntryQuantity,
                                      StaticOrderSize = accountEntryQuantity,
                                      EntryBar = CurrentBar,
                                      OrderAction = OrderAction.SellShort,
                                      isBeingReversed = false,
                                      exitSignal = "",
                                      exitMarks = 0,
                                      PriceIncrementCount = 1,
                                        isOrderClosed = false
          
                                  };
          
                                  // Printing the values of the properties
          
                              orders.Add(order);
                              orderCounter++; // Increment the counter for the next order
          
          
                              }
                              //LONG
                              else if(direction > 0)
                              {
          
          
                                  string OrderId = GenerateSignalId();
                                  entrySignalId = OrderId + orderCounter.ToString();
          
                                  EnterLongLimit(0,true,accountEntryQuantity,entryPrice, entrySignalId);
                                  //EnterLong(accountEntryQuantity,entrySignalId);
                                  OrderRecord order = new OrderRecord
                                  {
                                       OrderId = entrySignalId,
                                      LimitPrice = entryPrice,
                                      StopPrice = stopLossPrice,
                                      TakeProfitPrice = takeProfitPrice,
                                      EntryPrice = entryPrice,
                                      OrderSize = accountEntryQuantity,
                                      StaticOrderSize = accountEntryQuantity,
                                      EntryBar = CurrentBar,
                                      OrderAction = OrderAction.Buy,
                                      isBeingReversed = false,
                                      exitSignal = "",
                                      exitMarks = 0,
                                      PriceIncrementCount = 1,
                                      isOrderClosed = false
          
          
                                  };
          
                                  // Printing the values of the properties
          
                              orders.Add(order);
                              orderCounter++; // Increment the counter for the next order    
                              }
          
                  }
                  ​
          This part works but it's still called after I check the exit conditions, so within the same bar I shouldn't be able to declare an order id AND exit it. I specifically have code that prevents that.

          Comment


            #6
            I've determined that these trace errors go away when I don't manually exit positions using the custom class stop price, but that my same logic doesn't get respected by the native stop function. When I update my stops, I've tried updating the order itself and the custom class. However, there's no effect - he's a comparison of me dropping my custom incremental stops (like a trail) and then a pic of what happens when i don't exit the orders utilizing signals : ExitLongLimit(orderRecord.OrderSize, GetCurrentAsk(0), exitOrderIdStop, signal);

            Attached Files

            Comment


              #7
              Hello Skifree,

              I am still not clear on what you mean by manually exit the positions, if your strategy is not exiting the position using its own code then it won't be able to associate exits with entries. For a strategy to enter and exit a trade it needs to submit both orders so it knows about the position and can utilize the signal name. If you try to exit a position that the strategy did not generate you will see the error that you posted about because it does not have an entry with that signal name to pair the exit with.

              Reason='SignalName does not have a matching FromEntrySignal to exit'

              Based on the given details you will likely need to investigate your custom class if that is what is required to see the problem, if you exclude that custom class and it works that would indicate a problem with the logic in that class. Just as a heads up, a custom class is not the same class as your strategy so you won't be able to call order methods in that class and then have them relate to the strategy, the strategy instance needs to call those order methods its self. Generally if you make a custom class you need to pass the strategy instance into the class and then you would use that instance to call methods in the strategy like the following

              myStrategyInstance.EnterShortLimit(...);


              JesseNinjaTrader Customer Service

              Comment


                #8
                I just have a class 'scalpClass' which I run against an instrument. The entry looks something like this where the orderRecord class is just an object containint context for the order that isn't native to the order object (like entryBar).
                Code:
                string OrderId = GenerateSignalId();
                entrySignalId = OrderId + orderCounter.ToString();
                
                EnterShortLimit(0,true,accountEntryQuantity,entryP rice, entrySignalId);​
                EnterLongLimit(0,true,accountEntryQuantity,entryPr ice, entrySignalId);
                Once these buy, there are added to groups:
                Order > Dictionary
                orderRecord > List

                Each record created has a signal, and it is created at the end of OnBarUpdate()

                I believe this creates the entry order fine, and I also remove orders that are in a working state for too long

                The next time OnBarUpdate() runs, if we are not flat, I call some actions:

                Code:
                if(actualPositionQuantity > 0 )
                {
                
                ProcessExitConditions(); /// exit any qualified orders
                ProcessAdjustments(); /// perform trailing-like adjustments to stop and take price in orderRecord
                updateMarks();  /// update visual markers on chart
                
                }
                In ProcessExitConditions();, I go through all orders and their associated orderRecords and I confirm:

                - The dictionary Order (NT8 class) has a signal name order.name, and order.name can be used to pull an orderRecord.
                - the order is not null and the orderRecord is not null
                - the order has the orderAction we want, such as SellShort or Buy (BuyToCover and Sell are skipped because they can't be exited)


                Code:
                protected void ProcessExitConditions()
                        {
                
                            // for each ORDER
                            foreach (KeyValuePair<string, Order> orderPair in orderDictionary)
                            {
                
                                // IS KEY NULL
                                if (orderPair.Value == null)
                                {
                                   DebugPrint("Null value for key " + orderPair.Key+" for orderPair Value"+orderPair.Value);
                                    continue;
                                }
                
                                Order order = orderPair.Value;
                
                                string signal = order.Name;
                
                                OrderRecord orderRecord = GetOrderRecordFromStringId(orders,signal);
                                int barAge = (CurrentBars[0] - orderRecord.EntryBar);
                                string exitReason = "";
                                string exitOrderIdStop = "";
                
                                double profit = (orderRecord.OrderAction == OrderAction.Buy) ? ((GetCurrentAsk(0) - orderRecord.EntryPrice)*order.Quantity) : ((orderRecord.EntryPrice - GetCurrentAsk(0))*order.Quantity);
                                int orderAge = CurrentBars[0] - orderRecord.EntryBar;
                                if (order != null && orderRecord != null)
                                    {
                                        if(CurrentBar-orderRecord.EntryBar > 1)
                                            {
                                        //    Print(signal+" = "+orderRecord.OrderId+" Process exit for: with "+order.OrderAction+ "/ "+order.OrderState);
                                                // Check if the order is in a working state
                                                if (order.OrderAction == OrderAction.BuyToCover ||order.OrderAction == OrderAction.Sell )
                                                {
                                                   //skip if the order was exited
                
                                                    continue;
                                                }
                                                // if we want to exit, we need to use FILLED SELLSHORT orders
                                                //////////////////////////////////
                                                ///SHORT ORDERS
                                                ///
                
                                                else if (order.OrderAction == OrderAction.SellShort && (order.OrderState == OrderState.PartFilled || order.OrderState == OrderState.Filled) && order.OrderState != OrderState.Working && order.OrderState != OrderState.Cancelled)// && Position.MarketPosition == MarketPosition.Short)
                                                {​
                The exit condition is pretty straight forward:
                I confirm that the state is something valid (perhaps I am missing one here?)
                If we crossed below the stop, create an exitOrderId (signal) and map it to the entry signal so that we can maintain the relationship for some OnExecutionUpdate() actions

                Code:
                if (order.OrderAction == OrderAction.Buy && (order.OrderState == OrderState.PartFilled || order.OrderState == OrderState.Filled) && order.OrderState != OrderState.Working && order.OrderState != OrderState.Cancelled)
                                                {
                                                    if((GetCurrentAsk(0) < orderRecord.StopPrice)|| GetCurrentAsk(0) > orderRecord.TakeProfitPrice)
                                                    {
                                                        if (Open[1] < orderRecord.StopPrice && orderRecord.StopPrice < Open[0])/// Did price CROSS BELOW stop
                                                        {
                
                                                            exitReason = "@ExitLongStop";
                                                            exitOrderIdStop = signal+exitReason;
                                                            if(!(exitToEntryOrderMap.ContainsKey(exitOrderIdStop)))
                                                            {
                                                                exitToEntryOrderMap[exitOrderIdStop] = signal;
                                                            }
                                                            ExitLongLimit(orderRecord.OrderSize, GetCurrentAsk(0), exitOrderIdStop, signal);
                                                        }
                
                                                        else if (GetCurrentAsk(0) > orderRecord.TakeProfitPrice)/// If increment isn't triggered, use standard take profit
                                                        {
                                                            exitReason = "@ExitLongTP";
                                                            exitOrderIdStop = signal+exitReason;
                                                            if(!(exitToEntryOrderMap.ContainsKey(exitOrderIdStop)))
                                                            {
                                                                exitToEntryOrderMap[exitOrderIdStop] = signal;
                                                            }
                                                            //ExitLongLimit(orderRecord.OrderSize, GetCurrentAsk(0), exitOrderIdStop, signal);
                                                        }
                
                                                    }
                
                                                }​
                Any idea how this causes the trace error? The strategy is running right now, but I don't know what might be skipped or missing based on these errors.

                Comment


                  #9
                  Hello Skifree,

                  You would need to reduce what you are doing to find what part is related to the error. If you needed help with that part of the code you could extract just that part of the code into a seperate file so we can see the full context of what is being done. Looking at the blocks of code you provided does not help to know what the overall problem with that logic is.

                  To avoid the error that you are seeing the strategy needs to submit both the entry and exit order its self, that would mean the main strategy instance and not a custom class. If a custom class is used the strategy instance needs to be passed to that class so you can call the order methods from the strategy instance its self.

                  As long as the strategy is submitting both the entry and exit order and the same signal name is used for the entries Signal Name and the strategies FromEntrySignalName that error won't be present. The error will be present if a strategy cannot locate an entry order that it had submitted with the name you are providing in the exit.






                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    this is what i mean by a custom class:

                    Code:
                    protected class OrderRecord
                            {
                                public string OrderId { get; set; }
                                public double LimitPrice { get; set; }
                                public double StopPrice { get; set; }
                                public double TakeProfitPrice { get; set; } // Add this
                                public double EntryPrice { get; set; } // Add this
                                public double Profit { get; set; }  // Add this
                                public int OrderSize { get; set; }
                                public int StaticOrderSize { get; set; }
                                public int EntryBar { get; set; }
                                public OrderAction OrderAction { get; set; } // Use the correct casing for OrderAction
                                public bool isBeingReversed { get; set; }
                                public string exitSignal { get; set; }
                                public int exitMarks { get; set; }
                                public int PriceIncrementCount { get; set; }
                                public bool isOrderClosed { get; set; }
                    
                    
                            }​
                    the strategy itself is still

                    Code:
                    namespace NinjaTrader.NinjaScript.Strategies
                    {
                        public class ScalpClass : Strategy
                        {
                            ​

                    Comment


                      #11
                      Hello Skifree,

                      If any of your order methods reside in the class OrderRecord you would need to pass in an instance to the strategy as mentioned, that class is not a strategy and has no specific association with your strategy. If your main strategy is the class ScalpClass which inherits from strategy that will be able to submit orders or use other NinjaScript functions in its code.



                      JesseNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by fx.practic, 10-15-2013, 12:53 AM
                      5 responses
                      5,404 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
                      8 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