Skip to content

Model PortfolioOptimizationBacktestDemo

github-actions[bot] edited this page Oct 18, 2026 · 8 revisions

PortfolioOptimizationBacktestDemo — AlgoQuant usage tutorial

Wiki home · Tutorials · Execution records

These examples are tutorials that teach how to use AlgoQuant. Their numerical results are incidental and irrelevant to their teaching purpose. Use them to learn API calls, simulation setup, data flow and reporting. The technical examples use synthetic quick-mode data. Read a result here as an illustration of a library call, not as investment evidence.

Tutorials and quantitative model references · Pinned source · Javadoc

Paper introduction

Harry Markowitz, Portfolio Selection (1952) explains diversification as a portfolio problem: an asset's risk cannot be judged independently of how its returns move with the other holdings. Expected portfolio return is a weighted mean; portfolio variance depends on the full covariance matrix.

The efficient set contains portfolios for which expected return cannot be improved without accepting more variance, or variance reduced without giving up expected return. An investor selects a tradeoff according to preferences and constraints. Holding many positively correlated assets need not provide much diversification.

The framework assumes the relevant return means and covariances are supplied. In practice, estimates can be unstable and constraints can materially change the answer. A solved optimization is therefore a set of weights under assumptions, not evidence of future outperformance. The original article was reviewed. Rolling calibration, rebalance schedules, costs and the dataset used here belong to the demo, not to the static 1952 formulation.

How this demo connects to the idea

A rolling backtest separates estimated weights from realized returns after rebalancing. Follow the data-window and rebalance callbacks to see when each estimate is available. Transaction and turnover conventions belong to the simulation, not the static mean–variance equations.

What this example teaches

Backtests a particular optimizer to see its performance over time using historical data.

Using this example. Evaluates a portfolio optimizer over historical windows rather than computing one static allocation. Read the configured estimation, rebalancing and simulation periods together. Training inputs must be available before the corresponding rebalance decision.

Model and publication. Related theory and the scope of this example are distinguished below.

Implementation in this tutorial. A rolling backtest separates estimated weights from realized returns after rebalancing. Follow the data-window and rebalance callbacks to see when each estimate is available. Transaction and turnover conventions belong to the simulation, not the static mean–variance equations.

Verified execution record

Item Value
Status completed
Run 39122217560
UTC publication 2026-10-18 11:43 UTC
Source commit f497645628296f441e1e254127a7547d15106bbc
Data mode original main defaults
Exit code 0
Elapsed seconds 15.4
Time limit seconds 1800
Maximum Java heap 6g
AlgoQuant source commit aba178deb0e78dc72184546a1e0934c3abce74a1
NM Dev source commit 04c12d1640fb700f52a2f7dc5cadf560040490e6
Combined runtime SHA-256 c1199a1c204cc4ffdf937b756bceacaf30bb64f35a844770490fea268742379b

The original main returned successfully without a detected calculation error. Read its assumptions before interpreting it.

Reproduce the calculation

Follow the repository setup for JDK 8, Maven credentials and an external NM license. From the repository root:

mvn -U -Dmaven.test.skip=true clean package
mvn exec:java -Dexec.mainClass=tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimizationBacktestDemo

The automated run uses scripts/ModelDemoRunner.java to initialize the external license before the unchanged main, save JFreeChart windows as PNGs, and exit after main returns. It uses an isolated working directory with the repository datasets/configuration linked in. Running directly may leave desktop windows open.

Observed output

11:17:17.031 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - looking up configuration file: config.default.xml
11:17:17.034 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - looking up configuration file: config/config.default.xml
11:17:17.034 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - loading config file: file:/home/runner/work/algoquant/algoquant/target/model-results/PortfolioOptimizationBacktestDemo/work/config/config.default.xml


