[Quantification] Obtaining the data source of the Fama-French three-factor model in actual combat

Author: fantasy good

Source: Hang Seng LIGHT Cloud Community

This article is mainly based on the process practice of Hengyoushu community to obtain the data sources required by the Fama-French three-factor model.

Original: [Quantification] How much profit can be achieved by selecting stocks through the Fama-French three-factor model?

Introduction to the Fama-French Three-Factor Model

The Fama-French three-factor model (hereinafter referred to as the "three-factor model") was proposed by Fama and French in the early 1990s. It believes that in an ideal state, the excess returns of assets are composed of market returns, scale returns, and value returns. consists of parts.

Collect data source process

Register Hengyoushu platform

Hengyoushu Profile

Hengyoushu Financial Data Community ( https://udata.hs.net ), originated from Hang Seng’s financial data open and visualized community, aims to provide quantitative investment enthusiasts, financial practitioners, university teachers and students, government agencies and financial media, etc. Crowd provides professional financial data services to meet the diverse data analysis and investment research needs of different users.

Hengyoushu provides financial data covering stocks, funds, bonds, options futures, and Hong Kong stocks (see Appendix 1 for the data catalog). Provide functions such as online preview, online download and online debugging, simple and efficient API interface (interface languages ​​include HTTP, Python, MATLAB, Java), rich interface documents and help documents, so that users can obtain data conveniently and quickly.

In the future, Hengyoushu will continue to expand professional data, improve product functions, and continuously improve service capabilities.

Register and log in to Hengyoushu platform to obtain data interface request Token, address: https://udata.hs.net/console/overAllView.

image-20211120212333694.png

Get the required data sources

According to the requirements, the following data fields need to be obtained:

  • Transaction Date : Transaction Date
  • Opening price : the opening price of the stock on that day
  • Highest price : the highest price of stock trading on that day
  • Lowest price : the lowest price of stock trading on the day
  • Closing Price : The closing price of the stock
  • Stock code : the code of the stock, Shanghai stocks end with sh, and Shenzhen stocks end with sz
  • Increase and decrease : the true and accurate increase and decrease after reinstatement
  • Trading volume : the stock trading volume of the day
  • Turnover : Stock turnover on the day
  • Total market capitalization : the total share price of the stock on that day
  • Turnover rate : The proportion of stocks that change hands on the day
  • Whether to trade : whether the stock is traded on the last trading day of this month
  • Is there a daily limit : whether the stock has a daily limit at the close of the last trading day of this month
  • Whether the limit is lower : whether the closing price of the stock on the last trading day of this month is lower or not
  • P/E Ratio TTM : Last 12 Months Price Earnings Ratio
  • Price-to-book ratio : price-to-book ratio, stock price / net assets per share in the most recent financial report

The interface that needs to obtain data is as follows:

  • 1.1.2 Trading Calendar ( https://udata.hs.net/datas/200/)
    • Get trading day time
  • 1.2.1 Daily Stock Quotes ( https://udata.hs.net/datas/332/)
    • Obtain the Shanghai and Shenzhen daily market, including yesterday's closing price, opening price, highest price, lowest price, closing price, trading volume, transaction amount and other data;
  • 1.2.3 Monthly Stock Quotes ( https://udata.hs.net/datas/334/)
    • Get the Shanghai and Shenzhen monthly market, including data such as the closing price before the month, the opening price of the month, the highest price of the month, the lowest price of the month, the closing price of the month, the monthly trading volume, and the monthly trading amount;
  • 1.4.27 Basic Valuation Information ( https://udata.hs.net/datas/406/)
    • Use the financial indicators disclosed in regular reports to conduct valuation analysis on listed companies, mainly including dividend rate, price-to-book ratio, price-to-sales ratio, price-to-cash ratio and other indicators, and support simultaneous input of multiple stock codes;

The program code of the data part is as follows:

import hs_udata as hs
import pandas as pd

# 获取从开始日期到目前日期的每个月的最后一个交易日
def get_trade_dates(token, start_date):
    hs.set_token(token)
    trade_date = hs.trading_calendar(secu_market='83',
                                     if_trading_day='1',
                                     if_month_end='1',
                                     start_date=start_date)
    return trade_date


# 获取记录A股上市、退市股票交易代码、股票名称、上市状态等信息;
def get_stock_a(token):
    hs.set_token(token)
    # 默认取全部,1-上市,2-终止;
    listed_state = "1"
    fields = "secu_code,chi_name,hs_code,secu_market,listed_sector"
    stock_datas = hs.stock_list(listed_state, fields)
    return stock_datas


# 获取 交易日期 股票行情,公司等信息
def get_stock_month_trade(token, secu_code, trade_date):
    hs.set_token(token)

    # 获取股票交易信息
    # 获取数据:证劵代码,交易日期,开盘价,最高价,收盘价,涨跌幅,成交数量,成交额,换手率,涨跌停状态,交易状态
    stock_fields = "prod_code,trading_date,open_price,high_price,close_price,px_change_rate," \
                   "business_amount,business_balance,turnover_ratio,up_down_status,turnover_status"
    stock_data = hs.stock_quote_daily(en_prod_code=secu_code,
                                      trading_date=trade_date,
                                      adjust_way=2,
                                      fields=stock_fields)

    # 获取公司估值等信息
    # 获取数据:证劵代码,交易日期,总市值,总市值(证监会算法),市净率PB(最新财报,LF),市盈率(最新年报,LYR)
    value_fields = "prod_code,total_market_value,total_market_value_zjh,pb_lf,pe_rate_lyr"
    value_data = hs.valuation_info(en_prod_code=secu_code,
                                   trading_date=trade_date,
                                   fields=value_fields)
    res_data = pd.merge(stock_data, value_data, how='left', on='prod_code')
    return res_data

The results obtained by executing the program are shown in the figure:

image-20211120191635802.png

Summarize

Obtaining financial data such as stocks through Hengyoushu is very easy to operate, which greatly improves the efficiency of financial data analysis and other applications.

Guess you like

Origin blog.csdn.net/weixin_44433834/article/details/122620043