600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 使用python语言 利用tushare进行股票筛选

使用python语言 利用tushare进行股票筛选

时间:2021-08-17 16:49:14

相关推荐

使用python语言 利用tushare进行股票筛选

由于在2月18日之后,大盘产生剧烈波动,在此期间白马股对资金的反应十分平淡,存在白马股下跌的情形,所以想利用tushare筛选2-18日到3-9日这几十天内上涨的股票,并进一步分析其特征。基本思路如下,程序在最后。

1.打开excel文件,股票池。

2.读取文件的每一行

3.利用当前此行的股票代码获取基本面

4.利用基本面信息筛选达到条件股票

5.将此股票基本面信息存入新建excel文件中

import xlrdimport reimport tushare as tsimport matplotlib.pyplot as pltsh_stock = xlrd.open_workbook(r'D:\Program Files (x86)\python\open_study\上学年\Daliy_Stock\上证股票list.xls') sh_stock_sheet1 = sh_stock.sheets()[0]#获取第一列的整列的内容col = sh_stock_sheet1.col_values(0)#第一列内容为一个列表,列表内是一组、一组的字符串。#建立一个循环,将列表内的字符串依次读出#print(len(col))for index in range(len(col)):#获取列的第[index]个元素#print(index)# print('当前股票:',col[index])#对 col[index] 去除括号#对于pile()函数进行配置,无需改变参数p1 = pile(r'[(](.*?)[)]', re.S)#获取列的第[index]个元素内容stock_index = col[index]#print('列的第[index]个元素内容:',stock_index)'列的第[index]个元素内容: 金龙鱼(300999),这是一个字符串'#获取 列的第[index]个元素 中括号内 编号stock_index_number = re.findall(p1, stock_index)count_number = len(col)-indexprint(count_number)#print('当前股票编号:',stock_index_number)"当前股票编号: ['300999'],列表内为字符串,而不是int "#print('当前股票编号:',stock_index_number[0])"获取当前股票编号的历史数据,用最高价格表示""当股票编号为退市时,使用try- except 语句进行处理"try:#ts.get_hist_data(stock_index_number[0]) 函数传入的应为字符串数据,而不是列表,要加[0]hist_data = ts.get_hist_data(stock_index_number[0])if hist_data.loc['-02-18',['high']].values[0] < hist_data.loc['-03-09',['high']].values[0]:print('当前股票编号:',stock_index_number[0])#将筛选出的股票存入文件中!!#第[index]个股票的 日期内数据filter_data = ts.get_hist_data(stock_index_number[0],start='-02-18',end='-03-09')stock_index1=stock_index.replace("*", "O");#建立文件名,由字符串组成path ='D:\stock_data/up_stock_上证_'+stock_index1+'.csv'#建立文件file = open(path,"w")#写入数据filter_data.to_csv(path,columns = filter_data.columns,index = True)#stock_index_number_data = hist_data.loc[['-02-18','-03-09'],['open', 'high','close','low','volume',' price_change', 'p_change']]#print(stock_index_number_data )#print('当前股票编号:',stock_index_number[0])#print('-02-18 high price : ',hist_data.loc['-02-18',['high']].values[0])#print('-03-09 high price : ',hist_data.loc['-03-09',['high']].values[0])except (KeyError) :continue#if index == 5:#breakprint('\n end!')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。