Statistic mode of parameters after doing a backtest

I would like to add a column of statistc mode of each parameter of the best first values of a selected metric, sorted the higest first, after doing a backtest.
For instance, after doing a backtest I would have a new column with the statistic mode of the first hundred (higest first) values of RRR.


While I am waiting for an answer I have been studying the Portfolio backtester Interface trying to solve my goal and I have found the problem to get the report list from the optimization to manage it in the code…
Any suggestion?
Of course I would be greatful if I received some global suggestion for the topic…:blush::blush::blush:

It is all described in manual:

1 Like

Dear Tomasz,
Of course I have folloved your indication (Actually I alredy did it before creating the topic) and I have copied the first example (expectancy) and I didn’t get the solution (just copy). After backtesting (I have add some lines to adapt to futures mode (it works properly).
I show you the code below:

//Now custom-backtest procedure follows 
if( Status("action") == actionPortfolio )
    bo = GetBacktesterObject();

    bo.Backtest(); // run default backtest procedure

    st = bo.GetPerformanceStats(0); // get stats for all trades

    // Expectancy calculation (the easy way)
    // %Win * AvgProfit - %Los * AvgLos
    // note that because AvgLos is already negative
    // in AmiBroker so we are adding values instead of subtracting them
    // we could also use simpler formula NetProfit/NumberOfTrades
    // but for the purpose of illustration we are using more complex one :-)
    expectancy = st.GetValue("WinnersAvgProfit")*st.GetValue("WinnersPercent")/100 +

    // Here we add custom metric to backtest report
    bo.AddCustomMetric( "Expectancy ($)", expectancy );
// your trading system here
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Com = Param("ComisiĂłn", 100, 10, 150, 10);
SetOption( "initialequity", 100000 ); // starting capital   
SetOption("CommissionMode", 2);
SetOption("commissionamount",Com);  //COMISIĂ“N INDIVIDUAL (ENTRADA O SALIDA)


fast = Optimize("fast", 12, 5, 20, 1 );
slow = Optimize("slow", 26, 10, 25, 1 );
short = cover = 0; 

PositionSize = MarginDeposit = 1;
distancia = 3*ATR(14);
for(i=0; i<BarCount; i++)
if(Buy[i]) PlotText("buy\n" + BuyPrice[i], i, H[i]-distancia[i], colorGreen );
if(Sell[i] AND !Short[i]) PlotText("sell\n" + SellPrice[i], i, L[i]+distancia[i], colorRed);
if(Short[i]) PlotText("short\n" + ShortPrice[i], i,  L[i]+distancia[i], colorRed);
if(Cover[i] AND !Buy[i]) PlotText("cover\n" + CoverPrice[i], i, H[i]-distancia[i], colorGreen );

PlotShapes(IIf(Sell AND !Short,shapeDownArrow,shapeNone),colorRed,0,H,-15);   
PlotShapes(IIf(Sell AND !Short ,shapeHollowCircle,shapeNone),colorRed,0,SellPrice,0);   
PlotShapes(IIf(Cover AND !Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15);   
PlotShapes(IIf(Cover AND !Buy,shapeHollowCircle,shapeNone),colorGreen,0,CoverPrice,0); 

But I don’t get the result which the guide is showing. I mean, just show the normal columns, but It didn’t add the expectancy column (neither the report).
After that I have exactely copied the expalme and I got the same…
Actually, this a problem I have found trying to solve the original topic…
Thank you very much!!

Since I have carried on studying about the topic, I have tested the next examples (expectatncy examples 2 and 3) and they worked properly. Then I have checked the first expample again and It has just worked!!! I am happy for that but actually I don’t know why…
Anyway I am going on studying while I am waiting for an idea about the topic.
Thank you very much, despite the fact I haven’t received any new idea, sinde I am consolidating my knolowge about AB :blush::blush::blush:

In order to get the custom columns to display, you need to first run the backtest without producing the listing bo.backtest(True), then caclulate and add the custom metrics and then finally list the trades, which will also add the additional column(s).

if( Status("action") == actionPortfolio )
    bo = GetBacktesterObject();
    bo.Backtest(True); // run default backtest procedure without producing the trade listing

    // Calculate custom metric here

    // Here we add custom metric to backtest report
    bo.AddCustomMetric( "Expectancy ($)", expectancy );

    bo.ListTrades(); // List the trades with the additional columns

Further reading:

1 Like

Thank you very much (HelixTrader and Tomasz)!!I
Finally, I have already understood the three examples… I apreciate your time!! :sweat_smile::sweat_smile::sweat_smile:
So I will attempt to solve the original topic…
In order to solve it I would need a little bit more of help…
Imagine you have done an optimization of the first example here (
As you see, we have got many different results from each iteration. I would like to manage this list, because I want to calculate the statistic mode of the first “n” value of the optimized parameters when I have sorted the largest expectancy first (for instance, to follow the same example)
Thank you again… (while I am studying more)