关闭

比较精准的答案

39人阅读 评论(0) 收藏 举报

% 20160830
% 得到母基金的股票和股指期货持仓的交易日收益率
%计算母基金equity 下 PN,PH的占比计算,分别求得相应的日占比。
%{
Rate= Pn*Rs+ Ph*(Rs-Ri)+Ph*(Ri-Rsf)+Pcf*Rcf
Rs 股票净值收益率
Ri 股指期货指数收益率,股指+期货的收益率,反应的是股票 指数部分的收益
Rsf 股指期货净值收益率
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 order by tradingDay’,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);

%%
load(‘data0829/Rate.mat’);
%%
%当a10 为 1 时候,基金没有数据,当不等于1时有数据
a10 = length(Fundequity);
%for j=1:168 %因为 rate表只到168

j=100;
xday=Fundequity{j,1};
Fundday = Fundequity(:,1);
xf = find(strcmp(Fundday,xday));
Funddayinfo=Fundequity(xf,:);    %xf 就是占位数目,同收益率 有关
Fname = allInst(:,4);            %子基金
xa=find(strncmp(Fname,Fund,10)); %某个子基金下的某日的股票和期货
dayfdd=0;
dayfkk=0;
dayRateSf =0;
tstockProp =0;
tfutureProp =0;
TsRate =0; 
IIIrate=0;
if ~isempty(xa)                 %判断是否有股票 期货
    alldayinfo=allInst(xa,:);   %可能里面有重复的
    eday = alldayinfo(:,1);     %全部日期
    xb=find(strcmp(eday,xday));
    onedayinfo02=alldayinfo(xb,:);
    xdx = xf;                   %xdx= find(strcmp(dayUni,xday))  通过dayUni 确定信息在rateTable的第几行 问题在这

%% 判断股票和期货是否存在
%% 股票类,收益率 求收益的时候是取得股票marketValue的总价值 allInst 中求得
atype = ‘股票’;
ctype = onedayinfo02(:,2);
xc=find(strcmp(ctype,atype));
dayStockValue =0;
TsRate = 0; %当天的多股票累计收益率
if ~isempty(xc)
onedaystock=onedayinfo02(xc,:); %利用这个onedaystock 找到所有当日收益率,列值
aa= cell2mat(onedaystock(:,5)); % xday 从rateTable 可以找到相应的收益率,然后同股票的相乘
dayStockValue= sum(aa(:,1)); %股票就全加一下子,基金判断一下再加
for kt=1:length(aa) %kt =1;%第一只股票
yd= find(strcmp(instrumentCodeUni,onedaystock{kt,3}));

            if ~isempty(yd)     %有些股票不在Rate.mat表中
                sRate=rateTable(xdx,yd); %ook
                if ~isnan(sRate) 
                   % TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3}; %此处是股票总值
                   TsRate=TsRate+sRate*onedaystock{kt,5}/dayStockValue;
                end
            end % if ~isempty(yd)
        end

    end %length(xc)~=0 

%% 期货指数类,收益率
btype = ‘期货’; %多开和空开要相减
ctype = onedayinfo02(:,2);
xd1=find(strcmp(ctype,btype));

    if ~isempty(xd1) %判断xd 是否存在
        onedayfuture=onedayinfo02(xd1,:);

%%
ftype = ‘多开’;%找到多开 - 空开
ftype01 = ‘买入’;
htype = onedayfuture(:,6);
xed=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
if ~isempty(xed)
onedayfuturedd=onedayfuture(xed,:);
a1= cell2mat(onedayfuturedd(:,5));
dayfdd= sum(a1(:,1)); %基金多开累加
end
gtype = ‘空开’;
gtype01 = ‘卖出’;
jtype = onedayfuture(:,6);
xrk=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
if ~isempty(xrk)
onedayfuturekk=onedayfuture(xrk,:);
a2= cell2mat(onedayfuturekk(:,5));
dayfkk= sum(a2(:,1)); %基金空开累加
end
onedayfutureMarket =dayfdd-dayfkk;
%%
ftype = ‘多开’; %找到多开 - 空开
ftype01 = ‘买入’;
htype = onedayfuture(:,6);
xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
ddRate = 0; %当天的多期货的累计收益率
if ~isempty(xe)
onedayfuturedd=onedayfuture(xe,:);
a1= cell2mat(onedayfuturedd(:,5));
fprintf (‘%d\n’,j);
for kt=1:length(a1)
yd= find(strcmp(instrumentCodeUni,onedayfuturedd{kt,3}));
sRate=rateTable(xdx,yd); %ok fprintf(‘%d’,sRate);
if ~isnan(sRate)
ddRate=ddRate+sRate*onedayfuturedd{kt,5}/onedayfutureMarket;
end
end
dayfdd= sum(a1(:,1)); %基金多开累加
end

        gtype = '空开';  %找到多开 - 空开
        gtype01 = '卖出';
        jtype = onedayfuture(:,6);
        xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
        kkRate = 0; %当天的多期货的累计收益率   
        if ~isempty(xr)
            onedayfuturekk=onedayfuture(xr,:);
            a2= cell2mat(onedayfuturekk(:,5));
            for kt=1:length(a2)
                sRate=rateTable(xdx,yd); %ok fprintf('%d',sRate);
                if ~isnan(sRate)   
                    kkRate=kkRate+sRate*onedayfuturekk{kt,5}/onedayfutureMarket;
                end
            end
            dayfkk= sum(a2(:,1)); %基金空开累加
        end
        dayRateSf = ddRate - kkRate;  %多头收益 - 空头收益

