PDA

View Full Version : How to access the MFE and MAE for an individual trade?


JustCallMeDan
05-21-2010, 09:42 AM
Hi All,


Does anybody know how to access the MFE and MAE encountered by a single trade from within NinjaScript?

The context of my question is for use in a custom optimization type, where my override of the GetPerformanceValue method needs to access the MAE (Maximum Adverse Excursion) and MFE (Maximum Favourable Excursion) values for each individual trade.

The TradesPerformance interface permits access to AvgMfe and AvgMaE values for TradeCollection objects (through either .Points or .Percent or .Currency, e.g. systemPerformance.AllTrades.TradesPerformance.Perc ent.AvgMfe). However, no such interface seems to exist for Trade objects (e.g. systemPerformance.AllTrades[0])

So, we seem to have the situation where I can access an average of MFE and MAEs but not the individual values themselves! Obviously, NinjaTrader itself can access these values, as it can chart them in StrategyAnalyzer, and give the value for each if you roll the mouse over any individual datapoint.

Is there a method I have overlooked or an undocumented access that anybody is aware of?

I understand this may be outside the scope of support NinjaTrader folks can offer, but all help is greatly appreciated!


Kind Regards,

Dan

NinjaTrader_Tim
05-21-2010, 10:32 AM
Hi JustCallMeDan,

Correct, you would need to calculate these explicitly from your code.

jdfagan
12-03-2010, 12:07 PM
I think you can get to these values via this code:


double mae = systemPerformance.AllTrades.TradesPerformance.Curr ency.AvgMae;
double mfe = systemPerformance.AllTrades.TradesPerformance.Curr ency.AvgMfe;


JD

JustCallMeDan
12-05-2010, 06:21 AM
I think you can get to these values via this code:


double mae = systemPerformance.AllTrades.TradesPerformance.Curr ency.AvgMae;
double mfe = systemPerformance.AllTrades.TradesPerformance.Curr ency.AvgMfe;


JD
JD, these values represent the arithmetic mean (simple average) of MAE and MFE values for all trades. Because I required more detailed information regarding the distribution of these values, I had to calculate and store these values within the strategy along with each trade (the key there is to update your extra per-trade statistics on an increase in the size of the AllTrades trade collection to ensure you remain in sync with trades NinjaTrader has identified as complete, rather than all trade signals, which can result in values recorded for failed orders and incomplete trades etc.

Naturally, for average MFE and average MAE only, that code is sufficient.


Dan

J_o_s
02-11-2011, 02:55 AM
I think you can get to these values via this code:


double mae = systemPerformance.AllTrades.TradesPerformance.Curr ency.AvgMae;
double mfe = systemPerformance.AllTrades.TradesPerformance.Curr ency.AvgMfe;
JD
How can these values of an individual trade be accessed? I've experienced some success with the following code, though it is in de minority of the cases (5-10%) way off, compared with the StrategyAnalyzer output:


foreach (Trade t in Performance.AllTrades)
{
if (t.Entry.MarketPosition == MarketPosition.Long)
{
bestPriceReached = MAX(High, ((t.ExitExecution.BarIndex + 1) - t.EntryExecution.BarIndex))[(Count - 1) - (t.ExitExecution.BarIndex - 1)];
worstPriceReached = MIN(Low, ((t.ExitExecution.BarIndex + 1) - t.EntryExecution.BarIndex))[(Count - 1) - (t.ExitExecution.BarIndex - 1)];
}
else if (t.Entry.MarketPosition == MarketPosition.Short)
{
bestPriceReached = MIN(Low, ((t.ExitExecution.BarIndex + 1) - t.EntryExecution.BarIndex))[(Count - 1) - (t.ExitExecution.BarIndex - 1)];
worstPriceReached = MAX(High, ((t.ExitExecution.BarIndex + 1) - t.EntryExecution.BarIndex))[(Count - 1) - (t.ExitExecution.BarIndex - 1)]; ;
}
maeTrade = Math.Abs(worstPriceReached - t.Entry.Price);
mfeTrade = Math.Abs(bestPriceReached - t.Entry.Price);
etdTrade = Math.Abs(mfeTrade - t.ProfitCurrency);
}
Could someone give me an suggestion as how to calculate the MAE and MFE of an individual trade?

Regards,

NinjaTrader_Tim
04-14-2011, 11:07 AM
Hi J_o_s,

There is no direct way to calculate the statistic on the last trade, you'll need to calculate them manually in your code by grabbing the entry/exit values and price data between them.

Find more information on the formulas used at: http://www.ninjatrader.com/support/helpGuides/nt7/index.html?statistics_definitions.htm

kaywai
10-05-2011, 02:04 AM
Hi,

I came across this thread as I was searching for a formula for how MAE is calculated. And what I find peculiar about this thread is J_o_s is looking for the MAE for individual threads.

If you run your strategy through the strategy analyzer, and then click on the "Trades" tab, you can find the MAE, MFE and ETD of each individual thread. Or am I mistaken?

Regards

Kay Wai

JustCallMeDan
10-05-2011, 05:51 AM
Hi Kay,


It's true that you can access the MAE, MFE and ETD for an individual trade via the "Trades" tab. However, this appears to be calculated by NinjaTrader on a post-hoc basis, i.e. when you click on the tab rather than when the trades are performed in the original backtest. Also, there appears to be no way to access this within NinjaScript, post-hoc or otherwise.

If you want your strategy logic to use these values, rather than just view them from the NinjaTrader Trades tab after your simulation, you are limited to calculating them yourself in NinjaScript.



Kind Regards,

Dan

fsprea
05-07-2012, 04:27 PM
Blast from the past!

Thanks for this thread. The meaning is very clear, but... it is a pity, of course! Of course again, NT calculates those values to come to the average and to output them into the performance grid, but then...

...why not to expose them also as properties of some class/collection??? [I mean: single trade MAE and MFE, why not also ETD!].

I strongly vote for the insertion of this feature in the next developments, ... please! It shouldn't be a lot of work, because the code is already there...!

In the meanwhile... going on "hard coding" custom methods... (i.e. repeating the work and eating resources).

Bye

fsprea