·¢²¼Ê±¼ä:2022-12-18 21:04:38 ÎÄÕÂÀ´Ô´:»¥ÁªÍø
΢²© ΢ÐÅ QQ¿Õ¼ä

×ÔÓÉÏÖ½ðÁ÷Ä£ÐÍ-PythonʵÏÖ

×ÔÓÉÏÖ½ðÁ÷Ä£ÐÍ-PythonʵÏÖ

ÈçºÎ¹ÀËãÒ»¼Ò¹«Ë¾»òÒ»Ö»¹ÉƱµÄÄÚÔÚ¼ÛÖµ£¬Ò»Ö±ÊÇͶ×ÊÖеĺËÐÄÎÊÌâ¡£Õë¶ÔÕâ¸öÎÊÌ⣬ÎÒÃÇÔÚ¡¶¹ÉƱÈçºÎ¹ÀÖµ-Öªºõ¡·Ò»ÎÄÖÐÖصã½éÉÜÁË¡°×ÔÓÉÏÖ½ðÁ÷ÕÛÏÖ·¨¡±£¬ÕâÒ²ÊǸñÀ׶òÄ·¡¢°Í·ÆÌصȼÛֵͶ×Ê´ÓÒµÕß×îÍƼöµÄ¹ÀÖµ·½·¨¡£µ«ÊÇ£¬ÉÏÊй«Ë¾Óм¸Íò¼Ò£¬ÒªÒ»Ò»ºË¶ÔËûÃǵIJƱ¨£¬ÓÃÕâÖÖ·½·¨À´¹À¼Û£¬ÊǷdz£·ÑÁ¦µÄ¡£±¾ÎĽéÉÜÈçºÎʹÓÃPythonʵÏÖ×ÔÓÉÏÖ½ðÁ÷ÌùÏÖ·¨¶Ô¹ÉƱ½øÐйÀÖµ¡£

×ÔÓÉÏÖ½ðÁ÷Ä£ÐÍ

ÈÃÎÒÃǼòÒª»Ø¹ËÒ»ÏÂ×ÔÓÉÏÖ½ðÁ÷Ä£ÐÍ¡£

×ÔÓÉÏÖ½ðÁ÷Á¿£ºÊÇÆóÒµÔÚÂú×ãÔÙͶ×ÊÐèÒªºó²úÉúµÄÊ£ÓàÏÖ½ðÁ÷Á¿¡£Õⲿ·ÖÏÖ½ðÁ÷Á¿ÊÇÔÚ²»Ó°Ï칫˾¿É³ÖÐø·¢Õ¹µÄÇé¿öÏ£¬¿ÉÒÔ·ÖÅä¸ø¹É¶«µÄ×î´óÏÖ½ðÊý¶î¡£

¼òµ¥µØ˵£¬×ÔÓÉÏÖ½ðÁ÷Á¿£¨FCF£©µÈÓÚ¾­Óª»î¶¯²úÉúµÄÏÖ½ðÁ÷Á¿Óë×ʱ¾Ö§³ö£¨Capital Expenditures£¬CE£©Ö®²î¡£¼´£ºFCF=OCF-CE¡£

×ÔÓÉÏÖ½ðÁ÷Á¿ÕÛÏÖ·¨£ºÆóÒµµÄ¼ÛÖµÊÇÆóҵδÀ´ÄÜ×î´ó³Ì¶ÈµØÏò¹É¶«·ÖÅä¶àÉÙÏÖ½ð¡£ÁíÍ⣬δÀ´µÄÏÖ½ð²»ÈçÏÖÔÚµÄÏÖ½ð£¬ËùÒÔÐèÒªÒ»¶¨µÄÕÛ¿Û¡£

×ʱ¾Ö§³ö

×ÔÓÉÏÖ½ðÁ÷µÄ¼ÆË㹫ʽΪ£ºFCF=OCF-CE£¬ÆäÖÐÆóÒµµÄ¾­ÓªÏÖ½ðÁ÷£¨OCF£©Ò»°ã¿ÉÒÔÖ±½ÓÔÚ²ÆÎñ±¨¸æµÄÏÖ½ðÁ÷Á¿±íÖв鵽£¬µ«×ʱ¾Ö§³ö£¨CE£©ÐèÒª×Ô¼ººË¶Ô»ã×Ü£¬ºÜÄÑ׼ȷ¼ÆËã¡£