11:17:17.543 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - current directory: /home/runner/work/algoquant/algoquant/target/model-results/PortfolioOptimizationBacktestDemo/work
11:17:17.543 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - current config:
11:17:17.556 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - nm.license: /home/runner/work/algoquant/algoquant/license/nm.lic
11:17:17.558 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - logback.configurationFile: /home/runner/work/algoquant/algoquant/config/model-demo-logback.xml
11:17:17.558 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - DATA_FOLDER: ./test-data
11:17:17.558 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - DATA_GAINCAPITAL: ${DATA_FOLDER}/processed/gaincapital/extracted-from-raw-arrange-by-dates
11:17:17.559 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - DATA_BLOOMBERG: ${DATA_FOLDER}/processed/bloomberg
11:17:17.559 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - DATA_YAHOO: ${DATA_FOLDER}/yahoo
11:17:17.559 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - DATA_SGX: ${DATA_FOLDER}/raw/sgx
11:17:17.559 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - DATA_HOLIDAYS: ./log/holidays
11:17:17.559 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - DATA_COMPUSTAT_HK: ${DATA_FOLDER}/compustat/hk
11:17:17.560 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - PDF_OUTPUT_FOLDER: ./log/
11:17:17.560 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - PDF_PLOT_WIDTH: 800
11:17:17.560 [main] INFO  tech.nmfin.algoquant.util.config.AQConfig - PDF_PLOT_HEIGHT: 600
11:17:20.550 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory - failed to load from file, reason: no data read from file; load from web
11:17:20.552 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory - reading Yahoo! data for AAPL from the Internet from 2014-07-01T00:00 to 2017-01-01T16:00
11:17:20.648 [main] INFO  t.n.a.d.h.yahoo.YahooEODCsvZipFileWriter - writing csv.zip file ./log/20261018/AAPL.csv.zip
11:17:20.669 [main] INFO  t.n.a.d.h.yahoo.YahooEODCsvZipFileWriter - finished writing ./log/20261018/AAPL.csv.zip
11:17:20.672 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory - failed to load from file, reason: no data read from file; load from web
11:17:20.672 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory - reading Yahoo! data for GOOG from the Internet from 2014-07-01T00:00 to 2017-01-01T16:00
11:17:20.758 [main] INFO  t.n.a.d.h.yahoo.YahooEODCsvZipFileWriter - writing csv.zip file ./log/20261018/GOOG.csv.zip
11:17:20.769 [main] INFO  t.n.a.d.h.yahoo.YahooEODCsvZipFileWriter - finished writing ./log/20261018/GOOG.csv.zip
11:17:20.769 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory - failed to load from file, reason: no data read from file; load from web
11:17:20.769 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory - reading Yahoo! data for MSFT from the Internet from 2014-07-01T00:00 to 2017-01-01T16:00
11:17:20.872 [main] INFO  t.n.a.d.h.yahoo.YahooEODCsvZipFileWriter - writing csv.zip file ./log/20261018/MSFT.csv.zip
... excerpt shortened; complete stdout is in the release archive ...
2016-09-09: -3.219593399119154E8
2016-09-12: -3.299896361162484E8
2016-09-13: -3.2865975166722953E8
2016-09-14: -3.0970387764693743E8
2016-09-15: -3.1969685585689455E8
2016-09-16: -3.2841709335740364E8
2016-09-19: -3.249885198606131E8
2016-09-20: -3.088876195912763E8
2016-09-21: -3.3245718806013525E8
2016-09-22: -3.1071722397864157E8
2016-09-23: -2.9938675032078004E8
2016-09-26: -3.068939386622669E8
2016-09-27: -3.254824077663411E8
2016-09-28: -3.30513338990852E8
2016-09-29: -3.235487727440576E8
2016-09-30: -3.276165432101631E8
2016-10-03: -3.3181437408715165E8
2016-10-04: -3.1821021213839746E8
2016-10-05: -3.3104463859766555E8
2016-10-06: -3.3721213117569745E8
2016-10-07: -3.446145543420477E8
2016-10-10: -3.331738157127305E8
2016-10-11: -3.14933693166368E8
2016-10-12: -3.092896461570829E8
2016-10-13: -3.22457513392113E8
2016-10-14: -3.405480146913427E8
2016-10-17: -3.300511054407474E8
2016-10-18: -3.0337282171538293E8
2016-10-19: -2.811479165642619E8
2016-10-20: -2.8389253575860417E8
2016-10-21: -3.518983097916118E8
2016-10-24: -3.633079221195564E8
2016-10-25: -3.80177401131361E8
2016-10-26: -3.7865572673693764E8
2016-10-27: -3.66307139563455E8
2016-10-28: -3.5531601391865575E8
2016-10-31: -3.845451934580684E8
2016-11-01: -3.7337557823381937E8
2016-11-02: -4.0134575047841614E8
2016-11-03: -4.059286555432673E8
2016-11-04: -3.85581562647826E8
2016-11-07: -3.93475649478355E8
2016-11-08: -3.7716077551263577E8
2016-11-09: -3.8045436037958455E8
2016-11-10: -3.787952336269452E8
2016-11-11: -4.1450869757679206E8
2016-11-14: -4.2006680332808435E8
2016-11-15: -4.04262042314454E8
2016-11-16: -4.272800375669101E8
2016-11-17: -4.409695624554887E8
2016-11-18: -4.603629929635064E8
2016-11-21: -4.619033216237104E8
2016-11-22: -4.72999879539207E8
2016-11-23: -4.664024670010351E8
2016-11-25: -4.7143508087358105E8
2016-11-28: -4.556693826384406E8
2016-11-29: -4.6364152807480973E8
2016-11-30: -4.662764627334445E8
2016-12-01: -4.54073722304359E8
2016-12-02: -4.508528858937377E8
2016-12-05: -4.464277493668433E8
2016-12-06: -4.5081119955072904E8
2016-12-07: -4.696036832935959E8
2016-12-08: -4.495730850193411E8
2016-12-09: -4.5518816795969E8
2016-12-12: -4.585088457648245E8
2016-12-13: -4.7551445694430983E8
2016-12-14: -4.633716654342016E8
2016-12-15: -4.611516725299666E8
2016-12-16: -4.7147651135336375E8
2016-12-19: -5.0794605453303874E8
2016-12-20: -5.0103666928382874E8
2016-12-21: -5.064607149171276E8
2016-12-22: -5.117629578435401E8
2016-12-23: -5.065121872107905E8
2016-12-27: -5.0703239296269095E8
2016-12-28: -5.1246469825552726E8
2016-12-29: -5.153918006661477E8
2016-12-30: -6.283215780218053E8

