etccapital
MultiFactor
Jupyter Notebook

Built a practical Multi-Factor Backtesting Framework from scratch based on Huatai Security's(One of China's largest sell side) financial engineering report. Steps include factor data collection and preprocessing, factor combination, portfolio optimization and risk return analysis.

Last updated Jun 28, 2026
79
Stars
29
Forks
1
Issues
0
Stars/day
Attention Score
45
Language breakdown
Jupyter Notebook 98.6%
Python 1.4%
Batchfile 0.0%
Shell 0.0%
โ–ธ Files click to expand
README

Multifactor project

Description

This is a practical multi-factor backtesting framework from scratch based on Huatai Security's(one of China's largest sell side) financial engineering report, as a part of the quantitative finance research project development in ETC Investment Group. Steps include factor data collection and preprocessing, single factor testing, building return model, building risk model, and result analysis.

Do not distribute for use without explicit consent from contributing members of ETC Quant.


Project environment

To set up the project, first install anaconda and github cli. (Currently only compatible with windows)
  • Open CMD/bash
  • cd to navtigate to desired folder location
  • git clone https://github.com/etccapital/MultiFactor to clone the lastest version of the repo
  • (Linux/MacOS) conda env create -f environment.yml to download configure the all packages needed \
(Windows) ./makefile_win.bat "setup" to download configure the all packages needed
  • Use conda env list to list all conda packages available. Make sure environment multifactor is in the list
  • Download rq_crendential.json and save it to root project folder
  • Convert target python files into jupyter notebooks. See "Version Control of .ipynb Files" section below.
To inspect packages installed or to make changes:
  • Open CMD/bash
  • Use conda activate multifactor

Workflow

  • Download zipped price data and extract them to .data/price
  • Download factor data from ricequant with datadownloadand_process.ipynb
  • Or download zipped factor data and extract them to .data/factor

Project structure

Use command tree in command line to generate the following folder structure. Whenever you change the folder structure, please update the following diagram and update the corresponding file to the OneDrive folder.
.
โ”œโ”€โ”€ Data
โ”‚ย ย  โ”œโ”€โ”€ factor
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ cashflow
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ cashflowpersharettm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ cashflowratio_ttm.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ dividend
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ financial_quality
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ debttoassetratiottm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ fixedassetratio_ttm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ returnonequity_ttm.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ growth
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ increvenuettm.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ momentum
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ size
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ marketcap3.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ technical
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ value
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ booktomarketratiottm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ ev_ttm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ pbratiottm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ pcfratiottm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ peratiottm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ pegratiottm.h5
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ psratiottm.h5
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ volatility
โ”‚ย ย  โ”œโ”€โ”€ index_data
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ sh000300.csv
โ”‚ย ย  โ”œโ”€โ”€ raw_data
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ dfbasicinfo.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ industry_mapping.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ is_st.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ is_suspended.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ listed_dates.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ stock_names.h5
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ rebalancing_dates.h5
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ industrycodeto_names.xlsx
โ”‚ย ย  โ”œโ”€โ”€ stock_data
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ sh600000.csv
โ”‚ย ย  โ”‚ย ย  ...
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ sz301039.csv
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ environment.yml
โ”œโ”€โ”€ makefiles
โ”‚ย ย  โ”œโ”€โ”€ makefilemacnotebooktopy.sh
โ”‚ย ย  โ”œโ”€โ”€ makefilemacpytonotebook.sh
โ”‚ย ย  โ””โ”€โ”€ makefile_win.bat
โ”œโ”€โ”€ not useful temporarily
โ”‚ย ย  โ”œโ”€โ”€ Dataloader.py
โ”‚ย ย  โ””โ”€โ”€ Ricequant API.ipynb
โ”œโ”€โ”€ notebook
โ”‚ย ย  โ”œโ”€โ”€ Alphalens_new.ipynb
โ”‚ย ย  โ”œโ”€โ”€ Alphalenssinglefactor_testing.ipynb
โ”‚ย ย  โ”œโ”€โ”€ data_download.ipynb
โ”‚ย ย  โ”œโ”€โ”€ datadownloadand_process.ipynb
โ”‚ย ย  โ”œโ”€โ”€ factor_combination.ipynb
โ”‚ย ย  โ”œโ”€โ”€ portfolio_optimization.ipynb
โ”‚ย ย  โ””โ”€โ”€ singlefactoranalysis.ipynb
โ”œโ”€โ”€ rq_credential.json
โ”œโ”€โ”€ scripted_notebook
โ”‚ย ย  โ”œโ”€โ”€ Alphalens_new.py
โ”‚ย ย  โ”œโ”€โ”€ Alphalenssinglefactor_testing.py
โ”‚ย ย  โ”œโ”€โ”€ data_download.py
โ”‚ย ย  โ”œโ”€โ”€ datadownloadand_process.py
โ”‚ย ย  โ”œโ”€โ”€ factor_combination.py
โ”‚ย ย  โ”œโ”€โ”€ portfolio_optimization.py
โ”‚ย ย  โ””โ”€โ”€ singlefactoranalysis.py
โ””โ”€โ”€ src
    โ”œโ”€โ”€ init.py
    โ”œโ”€โ”€ constants.py
    โ”œโ”€โ”€ dataloader.py
    โ”œโ”€โ”€ factor_combinator.py    
    โ”œโ”€โ”€ portfolio_optimizer.py
    โ”œโ”€โ”€ preprocess.py
    โ””โ”€โ”€ utils.py

Version Control of .ipynb Files

Currently, we have the following notebooks on our local laptops: datadownloadandprocess.ipynb Alphalenssinglefactortesting.ipynb However, version control will be impossible if we directly push them to the repo in the form of .ipynb files. This is because jupyter notebooks are json files and cannot be displayed properly in github. As a result, we will use jupytext(pip install jupytext --upgrade) to convert between .ipynb and .py files, and store only .py files in the shared repo. Taking datadownloadandprocess.ipynb as an example, when you finish editing it on your local laptop, run jupytext --to py:percent datadownloadandprocess.ipynb in CMD and the changes will be updated to datadownloadandprocess.py. Then you can merge changes and resolve conflicts in datadownloadandprocess.py as in other python files. To fetch changes from datadownloadandprocess.py to datadownloadandprocess.ipynb, run jupytext --to notebook --update datadownloadand_process.py in CMD. Note that the --update option is essential as it will only update the code and comments in the .ipynb file while preserving graphs and outputs. \

To save your time, we have made the following shell scripts/makefiles:

(On Windows) \ ./makefiles/makefilewin.bat "scriptto_notebook" to covert scripts to notebooks \ ./makefiles/makefilewin.bat "notebookto_script" to covert notebooks to scripts

(On Mac) \ sh ./makefiles/makefilemacpytonotebook.sh to covert scripts to notebooks \ sh ./makefiles/makefilemacnotebooktopy.sh to covert notebooks to scripts

Note: make sure to update the makefile script if more notebooks are added

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท etccapital/MultiFactor ยท Updated daily from GitHub