前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >手把手教你利用Python爬虫分析基金、股票

手把手教你利用Python爬虫分析基金、股票

作者头像
Python知识大全
发布2021-03-16 10:44:42
1.7K0
发布2021-03-16 10:44:42
举报
文章被收录于专栏:Python 知识大全

从前大家朋友圈都在晒美食,晒旅游,晒玩乐,现在翻来朋友圈一看,竟然有很多人在晒炒股。这是一个好现象,说明人民日益增长的美好生活需要,已经从温饱休息,变成了投资和理财。股票和基金等似乎依然还是大众眼中新鲜和高级的事物,买过股票,涨涨跌跌,也值得网上凡尔赛一番。 在通货膨胀的时代,钱放着就是在贬值。如果你有余钱且有些许碎片化时间的话,投资和理财是很有必要的。股票对于大部分散户来说,无疑是坐等着被割韭菜。所以,比起股票,对于散户,我更建议买一些基金。当然,若是真的钟情于股票,倒是可以花一些无关紧要丢了也罢的小钱玩一玩。 经常会听到别人喜欢给人推荐股票,这种人都是新手。因为真正经历了股海沉浮的人,是不敢给人推荐股票的,这句话懂的人都懂。每一个炒股的人,都应该有自己的选股系统,否则,你凭别人推荐赢得的钱,迟早会凭自己的无知输掉。 我想了想,花了一天的时间,爬了些数据,写了两个基金推荐的程序。一个是网上流行的 4433 法则,另外一个我自己想的,基于最受欢迎股票的持仓稳合度。我不买基金。需要我推荐基金的可以找我。去年来行情那么好,投资回报只做到 30% 的,都算是差的。 不妨和大家分享一下我的选股和选基金的思路。简单来说就是“抄作业”。 作为专业投资机构,基金公司选择股票都有特定的程序,一般基金公司有自己的研究人员,研究人员把自己的研究结果汇总给基金经理,基金公司也会从券商的研究机构那里付费买研究报告,另外,基金公司的研究团队还经常到上市企业实地考察,以便了解第一手资料。基金经理根据汇总过来的资料和自己的经验判断大盘走势、板块趋势及个股存在的机会,然后有的还要经过开会讨论,集思广益。最后才让操盘人员买卖股票。 作为普通人,我们大概率是比不上这些机构的。那么我们应该怎么做?我们可以抄基金公司的作业呀,把别人的成果据为己有,站在巨人的肩膀上看问题,不香么。现在问题来了,根据法律规定基金公司在特定时间,只会公布上一个季度的持仓数据,那么它的作业就是老作业,布置了新作业,却交上一次的作业,肯定是不行的。那有什么办法呢?有。就是多抄几分作业,用频率来替代概率。我的程序可以抄所有基金公司的作业,把它整合成一个作业,虽然不那么完美,但是总归是不错的。怎么样把七千多份作业抄成一份,这就是我的卖点所在。我不懂选股,但是我希望能站在基金这个巨人的肩膀上看问题,总是不会错的。 学数据科学的应该清楚,数据分析的三板斧,其实非常有用的一招就是“count”(数数),小学就会的,最简单的,也是非常有效的。

废话不多说,直接上菜。

第一步:基金数据爬取

打开天天基金网(https://fund.eastmoney.com/),通过浏览器的开发者工具,我们能观察到用户的请求和数据的返回过程。从而利用正则表达式,以及 xpath 等工具,辅以一点 python 爬虫的知识,很容易就能获取到每支基金的增长率和持仓情况。

我所用到的代码如下。

XMtool.py:

代码语言:javascript
复制
# In[]: 
#!/usr/bin/env python
# coding: utf-8
# encoding=utf-8
import pandas as pd
import requests
from lxml import etree
import re
#import collections
import numpy as np


# In[]: 
sample = '150000'#样本数量
sc = '6yzf'#排序键值
st = 'desc'#排序方式
ft = 'gp'#基金类型
dx = '1'#是否可购
season = 1#季度选择


r1r = 1#日增长率
r1z = 1#近1周
r1y = 1#近1月
r3y = 0.3333#近3月
r6y = 0.3333#近6月
r1n = 0.25#近1年
r2n = 0.25#近2年
r3n = 0.25#近3年
rjnl = 0.25#今年来
rcll = 1#成立来


    
sd = '2021-01-07'
ed = '2021-02-07'

# In[] 在参数文书写单元后加上这么一段就可以了
#from PyQt5.QtWidgets import QInputDialog, QLineEdit, QDialog
from PyQt5.QtWidgets import QDialog
import sys
from PyQt5.QtWidgets import QApplication
import dialog
class TestDialog1(QDialog,dialog.Ui_XMtool):  
    def __init__(self,parent=None):  
        super(TestDialog1,self).__init__(parent)  
        self.setupUi(self)

app=QApplication(sys.argv)  
dlg=TestDialog1()  
dlg.show()  
app.exec_()

sample = dlg.sample.text() #样本数量
sc = dlg.sc.currentText() #排序键值
st = dlg.st.currentText() #排序方式
ft = dlg.ft.currentText() #基金类型
dx = dlg.dx.currentText() #是否可购
season = int(dlg.season.currentText()) #季度选择


r1r = float(dlg.r1r.text()) #日增长率
r1z = float(dlg.r1z.text())#近1周
r1y = float(dlg.r1y.text())#近1月
r3y = float(dlg.r3y.text())#近3月
r6y = float(dlg.r6y.text())#近6月
r1n = float(dlg.r1n.text())#近1年
r2n = float(dlg.r2n.text())#近2年
r3n = float(dlg.r3n.text())#近3年
rjnl = float(dlg.rjnl.text())#今年来
rcll = float(dlg.rcll.text())#成立来

# In[]:
header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36',
        'Referer': 'http://fund.eastmoney.com/data/fundranking.html',
        'Cookie':'st_si=74949607860286; st_asi=delete; ASP.NET_SessionId=gekyucnll0wte0wrks2rr23b; _adsame_fullscreen_18503=1; EMFUND1=null; EMFUND2=null; EMFUND3=null; EMFUND4=null; EMFUND5=null; EMFUND6=null; EMFUND7=null; EMFUND8=null; EMFUND0=null; EMFUND9=02-07 16:37:21@#$%u521B%u91D1%u5408%u4FE1%u5DE5%u4E1A%u5468%u671F%u80A1%u7968A@%23%24005968; st_pvi=90009717841707; st_sp=2021-02-07%2012%3A14%3A29; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=21; st_psi=2021020716562364-0-0372414431'
}
url = 'http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft='+ft+'&rs=&gs=0&sc='+sc+'&st='+st+'&sd='+sd+'&ed='+ed+'&qdii=&tabSubtype=,,,,,&pi=1&pn='+sample+'&dx='+dx+'&v=0.2692835962833908'
response = requests.get(url=url, headers=header)
text = response.text
data = text.split('=')[1]#使用等号分开去后面一部分
compile_data = re.findall("{datas:\\[(.*)\\],allRecords", str(data))[0]#取datas和allRecords中的所有内容
strip_data = str(compile_data).strip('[').strip(']')#移除字符串头尾的中括号
replace_quta = strip_data.replace('"', "")#双引号替换为空
quota_arrays = replace_quta.split(",")#使用逗号转为列表
intervals = [[i * 25, (i + 1) * 25] for i in range(15000)]#生成10000个区间,每个区间长度为25
narrays = []
for k in intervals:
    start, end = k[0], k[1]
    line = quota_arrays[start:end]#将条目25个分为一组,表示一只基金
    narrays.append(line)
header = ["基金代码", "基金简称", "基金条码", "日期",
          "单位净值", "累计净值", "日增长率", "近1周", "近1月", "近3月", "近半年", "近1年", "近2年", "近3年",
          "今年来", "成立来", "其他1", "其他2", "其他3", "其他4", "其他5", "其他6", "其他7", "其他8", "其他9"]
df = pd.DataFrame(narrays, columns=header)#生成pd数据结构
df.dropna()
total = df.count()[0]
print("共有{}支基金!".format(total))
df = df.head(total)
df_part = df[["基金代码", "基金简称", "日增长率", "近1周", "近1月", "近3月", "近半年", "近1年", "近2年", "近3年",
          "今年来", "成立来"]]#挑选部分感兴趣的条目
df.to_csv("./基金增长率.csv", encoding="utf_8_sig")

# In[]:
df_picked_part = df_part
rates = [r1r,r1z,r1y,r3y,r6y,r1n,r2n,r3n,rjnl,rcll]
i = -1
for sc in ["日增长率", "近1周", "近1月", "近3月", "近半年", "近1年", "近2年", "近3年",
          "今年来", "成立来"]:
    i = i+1
    #print(sc)
    rate = rates[i]
    rate_num = int(total*rate)
    df_tmp = df_part.sort_values(by=[sc], ascending=False, axis=0)
    df_tmp = df_tmp.head(rate_num)
    df_picked_part = pd.merge(df_picked_part,df_tmp,how='inner')
print(df_picked_part.head(10))
df_picked_part.to_csv("./4433法则结果.csv", encoding="utf_8_sig")