Execution caveat: Main returned, but the log contains exceptions, failed operations or undefined values. This records process completion only; inspect the diagnostic below before treating any output as a valid backtest.

Diagnostic and prerequisites

2016-12-16: -4.7147651135336375E8
2016-12-19: -5.0794605453303874E8
2016-12-20: -5.0103666928382874E8
2016-12-21: -5.064607149171276E8
2016-12-22: -5.117629578435401E8
2016-12-23: -5.065121872107905E8
2016-12-27: -5.0703239296269095E8
2016-12-28: -5.1246469825552726E8
2016-12-29: -5.153918006661477E8
2016-12-30: -6.283215780218053E8
DEMO_RUNNER_MAIN_RETURNED
DEMO_RUNNER_CHARTS 0

Charts from this run

Reported performance statistics — part 1

Products and aggregate shown exactly as reported; P/L is monetary, not percent. Currency conventions belong to the demo.

Derived from stdout.log using the precision available in that file. Download plotted data. Undefined or malformed cells are omitted, never replaced by zero.

Chart 1

Reported performance statistics — part 2

Products and aggregate shown exactly as reported; P/L is monetary, not percent. Currency conventions belong to the demo.

Derived from stdout.log using the precision available in that file. Download plotted data. Undefined or malformed cells are omitted, never replaced by zero.

Chart 2

Reported performance statistics — part 3

Products and aggregate shown exactly as reported; P/L is monetary, not percent. Currency conventions belong to the demo.

Derived from stdout.log using the precision available in that file. Download plotted data. Undefined or malformed cells are omitted, never replaced by zero.

Chart 3

Reported performance statistics — part 4

Products and aggregate shown exactly as reported; P/L is monetary, not percent. Currency conventions belong to the demo.

Derived from stdout.log using the precision available in that file. Download plotted data. Undefined or malformed cells are omitted, never replaced by zero.

Chart 4

Reported performance statistics — part 5

Products and aggregate shown exactly as reported; P/L is monetary, not percent. Currency conventions belong to the demo.

Derived from stdout.log using the precision available in that file. Download plotted data. Undefined or malformed cells are omitted, never replaced by zero.

Chart 5

Reported performance statistics — part 6

Products and aggregate shown exactly as reported; P/L is monetary, not percent. Currency conventions belong to the demo.

Derived from stdout.log using the precision available in that file. Download plotted data. Undefined or malformed cells are omitted, never replaced by zero.

Chart 6

Printed daily account series

Uses only the values actually printed; a flat zero series remains flat and is not evidence of profitable trading.

Derived from stdout.log using the precision available in that file. Download plotted data. Undefined or malformed cells are omitted, never replaced by zero.

Chart 7

Downloadable evidence

Download PortfolioOptimizationBacktestDemo.zip contains stdout, stderr, the execution record, checksums and any exported CSV/chart files.

Export Bytes
files/derived-charts/charts.json 3703
files/derived-charts/derived-01.csv 918
files/derived-charts/derived-01.png 151870
files/derived-charts/derived-02.csv 1111
files/derived-charts/derived-02.png 146871
files/derived-charts/derived-03.csv 1145
files/derived-charts/derived-03.png 147619
files/derived-charts/derived-04.csv 1060
files/derived-charts/derived-04.png 147327
files/derived-charts/derived-05.csv 831
files/derived-charts/derived-05.png 146544
files/derived-charts/derived-06.csv 401
files/derived-charts/derived-06.png 104635
files/derived-charts/derived-07.csv 24288
files/derived-charts/derived-07.png 89323

Interpreting the result

Read the reported units: optimizer weights, model-fit coefficients, forecast correlations and simulated P/L are different outputs. Synthetic runs demonstrate the API rather than historical market returns. Inspect undefined values and open positions instead of replacing them with zero. Chart exports visualize exactly this run.

For model assumptions, original publications and adaptation limits, use the literature guide.

Clone this wiki locally