最近跑了些回测,顺便把优矿和聚宽数据处理的一点代码记录下来
优矿可以取数到CSV(excel是压缩文件,超耗内存,不建议),然后下载到本地,代码很简单:
import pandas as pd
start = '2021-07-01' # 采集起始时间
end = '2021-08-07' # 采集结束时间
ConBonds = DataAPI.BondGet(typeID="02020113",field=u"secID",pandas="1")
MeiRi = DataAPI.MktConsBondPerfGet(beginDate=start,endDate=end,secID=ConBonds['secID'],field=u"tradeDate,secID,closePriceBond,bondPremRatio,remainSize",pandas="1")
MeiRi.to_csv('year21new.csv' , index=0)
如果需要再从其他函数取某些数据比如转股时间一并导出,可以用pd.merge关联
聚宽的数据分散在几张表里,并且没有每日溢价率,如果想要用聚宽数据做回测,要稍微多处理一下:
import pandas as pd
from jqdata import bond
from sqlalchemy.sql.expression import or_
today='2018-03-13' #取数的日期
eee=pd.merge(ddd,kkk,on='code')
ttt=pd.merge(eee,ccc,left_on='company_code',right_on='code')
优矿可以取数到CSV(excel是压缩文件,超耗内存,不建议),然后下载到本地,代码很简单:
import pandas as pd
start = '2021-07-01' # 采集起始时间
end = '2021-08-07' # 采集结束时间
ConBonds = DataAPI.BondGet(typeID="02020113",field=u"secID",pandas="1")
MeiRi = DataAPI.MktConsBondPerfGet(beginDate=start,endDate=end,secID=ConBonds['secID'],field=u"tradeDate,secID,closePriceBond,bondPremRatio,remainSize",pandas="1")
MeiRi.to_csv('year21new.csv' , index=0)
如果需要再从其他函数取某些数据比如转股时间一并导出,可以用pd.merge关联
聚宽的数据分散在几张表里,并且没有每日溢价率,如果想要用聚宽数据做回测,要稍微多处理一下:
import pandas as pd
from jqdata import bond
from sqlalchemy.sql.expression import or_
today='2018-03-13' #取数的日期
先取某日(today)的可转债及正股代码
aaa=bond.run_query(query(bond.CONBOND_BASIC_INFO.code,bond.CONBOND_BASIC_INFO.company_code).filter(bond.CONBOND_BASIC_INFO.list_date <= today,or_(bond.CONBOND_BASIC_INFO.delist_Date > today,bond.CONBOND_BASIC_INFO.delist_Date==None)))取某日可转债收盘价
bbb=bond.run_query(query(bond.CONBOND_DAILY_PRICE.code,bond.CONBOND_DAILY_PRICE.close).filter(bond.CONBOND_DAILY_PRICE.money>0,bond.CONBOND_DAILY_PRICE.date==today,bond.CONBOND_DAILY_PRICE.code.in_(list(aaa['code']))))取某日正股收盘价
ccc=get_price(list(aaa['company_code']), start_date=today, end_date=today, fq=None, panel=False)取某日(today)的最新转股价
kkk=bond.run_query(query(bond.CONBOND_CONVERT_PRICE_ADJUST.code,bond.CONBOND_CONVERT_PRICE_ADJUST.new_convert_price).filter(bond.CONBOND_CONVERT_PRICE_ADJUST.code.in_(list(aaa['code'])),bond.CONBOND_CONVERT_PRICE_ADJUST.adjust_date<=today)).groupby('code').last()下面把几组数据关联起来形成一个dataframe
ddd=pd.merge(bbb,aaa,on='code')eee=pd.merge(ddd,kkk,on='code')
ttt=pd.merge(eee,ccc,left_on='company_code',right_on='code')