# In[]:
rank_codes = df_part['基金代码'].values.tolist()
#len_codes = len(rank_codes)
stocks_array = []
stock_funds = []
total_part = int(total/100)+1 #每百分之一报一次进度
for index, code in enumerate(rank_codes):
#    if index < 1:
#        print("<" * 30 + "所有基金的股票池前10情况" + ">" * 30)
#    print(code)
    if index%total_part==0:
        print("<" * 30 + "获得基金持仓数据中:"+str(index)+"/"+str(total)+ ">" * 30)
    url = "http://fundf10.eastmoney.com/FundArchivesDatas.aspx?type=jjcc&code={}&topline=10&year=&month=&rt=0.5032668912422176".format(code)
    head = {
    "Cookie": "EMFUND1=null; EMFUND2=null; EMFUND3=null; EMFUND4=null; EMFUND5=null; EMFUND6=null; EMFUND7=null; EMFUND8=null; EMFUND0=null; st_si=44023331838789; st_asi=delete; EMFUND9=08-16 22:04:25@#$%u4E07%u5BB6%u65B0%u5229%u7075%u6D3B%u914D%u7F6E%u6DF7%u5408@%23%24519191; ASP.NET_SessionId=45qdofapdlm1hlgxapxuxhe1; st_pvi=87492384111747; st_sp=2020-08-16%2000%3A05%3A17; st_inirUrl=http%3A%2F%2Ffund.eastmoney.com%2Fdata%2Ffundranking.html; st_sn=12; st_psi=2020081622103685-0-6169905557"
    ,
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"}
    response = requests.get(url, headers=head)
    text = response.text
    div = re.findall('content:\\"(.*)\\",arryear', text)[0]
    html_body = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>test</title></head><body>%s</body></html>' % (
        div)#构造网页
    html = etree.HTML(html_body)#将传进去的字符串转变成_Element对象
    stock_info = html.xpath('//div[{}]/div/table/tbody/tr/td/a'.format(season))
    # for ii in stock_info:
    #     print(ii.text)
    #print(season)
    stock_money = html.xpath('//div[{}]/div/table/tbody/tr/td[@class="tor"]'.format(season))
    if stock_money == []:
        stock_money = html.xpath('//div[{}]/div/table/tbody/tr/td[@class="toc"]'.format(season))
    stock_attr = []
    # for i in range(0,len(stock_money)):
    
    stock_money_text = []
    for ii in stock_money:
        ii_text = ii.text
 #       print(ii_text)
        if ii_text!=None:
            ii.text = ii.text.replace('---','0')
            stock_money_text.append(float(ii.text.replace(',','').replace('%','')))
    
#        print(ii.text)
   # stock_money_text.dropna()
    stock_one_fund = []
    if len(stock_info)!=0 and len(stock_money_text)!=0:
        count = -1
        for i in range(0,len(stock_info)):
            stock = stock_info[i]
            if stock.text==None:
                stock.text = '缺失'
            tmp0 = stock.text.split('.')
            tmp = tmp0[0]          
            if stock.text and (tmp.isdigit() or (tmp.isupper() and tmp.isalnum() and len(tmp0)>1)):
    #        if stock.text and stock.text.isdigit():
               # list_tmp = [stock.text,stock_info[i+1].text]
                count = count+1
                stock_one_fund.append([stock_info[i+1].text,
                                       stock_money_text[3*count+0],
                                       stock_money_text[3*count+1],
                                       stock_money_text[3*count+2]])
    #            print(stock_info[i+1].text)
#    if len(stock_one_fund)>1:
  #      print("基金代码:{}".format(code), "基金持有前10股票池", stock_one_fund)
    stock_funds.append([code,stock_one_fund])
        # print(code)
        # print(stock_one_fund)
#    else:
#        print('发现无持仓基金!')
#   if len(stock_one_fund) > 1 and stock_one_fund:
    stocks_array.extend(stock_one_fund)
print("<" * 30 + "获得基金持仓数据中:done!!!"+ ">" * 30)
# print("test")
tmp = pd.DataFrame(stock_funds,columns=['基金代码','十大重仓'])
df_funds_info_extend = pd.merge(df_part,tmp,how='inner',on='基金代码')
df_funds_info_extend.set_index('基金代码')
df_funds_info_extend.to_csv("./基金持仓.csv", encoding="utf_8_sig")


# In[]:
stock_info_list = []
for row in df_funds_info_extend.iterrows():
    tenpos = row[1]['十大重仓']
    fund_jc = row[1]['基金简称']
    if len(tenpos)!=0:
        tmp = [tenpos[0][0],fund_jc,tenpos[0][1],tenpos[0][2],tenpos[0][3]]
        stock_info_list.append(tmp)
df_stock_info = pd.DataFrame(stock_info_list,columns=['股票简称','所属基金','占净值比例','持股数_万','持仓市值_万'])
df_stock_info.to_csv("./股票被持有信息.csv", encoding="utf_8_sig")

