ff

来源:互联网 发布:监控网络接入设备 编辑:程序博客网 时间:2024/04/29 05:10
% 20160825
% 得到母基金的股票和股指期货持仓的交易日收益率
%计算母基金equity 下 PN,PH的占比计算,分别求得相应的日占比。
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);


%% 提取母基金的equity值
foc= FOFCode{1,1}; %母基金名称
Index=['tradingday,productCode,equity'];
%sql=sprintf('select %s from account where productCode = %d',Index,foc);
sql=sprintf('select %s from account where productCode = %s',Index,foc);
cursorA=exec(conn,sql);
cursorA=fetch(cursorA);
FOFequity=cursorA.Data;
close(cursorA);




%% 提取母基金所有股票和期货信息
FundCodeValue=eval(FOFCode{1});%cell转数值为母基金的代码    
Index=['tradingDay,instType,instrumentCode,productCode,marketValue,direction'];
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,'期货')); %这个是找到x的位置数值
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));  %存储所有不重复的股票代码 
for i=1:1:length(allInst(:,1))
    dayAll{i,1}=allInst{i};              % 日期在第一列
end
dayUni=unique(dayAll);


%% 以上已经将母基金包含的信息全部得到了,下面先求子基金代码,
FundCodeValue=eval(FOFCode{1});%cell转数值为母基金的代码 ,数值转换  
Index=['productCode,accountType,parentProductCode'];
sql=sprintf('select %s from product where parentProductCode=%d ',Index,FundCodeValue);
cursorA=exec(conn,sql);
cursorA=fetch(cursorA);
Fundname=cursorA.Data;
close(cursorA);


%% 子基金运算


%% 提取子基金的equity值
Fund = Fundname{1,1}; %子基金名称,这里仅仅是一个子基金,改行不改列
Index=['tradingday,productCode,equity'];
sql=sprintf('select %s from account where productCode = %s order by tradingDay',Index,Fund);
cursorA=exec(conn,sql);
cursorA=fetch(cursorA);
Fundequity=cursorA.Data;
close(cursorA);




%% 计算每一个股票PH和PN 的值
% 先要测算PH和PN那个比较大,暂时按照PH为正数来计算。当PH为负数的时候alpha和beta 还有basis
% 他们之间beta = PN * RS, alpha = PH*(Rs-Ri), basis = PH(Ri -Rsf),Pcf*Rcf
% 这是个日均概念, allInst 下的股票总市值/equity ,期货市值/equity 
% 其中 RS = weight* rate 权重*收益。RI用谁?
%每天一计算最后得到总数 
%这里每天有不同的子基金,所以要区别开,先计算单独的子基金


%%
%20160106 有问题
%当a10 为 1 时候,基金没有数据,当不等于1时有数据
a10 = length(Fundequity);
for j=1:a10 
    xday=Fundequity{j,1};
    Fundday = Fundequity(:,1);
    xf = find(strcmp(Fundday,xday));
    Funddayinfo=Fundequity(xf,:); 
    Fname = allInst(:,4);            %子基金
    xa=find(strncmp(Fname,Fund,10)); %某个子基金下的某日的股票和期货
    if length(xa)~=0 %判断是否有股票 期货
        alldayinfo=allInst(xa,:);   %可能里面有重复的
        eday = alldayinfo(:,1);     %全部日期
        xb=find(strcmp(eday,xday));
        onedayinfo02=alldayinfo(xb,:);
        %判断股票和期货是否存在
        atype = '股票';
        ctype = onedayinfo02(:,2); 
        xc=find(strcmp(ctype,atype));
        if length(xc)~=0
            onedaystock=onedayinfo02(xc,:);
            aa= cell2mat(onedaystock(:,5));
            dayStockValue= sum(aa(:,1)); %股票就全加一下子,基金判断一下再加
        end
        btype = '期货'; %多开和空开要相减
        ctype = onedayinfo02(:,2); 
        xd=find(strcmp(ctype,btype));
        if length(xd)~=0 %判断xd 是否存在
            onedayfuture=onedayinfo02(xd,:);
            %找到多开 - 空开
            ftype = '多开';
            ftype01 = '买入';
            htype = onedayfuture(:,6);
            xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
            if length(xe)~=0
                onedayfuturedd=onedayinfo02(xe,:);
                a1= cell2mat(onedayfuturedd(:,5));
                dayfdd= sum(a1(:,1)); %基金多开累加
            end
            gtype = '空开';
            gtype01 = '卖出';
            jtype = onedayfuture(:,6);
            xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
            if length(xr)~=0
                onedayfuturekk=onedayinfo02(xr,:);
                a2= cell2mat(onedayfuturekk(:,5));
                dayfkk= sum(a2(:,1)); %基金空开累加
            end
         else
            dayfdd=0;
            dayfkk=0;
        end
        tstockRate = dayStockValue/Funddayinfo{1,3};
        tfutureRate = (dayfdd-dayfkk)/Funddayinfo{1,3};
    end
    infodatak{j,1}=xday;
    infodatak{j,2}=tstockRate;
    infodatak{j,3}=tfutureRate;
    infodatak{j,5}=dayStockValue;
    infodatak{j,6}=(dayfdd-dayfkk);
    
    %收集日期和上面两个数,其余的在循环外计算


end


%% 单日的信息 当tfutureRate 小于 0 时,说明没有对冲,没有PH,  
%   当tfutureRate 小于 0,说明没有对冲 只有beta =tstockRate *股票收益+tfutureRate*股指期货收益
%   PH=0,
%   当tfutureRate小于tstockRate,PN = tstockRate- tfutureRate  PH =tfutureRate  
%   当tfutureRate大于tstockRate,PH 还是对冲部分,而beta 是PK = tfutureRate -tstockRate
%   PK*股指收益率就是beta部分  保存每只基金的每天三列,存在就是有,不存在就是  0 








save('infodatak.mat','infodatak'); 






































%% 从wind拉取, 因为已经拉取了所以暂时不用,只要读取Rate.mat里面的东西就行。
%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)),'PriceAdj=F'); 
%rateTable=w_wsd_data/100;
%save(['./data/','Rate.mat'],'dayUni','instrumentCodeUni','rateTable');
%toc
%%





0 0
原创粉丝点击