自己做量化交易软件(31)小白量化实战5–板块股票池与基本面选股
在小白量化软件中,股票池与板块是一样的,都是股票代码列表。前面博客有零散知识的介绍。
小白量化学习(4)基本面选股与股票池
https://blog.csdn.net/hepu8/article/details/93378707
自己做量化交易软件(17)通过基本面建立自己的股票池
https://blog.csdn.net/hepu8/article/details/88048476
自己做量化交易软件(21)通达信自选股文件的读写程序
https://blog.csdn.net/hepu8/article/details/106425443
如果我们采用通达信软件,经常也会使用通达信股票代码格式和通达信列表格式。我们可以打开自己的通达信软件,看看有什么板块,例如对“特斯拉”板块感兴趣,看看包含什么股票代码。
这些是小白量化的基本功能,获取股票所属板块,获取板块的所有股票代码,板块股票代码合并,股票代码去重,转化为通达信板块数据格式,保存到通达信自选股2板块文件中。我下面直接给出源代码,代码有详细说明,不懂在读者群中问。
#小白量化:板块与自选股 股票池
#购买<零基础搭建量化投资系统>正版书,送小白量化软件源代码。
# https://item.jd.com/61567375505.html
#独狼荷蒲qq:2775205
#通通python量化群:524949939
#电话微信:18578755056
#微信公众号:独狼股票分析
#日期:2021-01-09
import HP_tdx as htdx #小白量化通达信行情模块
#连接通达信行情服务器
hq=htdx.TdxInit(ip='183.57.72.22',port=7709) ##初始化通达信
#获取股票所属板块
code='000776'
bk=htdx.getblock3(code)
print(code,' 所属板块 :',bk)
code='600655'
bk=htdx.getblock3(code)
print(code,' 所属板块 :',bk)
#获取板块的所有股票代码
bkn='特斯拉'
codes=htdx.getblock2(bkn) #返回板块中的股票
print(bkn,' 板块的所有股票 :',codes)
#板块股票合并
codes1=htdx.getblock2('近期强势') #返回板块中的股票
codes2=htdx.getblock2('深证300') #返回板块中的股票
codes3=htdx.getblock2('深证成指') #返回板块中的股票
codes4=htdx.getblock2('上证380') #返回板块中的股票
codes=codes1+codes2+codes3+codes4
print(len(codes),codes)
#代码去重
codes=list(set(codes)) #利用集合去掉重复代码
print(len(codes),codes)
#保存为通达信自选股,需要重新启动通达信软件,才能看到
#通达信软件的自选股文件的目录为:C:\tdx目录\T0002\blocknew
path='C:\\zd_gfzq\\T0002\\blocknew\\' #通道信软件自选股路径
#转化为通达信板块数据格式
bk2=htdx.tdxcodes(codes)
print(bk2)
#保存到通达信自选股板块文件中
htdx.putzxgfile(bk2,path+'ZXG2.blk')
#断开连接行情服务器
htdx.disconnect()
上面程序输出很多信息。
000776 所属板块 : ['泛珠三角', '深证300', '大盘股', '深证价值', '参股新股', 'MSCI成份', '保险重仓', '融资融券', '含H股', '沪深300', '国证价值', '国证红利', '证金持股', '深证成指', '深证红利', '深证100', '300周期', '罗素大盘', '大盘价值', '中证100']
600655 所属板块 : ['MSCI中盘', '黄金概念', '昨日连板', '融资增加', '300非周', '融资融券', '昨日涨停', '沪深300', '中证200', '近期新高', '国证价值', '长三角', '要约收购', '银河99', '员工持股', '最近异动', '最近多板', '罗素大盘', '中盘价值', '昨日上榜', '上证380', '近期强势', '上海自贸', '中证龙头']
特斯拉 板块的所有股票 : ['000009', '000012', '000030', '000049', '000400', '000551', '000559', '000636', '000700', '000800', '000821', '000839', '000887', '000970', '000973', '000988', '002009', '002028', '002036', '002045', '002048', '002050', '002055', '002056', '002085', '002091', '002101', '002121', '002126', '002130', '002156', '002160', '002179', '002182', '002196', '002203', '002206', '002239', '002245', '002276', '002328', '002334', '002340', '002363', '002382', '002384', '002389', '002403', '002405', '002407', '002441', '002459', '002460', '002466', '002472', '002484', '002497', '002501', '002510', '002516', '002533', '002536', '002540', '002547', '002579', '002590', '002594', '002595', '002623', '002635', '002662', '002664', '002666', '002709', '002738', '002812', '002815', '002823', '002838', '002850', '002965', '002975', '300014', '300035', '300037', '300041', '300068', '300073', '300088', '300100', '300115', '300127', '300134', '300160', '300169', '300176', '300184', '300222', '300230', '300256', '300258', '300276', '300278', '300279', '300293', '300310', '300320', '300328', '300337', '300351', '300376', '300382', '300400', '300420', '300428', '300433', '300445', '300450', '300476', '300580', '300619', '300626', '300643', '300644', '300707', '300709', '300718', '300748', '300750', '300806', '300817', '300863', '300890', '300919', '600019', '600110', '600114', '600143', '600215', '600330', '600335', '600366', '600390', '600418', '600480', '600525', '600577', '600580', '600611', '600660', '600683', '600699', '600741', '600848', '600869', '600884', '600885', '601137', '601689', '601702', '603009', '603011', '603026', '603035', '603050', '603066', '603158', '603159', '603179', '603197', '603228', '603266', '603305', '603348', '603358', '603659', '603667', '603730', '603799', '603920', '603960', '603997', '688022', '688157', '688529', '688559']
804 ['000155', '000538', '000570', '000661', '000708', '000718', '000733', '000767', '000799', '000927', '002041', '002064', '002074', '002080', '002164', '002202', '002324', '002340', '002342', '002414', '002460', '002466', '002497', '002594', '002617', '002625', '002709', '002738', '002759', '300001', '300014', '300082', '300087', '300115', '300274', '300568', '300618', '300677', '300696', '300699', '300712', '300750', '300999', '600031', '600038', '600059', '600110', '600176', '600189', '600219', '600309', '600346', '600409', '600460', '600499', '600586', '600655', '600660', '600685', '600711', '600760', '600809', '600862', '600882', '600893', '601012', '601016', '601058', '601633', '601899', '601908', '601919', '603063', '603087', '603185', '603259', '603267', '603392', '603486', '603613', '603799', '603882', '603993', '688388', '688390', '000001', '000002', '000009', '000021', '000027', '000028', '000031', '000046', '000050', '000063',。。。
我们前面也零散介绍了有关通达信行情相关的知识。
自己做量化交易软件(20)通达信公式选股程序的实现
https://blog.csdn.net/hepu8/article/details/105451351
自己做量化交易软件(24)小白量化增强仿通达信财务基本数据公式
https://blog.csdn.net/hepu8/article/details/106972813
因为基本面被大家认可,大家都来买买买,所以股票价格涨了。
假设基本面选股条件如下,我们如何实现选股。
1、 5日成交量合计换手率大于15%。
2、 市盈率大于0且小于50。
写成通达信选股公式如下:
A1:=SUM(VOL,5);
A2:=IF(A1/CAPITAL*100>15,1,0);
A3:=IF(CLOSE/FINANCE(33)>1,1,0)*IF(CLOSE/FINANCE(33)<50,1,0);
AX:A2+A3>=2;
下面看,用Python如何实现选股。 我们仍然用仿通达信公式格式。
#小白量化选股 :基本面指标选股
#购买<零基础搭建量化投资系统>正版书,送小白量化软件源代码。
# https://item.jd.com/61567375505.html
#独狼荷蒲qq:2775205
#通通python量化群:524949939
#电话微信:18578755056
#微信公众号:独狼股票分析
#日期:2021-01-09
import time
import HP_tdx as htdx
import HP_tdxgs as hgs
from HP_formula import *
global CLOSE,LOW,HIGH,OPEN,VOL
global C,L,H,O,V
'''
财务函数
FINANCE(1) 总股本(万股)
FINANCE(2) 市场类型
FINANCE(3) 沪深品种类型
FINANCE(4) 沪深品种通达信二级行业代码
FINANCE(5) B股(万股)
FINANCE(6) H股(万股)
FINANCE(7) 流通股本(万股)
CAPITAL 流通股本(手)
FINANCE(8) 股东人数(户)(上市公司的最新数据)
FINANCE(9) 资产负债率
FINANCE(10) 总资产
FINANCE(11) 流动资产
FINANCE(12) 固定资产
FINANCE(13) 无形资产
FINANCE(14) 长期投资
FINANCE(15) 流动负债
FINANCE(16) 长期负债
FINANCE(17) 资本公积金金
FINANCE(18) 每股公积金金
FINANCE(19) 股东权益
FINANCE(20) 主营收入
FINANCE(21) 主营利利益
FINANCE(22) 其它利利益
FINANCE(23) 营业利利益
FINANCE(24) 投资收益
FINANCE(25) 补贴收入
FINANCE(26) 营业外收支
FINANCE(27) 上年年损益调整
FINANCE(28) 利利益总额
FINANCE(29) 税后利利益
FINANCE(30) 净利利益
FINANCE(31) 未分配利利益
FINANCE(32) 每股未分配利利润
FINANCE(33) 每股收益
FINANCE(34) 每股净资产
FINANCE(35) 调整每股净资产
FINANCE(36) 股东权益比
FINANCE(40) 流通市值
FINANCE(41) 总市值
FINANCE(42) 上市日期
'''
def FINANCE(x):
return hgs.FINANCE(x)
#CAPITAL 流通股本(手)
def CAPITAL():
return hgs.Capital
hq=htdx.TdxInit(ip='183.57.72.22',port=7709) ##初始化通达信
bkn='沪深300'
codes=htdx.getblock2(bkn) #返回板块中的股票
print('小白量化选股: 基本面指标选股')
print('输出股票池或板块中股票')
print(bkn,len(codes),' 板块中股票 :',codes)
#仿通达信选股函数模板
def function(cd):
global CLOSE,LOW,HIGH,OPEN,VOL
global C,L,H,O,V
#获取通达信财物数据
#nMarket = get_market(cd)
hgs.readbase(htdx.get_market(cd),cd)
df3=htdx.get_security_bars(nCategory=4,nMarket = htdx.get_market(cd),code=cd,\
nStart=0, nCount=10) #获取指定范围的证券K线
##数据规格化
df3.dropna(inplace=True)
#小白数据规格化
mydf=df3.copy()
CLOSE=mydf['close']
LOW=mydf['low']
HIGH=mydf['high']
OPEN=mydf['open']
VOL=mydf['volume']/100 #通达信软件是手
C=mydf['close']
L=mydf['low']
H=mydf['high']
O=mydf['open']
V=mydf['volume']/100 #通达信软件是手
#仿通达信,大智慧公式计算和选股
A1=SUM(VOL,5);
A2=IF(A1/CAPITAL()*100>15,1,0);
A3=IF(CLOSE/FINANCE(33)>1,1,0)*IF(CLOSE/FINANCE(33)<50,1,0);
AX=IF(A2+A3>=2,1,0);
#转化为买点信号
B2=list(AX)
if B2[len(B2)-1]==1:
return 1
else:
return 0
#下面开始进行板块或股票池选股
myblock=[] #选股池
start = time.time()
for code in codes:
buy=function(code) #计算股票选股函数
if buy==1:
myblock.append(code)
print('输出选出的股票池',len(myblock))
print(myblock)
end=time.time()
print('时间:',round(start,2),round(end,2),round(end-start,2),'秒')
程序运行结果如下:
小白量化选股: 基本面指标选股
输出股票池或板块中股票
沪深300 300 板块中股票 : ['000001', '000002', '000063', '000066', '000069', '000100', '000157', '000166', '000333', '000338', '000425', '000538', '000568', '000596', '000625', '000627', '000651', '000656', '000661', '000671', '000703', '000708', '000723', '000725', '000728', '000768', '000776', '000783', '000786', '000858', '000860', '000876', '000895', '000938', '000961', '000963', '000977', '001979', '002001', '002007', '002008', '002024', '002027', '002032', '002044', '002049', '002050', '002120', '002129', '002142', '002146', '002153', '002157', '002179', '002202', '002230', '002236', '002241', '002252', '002271', '002304', '002311', '002352', '002371', '002384', '002410', '002414', '002415', '002422', '002456', '002460', '002463', '002475', '002493', '002508', '002555', '002558', '002594', '002600', '002601', '002602', '002607', '002624', '002673', '002714', '002736', '002739', '002773', '002812', '002821', '002841', '002916', '002938', '002939', '002945', '002958', '003816', '300003', '300014', '300015', '300033', '300059', '300122', '300124', '300136', '300142', '300144', '300347', '300408', '300413', '300433', '300498', '300529', '300601', '300628', '300676', '600000', '600004', '600009', '600010', '600011', '600015', '600016', '600018', '600019', '600025', '600027', '600028', '600029', '600030', '600031', '600036', '600048', '600050', '600061', '600066', '600068', '600085', '600104', '600109', '600111', '600115', '600118', '600150', '600161', '600176', '600177', '600183', '600196', '600208', '600233', '600271', '600276', '600297', '600299', '600309', '600332', '600340', '600346', '600352', '600362', '600369', '600383', '600390', '600406', '600436', '600438', '600482', '600487', '600489', '600498', '600519', '600522', '600547', '600570', '600584', '600585', '600588', '600600', '600606', '600637', '600655', '600660', '600690', '600703', '600705', '600741', '600745', '600760', '600763', '600795', '600809', '600837', '600845', '600848', '600872', '600886', '600887', '600893', '600900', '600918', '600919', '600926', '600958', '600989', '600998', '600999', '601006', '601009', '601012', '601021', '601066', '601077', '601088', '601100', '601108', '601111', '601117', '601138', '601155', '601162', '601166', '601169', '601186', '601198', '601211', '601216', '601225', '601229', '601231', '601236', '601238', '601288', '601318', '601319', '601328', '601336', '601360', '601377', '601390', '601398', '601555', '601577', '601600', '601601', '601607', '601618', '601628', '601633', '601658', '601668', '601669', '601688', '601696', '601698', '601727', '601766', '601788', '601800', '601808', '601816', '601818', '601838', '601857', '601872', '601877', '601878', '601881', '601888', '601899', '601901', '601916', '601919', '601933', '601939', '601985', '601988', '601989', '601990', '601998', '603019', '603087', '603156', '603160', '603195', '603259', '603288', '603369', '603392', '603501', '603658', '603799', '603833', '603899', '603986', '603993', '688008', '688009', '688012', '688036']
输出选出的股票池 16
['000538', '000625', '002008', '002157', '002202', '002236', '002384', '002555', '002673', '300136', '300676', '600109', '600655', '600989', '601066', '601838']
时间: 1610260670.1 1610260678.45 8.35 秒
因数据除权问题,有点差异,总体影响不大。
请持续关注我的博客,我的进步,就是你的进步!