# In[]
#df_stock_info.loc[:,['股票简称','持股数_万','持仓市值_万','占净值比例']]
df_stock_info_cp = df_stock_info
df_stock_info_cp['所属基金cp'] = df_stock_info['所属基金']
df_stock_info_gb = df_stock_info_cp.groupby('股票简称')
#df_stock_info.drop(axis=1,['所属基金'])
# for n in df_stock_info_gb:
#     print(n)
#     print('\n')
#stock_agg_result = df_stock_info_gb.agg({'持股数_万':np.sum,'持仓市值_万':np.sum,'占净值比例':np.mean})
stock_agg_result = df_stock_info_gb.agg({'持股数_万':np.sum,'持仓市值_万':np.sum,'占净值比例':np.mean,'所属基金':len,'所属基金cp':list})
stock_agg_result.columns = ['被持股数_万','被持仓市值_万','平均占比','所属基金数目','所属基金集合']
stock_agg_result.to_csv("./股票被持有信息统计.csv", encoding="utf_8_sig")
# df_stock_info_gb.to_csv("./测试.csv", encoding="utf_8_sig")

# In[]
rank = 10
stock_agg_result = stock_agg_result.sort_values(by="所属基金数目",ascending=False)
stock_agg_result_head0 = stock_agg_result.head(rank)
stock_agg_result = stock_agg_result.sort_values(by="被持仓市值_万",ascending=False)
stock_agg_result_head1 = stock_agg_result.head(rank)
stock_agg_result = stock_agg_result.sort_values(by="平均占比",ascending=False)
stock_agg_result_head2 = stock_agg_result.head(rank)
funds_stocks_count = []
for st_funds_ in stock_funds:
    #st_funds_ = stock_funds[0]
    st_funds = st_funds_[1]
    tmp = [i[0] for i in st_funds]
    df_stock_funds = pd.DataFrame(tmp,columns=['股票简称'])
#    print(df_stock_funds)
    count0 = pd.merge(stock_agg_result_head0,df_stock_funds,how='inner',on='股票简称').iloc[:,0].size
    count1 = pd.merge(stock_agg_result_head1,df_stock_funds,how='inner',on='股票简称').iloc[:,0].size
    count2 = pd.merge(stock_agg_result_head2,df_stock_funds,how='inner',on='股票简称').iloc[:,0].size
    jc_tmp = df_part[df_part['基金代码']==st_funds_[0]].iloc[0,1]
    funds_stocks_count.append([jc_tmp,count0,count1,count2])
df_funds_stock_count = pd.DataFrame(funds_stocks_count,columns = ['基金简称','优仓数目_所属基金数','优仓数目_被持仓市值','平均占比'])
df_funds_stock_count = df_funds_stock_count.sort_values(by=["优仓数目_所属基金数"], ascending=False, axis=0)
df_funds_stock_count = pd.merge(df_funds_stock_count,df_part,how='inner',on='基金简称')
df_funds_stock_count.to_csv("./基金持受欢迎股数目统计.csv", encoding="utf_8_sig")

dialog.py

