[Python Data Science | 11] Application Combat: My First Open Source Project - Fund Scheduled Investment Backtesting Tool

This is the 60th post in Machine Future

Original address: https://robotsfutures.blog.csdn.net/article/details/127712752

"Python Data Science Quick Start Series" quick navigation:



Write at the beginning:

  • Blog Introduction: Focus on the AIoT field, follow the pulse of the future era, and record the technological growth on the way!
  • Blogger community: AIoT machine intelligence , welcome to join!
  • Column introduction: Master data science commonly used libraries Numpy, Matploblib, Pandas from 0 to 1.
  • Facing the crowd: AI primary learners

1. Project overview

The project is a fund portfolio investment tool that bloggers want to achieve.
The blogger's fund investment philosophy is long-term fixed investment, buying the market index, diversifying investment, and giving priority to keeping the principal.

1.1 Diversified investment

  • Divide into different categories: such as Shanghai and Shenzhen 300, China Securities 500, ChiNext;

    • For example, the CSI 300 is the 300 companies with the best performance in the Shanghai and Shenzhen stock markets, which represents the Chinese economy and is stable, but the income may be relatively low, and more allocations can be made;
    • CSI 500 is the 500 best-performing companies in the Chinese securities market, with a wider scope;
    • ChiNext is an entrepreneurial enterprise in China's stock market. It has great momentum and represents a new force in the future of China's economy. It has high risks and high returns. You can allocate less
    • The gold index is a fund that tracks the price of physical gold. As the saying goes: Buy gold in troubled times and buy stocks in prosperous times. When the stock market does not perform well, gold may perform well
    • The debt index is a fund that tracks national debt, and is the fixed cornerstone of the fund of funds. When other value targets perform poorly, the overall performance will not be too bad;
  • Distributed to different countries: China, the United States (S&P 500, NASDAQ 100), etc.

    • S&P 500, the 500 best-performing companies in the U.S. market
    • Nasdaq 100, most technology companies are listed on Nasdaq, you know the value
  • At present, the blogger's fund portfolio ratio is roughly

    • CSI 300 25%
    • CSI 500 15%
    • GEM 10%
    • Gold 10%
    • 20% debt
    • S&P 500 15%
    • Nasdaq 100 5%

    The blogger did not test more share ratios, and can test them by himself.

1.2 Periodic rebalancing

When the fund portfolio is created, different funds have different shares. After a period of operation, the shares will change. Some funds may rise a lot, and some funds may fall a little. The reason why some people buy funds and do not make money is What, it won't be sold, and the money that has risen has flowed out again.
The stock market is cyclical, with ups and downs and ebbs and flows. Through regular rebalancing, the fund shares that have been running for a period of time are reconfigured to the initial share ratios, and the purpose of selling high and buying low is realized in disguise, and sharpening is achieved. The purpose of Pinggu is to earn more excess returns.

It is recommended to operate the rebalancing cycle once a year, which can reduce costs.

2. User Guide

2.1 Start and run

2.1.1 Method 1

Directly open the CSDN cloud IDE to run automatically: https://idegitcode.net/RobotFutures/1024

2.1.2 Method 2

  • Download the source code from gitcode
git clone https://gitcode.net/RobotFutures/1024.git
  • start running
./env.sh&&pip install -r requirements.txt&&python fund_portfolio_backtesting_tool.py

2.2 Directory structure

├── CHANGELOG.MD                        # 修订记录
├── README.md                           # 使用文档
├── fund_fee_list.csv                   # 爬取的公募基金交易费用
├── fund_portfolio_backtesting_tool.py  # 基金组合回测工具程序
├── log.txt                             # 日志输出
├── package.json    
├── preview.yml                         # 自动启动运行脚本配置
├── requirements.txt                    # 项目所需库
├── src
├── 公募基金概要数据.csv                 # 下载的公募基金概要数据,包含交易费用
├── 回测结果                             # 回测计算结果
    └── 2022-11-06_01-23-39             # 回测时间
        ├── fund_portfolio_result.csv   # 基金组合清单及份额占比
        ├── fund_portfolio_trend.png    # 基金组合与沪深300对比
        ├── 沪深300基金参考.csv          # 沪深300参考基金历史数据
        └── 自选组合基金回测数据.csv     # 基金组合回测历史数据
├── 数据可靠性验证                       # 使用EXCEL验证组合基金复权净值的数据可靠性
    ├── 数据可靠性验证.xlsx             # 数据可靠性数据验证文档,使用EXCEL函数来实现
    ├── fund_portfolio_result.csv       # 基金组合份额数据
    └── 自选组合基金回测数据.csv        # 基金组合回测历史数据
├── 基金关键字筛选结果                   # 大盘基金关键字及策略筛选结果
├── 基金组合筛选结果列表                 # 筛选结果基金性能指标
    ├── fund_portfolio_result.csv       # 基金组合清单及份额占比
    ├── fund_portfolio_trend.png        # 基金组合与沪深300对比
    ├── 沪深300基金参考.csv              # 沪深300参考基金历史数据
    └── 自选组合基金回测数据.csv        # 基金组合回测历史数据
