Python量化行业温度计自动生成,跟踪市场温度
每个投资者都知道,投资不能盲目投资瞎投资,不知道市场状态,市场当前所处的历史位置,不知道现在的市场情绪,长期以往是很难获得投资成功的,因此我们就要不断的研究,去认识市场,去感知市场,与市场同步,这样可能长期下去,或许可能让投资清楚明白。
那么作为一个量化投资者来说,我们该如何了解市场的位置和状态呢。YU股思基开发了一个行业市场温度计,以此来跟踪市场所处的历史水平和位置,每个人关注的因子都不太一样,这里仅仅是YU股思基的一个方法,这里风分享给大家。
Python量化实现行业温度计自动生成
行业温度计实现主要逻辑:
1。获取31个申万一级行业的估值数据市盈率PE市净率PB计算任意历史区间的估值数据水位(可设置)YU股思基使用的是滚动近五年的数据
2。根据市盈率和10年期国债收益率计算行业ERP因子和因子所处水位依然是近五年的水位
3。获取每日行业的涨跌幅和今年以来的涨跌幅关注行业的当前行情
4。按照三个因子PE、PB、ERP三个因子的水位分配权重30、30、40,得到31个行业综合温度计水平和位置
Python量化实现代码:
1。导入相关模块和库设置金融数据接口importpandasaspdimporttushareastsimportakshareasakimporttimeimportdatetimefromdateutil。relativedeltaimportrelativedeltaimportwarningswarnings。filterwarnings(ignore)忽略弃用模块警告tushare接口授权token你的接口prots。proapi(tokentoken)ts。settoken(tokentoken)使用复权行情必须设置接口token
2。定义一个类功能是:计算某个时间的固定窗口期的前一个时间,时间间隔可以是年月日定义一个类,功能是:计算某个时间的固定窗口期的前一个时间,时间间隔可以是年、月、日classCalLastDate():类的初始化definit(self,date,deltanum):这个是类的特殊方法,在的类的实例化时即被调用self。datedateself。deltanumdeltanum以年为间隔defdatedeltabyyear(self):enddatepd。todatetime(self。date)用pandas把字符串时间转换为日期格式startdate(enddaterelativedelta(yearsself。deltanum))。strftime(Ymd)print(startdate)returnstartdate以月为间隔defdatedeltabymonth(self):enddatepd。todatetime(self。date)用pandas把字符串时间转换为日期格式startdate(enddaterelativedelta(monthsself。deltanum))。strftime(Ymd)print(startdate)returnstartdate以天为间隔defdatedeltabyday(self):enddatepd。todatetime(self。date)用pandas把字符串时间转换为日期格式startdate(enddaterelativedelta(daysself。deltanum))。strftime(Ymd)print(startdate)returnstartdate
3。定义一个计算一组数据分为数的函数和方法计算每个数在一组数据中的分位值defcalpercentile(datalist):numpercentilelist〔〕sortdatalistsorted(datalist)print(sortdatalist)fornumindatalist:numindexsortdatalist。index(num)print(numindex)numpercentileround(((numindex1)len(sortdatalist))100,2)百分位值print(numpercentile)numpercentilelist。append(numpercentile)returnnumpercentilelist
4。定义一个获取申万31个一级行业的历史行情的方法行业表现:申万一级行业,历史行情defgetindustrythemometerinfo(date):获取最终字段columns〔交易日期,行业名称,收盘价,涨跌幅(),PE,PE温度,PB,PB温度,ERP,ERP温度,今年以来收益(),行业综合温度〕dfpd。DataFrame(columnscolumns)10年期国债收益率bondzhusratedfak。bondzhusrate()dfbond10bondzhusratedf〔〔日期,中国国债收益率10年〕〕dfbond10。columns〔tradedate,Bond10Year〕dfbond10〔tradedate〕dfbond10〔tradedate〕。astype(str)dfbond10〔tradedate〕dfbond10〔tradedate〕。str。replace(,)申万31个一级行业代码swindustrycodelist〔801120。SI,801980。SI,801200。SI,801770。SI,801760。SI,801750。SI,801730。SI,801140。SI,801050。SI,801010。SI,801130。SI,801110。SI,801030。SI,801080。SI,801230。SI,801740。SI,801790。SI,801960。SI,801880。SI,801710。SI,801040。SI,801150。SI,801210。SI,801890。SI,801170。SI,801970。SI,801160。SI,801780。SI,801720。SI,801180。SI,801950。SI〕设置计算时长caldateCalLastDate(datedate,deltanum5)last5yearsdatecaldate。datedeltabyyear()forcodeinswindustrycodelist:columnsfor〔交易日期,行业名称,收盘价,涨跌幅(),PE,PE温度,PB,PB温度,ERP,ERP温度,行业综合温度〕dfforpd。DataFrame(columnscolumnsfor)获取当日行情和年初第一天行情,计算今年以来收益率dfswindustrypro。swdaily(tscodecode,startdatedate,enddatedate)startdate202220220104dfswindustrystartdate2022pro。swdaily(tscodecode,startdatestartdate2022,enddatestartdate2022)print(dfswindustrystartdate2022)pctchange2022round(((dfswindustry〔close〕〔0〕dfswindustrystartdate2022〔close〕〔0〕)dfswindustrystartdate2022〔close〕〔0〕)100,2)print(pctchange2022)计算各指标近5年的分位数温度,PE,PB,ERP获取五年前的时间,并获取分位数据dfswindustry5yearspro。swdaily(tscodecode,startdatelast5yearsdate,enddatedate)pelistdfswindustry5years〔pe〕。tolist()pblistdfswindustry5years〔pb〕。tolist()pepercentilelistcalpercentile(pelist)pbpercentilelistcalpercentile(pblist)dfswindustry5years〔pepercentile〕pepercentilelistdfswindustry5years〔pbpercentile〕pbpercentilelist计算ERPdfswindustry5yearsERPdfswindustry5years。merge(dfbond10,howleft,ontradedate)dfswindustry5yearsERP〔ERP〕round(((1dfswindustry5yearsERP〔pe〕)100dfswindustry5yearsERP〔Bond10Year〕),2)ERPlistdfswindustry5yearsERP〔ERP〕。tolist()ERPpercentlistcalpercentile(ERPlist)dfswindustry5yearsERP〔ERPpercentile〕ERPpercentlistdfswindustry5yearsERP〔Thermometer〕round(dfswindustry5yearsERP〔pepercentile〕0。3dfswindustry5yearsERP〔pbpercentile〕0。3dfswindustry5yearsERP〔ERPpercentile〕0。4,2)按照各指标赋予权重dfswindustry5yearsERPdfswindustry5yearsERP〔〔tradedate,name,close,pctchange,pe,pepercentile,pb,pbpercentile,ERP,ERPpercentile,Thermometer〕〕数据处理结果dfresultdfswindustry5yearsERPdfresult。columnscolumnsfordfresult〔今年以来收益〕pctchange2022dffor。loc〔len(dffor)〕dfresult。loc〔0〕。valuesdffor〔今年以来收益()〕〔pctchange2022〕对结果数据字段重新排序dffordffor〔〔交易日期,行业名称,收盘价,涨跌幅(),PE,PE温度,PB,PB温度,ERP,ERP温度,今年以来收益(),行业综合温度〕〕df。loc〔len(df)〕dffor。loc〔0〕。valuesprint(df)returndf
5。定义量化的主逻辑代码ifnamemain:记录程序执行时间starttimetime。time()print(行业市场综合温度计开始执行。。。。。)保存最终结果数据ExcelWriterpd。ExcelWriter(IndustryThermometer。xlsx)获取当天日期todaydatedatetime。datetime。now()。strftime(Ymd)交易日Lasttradedategetlasttradedate(datetodaydate)currenttimetime。time()print(程序已执行:str(round(currenttimestarttime,2))s)dfgetindustrythemometerinfo(dateLasttradedate)df。sortvalues(by涨跌幅(),inplaceTrue,ascendingFalse)按涨跌幅降序排列df〔序号〕〔xforxinrange(1,len(df)1)〕dfdf〔〔序号,交易日期,行业名称,收盘价,涨跌幅(),PE,PE温度,PB,PB温度,ERP,ERP温度,今年以来收益(),行业综合温度〕〕currenttimetime。time()print(程序已执行:str(round(currenttimestarttime,2))s)df。toexcel(excelwriterExcelWriter,sheetname温度计,indexFalse,startrow1)ExcelWriter。close()currenttimetime。time()print(程序已执行:str(round(currenttimestarttime,2))s)print(当日温度计数据已生成,请查看。。。。)
(想要获取完整代码关注YU股思基,私信获取)
Python量化实现结果如下:
通过结果大家可以每日跟踪市场各行业和行情,并清楚的看出行业的位置对做好行业轮动具有很好的参考意义。
但是大家要注意的是,没有任何一个方法跟踪市场是完全正确的,只能是对我们的认知起到很好的辅助作用。
坚持理性研究,量化投资,做有价值的事情,做正确的事情,坚定信心,静待开花结果。
免责声明:投资有风险,投资需谨慎!本文仅为根据市场公开资料及个人理解研究分析,不作为投资依据,文中涉及到的标的仅作为举例,不构成投资建议,所提到的观点仅代表个人的意见,所涉及标的不作推荐,风险自负。
坚持不易,点赞、关注、分享三连击!