Could anybody help me whit it?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Optimalization Error
Collapse
X
-
Hello
When you get this message it could possibly mean there is an error in your logic possibly creating an infinite recursion..
I would recommend taking a look at this post as another user has had a similar question
There are a couple of ways to debug this.
First double check your logic to ensure there are no unwanted loops causing this to happen.
If there are no errors you can also try using this undocumented property in your Initialize method
Please let me know if I may be of additional assistance.Code:MaxProcessedEvents = 100; // or a number of your choosing
-
I adde condition but it still writes error
**NT** Error on calling 'OnOrderUpdate' method for strategy 'Aranka/d704eaa931bf4aa5a0bb6cbc7671a22e': More than 99 subsequent user eventsCould you hlep me?PHP Code:#region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Indicator; using NinjaTrader.Gui.Chart; using NinjaTrader.Strategy; #endregion // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { /// <summary> /// Enter the description of your strategy here /// </summary> [Description("Enter the description of your strategy here")] public class Aranka : Strategy { #region Variables private IOrder entryOrder = null; // This variable holds an object representing our entry order private IOrder stopOrder = null; // This variable holds an object representing our stop loss order private IOrder targetOrder = null; // This variable holds an object representing our profit target order private double diff = 1; // Default setting for Diff private int profitTarget = 1; // Default setting for ProfitTarget private IOrder entryOrder1 = null; // This variable holds an object representing our stop loss order // User defined variables (add any user defined variables below) #endregion /// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { CalculateOnBarClose = true; MaxProcessedEvents = 99; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Submit an entry limit order if we currently don't have an entry order open if (Low[0] == CurrentDayOHL().CurrentLow[0] && entryOrder == null && Close[0] > Open[0]) { /* The entryOrder object will take on a unique ID from our EnterLong() that we can use later for order identification purposes in the OnOrderUpdate() method. */ entryOrder = EnterLong(1, "MyEntry"); } if (Low[0] == CurrentDayOHL().CurrentLow[0]&& (entryOrder != null||entryOrder1 != null) && Close[0] > Open[0] && High[0] < Variable0 - Diff * TickSize) { /* The entryOrder object will take on a unique ID from our EnterLong() that we can use later for order identification purposes in the OnOrderUpdate() method. */ entryOrder1 = EnterLong(1, "MyEntry1"); } } /// <summary> /// Called on each incoming order event /// </summary> protected override void OnOrderUpdate(IOrder order) { if (entryOrder != null && entryOrder.OrderState == OrderState.Filled) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); } if ((entryOrder != null && entryOrder.OrderState == OrderState.Filled) && (entryOrder1 != null && entryOrder1.OrderState == OrderState.Filled)) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); } if ((entryOrder != null && entryOrder == order) || (entryOrder1 != null && entryOrder1 == order)) { if (order.OrderState == OrderState.Cancelled && order.Filled == 0) { entryOrder1 = null; entryOrder = null; } } } /// <summary> /// Called on each incoming execution /// </summary> protected override void OnExecution(IExecution execution) { if ((entryOrder != null && entryOrder == execution.Order ) || (entryOrder1 != null && entryOrder1 == execution.Order)) Variable0 = execution.Price; Print(Variable0); } /// <summary> /// /// </summary> protected override void OnPositionUpdate(IPosition position) { if (Position.MarketPosition == MarketPosition.Flat) { entryOrder1 = null; entryOrder = null; } DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight); } #region Properties [Description("")] [GridCategory("Parameters")] public double Diff { get { return diff; } set { diff = Math.Max(0, value); } } [Description("")] [GridCategory("Parameters")] public int ProfitTarget { get { return profitTarget; } set { profitTarget = Math.Max(1, value); } } #endregion } }
Comment
-
Hello tranta,
Thank you for the question.
I took a look into the code you provided.
I have used a Print statement in each of your order handling statements and found where this message would be stemming from.
It looks like in the logic in the OnOrderUpdate method the ExitLongLimit is being called over and over again.
Here is the section of code that I found is being called
When using the print you can see it happens both on a chart and also in the back test through the output window. The back test gives you the message that it is being called more than X amount of times but not in the chart.Code:protected override void OnOrderUpdate(IOrder order) { if (entryOrder != null && entryOrder.OrderState == OrderState.Filled) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); Print("Order"); // this is what is being called over and over }
I tried setting a higher limit to the MaxProcessedEvents but it would seem this loops no matter how high the number is so I don't believe MaxProcessedEvents would be helpful in this case as there is no end to the loop that is created.
From here I would recommend taking a look at your OnOrderUpdate method and see if you can re work the logic here to prevent the loop.
Please let me know if I may be of additional assistance.
Comment
-
Look again at your OnOrderUpdate(). The argument that you supply is never referenced in the body of the event handler, so the event is called on EVERY order update. Your argument is order, but inside the body of your event handler, the reference is to entryOrder. As long as the body conditions are met, you are adding to whatever structure holds the data, and per your code, that data is independent of whatever order is actually being processed.Originally posted by tranta View PostI adde condition but it still writes error
Could you hlep me?PHP Code:#region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Indicator; using NinjaTrader.Gui.Chart; using NinjaTrader.Strategy; #endregion // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { /// <summary> /// Enter the description of your strategy here /// </summary> [Description("Enter the description of your strategy here")] public class Aranka : Strategy { #region Variables private IOrder entryOrder = null; // This variable holds an object representing our entry order private IOrder stopOrder = null; // This variable holds an object representing our stop loss order private IOrder targetOrder = null; // This variable holds an object representing our profit target order private double diff = 1; // Default setting for Diff private int profitTarget = 1; // Default setting for ProfitTarget private IOrder entryOrder1 = null; // This variable holds an object representing our stop loss order // User defined variables (add any user defined variables below) #endregion /// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { CalculateOnBarClose = true; MaxProcessedEvents = 99; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Submit an entry limit order if we currently don't have an entry order open if (Low[0] == CurrentDayOHL().CurrentLow[0] && entryOrder == null && Close[0] > Open[0]) { /* The entryOrder object will take on a unique ID from our EnterLong() that we can use later for order identification purposes in the OnOrderUpdate() method. */ entryOrder = EnterLong(1, "MyEntry"); } if (Low[0] == CurrentDayOHL().CurrentLow[0]&& (entryOrder != null||entryOrder1 != null) && Close[0] > Open[0] && High[0] < Variable0 - Diff * TickSize) { /* The entryOrder object will take on a unique ID from our EnterLong() that we can use later for order identification purposes in the OnOrderUpdate() method. */ entryOrder1 = EnterLong(1, "MyEntry1"); } } /// <summary> /// Called on each incoming order event /// </summary> protected override void OnOrderUpdate(IOrder order) { if (entryOrder != null && entryOrder.OrderState == OrderState.Filled) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); } if ((entryOrder != null && entryOrder.OrderState == OrderState.Filled) && (entryOrder1 != null && entryOrder1.OrderState == OrderState.Filled)) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); } if ((entryOrder != null && entryOrder == order) || (entryOrder1 != null && entryOrder1 == order)) { if (order.OrderState == OrderState.Cancelled && order.Filled == 0) { entryOrder1 = null; entryOrder = null; } } } /// <summary> /// Called on each incoming execution /// </summary> protected override void OnExecution(IExecution execution) { if ((entryOrder != null && entryOrder == execution.Order ) || (entryOrder1 != null && entryOrder1 == execution.Order)) Variable0 = execution.Price; Print(Variable0); } /// <summary> /// /// </summary> protected override void OnPositionUpdate(IPosition position) { if (Position.MarketPosition == MarketPosition.Flat) { entryOrder1 = null; entryOrder = null; } DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight); } #region Properties [Description("")] [GridCategory("Parameters")] public double Diff { get { return diff; } set { diff = Math.Max(0, value); } } [Description("")] [GridCategory("Parameters")] public int ProfitTarget { get { return profitTarget; } set { profitTarget = Math.Max(1, value); } } #endregion } }
Comment
-
Even before that, what are the errors recorded in your log?Originally posted by tranta View PostThank you, but could you be more specific? Im beginner in the coding.
In the meantime, here is a piece of what you wrote, with the mismatched non-references highlighted in red text.
Code:[COLOR=#000000][COLOR=#FF8000][/COLOR][COLOR=#007700]protected [/COLOR][COLOR=#0000BB]override void OnOrderUpdate[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]IOrder [B][COLOR=Red]order[/COLOR][/B][/COLOR][COLOR=#007700]) { if ([/COLOR][COLOR=#0000BB][B][COLOR=Red]entryOrder[/COLOR][/B] [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]null [/COLOR][COLOR=#007700]&& [/COLOR][COLOR=Red][B]entryOrder[/B][/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]OrderState [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#0000BB]OrderState[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Filled[/COLOR][COLOR=#007700]) { [/COLOR][COLOR=#0000BB]ExitLongLimit[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]Position[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]AvgPrice [/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#0000BB]ProfitTarget[/COLOR][COLOR=#007700]* [/COLOR][COLOR=#0000BB]TickSize[/COLOR][COLOR=#007700]); } if (([/COLOR][COLOR=#0000BB][B][COLOR=Red]entryOrder[/COLOR][/B] [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]null [/COLOR][COLOR=#007700]&& [/COLOR][B][COLOR=Red]entryOrder[/COLOR][/B][COLOR=#007700].[/COLOR][COLOR=#0000BB]OrderState [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#0000BB]OrderState[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Filled[/COLOR][COLOR=#007700]) && ([/COLOR][COLOR=#0000BB]entryOrder1 [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]null [/COLOR][COLOR=#007700]&& [/COLOR][COLOR=#0000BB]entryOrder1[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]OrderState [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#0000BB]OrderState[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Filled[/COLOR][COLOR=#007700])) { [/COLOR][COLOR=#0000BB]ExitLongLimit[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]Position[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]AvgPrice [/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#0000BB]ProfitTarget[/COLOR][COLOR=#007700]* [/COLOR][COLOR=#0000BB]TickSize[/COLOR][COLOR=#007700]); } ...} [/COLOR][/COLOR]
Comment
-
Hello tranta,
I just wanted to follow up with the error you have attached.
Is this image prior to or after the changes to your protected override void OnOrderUpdate(IOrder order) section?
I ran the code using the changes that koganam has suggested and was able to eliminate the error from being listed. This is the code I have tried:
Note the text in red, these are the items I have changed otherwise it is identical to what you have created.Code:protected override void OnOrderUpdate(IOrder [COLOR="Red"]order[/COLOR]) { if ([COLOR="red"]order [/COLOR]!= null && [COLOR="red"]order[/COLOR].OrderState == OrderState.Filled) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); } if (([COLOR="red"]order [/COLOR]!= null && [COLOR="red"]order[/COLOR].OrderState == OrderState.Filled) && (entryOrder1 != null && entryOrder1.OrderState == OrderState.Filled)) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); } if ((entryOrder != null && entryOrder == order) || (entryOrder1 != null && entryOrder1 == order)) { if (order.OrderState == OrderState.Cancelled && order.Filled == 0) { entryOrder1 = null; entryOrder = null; } } }
I have changed the entryOrder to order which is being returned from the OnOrderUpdate.
Also i have remove the MaxProcessedEvents from Initialize because the error is not being reported so it is not needed.
Please let me know if I may be of additional assistance.
Comment
-
Thank you for your ansver, I made changes in the code how you wrote, but now stategy doesnt exit from positions. Could you help me whit it?Originally posted by NinjaTrader_Jesse View PostHello tranta,
I just wanted to follow up with the error you have attached.
Is this image prior to or after the changes to your protected override void OnOrderUpdate(IOrder order) section?
I ran the code using the changes that koganam has suggested and was able to eliminate the error from being listed. This is the code I have tried:
Note the text in red, these are the items I have changed otherwise it is identical to what you have created.Code:protected override void OnOrderUpdate(IOrder [COLOR="Red"]order[/COLOR]) { if ([COLOR="red"]order [/COLOR]!= null && [COLOR="red"]order[/COLOR].OrderState == OrderState.Filled) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); } if (([COLOR="red"]order [/COLOR]!= null && [COLOR="red"]order[/COLOR].OrderState == OrderState.Filled) && (entryOrder1 != null && entryOrder1.OrderState == OrderState.Filled)) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); } if ((entryOrder != null && entryOrder == order) || (entryOrder1 != null && entryOrder1 == order)) { if (order.OrderState == OrderState.Cancelled && order.Filled == 0) { entryOrder1 = null; entryOrder = null; } } }
I have changed the entryOrder to order which is being returned from the OnOrderUpdate.
Also i have remove the MaxProcessedEvents from Initialize because the error is not being reported so it is not needed.
Please let me know if I may be of additional assistance.
PHP Code:#region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Indicator; using NinjaTrader.Gui.Chart; using NinjaTrader.Strategy; #endregion // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { /// <summary> /// Enter the description of your strategy here /// </summary> [Description("Enter the description of your strategy here")] public class Aranka : Strategy { #region Variables private IOrder entryOrder = null; // This variable holds an object representing our entry order private IOrder stopOrder = null; // This variable holds an object representing our stop loss order private IOrder targetOrder = null; // This variable holds an object representing our profit target order private double diff = 1; // Default setting for Diff private int profitTarget = 1; // Default setting for ProfitTarget private IOrder entryOrder1 = null; // This variable holds an object representing our stop loss order // User defined variables (add any user defined variables below) #endregion /// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { CalculateOnBarClose = true; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (Low[0] == CurrentDayOHL().CurrentLow[0]&& (entryOrder != null||entryOrder1 != null) && Close[0] > Open[0] && High[0] < Variable0 - Diff * TickSize) { /* The entryOrder object will take on a unique ID from our EnterLong() that we can use later for order identification purposes in the OnOrderUpdate() method. */ entryOrder1 = EnterLong(1, ""); } // Submit an entry limit order if we currently don't have an entry order open if (Low[0] == CurrentDayOHL().CurrentLow[0] && entryOrder == null && Close[0] > Open[0]) { /* The entryOrder object will take on a unique ID from our EnterLong() that we can use later for order identification purposes in the OnOrderUpdate() method. */ entryOrder = EnterLong(1, ""); } } /// <summary> /// Called on each incoming order event /// </summary> protected override void OnOrderUpdate(IOrder order) { if (order != null && order.OrderState == OrderState.Filled) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); // ExitLongLimit(Mn, Position.AvgPrice + ProfitTarget* TickSize, "Flat", "MyEntry" ); // ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize, "", ""); // if (Close[0] > Position.AvgPrice + ProfitTarget* TickSize) // { // ExitLong(); // // } // } if ((order != null && order.OrderState == OrderState.Filled) && (entryOrder1 != null && entryOrder1.OrderState == OrderState.Filled)) { ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize); // ExitLongLimit(Mn, Position.AvgPrice + ProfitTarget* TickSize, "Flat", "MyEntry" ); // ExitLongLimit(Mn, Position.AvgPrice + ProfitTarget* TickSize, "Flat1", "MyEntry1" ); // ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize, "", "" ); // ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize, "MyFlat", "MyEntry"); } if ((entryOrder != null && entryOrder == order) || (entryOrder1 != null && entryOrder1 == order)) { if (order.OrderState == OrderState.Cancelled && order.Filled == 0) { entryOrder1 = null; entryOrder = null; } } } /// <summary> /// Called on each incoming execution /// </summary> protected override void OnExecution(IExecution execution) { if ((entryOrder != null && entryOrder == execution.Order ) || (entryOrder1 != null && entryOrder1 == execution.Order)) Variable0 = execution.Price; Print(Variable0); } /// <summary> /// /// </summary> protected override void OnPositionUpdate(IPosition position) { if (Position.MarketPosition == MarketPosition.Flat) { entryOrder1 = null; entryOrder = null; } DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight); } #region Properties [Description("")] [GridCategory("Parameters")] public double Diff { get { return diff; } set { diff = Math.Max(0, value); } } [Description("")] [GridCategory("Parameters")] public int ProfitTarget { get { return profitTarget; } set { profitTarget = Math.Max(1, value); } } #endregion } }
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
649 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
370 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
573 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
576 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment