if ((execution.Order.OrderAction == OrderAction.BuyToCover || execution.Order.OrderAction == OrderAction.Sell) && execution.Order.OrderState == OrderState.Filled)
{
Trade last = SystemPerformance.AllTrades.Last();
var a = new []{
last.TradeNumber.ToString(),
execution.Instrument.FullName,
last.Quantity.ToString(),
last.Entry.Price.ToString("F"),
last.Exit.Price.ToString("F"),
last.Entry.Time.ToShortDateString(),
last.Exit.Time.ToShortDateString(),
last.Entry.Name,
last.Exit.Name,
last.ProfitCurrency.ToString("F"),
SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit.ToString("F"),
last.MaeCurrency.ToString("F"),
last.MfeCurrency.ToString("F")
};
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy analyzer multiple iteration error
Collapse
X
-
Strategy analyzer multiple iteration error
I'm using the code below to generate some trade statistics in the strategy analyzer. This works in a single iteration without error. When I run it with 50 iterations it generates errors. The error generated is that the "sequence contains no elements" on a different bar number for each iteration. The code is placed in OnExecutionUpdate section. The aim is to generate these stats when a trade is closed. If this code is removed, no errors are generated. Is it possible to run this with multiple iterations?
Code:Tags: None
-
Hello Matts,
Thank you for the post.
It looks like this is likely the AllTrades.Last method causing this error however I don't see you have any error checking here either. If this is currently being run when there are no trades in the collection, that is likely the problem as the Last has nothing to return. To avoid this error you can surround this statement with a check if there is a count:
The reason that this is likely only showing up in the optimization is the change of parameters causing a different sequence of trades to happen. You could additionally see this in a standard test if this was called when there were 0 trades in the collection, for example on bar 0 of OnBarUpdate. I would suspect this works in the standard test because of the sequence the orders are being placed in combination with the used if statement which is targeting specific order types.Code:if (SystemPerformance.AllTrades.Count > 0) { }
I look forward to being of further assistance.
-
Thanks for reply Jesse. I tested with the if statement as shown below. I also removed the order condition statement. This prevented the error from occurring, however at no time was the count greater than zero. The strategy analyzer was able to complete, but I'm unable to access the trade stats.
Code:if (SystemPerformance.AllTrades.Count > 0) { Print("Count is greater than zero"); else { Print("Order execution but there are no trades!");[INDENT]}[/INDENT]
Comment
-
Hello Matts,
Thank you for your reply.
I tried this on my end and see a count, are you certain the script is placing paired trades in that test? I would suggest doing a more simple test by using the SampleMACrossOver (make a duplicate) then add a Print and OnExecution to make sure that's working. For example:
I have attached the test script I used which I can see the trade count increment.Code:protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) { Print(SystemPerformance.AllTrades.Count); }
This will help to confirm the collection is working in the use case. If that is working, the problem is very likely due to something in the logic of the script when used in the optimization. If that is true, I would likely need a more specific test script which demonstrates the problem to proceed with troubleshooting.
I look forward to being of further assistance.
Attached Files
Comment
-
Hello Matts,
Thank you for your reply.
That explains a lot if you are using the 32-bit process, you can find details about this here:
IncludeTradeHistoryInBacktest is needed here however when using an Optimization specifically, this is false for 32 bit processes. You would need to use the 64 bit to bypass this.During Strategy Analyzer Optimization in a 32-bit process, the IncludeTradeHistoryInBacktest property is forced to false due to the limited resources available in a 32-bit environment. You must use a 64-bit process if trade history is needed during optimization.
I look forward to being of further assistance.
Comment
-
Thanks Jesse, I wasn't aware of this limitation. Also I thought it was a standard test, but really it qualifies as an optimization because it uses multiple instruments. Is there any way to forward test a strategy on daily data? I tried using the Sim account, but if the connection is disconnected, the strategy is disabled.
Comment
-
Hello Matts,
Thank you for your response.
If you need realtime data for your forward testing than unfortunately any disconnect would force the strategy to disable.
Have you looked into Walk Forward Optimizations? These allow for optimizing over a period and then forward testing over the next period. You can find more details at the following link: https://ninjatrader.com/support/help...e_a_strate.htm
Please let me know if you have any questions.
Comment
-
I've done some walk forward optimizations before which I found useful, but I've found that the only way I can properly test a strategy is to paper trade it with current market prices before I would consider using it with real money. For a daily strategy, I also want to create some conditions that will make for best entry and exit price, but I don't have historical tick or minute data to do this.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
44 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
124 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
65 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
42 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
46 views
0 likes
|
Last Post
|

Comment