%% 期货收益率计算,RI
%先用时间找到 IF IC IH 这三个变量的值,然后根据下面的分类分别相乘
% AIF=IF*(多头-空头)/期货净头寸; AIC=IC*(多头-空头)/期货净头寸; AIH=IH*(多头-空头)/期货净头寸;
% 想法没有错,确实是指数收益
% RI= AIF+AIC+AIH

       onedayfuture=onedayinfo02(xd1,:);
       TFRate=rateTable(xdx,1838); 
       TCRate=rateTable(xdx,1839); 
       THRate=rateTable(xdx,1840); 
       %判断是IF IC IH, 三次判断如果有就是 value 如果没有就是 0 

%% IH
xih=find(strncmp(onedayfuture(:,3),’IH’,2));
ihvalue =0;
if ~isempty(xih)
onedayfutureih=onedayfuture(xih,:);
ftype = ‘多开’; %找到多开 - 空开
ftype01 = ‘买入’;
htype = onedayfutureih(:,6);
xe01=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
ihdayfdd =0;
if ~isempty(xe01)
onedayfuturedd=onedayfutureih(xe01,:);
a1= cell2mat(onedayfuturedd(:,5));
ihdayfdd= sum(a1(:,1)); %基金多开累加
end
gtype = ‘空开’;
gtype01 = ‘卖出’;
jtype = onedayfutureih(:,6);
xr01=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
ihdayfkk =0;
if ~isempty(xr01)
onedayfuturekk=onedayfutureih(xr01,:);
a2= cell2mat(onedayfuturekk(:,5));
ihdayfkk= sum(a2(:,1)); %基金空开累加
end
ihvalue =(ihdayfdd-ihdayfkk)/onedayfutureMarket;
end
%% IC
xic=find(strncmp(onedayfuture(:,3),’IC’,2));
icvalue =0;
if ~isempty(xic)
onedayfutureic=onedayfuture(xic,:);

            ftype = '多开';  %找到多开 - 空开
            ftype01 = '买入';
            htype = onedayfutureic(:,6);
            xe02=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
            icdayfdd = 0;
            if ~isempty(xe02)
                onedayfuturedd=onedayfutureic(xe02,:);
                a1= cell2mat(onedayfuturedd(:,5));
                icdayfdd= sum(a1(:,1)); %基金多开累加
            end
            gtype = '空开';
            gtype01 = '卖出';
            jtype = onedayfutureic(:,6);
            xr02=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
            icdayfkk =0;
            if ~isempty(xr02)
                onedayfuturekk=onedayfutureic(xr02,:);
                a2= cell2mat(onedayfuturekk(:,5));
                icdayfkk= sum(a2(:,1)); %基金空开累加
            end
            icvalue =(icdayfdd-icdayfkk)/onedayfutureMarket;
       end

%% IF
xif=find(strncmp(onedayfuture(:,3),’IF’,2));
ifvalue =0;
if ~isempty(xif)
onedayfutureif=onedayfuture(xif,:);

            ftype = '多开'; %找到多开 - 空开
            ftype01 = '买入';
            htype = onedayfutureif(:,6);
            xe03=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
            ifdayfdd =0;
            if ~isempty(xe03)
                onedayfuturedd=onedayfutureif(xe03,:);
                a1= cell2mat(onedayfuturedd(:,5));
                ifdayfdd= sum(a1(:,1)); %基金多开累加
            end
            gtype = '空开';
            gtype01 = '卖出';
            jtype = onedayfutureif(:,6);
            xr03=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
            ifdayfkk =0;
            if ~isempty(xr03)
                onedayfuturekk=onedayfutureif(xr03,:);
                a2= cell2mat(onedayfuturekk(:,5));
                ifdayfkk= sum(a2(:,1)); %基金空开累加
            end
            ifvalue =(ifdayfdd-ifdayfkk)/onedayfutureMarket;
       end
       IIIrate= TFRate*ihvalue + TCRate*icvalue + THRate*ifvalue;             

%%
else
dayfdd=0;
dayfkk=0;
IIIrate =0; % 代表了指数收益
end
tstockProp = dayStockValue/Funddayinfo{1,3};
tfutureProp = (dayfdd-dayfkk)/Funddayinfo{1,3};

end

datafunda0001{j,1}=xday;
datafunda0001{j,2}=tstockProp;
datafunda0001{j,3}=tfutureProp;

datafunda0001{j,4}=TsRate;
datafunda0001{j,5}=IIIrate;
datafunda0001{j,6}=dayRateSf;

%end

save(‘datafunda0001.mat’,’datafunda0001’);

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10730次
    • 积分:662
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:17篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论