Select stocks based on P/E and Devidend Yeild
In this project, we use the P/E ratio and dividend yield to select and hold stocks in the Vietnamese stock market. The portfolio is rebalanced on the first day of each month; if that day is a holiday, rebalancing is carried out on the next trading day.
In value investing, one common approach to stock selection involves identifying undervalued companies with strong income potential. This strategy leverages two key financial metrics: the Price-to-Earnings (P/E) ratio and the dividend yield. The P/E ratio helps assess whether a stock is trading at a reasonable price relative to its earnings, while the dividend yield highlights its income-generating potential.
The core idea is to select stocks with a low P/E ratio and a high dividend yield, as these stocks may represent undervalued opportunities that also offer steady cash returns. This method is particularly effective in mature or stable markets, such as the Vietnamese stock market, where dividend-paying companies are often more established and financially sound.
We filter and maintain a portfolio of stocks with a price-to-earnings (P/E) ratio within the range of (0, 15) and a dividend yield (DY) greater than 0.01. The dividend yield is calculated as:
- DY = DPS / Price
where DPS denotes dividends per share. Existing holdings are sold before purchasing new stocks at each rebalancing period. The strategy does not yet account for trading volume; it is assumed that all stocks can be bought immediately without liquidity constraints.
- Data source: Algotrade database
- Data period: from 2022-01-01 to 2025-01-01
- Financial and closing price data are extracted and stored in a .csv file to reduce the time required for subsequent actions.
- Each sell or buy side will be charge 0.035% fee.
- The daily close price is collected from Algotrade database using SQL queries.
- The data is collected using the script
data_loader.py - The data is stored in the
data/pe_dps.csvanddata/vnindex.csvfiles.
- P/E and DPS are calculated based on:
- Net Profit After Tax Atributed To Shareholder
- Outstanding share
- Dividends paid
- The data is also collected using the
data_loader.pyfile.
- Set up python virtual environment
python -m venv venv
source venv/bin/activate # for Linux/MacOS
.\venv\Scripts\activate.bat # for Windows command line
.\venv\Scripts\Activate.ps1 # for Windows PowerShell- Install the required packages
pip install -r requirements.txt- (OPTIONAL) Create
.envfile in the root directory of the project and fill in the required information. The.envfile is used to store environment variables that are used in the project. The following is an example of a.envfile:
DB_NAME=<database name>
DB_USER=<database user name>
DB_PASSWORD=<database password>
DB_HOST=<host name or IP address>
DB_PORT=<database port>Data can be download directly from Google Drive. The data files are stored in the data folder with the following folder structure:
data
├── is
│ ├── pe_dps.csv
│ └── vnindex.csv
└── os
├── pe_dps.csv
└── vnindex.csv
You should place this folder to the current PYTHONPATH for the following steps.
To collect data from database, run this command below in the root directory:
python data_loader.pyThe result will be stored in the data/pe_dps.csv and data/vnindex.csv
Specify period and parameters in parameter/backtesting_parameter.json file.
python backtesting.pyThe results are stored in the result/backtest/ folder.
To run the optimization, execute the command in the root folder:
python optimization.pyThe optimization parameter are store in parameter/optimization_parameter.json. After optimizing, the optimized parameters are stored in parameter/optimized_parameter.json.
To run the out-of-sample backtesting results, execute this command
python evaluation.pyThe script will get value from parameter/optimized_parameter.json to execute. The results are stored in the result/optimization folder.
Running the in-sample backtesting by execute the command:
python backtesting.py- Backtesting results are stored in the
result/backtest/folder. - Used metrics to compare with VNINDEX are:
- Sharpe ratio (SR)
- Sortino ratio (SoR)
- Information ratio (Inf)
- Maximum drawdown (MDD)
- We use a risk-free rate of 6% per annum, equivalent to approximately 0.023% per day, as a benchmark for evaluating the Sharpe Ratio (SR) and Sortino Ratio (SoR).
- The backtesting results with VNINDEX benchmark is constructuted from 2019-01-01 to 2022-01-01.
| Metric | Value |
|---------------------------|------------------------------------|
| Sharpe Ratio | 1.2971 |
| Information Ratio | 0.4734 |
| Sortino Ratio | 1.7297 |
| Maximum Drawdown (MDD) | -0.2828 |
| HPR (%) | 104.9 |
| Excess HPR (%) | 35.97 |
| Monthly return (%) | 2.2617 |
| Excess monthly return (%) | 0.5012 |
| Annual return (%) | 27.09 |
- The HPR chart. The chart is located at:
result/backtest/hpr.svg - Drawdown chart. The chart is located at
result/backtest/drawdown.svg
The configuration of optimization is stored in parameter/optimization_parameter.json you can adjust the range of parameters. Random seed is used for reconstructing the optimization process. The optimized parameter is stored in parameter/optimized_parameter.json
The optimization process can be reproduced by executing the command:
python optimization.pyThe currently found optimized parameters with the seed 2024 are:
{
"pe": [0, 10],
"dy": [0.056272982721535775, 1e6]
}- Specify the out-sample period and parameters in
parameter/backtesting_parameter.jsonfile. - The out-sample data is loaded on the previous step. Refer to section Data for more information.
- To evaluate the out-sample data run the command below
python evaluation.py- The out-sample backtesting results with VNINDEX benchmark is constructuted from 2022-01-01 to 2024-01-01.
| Metric | Value |
|---------------------------|------------------------------------|
| Sharpe Ratio | -0.3497 |
| Information Ratio | 0.7441 |
| Sortino Ratio | -0.4363 |
| Maximum Drawdown (MDD) | -0.4604 |
| HPR (%) | -3.170 |
| Excess HPR (%) | 18.26 |
| Monthly return (%) | -0.1904 |
| Excess monthly return (%) | 0.8778 |
| Annual return (%) | -4.243 |
- The HPR chart. The chart is located at
result/optimization/hpr.svg. - Drawdown chart. The chart is located at
result/optimization/drawdown.svg.
[1] ALGOTRADE, Algorithmic Trading Theory and Practice - A Practical Guide with Applications on the Vietnamese Stock Market, 1st ed. DIMI BOOK, 2023, pp. 64–67. Accessed: Apr. 30, 2025. [Online]. Available: Link
[2] ALGOTRADE, “Weighting Methods Used in Smart-Beta Strategy,” Website, Jul. 10, 2024. Available: Link (accessed Apr. 30, 2025).