想买低价股?看看利用Python对A股低价股的这些数据分析!
大盘不稳,高价股让人心惊,想换点低价股试试水?
新手模拟交易玩腻了,想试试实盘炒股又怕风险太大,考虑从低价股入手?
别看着股价低就下手,看看下面对一些低价股的简单筛选和分析,或许对你有所帮助。
系统环境:Python+Tushare+Matplotlib
一、获取行情数据
import tushare as ts # 获取实时行情数据 hq = ts.get_today_all() # 节选出股票代码code、名称name、涨跌幅changepercent、股价trade hq = hq[['code','name','changepercent','trade']] # 筛选出当前股价高于0元低于3元的股票信息 mins = hq.trade>0.00 maxs = hq.trade<=2.99 allselect = mins & maxs data = hq[allselect].sort('trade')
得到股价小于3元的低价股票信息,然后对深圳成指和异常股票进行剔除:
# 提取低价股股票代码,并剔除深圳成指股票 code_list = [] for c in data.code: if c[0] != "0": code_list.append(c) code_list
得到九只股票的代码:
['600401', '601005', '600022', '601558', '601258', '601880', '600307', '600282', '600231']
然后获取2014-12-01至2016-12-01上证指数的历史行情数据:
# 获取2014-12-01至2016-12-01的上证指数历史行情数据 sh_hist_data = ts.get_hist_data(code='sh',start='2014-12-01',end='2016-12-01') sh_hist_data = sh_hist_data[['open','high','close','low','volume','price_change','p_change']].sort_index()
引入matplotlib模块:
import matplotlib.pyplot as plt from matplotlib.pylab import datestr2num import matplotlib # 设置中文字体 font = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
二、对比上证指数每日最低价和各只低价股每日最低价的价格走势
%matplotlib inline sh_hist_data.head(10) dates = [datestr2num(i) for i in sh_hist_data.index] plt.figure(figsize=(40,30)) # 新建一个子图,绘制上证指数走势 plt.subplot(311) plt.title("2014年12月1日至2016年12月1日上证指数最低价走势",fontsize=30) plt.xticks(rotation=20,fontsize=26) plt.yticks(fontsize=26) plt.plot_date(dates,sh_hist_data.low,'-',linewidth=2.5) plt.grid(True) # 遍历低价股代码列表,绘制股价走势 for i in code_list[0:4]: hist_data = ts.get_hist_data(code=i,start='2014-12-01',end='2016-12-01') code_data = hist_data[['open','high','close','low','volume','price_change','p_change']].sort_index() dates = [datestr2num(t) for t in code_data.index] plt.subplot(312) plt.title("2014年12月1日至2016年12月1日各低价股最低价走势",fontsize=30) plt.xticks(rotation=20,fontsize=26) plt.yticks(fontsize=26) plt.plot_date(dates,code_data['low'],'-',linewidth=2.5,label=i) plt.legend(loc=1,fontsize=22) plt.grid(True) for i in code_list[4:]: hist_data = ts.get_hist_data(code=i,start='2014-12-01',end='2016-12-01') code_data = hist_data[['open','high','close','low','volume','price_change','p_change']].sort_index() dates = [datestr2num(t) for t in code_data.index] plt.subplot(313) plt.title("2014年12月1日至2016年12月1日各低价股最低价价走势",fontsize=30) plt.xticks(rotation=20,fontsize=26) plt.yticks(fontsize=26) plt.plot_date(dates,code_data['low'],'-',linewidth=2.5,label=i) plt.legend(loc=1,fontsize=22) plt.grid(True)
得到以下图表:
看两年的最低价走势,与上证大盘走势最不吻合的股票有:601258、600231、601880、600401
三、对比各股历史平均成交量
# 对各低价股的历史平均成交量做可视化处理 import numpy as np font = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc') codes = [] volumes = [] for i in code_list: histdata = ts.get_hist_data(code=i,start='2014-12-01',end='2016-12-01') volume_data = histdata['volume'].mean()/10000 codes.append(i) volumes.append(volume_data) # print(codes,volumes) x = np.arange(len(codes)) # 获取各只股票平均成交量的平均值 mean_volume = float(sum(volumes)/len(volumes)) # 将成交量均值绘制一条直线 plt.axhline(y=mean_volume,color='r') plt.xticks(x,codes,rotation=45) plt.title("各低价股两年平均成交量比较") plt.bar(x,volumes,0.5,color='teal') plt.xlabel("股票代码",fontproperties=font) plt.ylabel("平均成交量(万元)",fontproperties=font) plt.grid(True)
得到如下柱状图:
由上图可知以下信息:
- 两年平均成交量约为100(万元)
- 在其之上的股票有:600401、600307、600010
- 两年平均成交量最低的为:600231
四、对比上证和各股两年涨跌幅
# 上证指数及各低价股两年涨跌幅 p_changes = [] codes = [] sh_change = sh_hist_data.p_change.sum() for i in code_list: code_change = ts.get_hist_data(code=i,start='2014-12-01',end='2016-12-01').p_change.sum() codes.append(i) p_changes.append(code_change) codes[-1] = "上证" p_changes[-1] = sh_change x = np.arange(len(codes)) plt.axvline(x=sh_change,color='r') plt.title('上证及各低价股两年涨跌幅') plt.yticks(x,codes) plt.xlabel("涨幅(百分比)") # plt.barh([10],[sh_change],color='r') rets = plt.barh(x,p_changes,color='sage',align="center") plt.grid(True)
由上图可知:
- 上证两年的涨跌幅为30%
- 涨幅在此之下的股票有:601258、601005、600401
五、对比各股两年的平均换手率
# 各低价股的换手率对比 codes = [] tur = [] for i in code_list: # 获取两年的历史行情数据 turnovers = ts.get_hist_data(code=i,start='2014-12-01',end='2016-12-01').turnover # 获取存在数据天数 t_days = len(turnovers) # 对换手率进行相加 turnovers = turnovers.sum() # 计算平均换手率 avg_tur = turnovers/t_days codes.append(i) tur.append(avg_tur) x = np.arange(len(codes)) plt.title("各低价股两年平均换手率") plt.yticks(x,codes) plt.barh(x,tur,color='darkslategrey',align='center') plt.grid(True)
由上图可知:
- 两年平均换手率最高的为:601005重庆钢铁
- 两年平均换手率最低的为:600022山东钢铁
嗯,简单的筛选就已经结束了,不知道你心里有没有心仪的低价股了?