PortoSim allows you to backtest asset portfolios with pre-built or custom strategies
Find the full sample here.
First load prices from a CSV file
var sp500GoldMonthlyCSV = "sp500_gold_3yr_monthly.csv";
var prices = new CSVPriceSource(new FileReader(sp500GoldMonthlyCSV));
Then define a fixed allocation 70% stocks / 30% gold portfolio
var fixedAlloc = new FixedAllocation(Map.of("SP500TR", 0.7, "GOLD", 0.3));
Build a backtest with a rebalancing period of one year
var result = Backtest.withStrategy(fixedAlloc)
.setRebalancePeriod(ChronoUnit.YEARS) // rebalance every year
.run(prices); // test on the historic prices
Get the total return (120.26%)
System.out.println("Total return is " + result.full().totalReturn());