0825完成版
% % 20160825
% 得到母基金的股票和股指期貨持倉的交易日收益率
%計算母基金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=10;
? ? 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)); %某個子基金下的某日的股票和期貨
? ??
? ? if ~isempty(xa) %判斷是否有股票 期貨
? ? ? ? alldayinfo=allInst(xa,:); ? %可能里面有重復的
? ? ? ? eday = alldayinfo(:,1); ? ? %全部日期
? ? ? ? xb=find(strcmp(eday,xday));
? ? ? ? onedayinfo02=alldayinfo(xb,:);
? ? ? ??
? ? ? ? %xdx= find(strcmp(dayUni,xday));
? ? ? ? xdx = xf;
? ? ? ? %通過dayUni 確定信息在rateTable的第幾行 問題在這
? ? ? ??
%% ? ? %判斷股票和期貨是否存在
%% ? ? 股票類,收益率
? ? ? ? atype = '股票';
? ? ? ? ctype = onedayinfo02(:,2);?
? ? ? ? xc=find(strcmp(ctype,atype));
? ? ? ? if ~isempty(xc) ? ? ??
? ? ? ? ? ? onedaystock=onedayinfo02(xc,:); %利用這個onedaystock 找到所有當日收益率,列值
? ? ? ? ? ? aa= cell2mat(onedaystock(:,5));%xday ?從rateTable 可以找到相應的收益率,然后同股票的相乘?
? ? ? ? ? ? %kt =1;%第一只股票
? ? ? ? ? ? TsRate = 0; %當天的多股票累計收益率 ??
? ? ? ? ? ? for kt=1:length(aa)
? ? ? ? ? ? ? ? yd= find(strcmp(instrumentCodeUni,onedaystock{kt,3})); ?
? ? ? ? ? ? ? ? if ~isempty(yd) %有些股票不在Rate.mat表中
? ? ? ? ? ? ? ? ? ? sRate=rateTable(xdx,yd); %ook
? ? ? ? ? ? ? ? ? ? if isnan(sRate)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end % if ~isempty(yd)
? ? ? ? ? ? end
? ? ? ? ? ? dayStockValue= sum(aa(:,1)); %股票就全加一下子,基金判斷一下再加 ??
? ? ? ? end %length(xc)~=0?
? ? ? ??
? ? ? ?
%% ? ? 期貨指數類,收益率
? ? ? ? btype = '期貨'; %多開和空開要相減
? ? ? ? ctype = onedayinfo02(:,2);?
? ? ? ? xd1=find(strcmp(ctype,btype));
? ? ? ? dayfdd=0;
? ? ? ? dayfkk=0;
? ? ? ? dayRateSf =0;
? ? ? ? if ~isempty(xd1) %判斷xd 是否存在
? ? ? ? ? ? onedayfuture=onedayinfo02(xd1,:);
? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? ddRate=ddRate+sRate*onedayfuturedd{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? ddRate=ddRate+sRate*onedayfuturedd{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? kkRate=kkRate+sRate*onedayfuturekk{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? kkRate=kkRate+sRate*onedayfuturekk{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? dayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? end
? ? ? ? ? ? dayRateSf = kkRate -ddRate;
? ? ? ? ? ??
%% ? ? ? 期貨收益率計算,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?
? ? ? ? ? ?xih=find(strncmp(onedayfuture(:,3),'IH',2));
? ? ? ? ? ?ihvalue =0;
? ? ? ? ? ?if ~isempty(xih)
? ? ? ? ? ? ? ? onedayfutureih=onedayfuture(xih,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureih(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? ihdayfdd =0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureih(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? ihdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureih(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? ihdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureih(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? ihdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ihvalue =ihdayfdd-ihdayfkk;
? ? ? ? ? ?end
? ??
? ? ? ? ? ?xic=find(strncmp(onedayfuture(:,3),'IC',2));
? ? ? ? ? ?icvalue =0;
? ? ? ? ? ?if xic
? ? ? ? ? ? ? ? onedayfutureic=onedayfuture(xic,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureic(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? icdayfdd = 0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureic(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? icdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureic(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? icdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureic(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? icdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? icvalue =icdayfdd-icdayfkk;
? ? ? ? ? ?end
? ? ? ? ? ?
? ? ? ? ? ?xif=find(strncmp(onedayfuture(:,3),'IF',2));
? ? ? ? ? ?ifvalue =0;
? ? ? ? ? ?if xif
? ? ? ? ? ? ? ? onedayfutureif=onedayfuture(xif,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureif(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? ifdayfdd =0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureif(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? ifdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureif(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? ifdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureif(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? ifdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ifvalue =ifdayfdd-ifdayfkk;
? ? ? ? ? ?end
? ? ? ? ? ?IIIrate= TFRate*ihvalue + TCRate*icvalue + THRate*ifvalue;
? ? ? ? ? ? ?
%% ? ? ? ? ? ? ??
? ? ? ? else
? ? ? ? ? ? dayfdd=0;
? ? ? ? ? ? dayfkk=0;
? ? ? ? ? ? IIIrate =0; % 代表了指數收益
? ? ? ? ?end
? ? ? ? tstockRate = dayStockValue/Funddayinfo{1,3};
? ? ? ? tfutureRate = (dayfdd-dayfkk)/Funddayinfo{1,3};
? ??
? ? end
? ??
? ? infodata6{j,1}=xday;
? ? infodata6{j,2}=tstockRate;
? ? infodata6{j,3}=tfutureRate;
? ? infodata6{j,4}=TsRate;
? ? infodata6{j,5}=IIIrate;
? ? infodata6{j,6}=dayRateSf;
? ? ? ??
end ?
save('infodata6.mat','infodata6');
% 得到母基金的股票和股指期貨持倉的交易日收益率
%計算母基金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=10;
? ? 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)); %某個子基金下的某日的股票和期貨
? ??
? ? if ~isempty(xa) %判斷是否有股票 期貨
? ? ? ? alldayinfo=allInst(xa,:); ? %可能里面有重復的
? ? ? ? eday = alldayinfo(:,1); ? ? %全部日期
? ? ? ? xb=find(strcmp(eday,xday));
? ? ? ? onedayinfo02=alldayinfo(xb,:);
? ? ? ??
? ? ? ? %xdx= find(strcmp(dayUni,xday));
? ? ? ? xdx = xf;
? ? ? ? %通過dayUni 確定信息在rateTable的第幾行 問題在這
? ? ? ??
%% ? ? %判斷股票和期貨是否存在
%% ? ? 股票類,收益率
? ? ? ? atype = '股票';
? ? ? ? ctype = onedayinfo02(:,2);?
? ? ? ? xc=find(strcmp(ctype,atype));
? ? ? ? if ~isempty(xc) ? ? ??
? ? ? ? ? ? onedaystock=onedayinfo02(xc,:); %利用這個onedaystock 找到所有當日收益率,列值
? ? ? ? ? ? aa= cell2mat(onedaystock(:,5));%xday ?從rateTable 可以找到相應的收益率,然后同股票的相乘?
? ? ? ? ? ? %kt =1;%第一只股票
? ? ? ? ? ? TsRate = 0; %當天的多股票累計收益率 ??
? ? ? ? ? ? for kt=1:length(aa)
? ? ? ? ? ? ? ? yd= find(strcmp(instrumentCodeUni,onedaystock{kt,3})); ?
? ? ? ? ? ? ? ? if ~isempty(yd) %有些股票不在Rate.mat表中
? ? ? ? ? ? ? ? ? ? sRate=rateTable(xdx,yd); %ook
? ? ? ? ? ? ? ? ? ? if isnan(sRate)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end % if ~isempty(yd)
? ? ? ? ? ? end
? ? ? ? ? ? dayStockValue= sum(aa(:,1)); %股票就全加一下子,基金判斷一下再加 ??
? ? ? ? end %length(xc)~=0?
? ? ? ??
? ? ? ?
%% ? ? 期貨指數類,收益率
? ? ? ? btype = '期貨'; %多開和空開要相減
? ? ? ? ctype = onedayinfo02(:,2);?
? ? ? ? xd1=find(strcmp(ctype,btype));
? ? ? ? dayfdd=0;
? ? ? ? dayfkk=0;
? ? ? ? dayRateSf =0;
? ? ? ? if ~isempty(xd1) %判斷xd 是否存在
? ? ? ? ? ? onedayfuture=onedayinfo02(xd1,:);
? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? ddRate=ddRate+sRate*onedayfuturedd{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? ddRate=ddRate+sRate*onedayfuturedd{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? kkRate=kkRate+sRate*onedayfuturekk{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? kkRate=kkRate+sRate*onedayfuturekk{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? dayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? end
? ? ? ? ? ? dayRateSf = kkRate -ddRate;
? ? ? ? ? ??
%% ? ? ? 期貨收益率計算,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?
? ? ? ? ? ?xih=find(strncmp(onedayfuture(:,3),'IH',2));
? ? ? ? ? ?ihvalue =0;
? ? ? ? ? ?if ~isempty(xih)
? ? ? ? ? ? ? ? onedayfutureih=onedayfuture(xih,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureih(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? ihdayfdd =0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureih(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? ihdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureih(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? ihdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureih(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? ihdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ihvalue =ihdayfdd-ihdayfkk;
? ? ? ? ? ?end
? ??
? ? ? ? ? ?xic=find(strncmp(onedayfuture(:,3),'IC',2));
? ? ? ? ? ?icvalue =0;
? ? ? ? ? ?if xic
? ? ? ? ? ? ? ? onedayfutureic=onedayfuture(xic,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureic(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? icdayfdd = 0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureic(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? icdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureic(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? icdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureic(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? icdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? icvalue =icdayfdd-icdayfkk;
? ? ? ? ? ?end
? ? ? ? ? ?
? ? ? ? ? ?xif=find(strncmp(onedayfuture(:,3),'IF',2));
? ? ? ? ? ?ifvalue =0;
? ? ? ? ? ?if xif
? ? ? ? ? ? ? ? onedayfutureif=onedayfuture(xif,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureif(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? ifdayfdd =0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureif(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? ifdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureif(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? ifdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureif(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? ifdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ifvalue =ifdayfdd-ifdayfkk;
? ? ? ? ? ?end
? ? ? ? ? ?IIIrate= TFRate*ihvalue + TCRate*icvalue + THRate*ifvalue;
? ? ? ? ? ? ?
%% ? ? ? ? ? ? ??
? ? ? ? else
? ? ? ? ? ? dayfdd=0;
? ? ? ? ? ? dayfkk=0;
? ? ? ? ? ? IIIrate =0; % 代表了指數收益
? ? ? ? ?end
? ? ? ? tstockRate = dayStockValue/Funddayinfo{1,3};
? ? ? ? tfutureRate = (dayfdd-dayfkk)/Funddayinfo{1,3};
? ??
? ? end
? ??
? ? infodata6{j,1}=xday;
? ? infodata6{j,2}=tstockRate;
? ? infodata6{j,3}=tfutureRate;
? ? infodata6{j,4}=TsRate;
? ? infodata6{j,5}=IIIrate;
? ? infodata6{j,6}=dayRateSf;
? ? ? ??
end ?
save('infodata6.mat','infodata6');
% 得到母基金的股票和股指期貨持倉的交易日收益率
%計算母基金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=10;
? ? 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)); %某個子基金下的某日的股票和期貨
? ??
? ? if ~isempty(xa) %判斷是否有股票 期貨
? ? ? ? alldayinfo=allInst(xa,:); ? %可能里面有重復的
? ? ? ? eday = alldayinfo(:,1); ? ? %全部日期
? ? ? ? xb=find(strcmp(eday,xday));
? ? ? ? onedayinfo02=alldayinfo(xb,:);
? ? ? ??
? ? ? ? %xdx= find(strcmp(dayUni,xday));
? ? ? ? xdx = xf;
? ? ? ? %通過dayUni 確定信息在rateTable的第幾行 問題在這
? ? ? ??
%% ? ? %判斷股票和期貨是否存在
%% ? ? 股票類,收益率
? ? ? ? atype = '股票';
? ? ? ? ctype = onedayinfo02(:,2);?
? ? ? ? xc=find(strcmp(ctype,atype));
? ? ? ? if ~isempty(xc) ? ? ??
? ? ? ? ? ? onedaystock=onedayinfo02(xc,:); %利用這個onedaystock 找到所有當日收益率,列值
? ? ? ? ? ? aa= cell2mat(onedaystock(:,5));%xday ?從rateTable 可以找到相應的收益率,然后同股票的相乘?
? ? ? ? ? ? %kt =1;%第一只股票
? ? ? ? ? ? TsRate = 0; %當天的多股票累計收益率 ??
? ? ? ? ? ? for kt=1:length(aa)
? ? ? ? ? ? ? ? yd= find(strcmp(instrumentCodeUni,onedaystock{kt,3})); ?
? ? ? ? ? ? ? ? if ~isempty(yd) %有些股票不在Rate.mat表中
? ? ? ? ? ? ? ? ? ? sRate=rateTable(xdx,yd); %ook
? ? ? ? ? ? ? ? ? ? if isnan(sRate)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end % if ~isempty(yd)
? ? ? ? ? ? end
? ? ? ? ? ? dayStockValue= sum(aa(:,1)); %股票就全加一下子,基金判斷一下再加 ??
? ? ? ? end %length(xc)~=0?
? ? ? ??
? ? ? ?
%% ? ? 期貨指數類,收益率
? ? ? ? btype = '期貨'; %多開和空開要相減
? ? ? ? ctype = onedayinfo02(:,2);?
? ? ? ? xd1=find(strcmp(ctype,btype));
? ? ? ? dayfdd=0;
? ? ? ? dayfkk=0;
? ? ? ? dayRateSf =0;
? ? ? ? if ~isempty(xd1) %判斷xd 是否存在
? ? ? ? ? ? onedayfuture=onedayinfo02(xd1,:);
? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? ddRate=ddRate+sRate*onedayfuturedd{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? ddRate=ddRate+sRate*onedayfuturedd{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? kkRate=kkRate+sRate*onedayfuturekk{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? kkRate=kkRate+sRate*onedayfuturekk{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? dayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? end
? ? ? ? ? ? dayRateSf = kkRate -ddRate;
? ? ? ? ? ??
%% ? ? ? 期貨收益率計算,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?
? ? ? ? ? ?xih=find(strncmp(onedayfuture(:,3),'IH',2));
? ? ? ? ? ?ihvalue =0;
? ? ? ? ? ?if ~isempty(xih)
? ? ? ? ? ? ? ? onedayfutureih=onedayfuture(xih,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureih(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? ihdayfdd =0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureih(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? ihdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureih(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? ihdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureih(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? ihdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ihvalue =ihdayfdd-ihdayfkk;
? ? ? ? ? ?end
? ??
? ? ? ? ? ?xic=find(strncmp(onedayfuture(:,3),'IC',2));
? ? ? ? ? ?icvalue =0;
? ? ? ? ? ?if xic
? ? ? ? ? ? ? ? onedayfutureic=onedayfuture(xic,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureic(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? icdayfdd = 0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureic(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? icdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureic(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? icdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureic(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? icdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? icvalue =icdayfdd-icdayfkk;
? ? ? ? ? ?end
? ? ? ? ? ?
? ? ? ? ? ?xif=find(strncmp(onedayfuture(:,3),'IF',2));
? ? ? ? ? ?ifvalue =0;
? ? ? ? ? ?if xif
? ? ? ? ? ? ? ? onedayfutureif=onedayfuture(xif,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureif(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? ifdayfdd =0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureif(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? ifdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureif(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? ifdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureif(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? ifdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ifvalue =ifdayfdd-ifdayfkk;
? ? ? ? ? ?end
? ? ? ? ? ?IIIrate= TFRate*ihvalue + TCRate*icvalue + THRate*ifvalue;
? ? ? ? ? ? ?
%% ? ? ? ? ? ? ??
? ? ? ? else
? ? ? ? ? ? dayfdd=0;
? ? ? ? ? ? dayfkk=0;
? ? ? ? ? ? IIIrate =0; % 代表了指數收益
? ? ? ? ?end
? ? ? ? tstockRate = dayStockValue/Funddayinfo{1,3};
? ? ? ? tfutureRate = (dayfdd-dayfkk)/Funddayinfo{1,3};
? ??
? ? end
? ??
? ? infodata6{j,1}=xday;
? ? infodata6{j,2}=tstockRate;
? ? infodata6{j,3}=tfutureRate;
? ? infodata6{j,4}=TsRate;
? ? infodata6{j,5}=IIIrate;
? ? infodata6{j,6}=dayRateSf;
? ? ? ??
end ?
save('infodata6.mat','infodata6');
% 得到母基金的股票和股指期貨持倉的交易日收益率
%計算母基金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=10;
? ? 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)); %某個子基金下的某日的股票和期貨
? ??
? ? if ~isempty(xa) %判斷是否有股票 期貨
? ? ? ? alldayinfo=allInst(xa,:); ? %可能里面有重復的
? ? ? ? eday = alldayinfo(:,1); ? ? %全部日期
? ? ? ? xb=find(strcmp(eday,xday));
? ? ? ? onedayinfo02=alldayinfo(xb,:);
? ? ? ??
? ? ? ? %xdx= find(strcmp(dayUni,xday));
? ? ? ? xdx = xf;
? ? ? ? %通過dayUni 確定信息在rateTable的第幾行 問題在這
? ? ? ??
%% ? ? %判斷股票和期貨是否存在
%% ? ? 股票類,收益率
? ? ? ? atype = '股票';
? ? ? ? ctype = onedayinfo02(:,2);?
? ? ? ? xc=find(strcmp(ctype,atype));
? ? ? ? if ~isempty(xc) ? ? ??
? ? ? ? ? ? onedaystock=onedayinfo02(xc,:); %利用這個onedaystock 找到所有當日收益率,列值
? ? ? ? ? ? aa= cell2mat(onedaystock(:,5));%xday ?從rateTable 可以找到相應的收益率,然后同股票的相乘?
? ? ? ? ? ? %kt =1;%第一只股票
? ? ? ? ? ? TsRate = 0; %當天的多股票累計收益率 ??
? ? ? ? ? ? for kt=1:length(aa)
? ? ? ? ? ? ? ? yd= find(strcmp(instrumentCodeUni,onedaystock{kt,3})); ?
? ? ? ? ? ? ? ? if ~isempty(yd) %有些股票不在Rate.mat表中
? ? ? ? ? ? ? ? ? ? sRate=rateTable(xdx,yd); %ook
? ? ? ? ? ? ? ? ? ? if isnan(sRate)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? TsRate=TsRate+sRate*onedaystock{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end % if ~isempty(yd)
? ? ? ? ? ? end
? ? ? ? ? ? dayStockValue= sum(aa(:,1)); %股票就全加一下子,基金判斷一下再加 ??
? ? ? ? end %length(xc)~=0?
? ? ? ??
? ? ? ?
%% ? ? 期貨指數類,收益率
? ? ? ? btype = '期貨'; %多開和空開要相減
? ? ? ? ctype = onedayinfo02(:,2);?
? ? ? ? xd1=find(strcmp(ctype,btype));
? ? ? ? dayfdd=0;
? ? ? ? dayfkk=0;
? ? ? ? dayRateSf =0;
? ? ? ? if ~isempty(xd1) %判斷xd 是否存在
? ? ? ? ? ? onedayfuture=onedayinfo02(xd1,:);
? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? ddRate=ddRate+sRate*onedayfuturedd{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? ddRate=ddRate+sRate*onedayfuturedd{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? ? ? sRate =0; %如果當天沒有數據,當天的收益率就是 0;
? ? ? ? ? ? ? ? ? ? ? ? kkRate=kkRate+sRate*onedayfuturekk{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? kkRate=kkRate+sRate*onedayfuturekk{kt,5}/Funddayinfo{1,3};
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? dayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? end
? ? ? ? ? ? dayRateSf = kkRate -ddRate;
? ? ? ? ? ??
%% ? ? ? 期貨收益率計算,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?
? ? ? ? ? ?xih=find(strncmp(onedayfuture(:,3),'IH',2));
? ? ? ? ? ?ihvalue =0;
? ? ? ? ? ?if ~isempty(xih)
? ? ? ? ? ? ? ? onedayfutureih=onedayfuture(xih,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureih(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? ihdayfdd =0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureih(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? ihdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureih(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? ihdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureih(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? ihdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ihvalue =ihdayfdd-ihdayfkk;
? ? ? ? ? ?end
? ??
? ? ? ? ? ?xic=find(strncmp(onedayfuture(:,3),'IC',2));
? ? ? ? ? ?icvalue =0;
? ? ? ? ? ?if xic
? ? ? ? ? ? ? ? onedayfutureic=onedayfuture(xic,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureic(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? icdayfdd = 0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureic(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? icdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureic(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? icdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureic(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? icdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? icvalue =icdayfdd-icdayfkk;
? ? ? ? ? ?end
? ? ? ? ? ?
? ? ? ? ? ?xif=find(strncmp(onedayfuture(:,3),'IF',2));
? ? ? ? ? ?ifvalue =0;
? ? ? ? ? ?if xif
? ? ? ? ? ? ? ? onedayfutureif=onedayfuture(xif,:);
? ? ? ? ? ? ? ? %找到多開 - 空開
? ? ? ? ? ? ? ? ftype = '多開';
? ? ? ? ? ? ? ? ftype01 = '買入';
? ? ? ? ? ? ? ? htype = onedayfutureif(:,6);
? ? ? ? ? ? ? ? xe=find(strcmp(htype,ftype)|strcmp(htype,ftype01));
? ? ? ? ? ? ? ? ifdayfdd =0;
? ? ? ? ? ? ? ? if ~isempty(xe)
? ? ? ? ? ? ? ? ? ? onedayfuturedd=onedayfutureif(xe,:);
? ? ? ? ? ? ? ? ? ? a1= cell2mat(onedayfuturedd(:,5));
? ? ? ? ? ? ? ? ? ? ifdayfdd= sum(a1(:,1)); %基金多開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? gtype = '空開';
? ? ? ? ? ? ? ? gtype01 = '賣出';
? ? ? ? ? ? ? ? jtype = onedayfutureif(:,6);
? ? ? ? ? ? ? ? xr=find(strcmp(jtype,gtype)|strcmp(jtype,gtype01));
? ? ? ? ? ? ? ? ifdayfkk =0;
? ? ? ? ? ? ? ? if ~isempty(xr)
? ? ? ? ? ? ? ? ? ? onedayfuturekk=onedayfutureif(xr,:);
? ? ? ? ? ? ? ? ? ? a2= cell2mat(onedayfuturekk(:,5));
? ? ? ? ? ? ? ? ? ? ifdayfkk= sum(a2(:,1)); %基金空開累加
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ifvalue =ifdayfdd-ifdayfkk;
? ? ? ? ? ?end
? ? ? ? ? ?IIIrate= TFRate*ihvalue + TCRate*icvalue + THRate*ifvalue;
? ? ? ? ? ? ?
%% ? ? ? ? ? ? ??
? ? ? ? else
? ? ? ? ? ? dayfdd=0;
? ? ? ? ? ? dayfkk=0;
? ? ? ? ? ? IIIrate =0; % 代表了指數收益
? ? ? ? ?end
? ? ? ? tstockRate = dayStockValue/Funddayinfo{1,3};
? ? ? ? tfutureRate = (dayfdd-dayfkk)/Funddayinfo{1,3};
? ??
? ? end
? ??
? ? infodata6{j,1}=xday;
? ? infodata6{j,2}=tstockRate;
? ? infodata6{j,3}=tfutureRate;
? ? infodata6{j,4}=TsRate;
? ? infodata6{j,5}=IIIrate;
? ? infodata6{j,6}=dayRateSf;
? ? ? ??
end ?
save('infodata6.mat','infodata6');
總結
- 上一篇: 高精度文字识别工具,图片转文字工具免费版
- 下一篇: 光耦w314的各引脚图_P184光耦电路