×ʱ¾Ö§³ö£¨CE£©ÊÇÖ¸ÆóҵΪ³ÖÐø·¢Õ¹¶ø¹ºÖù̶¨×ʲú¡¢ÎÞÐÎ×ʲúµÈËù·¢ÉúµÄ·ÑÓᣴËÀàÖ§³öͨ³£¿ÉÒÔ×ʱ¾»¯£¬²¢ÇÒ×ʲúµÄʹÓÃÊÙÃü¿çÔ½¶à¸ö»á¼ÆÆڼ䡣Ϊ¼òµ¥Æð¼û£¬Í¨³£¿¼ÂÇÒÔÏÂÈýÀàÖ÷ÒªÖ§³ö£º

²éѯÆóÒµÏÖ½ðÁ÷

ÎÒÃÇͨ¹ýbaostockµÄquery_cash_flow_data½Ó¿Ú»ñÈ¡¼¾±¨Öеľ­ÓªÏÖ½ðÁ÷ÓëÀûÈóµÄ±ÈÀý£¬È»ºóͨ¹ýquery_profit_data½Ó¿Ú»ñÈ¡¼¾±¨ÖеÄÀûÈóÊý¾Ý£¬½«Á½ÕßÏà³ËµÃµ½¾­ÓªÏÖ½ðÁ÷¡£´ËÍ⣬µÚËļ¾¶ÈÒ»°ã¼Ç¼µÄÊÇÕû¸ö»á¼ÆÄê¶ÈµÄÊý¾Ý£¬Òò´Ë¿ÉÒÔÓÃÀ´¼ÆËãÈ«ÄêµÄ¾­ÓªÏÖ½ðÁ÷¡£

import baostock as bs
import pandas as pd
# µÇ½ϵͳ
lg = bs.login()
# ÏÔʾµÇ½·µ»ØÐÅÏ¢
print('login respond error_code:'+lg.error_code)
print('login respond  error_msg:'+lg.error_msg)
def query_anual_cf(stock_code, year, qt=4):
    cash_flow_list = []
    rs_cash_flow = bs.query_cash_flow_data(code=stock_code, year=year, quarter=qt)
    while (rs_cash_flow.error_code == '0') & rs_cash_flow.next():
        cash_flow_list.append(rs_cash_flow.get_row_data())
    result_cash_flow = pd.DataFrame(cash_flow_list, columns=rs_cash_flow.fields)
    profit_list = []
    rs_profit = bs.query_profit_data(code=stock_code, year=year, quarter=qt)
    while (rs_profit.error_code == '0') & rs_profit.next():
        profit_list.append(rs_profit.get_row_data())
    result_profit = pd.DataFrame(profit_list, columns=rs_profit.fields)
    result = float(result_cash_flow["CFOToNP"]) * float(result_profit["netProfit"])/100000000
    return result

Èôµ±ÄêÄ걨ÉÐδ³ö¾ß£¬¿ÉÏȼÆËãÒѳö¾ß¼¾¶ÈµÄ¾­ÓªÐÔÏÖ½ðÁ÷Á¿£¬²¢¼ÆËãÏà¶ÔÓÚÉÏÄêµÄͬ±È£¬ÆäÓà°´Öµ¼ÆËãÉÏÄê³ËÒÔ½ñÄêͬ±È¡£

def estimate_current_year_cf(stock_code, year, qt):
    previos_cf = query_anual_cf(stock_code, year-1, 4)
    previos_cf_p = query_anual_cf(stock_code, year-1, qt)
    cf_p = query_anual_cf(stock_code, year, qt)
    return cf_p + (previos_cf - previos_cf_p) * (cf_p/previos_cf_p)

×ʱ¾Ö§³öÏîÄ¿µÄ¼ÆËãÐèÒª²Î¿¼ÏêϸµÄÀûÈó±í¡£Õâ¸öÏîÄ¿ÔÝʱûÓÐÕÒµ½ºÃµÄÊý¾ÝÔ´£¬ËùÒÔÔÝʱ²»¿¼ÂÇÕâ¸öÏîÄ¿£¬ÒÔºóÔÙÕÒÊý¾ÝÔ´¡£

¹ÀÖµ

