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
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!
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
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:
Code:if (SystemPerformance.AllTrades.Count > 0) { }
I look forward to being of further assistance.JesseNinjaTrader Customer Service
-
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:
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 FilesJesseNinjaTrader Customer Service
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:
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.JesseNinjaTrader Customer Service
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 r68cervera, Today, 05:29 AM
|
0 responses
2 views
0 likes
|
Last Post
by r68cervera
Today, 05:29 AM
|
||
Started by geddyisodin, Today, 05:20 AM
|
0 responses
3 views
0 likes
|
Last Post
by geddyisodin
Today, 05:20 AM
|
||
Started by JonesJoker, 04-22-2024, 12:23 PM
|
6 responses
35 views
0 likes
|
Last Post
by JonesJoker
Today, 05:14 AM
|
||
Started by GussJ, 03-04-2020, 03:11 PM
|
12 responses
3,239 views
0 likes
|
Last Post
by Leafcutter
Today, 05:08 AM
|
||
Hi
by AveryFlynn
Started by AveryFlynn, Today, 04:57 AM
|
0 responses
6 views
0 likes
|
Last Post
by AveryFlynn
Today, 04:57 AM
|
Comment