代码语言:javascript
复制
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_XMtool(object):
    def setupUi(self, XMtool):
        XMtool.setObjectName("XMtool")
        XMtool.resize(701, 622)
        self.verticalLayout = QtWidgets.QVBoxLayout(XMtool)
        self.verticalLayout.setObjectName("verticalLayout")
        self.formGroupBox_2 = QtWidgets.QGroupBox(XMtool)
        self.formGroupBox_2.setObjectName("formGroupBox_2")
        self.gridLayout = QtWidgets.QGridLayout(self.formGroupBox_2)
        self.gridLayout.setObjectName("gridLayout")
        self.label_18 = QtWidgets.QLabel(self.formGroupBox_2)
        self.label_18.setObjectName("label_18")
        self.gridLayout.addWidget(self.label_18, 0, 0, 1, 1)
        self.pushButton = QtWidgets.QPushButton(self.formGroupBox_2)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 2)
        self.frame = QtWidgets.QFrame(self.formGroupBox_2)
        self.frame.setObjectName("frame")
        self.formLayout_4 = QtWidgets.QFormLayout(self.frame)
        self.formLayout_4.setObjectName("formLayout_4")
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setObjectName("label")
        self.formLayout_4.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
        self.r1r = QtWidgets.QLineEdit(self.frame)
        self.r1r.setObjectName("r1r")
        self.formLayout_4.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.r1r)
        self.label_2 = QtWidgets.QLabel(self.frame)
        self.label_2.setObjectName("label_2")
        self.formLayout_4.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
        self.r1z = QtWidgets.QLineEdit(self.frame)
        self.r1z.setObjectName("r1z")
        self.formLayout_4.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.r1z)
        self.label_3 = QtWidgets.QLabel(self.frame)
        self.label_3.setObjectName("label_3")
        self.formLayout_4.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
        self.r1y = QtWidgets.QLineEdit(self.frame)
        self.r1y.setObjectName("r1y")
        self.formLayout_4.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.r1y)
        self.label_4 = QtWidgets.QLabel(self.frame)
        self.label_4.setObjectName("label_4")
        self.formLayout_4.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_4)
        self.r3y = QtWidgets.QLineEdit(self.frame)
        self.r3y.setObjectName("r3y")
        self.formLayout_4.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.r3y)
        self.label_5 = QtWidgets.QLabel(self.frame)
        self.label_5.setObjectName("label_5")
        self.formLayout_4.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_5)
        self.r6y = QtWidgets.QLineEdit(self.frame)
        self.r6y.setObjectName("r6y")
        self.formLayout_4.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.r6y)
        self.label_6 = QtWidgets.QLabel(self.frame)
        self.label_6.setObjectName("label_6")
        self.formLayout_4.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.label_6)
        self.r1n = QtWidgets.QLineEdit(self.frame)
        self.r1n.setObjectName("r1n")
        self.formLayout_4.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.r1n)
        self.label_7 = QtWidgets.QLabel(self.frame)
        self.label_7.setObjectName("label_7")
        self.formLayout_4.setWidget(6, QtWidgets.QFormLayout.LabelRole, self.label_7)
        self.r2n = QtWidgets.QLineEdit(self.frame)
        self.r2n.setObjectName("r2n")
        self.formLayout_4.setWidget(6, QtWidgets.QFormLayout.FieldRole, self.r2n)
        self.label_8 = QtWidgets.QLabel(self.frame)
        self.label_8.setObjectName("label_8")
        self.formLayout_4.setWidget(7, QtWidgets.QFormLayout.LabelRole, self.label_8)
        self.r3n = QtWidgets.QLineEdit(self.frame)
        self.r3n.setObjectName("r3n")
        self.formLayout_4.setWidget(7, QtWidgets.QFormLayout.FieldRole, self.r3n)
        self.label_9 = QtWidgets.QLabel(self.frame)
        self.label_9.setObjectName("label_9")
        self.formLayout_4.setWidget(8, QtWidgets.QFormLayout.LabelRole, self.label_9)
        self.rjnl = QtWidgets.QLineEdit(self.frame)
        self.rjnl.setObjectName("rjnl")
        self.formLayout_4.setWidget(8, QtWidgets.QFormLayout.FieldRole, self.rjnl)
        self.label_10 = QtWidgets.QLabel(self.frame)
        self.label_10.setObjectName("label_10")
        self.formLayout_4.setWidget(9, QtWidgets.QFormLayout.LabelRole, self.label_10)
        self.rcll = QtWidgets.QLineEdit(self.frame)
        self.rcll.setObjectName("rcll")
        self.formLayout_4.setWidget(9, QtWidgets.QFormLayout.FieldRole, self.rcll)
        self.gridLayout.addWidget(self.frame, 1, 1, 1, 1)
        self.frame_2 = QtWidgets.QFrame(self.formGroupBox_2)
        self.frame_2.setObjectName("frame_2")
        self.label_11 = QtWidgets.QLabel(self.frame_2)
        self.label_11.setGeometry(QtCore.QRect(18, 18, 96, 24))
        self.label_11.setObjectName("label_11")
        self.label_12 = QtWidgets.QLabel(self.frame_2)
        self.label_12.setGeometry(QtCore.QRect(18, 61, 96, 24))
        self.label_12.setObjectName("label_12")
        self.sc = QtWidgets.QComboBox(self.frame_2)
        self.sc.setGeometry(QtCore.QRect(126, 61, 91, 30))
        self.sc.setObjectName("sc")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.addItem("")
        self.sc.setItemText(15, "")
        self.label_13 = QtWidgets.QLabel(self.frame_2)
        self.label_13.setGeometry(QtCore.QRect(18, 103, 96, 24))
        self.label_13.setObjectName("label_13")
        self.st = QtWidgets.QComboBox(self.frame_2)
        self.st.setGeometry(QtCore.QRect(126, 103, 90, 30))
        self.st.setObjectName("st")
        self.st.addItem("")
        self.st.addItem("")
        self.label_14 = QtWidgets.QLabel(self.frame_2)
        self.label_14.setGeometry(QtCore.QRect(18, 145, 96, 24))
        self.label_14.setObjectName("label_14")
        self.ft = QtWidgets.QComboBox(self.frame_2)
        self.ft.setGeometry(QtCore.QRect(126, 145, 90, 30))
        self.ft.setObjectName("ft")
        self.ft.addItem("")
        self.ft.addItem("")
        self.ft.addItem("")
        self.ft.addItem("")
        self.ft.addItem("")
        self.ft.addItem("")
        self.ft.addItem("")
        self.ft.addItem("")
        self.label_15 = QtWidgets.QLabel(self.frame_2)
        self.label_15.setGeometry(QtCore.QRect(18, 187, 96, 24))
        self.label_15.setObjectName("label_15")
        self.dx = QtWidgets.QComboBox(self.frame_2)
        self.dx.setGeometry(QtCore.QRect(126, 187, 55, 30))
        self.dx.setObjectName("dx")
        self.dx.addItem("")
        self.dx.addItem("")
        self.label_16 = QtWidgets.QLabel(self.frame_2)
        self.label_16.setGeometry(QtCore.QRect(18, 229, 96, 24))
        self.label_16.setObjectName("label_16")
        self.season = QtWidgets.QComboBox(self.frame_2)
        self.season.setGeometry(QtCore.QRect(126, 229, 56, 30))
        self.season.setObjectName("season")
        self.season.addItem("")
        self.season.addItem("")
        self.season.addItem("")
        self.season.addItem("")
        self.sample = QtWidgets.QLineEdit(self.frame_2)
        self.sample.setGeometry(QtCore.QRect(126, 18, 151, 30))
        self.sample.setObjectName("sample")
        self.gridLayout.addWidget(self.frame_2, 1, 0, 1, 1)
        self.label_17 = QtWidgets.QLabel(self.formGroupBox_2)
        self.label_17.setObjectName("label_17")
        self.gridLayout.addWidget(self.label_17, 0, 1, 1, 1)
        self.frame.raise_()
        self.frame_2.raise_()
        self.label_18.raise_()
        self.label_17.raise_()
        self.pushButton.raise_()
        self.verticalLayout.addWidget(self.formGroupBox_2)

        self.retranslateUi(XMtool)
        self.pushButton.clicked.connect(XMtool.accept)
        QtCore.QMetaObject.connectSlotsByName(XMtool)

    def retranslateUi(self, XMtool):
        _translate = QtCore.QCoreApplication.translate
        XMtool.setWindowTitle(_translate("XMtool", "股林秘籍小明三式"))
        XMtool.setToolTip(_translate("XMtool", "<html><head/><body><p>欢迎使用小明选股软件!</p></body></html>"))
        XMtool.setWhatsThis(_translate("XMtool", "<html><head/><body><p>不认识爸爸?</p></body></html>"))
        self.label_18.setText(_translate("XMtool", "基金数据参数设定:"))
        self.pushButton.setText(_translate("XMtool", "小明已确定"))
        self.label.setText(_translate("XMtool", "日增长率"))
        self.r1r.setText(_translate("XMtool", "1"))
        self.label_2.setText(_translate("XMtool", "近1周"))
        self.r1z.setText(_translate("XMtool", "1"))
        self.label_3.setText(_translate("XMtool", "近1月"))
        self.r1y.setText(_translate("XMtool", "1"))
        self.label_4.setText(_translate("XMtool", "近3月"))
        self.r3y.setText(_translate("XMtool", "0.33333"))
        self.label_5.setText(_translate("XMtool", "近6月"))
        self.r6y.setText(_translate("XMtool", "0.33333"))
        self.label_6.setText(_translate("XMtool", "近1年"))
        self.r1n.setText(_translate("XMtool", "0.25"))
        self.label_7.setText(_translate("XMtool", "近2年"))
        self.r2n.setText(_translate("XMtool", "0.25"))
        self.label_8.setText(_translate("XMtool", "近3年"))
        self.r3n.setText(_translate("XMtool", "0.25"))
        self.label_9.setText(_translate("XMtool", "今年来"))
        self.rjnl.setText(_translate("XMtool", "0.25"))
        self.label_10.setText(_translate("XMtool", "成立来"))
        self.rcll.setText(_translate("XMtool", "1"))
        self.label_11.setText(_translate("XMtool", "样本数量"))
        self.label_12.setText(_translate("XMtool", "排序键值"))
        self.sc.setItemText(0, _translate("XMtool", "6yzf"))
        self.sc.setItemText(1, _translate("XMtool", "dm"))
        self.sc.setItemText(2, _translate("XMtool", "jc"))
        self.sc.setItemText(3, _translate("XMtool", "jzrq"))
        self.sc.setItemText(4, _translate("XMtool", "dwjz"))
        self.sc.setItemText(5, _translate("XMtool", "ljjz"))
        self.sc.setItemText(6, _translate("XMtool", "rzdf"))
        self.sc.setItemText(7, _translate("XMtool", "zzf"))
        self.sc.setItemText(8, _translate("XMtool", "1yzf"))
        self.sc.setItemText(9, _translate("XMtool", "3yzf"))
        self.sc.setItemText(10, _translate("XMtool", "1nzf"))
        self.sc.setItemText(11, _translate("XMtool", "2nzf"))
        self.sc.setItemText(12, _translate("XMtool", "3nzf"))
        self.sc.setItemText(13, _translate("XMtool", "jnzf"))
        self.sc.setItemText(14, _translate("XMtool", "lnzf"))
        self.label_13.setText(_translate("XMtool", "排序方式"))
        self.st.setItemText(0, _translate("XMtool", "desc"))
        self.st.setItemText(1, _translate("XMtool", "asc"))
        self.label_14.setText(_translate("XMtool", "基金类型"))
        self.ft.setItemText(0, _translate("XMtool", "all"))
        self.ft.setItemText(1, _translate("XMtool", "gp"))
        self.ft.setItemText(2, _translate("XMtool", "hh"))
        self.ft.setItemText(3, _translate("XMtool", "zs"))
        self.ft.setItemText(4, _translate("XMtool", "qdii"))
        self.ft.setItemText(5, _translate("XMtool", "zq"))
        self.ft.setItemText(6, _translate("XMtool", "lof"))
        self.ft.setItemText(7, _translate("XMtool", "fof"))
        self.label_15.setText(_translate("XMtool", "是否可购"))
        self.dx.setItemText(0, _translate("XMtool", "1"))
        self.dx.setItemText(1, _translate("XMtool", "0"))
        self.label_16.setText(_translate("XMtool", "季度选择"))
        self.season.setItemText(0, _translate("XMtool", "1"))
        self.season.setItemText(1, _translate("XMtool", "2"))
        self.season.setItemText(2, _translate("XMtool", "3"))
        self.season.setItemText(3, _translate("XMtool", "4"))
        self.sample.setText(_translate("XMtool", "15000"))
        self.label_17.setText(_translate("XMtool", "四四三三法则参数:"))