¸ù¾ÝHow to Value Stocks - Zhihu(Öªºõ)£¬¼ÆËãδÀ´×ÔÓÉÏÖ½ðÁ÷ÐèÒª¹À¼ÆÄêÔö³¤ÂÊ¡£ÕâÀïÓùýÈ¥Ò»¶Îʱ¼äµÄƽ¾ùÔö³¤ÂÊÀ´¹ÀË㣬ÈçÏ¡£

def estimate_increasing_rate(stock_code, start_year, end_year):
    start_fcf = query_anual_fcf(stock_code, start_year)
    end_fcf = query_anual_fcf(stock_code, end_year)
    if end_fcf/start_fcf < 0:
        return 0
    return (end_fcf/start_fcf)**(1/(end_year-start_year)) -1

ΪÁË·½±ãÓ뵱ǰ¹É¼Û½øÐбȽϣ¬ÎÒÃÇÐèÒª½«¹À¼ÆµÄÆóÒµ¼ÛÖµ³ýÒÔ×ܹÉÊý£¬¿ÉÒÔʹÓÃÀûÈóÊý¾ÝÖеġ°totalShare¡±×ֶλñµÃ¡£

def query_stock_count(stock_code, year, quarter):
    profit_list = []
    rs_profit = bs.query_profit_data(code=stock_code, year=2020, quarter=4)
    while (rs_profit.error_code == '0') & rs_profit.next():
        profit_list.append(rs_profit.get_row_data())
    result_profit = pd.DataFrame(profit_list, columns=rs_profit.fields)
    return float(result_profit["totalShare"])/100000000

×îºóÄÄÄܲ鵽¹ÉƱµÄ×ÔÓÉÏÖ½ðÁ÷£¬¿ÉÒÔ¸ù¾ÝÔ¤¹ÀÔö³¤ÂÊ¡¢³ÖÐøÔö³¤³ÖÐøʱ¼ä¡¢ÌùÏÖϵÊýºÍ¿É³ÖÐøÔö³¤ÂÊÀ´¹ÀËã¹ÉƱµÄÄÚÔÚ¼ÛÖµ¡£

def estimate_price(fcf_init, stock_count, increase_rate=0.1, increase_year=10, R=0.08, g=0.03):
    ###
    # increase_rate Ô¤¹ÀÔö³¤ÂÊ
    # increase_year Ô¤¹ÀÔö³¤ÄêÊý
    # g ÓÀÐøÔö³¤ÂÊ
    # R ÕÛÏÖϵÊý
    ###
    total_fcf = 0
    for n in range(1, increase_year+1):
        cfn = fcf_init * (1+increase_rate)**n
        total_fcf += cfn/((1+R)**n)
    total_fcf += cfn * (1+g)/(R-g)/((1+R)**increase_year)
    return total_fcf/stock_count

ÒÔę́¹É·ÝΪÀý£¬Õû¸ö¼ÆËã¹ý³ÌÈçÏÂ

stock_code = "sh.600519"
incre_rate = estimate_increasing_rate(stock_code, 2012, 2020)
print(incre_rate)
# 0.2011945941597566
init_fcf = query_anual_cf(stock_code, 2020)
stock_cnt=query_stock_count(stock_code, 2020, 4)
estimate_price(init_fcf, stock_cnt, incre_rate, 10, 0.08, 0.03)
# 3227.5443645346045

×ܽá

ÆóÒµµÄÄÚÔÚ¼ÛÖµµÈÓÚδÀ´´´ÔìµÄ×ÔÓÉÏÖ½ðÁ÷µÄÌùÏÖÖµ¡£±¾ÎÄʹÓÃbaostockÌṩµÄPython API£¬ÌṩÁËÒ»¸ö¿ìËÙ¼ÆËãÕâ¸öÖµµÄ´úÂ룬¿ÉÒԺܷ½±ãµÄÕë¶Ô´óÁ¿µÄ¹ÉƱÄÄÄܲ鵽¹ÉƱµÄ×ÔÓÉÏÖ½ðÁ÷£¬¸ù¾ÝÕâ¸öÖµ½øÐмÆË㡣ѡ¹É¡£

Ïà¹ØÎÄÕÂÍƼö

ÁíÒ»ÊÓ½Ç

»»Ò»»»