Monte Carlo in Optimization Report

Hi, I added to my CBT code two Monte Carlo values:

//Sharpe Ratio a Monte Carlo

SetOption("MCEnable", True);
SetOption("MCRuns", 1000);
SetCustomBacktestProc("");
if(Status("action") == actionPortfolio)
{
bo = GetBacktesterObject();
bo.Backtest();

eq = bo.EquityArray;
ret = eq/Ref(eq,-1)-1;
Mean = Cum(ret)/LastValue(BarIndex()-1);
Odchylka = StDev(ret,LastValue(BarIndex()-1));

Sharpe = (Mean/Odchylka)*sqrt(252);

bo.AddCustomMetric("Sharpe Ratio",NumToStr(LastValue(Sharpe),1.3,False));
bo.AddCustomMetric("Usecek",LastValue(BarIndex()),decplaces=0);

mc = bo.GetMonteCarloSim();

if(mc)
{
	bo.AddCustomMetric("%DD5", mc.GetValue("MaxPercDrawdown", 5));
	bo.AddCustomMetric("AR25", mc.GetValue("CAR", 25));
}

}

Displaying values in the Backtest Report is OK:

image

But the Optimization Report only displays two columns with zero values:

image

How to display correct values in Optimization Report?

Many Thanks Milan

@minav, you need to change a line of your code:

SetOption( "MCEnable", 2 ); // value == 2 forces MC to be enabled everywhere (in every mode including optimization - SLOW !)

as well documented in the guide chapter: "Monte Carlo Simulation of your trading system".
Scroll down 3/4 the page to the section " How to control it from the formula level?"

1 Like

Many thanks. I read this chapter in the manual, but then copying the code from the following page I forgot to change it.

One question more:

I'd like to change number of decimal places in the variable "Usecek"

I tried following formula:
bo.AddCustomMetric("Usecek",LastValue(BarIndex()),DecPlaces=0);
but the number still has two decimal places.

What is the correct formula?

Milan

@minav,

See

SetOption( "MCEnable", 2 ); // value == 2 forces MC to be enabled everywhere (in every mode including optimization - SLOW !) This Line is not working for me and I have the same problem. Optimization Report only displays with zero values for example bo.AddCustomMetric( "FinalEq99",mc.GetValue( "FinalEquity", 99 ));99 .

////////////////MonteCarloSim//////////////////////
/*
MonteCarloSim object has one function GetValue( "field", percentile ) that allows to access CDF values. Available "field" values are:
•"FinalEquity"
•"CAR"
•"LowestEquity"
•"MaxDrawdown"
•"MaxPercDrawdown"

Now here is the sample code that presents how to add 30th percentile FinalEquityand CAR to the report:
*/
SetOption( "MCEnable", 2 ); // value == 2 forces MC to be enabled everywhere (in every mode including optimization - SLOW !)
SetOption( "MCRuns", 1000 ); 
SetCustomBacktestProc( "" ); 

if( Status( "action" )== actionPortfolio ) 
{ 
    bo = GetBacktesterObject(); 

    bo.Backtest(); // run default backtest procedure 
 
    // get access to Monte Carlo results 
    // note 1: it may be NULL if MC is NOT enabled 
    // note 2: MC results areavailable after Backtest() or PostProcess 
    // as MC simulation is donein final phase of post processing 
    mc = bo.GetMonteCarloSim(); 
    //st = bo.GetPerformanceStats(0);

 
         if( mc ) 
    { 
         // get 99-th percentile of final equity and CAR etc distribution 
        
        bo.AddCustomMetric( "FinalEq99",mc.GetValue( "FinalEquity", 99 ));
        bo.AddCustomMetric( "CAR99", mc.GetValue( "CAR", 99 ));
        bo.AddCustomMetric( "MaxSysD%99", mc.GetValue( "MaxPercDrawdown", 99 ));
        bo.AddCustomMetric( "CAR99/MDD99", mc.GetValue( "CAR", 99 )/ mc.GetValue( "MaxPercDrawdown" , 99 ));        

          
    } 
}

I have the same problem. can any one help please. in the Backtest Report is OK , but in the Optimization Report only displays with zero values (0.00) see the image999

First step is to run BACKTEST (not optimization) and look at the "Monte Carlo" tab to see if everything is produced fine.

1 Like

Thank you
Backtest report is good except CAR99/MDD99 is in negative number negative number

That is correct. Drawdowns (MDD) are negative. CAR (if your system is profitable) is positve. In your own formula you divide positive number by negative and that will get you negative result. If you want positive number just use minus sign in your calculation to reverse sign.

1 Like

If I use it from here as a general it will give null value just in the optimization not the backtest report Null so I tried just now to add it directly to every afl I use and then it worked great in the optimization.
thank you very much for your time and help.

bo.AddCustomMetric( "CAR99/MDD99", mc.GetValue( "CAR", 99 )/ mc.GetValue( "MaxPercDrawdown" , 99 ));

can you please change the code to be positive number

// just add minus sign
bo.AddCustomMetric( "CAR99/MDD99", - mc.GetValue( "CAR", 99 )/ mc.GetValue( "MaxPercDrawdown" , 99 ));
1 Like

Thank you very much.