├── 累计净值趋势                         # 下载的公募基金累计净值历史数据       
└── 累计回报趋势                        # 下载的公募基金累计回报历史数据    

2.3 Display of backtest results

  • Visual display of backtest results

  • self-selected fund of funds

Unnamed: 0,code,years,withdrawal,annual_return,total_return,sharp,calmar,volatility,name,type,scale,m_fee,c_fee,sale_fee,sub_fee,total_fee,share
16.0,002670,6.11,27.34,6.74,37.02,0.46,12.9743,19.4,万家沪深300指数增强A,股票指数,6.34,1.0,0.12,0.0,0.1,1.22,0.5
97.0,502000,7.55,29.38,9.81,57.14,0.62,18.3793,20.16,西部利得中证500指数增强A,股票指数,5.08,1.0,0.2,0.0,0.1,1.3,0.15
3.0,001879,5.42,37.64,12.36,75.57,0.59,16.5217,26.3,长城创业板指数增强A,股票指数,6.58,1.0,0.15,0.0,0.15,1.3,0.1
4.0,002610,6.43,20.27,7.04,38.89,0.65,15.5788,12.22,博时黄金ETF联接A,联接基金,8.27,0.5,0.1,0.0,0.06,0.66,0.1
134.0,002864,6.38,0.61,3.38,17.41,10.81,224.7164,0.32,广发安泽短债债券A,债券型,28.99,0.3,0.1,0.0,0.04,0.4399999999999999,0.05
4.0,050025,10.39,31.17,11.3,67.75,0.65,17.4237,20.85,博时标普500ETF联接A,QDII-指数,8.87,0.6,0.25,0.0,0.12,0.97,0.05
5.0,161130,5.37,28.32,13.13,81.43,0.66,23.5765,25.47,易方达纳斯达克100人民币,QDII-指数,7.36,0.8,0.25,0.0,0.12,1.17,0.05
  • Data reliability verification

Use EXCEL to calculate the reweighted net value of the fund portfolio according to the proportion of the portfolio fund shares. The test results are as follows (see the [Data Reliability Verification] catalog for details):

EXCEL calculates the reweighted net value of the fund portfolio

image-20221106234134442

fund_portfolio_backtestng_tool backtesting tool to calculate the reweighted net value of the fund portfolio

image-20221106234413472

It can be seen that the data of the two are consistent, so the data reliability of the tool can be guaranteed.

3. Self-built combination

Find this location of the code

if __name__ == "__main__":
    pd.set_option('display.max_rows', 1000)
    pd.set_option('display.max_columns', 10)

    # 获得公募基金基础数据(这里不用管,执行即可)
    df_base = get_fund_base_info()

    # 大盘基金筛选(这里就是基金筛选策略)
    # 筛选策略是关键字、最大历史回测,基金成立时间、基金最小规模、基金最大允许的交易费率
    # 可支持的关键字很多,从基金的分类通用关键字即可,例如半导体、新能源、量化等
    df_kpi_csi300 = get_fund_rank(fund_list=df_base, keywords='沪深300', max_withdrawal=60.0, establish_year=5, start='2018-01-01', end='2022-10-31')
    df_kpi_csi500 = get_fund_rank(df_base, '中证500', 50.0, 5, '2018-01-01', '2022-10-31')
    df_kpi_gem = get_fund_rank(df_base, '创业板', 50.0, 5, '2018-01-01', '2022-10-31')
    df_kpi_gold = get_fund_rank(df_base, '黄金', 50.0, 5, '2018-01-01', '2022-10-31')
    df_kpi_bond = get_fund_rank(df_base, '债', 30.0, 5, '2018-01-01', '2022-10-31')
    df_kpi_sp500 = get_fund_rank(df_base, '标普500', 50.0, 5, '2018-01-01', '2022-10-31')
    df_kpi_nasda = get_fund_rank(df_base, '纳斯达克', 50.0, 5, '2018-01-01', '2022-10-31')

    # 定投基金组合回测
    fund_portfolio_backtesting(
        # 这里填写上面获得基金分类数据集
        fund_kinds_list = [df_kpi_csi300, df_kpi_csi500, df_kpi_gem, df_kpi_gold, df_kpi_bond, df_kpi_sp500, df_kpi_nasda],
        # 这里配置对应上面的基金份额
        fund_share_cfg = [0.25, 0.15, 0.10, 0.10, 0.20, 0.15, 0.05],
        # 这里填写回测起始时间和结束时间,参考跟踪基金
        start_date = '2018-01-01', end_date = '2022-10-31', fund_id_ref = '160706'
    )

4. Contact me

Follow the author for more news, please subscribe to the blog https://blog.csdn.net/RobotFutures?spm=1010.2135.3001.5343 , please contact the blogger for technical exchange of fund portfolio value fixed investment (please note: fund technical exchange)

— Recommended by bloggers’ popular columns —

Guess you like

Origin blog.csdn.net/RobotFutures/article/details/127712752