Most straightforward way to get montecarlo values

Do I need to loop through monte carlo simulations to extract the values? If so, how would I do that? The Code below obviously doesn't work but hopefully will give you an idea of what I am trying to go for.

For example, we run 1000 simulations and I want to create a scatter plot of the total profit vs max drawdown. I am imagining something like


SetCustomBacktestProc("");
//Custom Metrics
if (Status("action")== actionPortfolio)
{
	
	SetOption("UseCustomBacktestProc",True);
	bo = GetBacktesterObject(); 
    bo.Backtest(); 
    
    SetOption("MCEnable",2);
	SetOption("MCRuns",1000);
	mc = bo.GetMonteCarloSim();
		
	if (mc)
	{
	fh = fopen("C:\\Program Files\\AmiBroker\\TestOutputs\\Values.csv","a");
		if (fh)
		fputs("Net Profit,MaxDD\n",fh);
		{
			for (i = 0; i < GetOption("MCRuns"); i++)
				nppct = mc.GetValue("NetProfitPercent",i);
				maxdd = mc.GetValue("MaxSystemDrawdownPercent",i);
				fputs(NumToStr(nppct) + "," + NumToStr(maxdd) +"\n",fh);
		}
		fclose(fh);
	}
}

I am trying to generate something like;

so that in turn I can use excel or matplotlib to generate something like;

Alternately, am I simply making things difficult for myself and there is an easier way for me to do this inside AmiBroker without having to learn lowlevelgrtaphics etc?

Your formula is incorrect. Please consult the manual. SetOption should be called BEFORE backtest.

Correctly written formula is included in the manual:

https://www.amibroker.com/guide/h_montecarlo.html

Also note that the values given by GetValue are not indexed by "run number". They are indexed by percentile. As documented again in the manual, so the code from the manual mc.GetValue( "FinalEquity", 30 ) gets 30th percentile.

1 Like

Noted, thanks.

Is there a way (without doing a 1000 step optimization) to extract the metrics from each individual run?