运行之后,需要填一些参数,如下。

确定之后,除了爬下来了我们后面所要用到的全部数据之外,我们还利用 4433 法则,对于基金进行了一个初步的分析和筛选。

第二步:股票增持计算

有了上面爬下来的原始数据之后,我们就可以统计:单股票被基金公司持有的数量、单股票被基金公司持有的市值和持有单股票基金公司的数目。对于不同的相邻季度,我们可以计算这三个量的增长,又得到三个新的指标。对于不同的指标进行降排序,我们可以得到股票在基金公司中的受欢迎程度,以此得到股票好坏度,指标值作为权重。不同的指标得到的不同的股票排序还可以拿前几取交集。从而我们就得到了基金公司们期待值比较高的股票。

我所用的代码如下:

代码语言:javascript
复制
#!/usr/bin/env python
# coding: utf-8
# In[]:
import pandas as pd
#import os
import tkinter as tk
from tkinter import filedialog
def getLocalFile():
    root=tk.Tk()
    root.withdraw()
    filePath=filedialog.askopenfilename()
    print('文件路径:',filePath)
    return filePath
#if __name__ == '__main__':
# In[]: 
file1 = getLocalFile()
file2 = getLocalFile()
#sheet1 = pd.read_csv('./all_1/股票被持有信息统计.csv')
sheet1 = pd.read_csv(file1)
sheet1
#sheet2 = pd.read_csv('./all_2/股票被持有信息统计.csv')
sheet2 = pd.read_csv(file2)
sheet3 = pd.merge(sheet1,sheet2,how='inner',on='股票简称')
sheet3
sheet3['增持股数'] = sheet3['被持股数_万_x'] - sheet3['被持股数_万_y']


