clear;clc
tic
%% 择时 配置 选股
%{
选择wind A 为基准因为,包含了全部28个行业
拆分出超额收益,
择时是净暴露仓位,择时是判断 Pn
超额收益 Rs- Rb 择时体现在beta之中
alpha + beta 就是超额收益
A 是万德全A收益率 ,Po 是设定的仓位是基准仓位
(情况1 只有多空时)超额利润
= Ps*rs - Po*A
= Ps*rs -Ps*A+ Ps*A- Po*A
= Ps(rs-A)+(Ps-Po)*a
%(Ps-Po)*a 代表择时能力
rs = Ws*Rs - Wi*Ri
Rs代表行业收益
Wi*Ri 申万 每个行业权重,每个行业收益
Wf*rf - Wi*Ri
(Wf*rf -Wf*Ri)+( Wf*Ri- Wi*Ri)
Wf*(rf -Ri)+ (Wf - Wi)*Ri
Wf*(rf -Ri) 选股能力
(Wf - Wi)*Ri 配置能力
2.0 用沪深300 为基准 ,则权重,但是可用申万的编制规则,将300 分到申万的28个桶之中,其中 Ri 可以重新计算
3.0 以对冲为主要
Ps*rs - Ph*ri = alpha - beta
= Ps*rs - Ph*ri (-Po*ri1)
= Ph(rs- ri)+Pn*rs(-Po*ri1)
= Ph(Wf*rf - Wi*Ri)+Pn*rs(-Po*ri1)
Ph(Wf*rf - Wi*Ri) % 包含了择时和选股能力
Pn*rs(-Po*ri1)
Pn*rs - Pn*ri1 + Pn*ri1 - Po*ri1
Pn(rs - ri1) + (Pn- Po)*ri1
(Pn- Po)*ri1 体现了择时能力
Pn(rs - ri1)近似为Pn(rs - ri) 如果没有对冲,就看股票的范围。
其中 ri1就是上面的ri 为的是区别而写出来
中证 800 就是中证300 加中证500
重点 (1)万德全 A (2)中证300 (3)支持指数复权
%}
%% 母基金名称
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
end
%end
%其中alldayinfo 中包含了 子基金 y一种全部信息,说有所有这些天的
load('index_constituent.mat')
load('C:\Users\liangtianxin\Desktop\liangtianxin\newtask\picktime\swRate.mat')
load('C:\Users\liangtianxin\Desktop\liangtianxin\newtask\picktime\data0829\datafunda0001.mat')
load('C:\Users\liangtianxin\Desktop\liangtianxin\newtask\picktime\data0829\Rate.mat')
%%
%多空
% Ps*(RS- A)
% PS*(WF*RF-WF*RI+WF*RI-WI*RI) WI是计算出来的 RI 是拉取出来的。
%Ps*WF*(RF-RI) 是选股能力 算式是PS1*WF1*(RF1-RI1) 然后进行累加 28行业
%其中 WFI = T/F, 其中T 子基金中在某个行业下全部股票的市值,F 是子基金股票方面的市值
%其中RF1 是个组合收益率R1*A/T+R2*B/T+R3*C/T+....的
% WI 是通过计算得来的,通过index_weight 这个表
%通过index_weight构建一个行业包含表,28行,然后用行业包含的28同每个日股票做一个交集运算,然后计算学会用交集运算
%stockName 是名称 全部股票
%stockRate 是收益率 全部股票
%行业代码
swCode={ '801010.SI','801020.SI','801030.SI','801040.SI','801050.SI','801080.SI','801110.SI','801120.SI','801130.SI',...
'801140.SI','801150.SI','801160.SI','801170.SI','801180.SI','801200.SI','801210.SI','801230.SI','801710.SI',...
'801720.SI','801730.SI','801740.SI','801750.SI','801760.SI','801770.SI','801780.SI','801790.SI','801880.SI','801890.SI'};
%用来求得行业的收益率
sw= length(swCode);
newSW=[];
newSW= swCode;
for i=1:sw
sw01= swCode{i};
ctype = index_weight(:,5);
xc=find(strcmp(ctype,swCode{i}));
if ~isempty(xc)
onedaycode=index_weight(xc,:);
%onedaycode 第二列 很有用,放到一个数组表中
b = length(onedaycode);
for j = 1:b
p=j+1;
newSW{p,i}= onedaycode{j,2};
end
end
end
%其中 newSW 就是股票归属列表。
%[apm,apn]= size(newSW);
% j= 100 ;
t= 4; %第t行业中包含了几只股票
newSW01=cellfun(@isempty,newSW(:,t));
newxv=find(newSW01,'0');
newp = newxv(1); %得出最后一位数目
newS= newp-1;
ap01 = [];
ap01= newSW(1:newS,t);
trade = intersect(ap01,onedaystock(:,3)); %第t行业中包含了几只股票
%index_weight 中包含行业权重和 需要用行业权重求指数权重
%end
%[w_wsd_data]=w.wsd('300364.SZ','ev,indexcode_wind','2016-06-21','2016-06-21','industryType=1')
%%
%{
[data_hangye,textdata_hangye,raw_hangye]=xlsread('行业权重.xlsx','Sheet1');
load('index_weight.mat');
k01= length(index_weight);
indexweight=[];
weight= 0;
for i= 1:28
j = i+1; %2-29
raw = textdata_hangye{j,1};
for p =1:k01
if find(strcmp(raw,index_weight{p,5}))
data_fund01 = index_weight(:,4);
aavalue = data_fund01{p,1};
if ~isnan(aavalue)
weight = weight+ aavalue; %第一列之中
end
end
end
indexweight{i,1}=weight;
weight= 0;
end
fundNameWeight(:,1)=textdata_hangye(2:end,1);
aa=0;
for i= 1:28
fundNameWeight{i,2}=indexweight{i,1};
aa= aa + indexweight{i,1};
end
save('fundNameWeight.mat','fundNameWeight');
%}
load('fundNameWeight.mat');