% 20160818 得到母基金的股票和股指期货持仓的交易日收益率
clear;clc
tic
%% 母基金名称
ProName=['("母基金二期")'];%['("母基金二期","母基金三期","母基金四期")']
%% 连接数据库
conn=database('report','uxmc','uxmc-123', 'com.mysql.jdbc.Driver',...
'jdbc:mysql://report001.mysql.rds.aliyuncs.com/report');
ping(conn);
%% 提取母基金代码
sql=sprintf('select productCode from product where productName in %s order by productCode',ProName);
cursorA=exec(conn,sql);
cursorA=fetch(cursorA);
FOFCode=cursorA.Data;
close(cursorA);
%% 提取母基金所有股票和期货信息
FundCodeValue=eval(FOFCode{1});%cell转数值为母基金的代码
Index=['tradingDay,instType,instrumentCode,productCode,marketValue'];
sql=sprintf('select %s from positions_history where left(productCode,5)=%d order by tradingDay',Index,FundCodeValue);
cursorA=exec(conn,sql);
cursorA=fetch(cursorA);
allInst=cursorA.Data;
close(cursorA);
%% 保留‘600’、‘601’、‘603’、‘000’、‘002’和‘300’打头的股票部分
instType=allInst(:,2);
instrumentCodeAll=allInst(:,3);
x=find(strncmp(instrumentCodeAll,'600',3)|strncmp(instrumentCodeAll,'601',3)...
|strncmp(instrumentCodeAll,'603',3)|strncmp(instrumentCodeAll,'000',3)...
|strncmp(instrumentCodeAll,'002',3)|strncmp(instrumentCodeAll,'300',3)...
|strcmp(instType,'期货'));
allInst=allInst(x,:);
%% 排除商品期货部分
instType=allInst(:,2);
instrumentCodeAll=allInst(:,3);
x=find(strncmp(instrumentCodeAll,'IC',2)|strncmp(instrumentCodeAll,'IF',2)...
|strncmp(instrumentCodeAll,'IH',2)|strcmp(instType,'股票'));
allInst=allInst(x,:);
allInst(:,3)=strrep(allInst(:,3),'CFFEX','CFE');%'CFFEX'替换为'CFE',便于wind查询
%% 提取股票、股指期货的日收益率
instrumentCodeUni=unique(allInst(:,3)); %存储所有不重复的股票代码
dayAll=[];
for i=1:1:length(allInst(:,1))
tempday= allInst{i,1}; % 日期在第一列
dayAll(i,1)= str2double(tempday);
end
dayUni=unique(dayAll);
%% 从wind拉取
instrumentCodeUni=[instrumentCodeUni;'000300.SH';'000905.SH';'000016.SH'];%加上沪深300,中证500,上证50
instStr='';
for i=1:1:length(instrumentCodeUni)
if i~=length(instrumentCodeUni)
str=sprintf('%s,',instrumentCodeUni{i});
else
str=instrumentCodeUni{i};
end
instStr=strcat(instStr,str);
end
w=windmatlab;
%[w_wsd_data]=w.wsd(instStr,'pct_chg',num2str(dayUni(1)),num2str(dayUni(end)));
[w_wsd_data]=w.wsd(instStr,'pct_chg',num2str(dayUni(1)),num2str(dayUni(end)),'PriceAdj=F');
rateTable=w_wsd_data/100;
save(['./data/','Rate03.mat'],'dayUni','instrumentCodeUni','rateTable');
toc