# In[]:
sheet3['增持市值'] = sheet3['被持仓市值_万_x'] - sheet3['被持仓市值_万_y']
#sheet3['增持占比'] = sheet3['平均占比_x'] - sheet3['平均占比_y']
sheet3['增持基金数量'] = sheet3['所属基金数目_x'] - sheet3['所属基金数目_y']
sheet3.to_csv('增持情况统计.csv',encoding="utf_8_sig")

第三步:好股基金选取

第二步中,我们其实已经得到了被基金公司看重的股票,如果炒股,直接取其前几,按其权重进行金额配置即可。现在问题是,国内股票交易,一手起步,选出来的股票很贵,比如说茅台,你不一定买得起。这时候,我们还是寄希望于买基金。我们希望选出的基金的持仓和我们选出的好股票集合的“相似度”尽可能高。相似度的衡量又很多方法。比如说:基金持有的十大重仓含有好股的数目、基金持有的十大重仓含有好股的市值、基金持有的十大重仓含有好股的占比、基金持有的十大重仓含有好股的加权占比(加权基于增持市值或增持基金数量)等等。

下面是我所用到的代码,细节可看。

代码语言:javascript
复制
#!/usr/bin/env python
# coding: utf-8

# In[]:
import pandas as pd
#import os


# In[]:
import tkinter as tk
from tkinter import filedialog
def getLocalFile():
    root=tk.Tk()
    root.withdraw()
    filePath=filedialog.askopenfilename()
    print('文件路径:',filePath)
    return filePath
