Py学习  »  Python

【Python技术】利用巴菲特指标计算A股估值进行价值投资

子晓聊技术 • 5 月前 • 155 次点击  

星球一些同学玩中长线价值投资的, 之前写了很多文章都是偏短线涨停分析、最高板之类的, 后续准备也写些 中长线的技术分析类的文章。 毕竟玩短线分析,其实最重要的是大盘情绪、概念板块、量价分析等,像其他的技术指标(kdj、macd、rsi、 boll)更多做辅助决策。我写的文章更多是通过数据量化的方式来捕捉这些信号。 至于看后觉得有没有用,就看每个人对数据的理解了。
那中长线价值投资呢,一定要对目标标的有合适的估值预估,低估时买入、高估时卖出。中长线价值投资, 不得不提之前说的巴菲特指标,还是以它为例,用程序来指导我们是如何使用的。
去年10月份的时候我写过一篇文章, 估计有人没看过。这里再普及下什么是巴菲特指标。

“巴菲特指标”的计算基于美国股市的市值与衡量国民经济发展状况的国民生产总值(GNP),巴菲特认为,若两者之间的比率处于70%至80%的区间之内,这时买进股票就会有不错的收益。但如果在这个比例偏高时买进股票,就等于在“玩火”。

其实不止美股, A股大方向其实类似。  低于70%可以买入, 高于80%处于高风险卖出, 在 70%至80%这区间数值动态调整仓位大小。


题外话:
 中长线投资,就是和时间做朋友,特别适合上班没时间盯盘的上班族。很多同学没时间盯盘,没有完整成熟的交易体系, 那怎么和专业的量化、机构、游资拼呢? 运气赚的钱,终会通过实力亏回去。

相关文章推荐:
【股海杂谈】指数长期投资,浅谈巴菲特指标

最后附上源代码,需要的自取。 备注:如果发现格式有多余的特殊字符,用普通浏览器打开复制应该没问题。


import akshare as akimport streamlit as stimport plotly.graph_objects as goimport pandas as pddef get_buffett_index():    """通过AKShare获取实时巴菲特指标数据"""    df = ak.stock_buffett_index_lg()    #print(df)    # 数据清洗与格式转换    latest_data = df.iloc[-1].to_dict()  # 取最新一条数据    return {        'date': pd.to_datetime(latest_data['日期']).strftime('%Y-%m-%d'),        'total_market'round(latest_data['总市值'] / 1e42),  # 转换为万亿元        'gdp'round(latest_data['GDP'] / 1e42),  # 转换为万亿元        'ratio'round(latest_data['总市值'] / latest_data['GDP'] * 1001),        'decade_percentile': latest_data['近十年分位数'],        'history_percentile': latest_data['总历史分位数']    }def get_sh_index():    """获取上证指数历史数据(含最新交易日)"""    df = ak.stock_zh_index_daily(symbol="sh000001")    return df[['date''open''high''low''close''volume']]def app():    # 创建Streamlit界面    st.title("A股巴菲特指标")    current_data = get_buffett_index()    df = get_sh_index().tail(200)    # 指标分析模块    with st.container():        st.subheader(f"{current_data['date'


    
]} 数据")        st.metric("巴菲特指标",                  f"{current_data['ratio']}%",                  help="总市值/GDP比率")        st.write(f"总市值:{current_data['total_market']} 万亿元")        st.write(f"GDP总量:{current_data['gdp']} 万亿元")        # 动态进度条(根据历史分位数)        progress_value = current_data['history_percentile'] * 100        st.progress(progress_value / 100,                    text=f"历史分位数:{progress_value:.1f}%")        # 智能仓位建议        if current_data['ratio'] 60:            st.success("建议仓位:100%(极度低估)")        elif 60 <= current_data['ratio'] 70:            st.success("建议仓位:80%-100%(价值区间)")        elif 70 <= current_data['ratio'] <= 80:            ratio = 1 - (current_data['ratio'] - 70) / 10            st.warning(f"建议仓位:{ratio * 100:.0f}%(合理区间)")        else:            st.error("建议仓位:<30%(高风险区域)")        # 动态K线图(含最新数据)        fig = go.Figure(data=[go.Candlestick(            x=df['date'],            open=df['open'],            high=df['high'],            low=df['low'],            close=df['close'],            increasing_line_color='red',            decreasing_line_color='green',            name='上证指数'        )])        fig.update_layout(            title='上证指数实时K线图',            yaxis_title='点位',            xaxis_rangeslider_visible=False        )        st.plotly_chart(fig, use_container_width=True)

如果我的分享对你投资有所帮助,不吝啬给个点赞关注。 

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/182073