python根据财务指标寻找价值股票
生活随笔
收集整理的這篇文章主要介紹了
python根据财务指标寻找价值股票
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
確定股票池
from concurrent.futures import ThreadPoolExecutor import urllib import os from time import sleep import pandas as pd # 上證代碼 shanghaicode = [] for i in range(600000, 606000, 1):shanghaicode.append(str(i))# 深證代碼 shenzhencode = [] for i in range(1000000, 1005000, 1):i = str(i)[1:]shenzhencode.append(i)?爬取數(shù)據(jù)
def get_data(num):url = 'http://quotes.money.163.com/service/lrb_' + str(num) + '.html'while True:try:content = urllib.request.urlopen(url, timeout=2).read()path = '利潤(rùn)表_multi/' + str(num) + '.csv'if os.path.exists(path):print(path + " already existed!!!")breakwith open('利潤(rùn)表_multi/' + str(num) + '.csv', 'wb') as f:f.write(content)print(num)sleep(1)except Exception as e:if str(e) == 'HTTP Error 404: Not Found':print(f"{num} : {e}")breakelse:print(e)多線程運(yùn)作
executor = ThreadPoolExecutor(max_workers=10) executor.map(get_data, shenzhencode) executor.shutdown()executor = ThreadPoolExecutor(max_workers=10) executor.map(get_data, shanghaicode) executor.shutdown()?讀取本地?cái)?shù)據(jù)
def generatefile(path):names = []for dirpath, dirnames, filenames in os.walk(path):names = filenamesreturn names datapath = '利潤(rùn)表_multi/' datalist = generatefile(datapath) invest = [] for data in datalist:try:path = datapath + datatemp = pd.read_csv(path, encoding='gbk', header=None)temp = pd.DataFrame(temp.values.T, index=temp.columns, columns=temp.index)temp.columns = temp.loc[0]temp = temp[1:]temp = temp[:-1]#temp['報(bào)告日期'] = temp['報(bào)告日期'].apply(convert_date)temp = temp[['報(bào)告日期','凈利潤(rùn)(萬(wàn)元)']]temp['凈利潤(rùn)(萬(wàn)元)'] = temp['凈利潤(rùn)(萬(wàn)元)'].astype(int)temp_g = pd.DataFrame(temp.groupby('報(bào)告日期').sum())temp_g = temp_g[:-1] # 去除2021temp_g.reset_index(inplace=True)temp_g = temp_g['凈利潤(rùn)(萬(wàn)元)']anu_diff = temp_g.diff()temp_g = temp_g.values# anu_diff = anu_diff.valuestemp_g = temp_g[::-1]ratio = 0.3if len(temp_g) >= 5:# rate = anu_diff[-5:]/temp_g[-6:-1]# if rate[-1] >= ratio and rate[-2] >= ratio and rate[-3] >= ratio and rate[-4] >= ratio:# invest.append(data)growth_anu = []for i in range(len(temp_g)):if i == (len(temp_g)-1):continueyear = temp_g[i]ex_year = temp_g[i+1]if i+1 <= 5:growth = (year - ex_year)/ex_yeargrowth_anu.append(growth)if growth_anu[0] >= ratio and growth_anu[1] >= ratio and growth_anu[2] >= ratio and growth_anu[3] >= ratio:invest.append(data)except Exception as e:print(data + f':{e}') invest = pd.DataFrame(invest) invest.to_excel('連續(xù)4年增長(zhǎng)30%.xls')總結(jié)
以上是生活随笔為你收集整理的python根据财务指标寻找价值股票的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 什么是IPsec协议
- 下一篇: 【狂神说】Redis笔记