#if __name__ == '__main__':


# In[]
print('请输入增持情况统计:')
increase_hold_add  = getLocalFile()
inc = pd.read_csv(increase_hold_add,index_col = 0)
inc
# In[]:
#print('请输入比率:')
str_num = input("Enter your number: ")
rate = int(str_num)
rate


# In[]:


inc_sort_zcgs = inc.sort_values(by=["增持股数"], ascending=False, axis=0)
inc_sort_zcsz = inc.sort_values(by=["增持市值"], ascending=False, axis=0)
inc_sort_zcjjsl = inc.sort_values(by=["增持基金数量"], ascending=False, axis=0)
inc_sort_zcgs
inc_sort_zcgs = inc_sort_zcgs.head(rate)
inc_sort_zcsz = inc_sort_zcsz.head(rate)
inc_sort_zcjjsl = inc_sort_zcjjsl.head(rate)
inc_merge = pd.merge(inc_sort_zcsz,inc_sort_zcjjsl,how='inner',on='股票简称')
inc_merge
intersec = inc_merge['股票简称']
intersec
print('选出来的前{}股票交集为:'.format(rate))
print(intersec)
print('共{}只!'.format(len(intersec)))


# In[]:


print('请选择基金持仓:')
funds_hold_add = getLocalFile()
funds_hold = pd.read_csv(funds_hold_add,index_col = 0)
stock_funds = funds_hold
stock_funds
intersec
intersec_ex = pd.merge(intersec,inc,how='inner',on='股票简称')
intersec_ex['权重_增持市值'] = intersec_ex['增持市值']/intersec_ex['增持市值'].sum()
intersec_ex['权重_增持基金数量'] = intersec_ex['增持基金数量']/intersec_ex['增持基金数量'].sum()
intersec_ex.to_csv("./好股.csv", encoding="utf_8_sig")


# In[ ]:

result = []# pd.DataFrame()
for row in stock_funds.iterrows():
    tenpos = row[1]['十大重仓']
    exec('tps='+tenpos)
    fund_jc = row[1]['基金简称']
    #tmp = [i[0] for i in tps]   
    #rate = [r[1] for r in tps]
    list_tmp = [[i[0],i[1]] for i in tps]
    df_stock_rate = pd.DataFrame(list_tmp,columns=['股票简称','股票占比'])
 #   good_stock_rate 
 #   df_stock_funds = pd.DataFrame(tmp,columns=['股票简称'])
#    print(df_stock_funds)
    good_stock = pd.merge(intersec_ex,df_stock_rate,how='inner',on='股票简称')#.iloc[:,0]
    count = good_stock['股票简称'].size
    rate_vector = good_stock['股票占比']
    total_rate = rate_vector.sum()
    tmp = (rate_vector.mul(good_stock['权重_增持市值']))#.sum
    tmp = tmp.sum()
    total_rate_weighted_zcsz = tmp
    tmp = (rate_vector.mul(good_stock['权重_增持基金数量']))#.sum
    tmp = tmp.sum()
    total_rate_weighted_zcjjsl = tmp
    #good_stock_jc = good_stock['基金简称']   
    result.append([fund_jc,count,total_rate,total_rate_weighted_zcsz,total_rate_weighted_zcjjsl])

pd_result = pd.DataFrame(result,columns = ['基金简称','好股数目','好股占比','加权好股占比_增持市值','加权好股占比_增持基金数量'])
pd_result = pd_result.sort_values(by='加权好股占比_增持市值',ascending=False)



pd_result = pd.merge(pd_result,stock_funds,how='inner',on='基金简称')


# In[]:


pd_result.to_csv("./基金持好股情况统计.csv", encoding="utf_8_sig")
print('完成!按任意键退出!')
stop = input() 

使用以往的数据做个测试验证,如下:

从上图可看出,用我的基金选择策略,选出来的基金一个月涨跌为 15 个点,两个指数基金翻车,只有百分之五,勉强跑得赢上证。

“风险越高,收益越高”总是不变的铁律,从这个角度来看,似乎就不必纠结于哪种方案或者策略收益是最高的,差不多就行了。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python 知识大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第一步:基金数据爬取
  • 第二步:股票增持计算
  • 第三步:好股基金选取
相关产品与服务
云开发 CLI 工具
云开发 CLI 工具(Cloudbase CLI Devtools,CCLID)是云开发官方指定的 CLI 工具,可以帮助开发者快速构建 Serverless 应用。CLI 工具提供能力包括文件储存的管理、云函数的部署、模板项目的创建、HTTP Service、静态网站托管等,您可以专注于编码,无需在平台中切换各类配置。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档