Created
April 13, 2020 02:32
-
-
Save refraction-ray/dd6c783c69301c839b8cf7961bb1ac43 to your computer and use it in GitHub Desktop.
基于 xalpha 的逐年指数涨幅拆解与分析
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xalpha as xa | |
import pandas as pd | |
import datetime as dt | |
def _percent(r): | |
return round((r-1)*100, 2) | |
def index_analysis(code, start, end): | |
dfe = xa.get_daily("peb-"+code, end=end, prev=20).iloc[-1] | |
end = dfe["date"].strftime("%Y%m%d") | |
dfs = xa.get_daily("peb-"+code, end=start, prev=20).iloc[-1] | |
start = dfs["date"].strftime("%Y%m%d") | |
d = {} | |
r2 = dfe["pe"]/dfs["pe"] | |
r1= xa.get_daily(code, end=end, prev=20).iloc[-1]["close"]\ | |
/xa.get_daily(code, end=start, prev=20).iloc[-1]["close"] | |
r3 = dfe["pb"]/dfs["pb"] | |
r4 = (float(xa.get_daily("teb-"+code, end=end, prev=20).iloc[-1]["e"])\ | |
/float(xa.get_daily("teb-"+code, end=start, prev=20).iloc[-1]["e"])) | |
r5 = (float(xa.get_daily("teb-"+code, end=end, prev=20).iloc[-1]["m"])\ | |
/float(xa.get_daily("teb-"+code, end=start, prev=20).iloc[-1]["m"])) | |
r6 = (float(xa.get_daily("teb-"+code, end=end, prev=20).iloc[-1]["b"])\ | |
/float(xa.get_daily("teb-"+code, end=start, prev=20).iloc[-1]["b"])) | |
d["指数涨幅"] = _percent(r1) | |
d["估值涨幅"] = _percent(r2) | |
# d["市值加权估值涨幅"] = _percent(r5/r4) | |
d["净资产估值涨幅"] = _percent(r3) | |
d["总利润涨幅"] = _percent(r4) | |
d["总净资产涨幅"] = _percent(r6) | |
d["有效利润涨幅"] = _percent(r1/r2) | |
# d["有效利润涨幅市值加权"] = _percent(r1*r4/r5) | |
d["有效净资产涨幅"] = _percent(r1/r3) | |
d["指数利润涨幅损益"] = _percent(r1/r2/r4) | |
d["指数净资产涨幅损益"] = _percent(r1/r3/r6) | |
# d["指数分红增发回购利润增益"] = round( (r6-1)*100, 3) | |
# d["指数权重利润增益"] = round(((1+r1/100)/(1+r2/100)/r4/r6-1)*100, 3) | |
return d | |
r = {} | |
for d in pd.date_range("2014", "2020", freq="Y"): | |
r["%s"%d.strftime("%Y")]\ | |
=index_analysis("SH000827", start=(d-dt.timedelta(days=365)).strftime("%Y%m%d"), end=d.strftime("%Y%m%d")) | |
pd.DataFrame(r).T | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment