CableTrader007
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Inline Strategy Metrics
Collapse
X
-
Inline Strategy Metrics
I would like to know how to call the strategy performance metrics in the strategy (I will then print to file). I see how to call the basic performance collection stats but how to call the custom optimization types/performance metrics using all of the trades. A push in the right direction will be appreciated and any examples all the better. Cheers!
CableTrader007Tags: None
-
Hello CableTrader007,
Thank you for your post.
You can only call the performance statistics of the strategy instance it self and not of other strategies or your account performance as a whole.
You can view the performance statistics available for All Trades at the following link: http://www.ninjatrader.com/support/h...erformance.htm
-
That is what I am attempting to do, call the performance of a strategy (historical and live) from the strategy code. I would like to include the metrics that are available during a backtest with the strategy analyzer including custom metrics I wrote. How do I access these metrics?
CableTrader007
Comment
-
CableTrader007, you would want to work with the TradesPerformance class here - https://www.ninjatrader.com/support/...erformance.htm
A reference sample on this topic including sample code could be for example found here - http://www.ninjatrader.com/support/f...ead.php?t=4084
Comment
-
Good on that part now, thank you! I can post some example code if that would help the community. I would also like to know how to call a custom optimization type similar to how the TradePerformance class objects are called. I have a fairly complex custom optimization type that would be much easier to call from each strategy (reusable) instead of coding it inline each strategy. How do I call this in a strategy? Thank you!
CableTrader007
Comment
-
I've got the following code:
This example code works fine if added to a strategy that trades 1 entry at a time but when applied to the same strategy allowing for multiple entries it only outputs the final position that gets flat, as expected. How can this be changed to output all of the positions values and not just the last entry that ends up getting the overall position flat?Code:protected override void OnPositionUpdate(IPosition position) { if(position.MarketPosition == MarketPosition.Flat) { if (Performance.AllTrades.Count > 0) { Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; double PerfMetric = Performance.AllTrades.TradesPerformance.Currency.AvgProfit /Performance.AllTrades.TradesPerformance.Currency.StdDev; Print(Time[0] + " PerfMetric: " + PerfMetric + " LastTradeVal: " + lastTrade); } } }
CableTrader007
Comment
-
CableTrader007, I would expect this code to give you that outcome as you specifically call it in OnPositionUpdate() when the full strategy position is flat. A trade itself as tracked by the performance object is a completed buy / sell execution, so the script would log the position as individual trades but only when the closing execution is seen. So what you would need to ask for at this point is the last x trades.
Or as alternative work with the collection returned from the GetTrades - https://www.ninjatrader.com/support/.../gettrades.htm
Support for custom performance metrics will be increased with our next major platform update, currently you would need to calculate inline in the script to access those measurements.
Comment
-
Remove the filter for a Flat position?Originally posted by CableTrader007 View PostI've got the following code:
This example code works fine if added to a strategy that trades 1 entry at a time but when applied to the same strategy allowing for multiple entries it only outputs the final position that gets flat, as expected. How can this be changed to output all of the positions values and not just the last entry that ends up getting the overall position flat?Code:protected override void OnPositionUpdate(IPosition position) { if(position.MarketPosition == MarketPosition.Flat) { if (Performance.AllTrades.Count > 0) { Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; double PerfMetric = Performance.AllTrades.TradesPerformance.Currency.AvgProfit /Performance.AllTrades.TradesPerformance.Currency.StdDev; Print(Time[0] + " PerfMetric: " + PerfMetric + " LastTradeVal: " + lastTrade); } } }
CableTrader007
Comment
-
I removed the Flat filter which does show additional positions. Unfortunately the trades listed now do not line up with the trades showing on the chart. Different Entry prices, etc.
Is there a better location to put this code in the strategy? My goal is to output this data for further analysis so basically I am looking for 1 line for each position and obviously listing out the correct data.
CableTrader007
Comment
-
Any ideas on how to just have it check once at the end of the day the metrics for the prior day? I can use a specified time which is easy but then need the cumulative metrics from the prior day not the current day.
As an aside, I have not been able to get each individual trader metrics our with multiple positions exiting at the same time, it just gives me the last trade and not all of the trades that exit at the same time but I think the daily metrics will work better.
Thank you.
- CableTrader007
Comment
-
Bertrand:
What I would like is at the end of the day to get the historical performance metric for all trades up to the end of the prior day. For example today at 4 pm I get the output of the performance metrics for the trades for the past month if that is the amount of historical data loaded.
So, I am guessing something like the code above but referencing the performance data that does not include the current days trades. I am not exactly sure how to exclude the current days trades. Thank you.Code:{ double PerfMetric = Performance.AllTrades.TradesPerformance.Currency.AvgProfit /Performance.AllTrades.TradesPerformance.Currency.StdDev; Print(Time[0] + " PerfMetric: " + PerfMetric); }
- CableTrader007
Comment
-
Hello CableTrader007,
Thank you for your response.
To achieve this, store the performance values in variables at the beginning of the session. Using Bars.FirstBarOfSession you can find out if the bar is the first bar of the day: http://www.ninjatrader.com/support/h...rofsession.htm
Comment
-
Further to the metrics in my previous message, is there a way to gather the statistics on just the most recent 30 trades and not all of the trades? Basically this could be a rolling window statistic and not just using more data as the number of trades increases. Cheers!
- CableTrader007
Comment
-
Hello CableTrader007,
Thank you for your post.
You can use the following as an example to calculate these values for the last 30 trades:
Code:#region Variables private DataSeries totProfit; #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() { totProfit = new DataSeries(this); } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if(Performance.AllTrades.Count > 30) { for(int i = 30; i >= 0; i--) { totProfit[i-1] = Performance.AllTrades[Performance.AllTrades.Count - i].ProfitCurrency; } double totAvgProfit = SMA(totProfit, 30)[0]; double totStdDev = StdDev(totProfit, 30)[0]; double perfMetric = totAvgProfit / totStdDev; } }
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
648 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
369 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
108 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
572 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
574 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment