择时 配置 选股 2016-9-12 图形正确


%%
%{
only hate road when you miss home
Ps  股票占比是第二列
Psf 股指期货占比是第三列
Rs  第四列 股票收益
Ri  第五列 指数收益,三个指数
Rsf 第六列 股指期货收益
if Ps>0, Psf<0  %有对冲 
    Pn= Ps+Psf
    if Pn>=0
    Ph = abs(Psf)
    elseif Pn<0
    Ph = abs(Ps) 
    Pn = Ps+Psf
    end
elseif  Ps>0, Psf>0  %无对冲 
    Pn =  Ps+Psf
end
%}
clear;clc
tic

load('C:\Users\lenovo\Desktop\test\alpha\data0829\datafunda0001.mat');
Ps = datafunda0001(:,2);
Psf = datafunda0001(:,3);
Rs = datafunda0001(:,4);
Ri = datafunda0001(:,5);
Rsf = datafunda0001(:,6);
for i01=1:168
    PH(i01,1) = 0;
    PN(i01,1) = 0;
    if (Ps{i01}>0&&Psf{i01}<0)
        fprintf('有对冲\n');
        if abs(Ps{i01})>abs(Psf{i01})
            PH(i01,1) = abs(Psf{i01});
            PN(i01,1) = (abs(Ps{i01})-abs(Psf{i01}));
        else %Ps{i}>Psf{i}
            PH(i01,1) = abs(Ps{i01});
            PN(i01,1) = (abs(Psf{i01})-abs(Ps{i01}));
        end    
     elseif (Ps{i01}>0&&Psf{i01}>0) %为什么没有基差呢,因为是做多,所以可能不涉及到交个问题吧,况且Ph为0 
        fprintf('无对冲\n');
        PH(i01,1) = 0;
    end
end

%% 择时 配置 选股  基本都是跟股票有关,似乎股指没有涉及,错了吧
%{
选择wind A 为基准因为,包含了全部28个行业
拆分出超额收益,
择时是净暴露仓位,择时是判断 Pn
超额收益  Rs- Rb 择时体现在beta之中
 alpha + beta 就是超额收益
A 是万德全A收益率 ,Po 是设定的仓位是基准仓位 取0.6

(情况1 只有多空时)超额利润
 = Ps*rs - Po*A
 = Ps*rs -Ps*A+ Ps*A- Po*A
 = Ps(rs-A)+(Ps-Po)*a
%(Ps-Po)*a 代表择时能力
rs = Wf*Rf - 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(rs - ri1)近似为Pn(rs - ri) 如果没有对冲,就看股票的范围。

(Pn- Po)*ri1 体现了择时能力 (Pn- Po)*Wi*Ri 
其中 ri1就是上面的ri 为的是区别而写出来
如果没有对冲的话,不一定用wind A 或者
中证 800 就是中证300 加中证500 
重点 (1)wind A (2)中证3003)支持指数复权
%}

%% 母基金名称
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);
%save('allInst.mat','allInst');

%% 保留‘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');%包含了子基金全部的股票代码和日收益率
load('swRate.mat');       %包含申万28行业代码和日收益率
%其中alldayinfo 中包含了 子基金 y一种全部信息,说有所有这些天的
load('index_constituent.mat'); %某天内的股票名称,代码 ,占比,行业代码,收益率,2016/8/26 多了三只票2908
load('C:\Users\lenovo\Desktop\test\newtask\picktime\swRate.mat');
load('C:\Users\lenovo\Desktop\test\newtask\picktime\data0829\datafunda0001.mat');
load('C:\Users\lenovo\Desktop\test\newtask\picktime\data0829\Rate.mat');
load('index_weight.mat', 'index_weight'); %同'index_constituent.mat' 一样 2016/8/25,只有2905只票

%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');
textdata_hangye 要改成 swCode
load('index_weight.mat');
k01= length(index_weight);
indexweight=[];
weight= 0;
for i= 1:28
    j = i+1; 
    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');


%%
%{
 多空
 Ps*(RS- A)
 PS*(WF*RF-WF*RI+WF*RI-WI*RI) WI是计算出来的 RI 是拉取出来的。
 Ps*WF*(RF-RI)  是选股能力 算式是PS1*WF1*(RF1-RI1) 然后进行累加 28行业
其中 WF1 = T01/F, 其中T01 子基金中在某个行业下全部股票的市值,F 是子基金股票方面的市值
其中RF1 是某个行业的组合收益率R1*A/T01+R2*B/T01+R3*C/T01+....=(R1*A+R2*B+R3*C)/T01+
的份子已经求得 因式分解 PS*((T01/F)* ((R1*A+R2*B+R3*C)/T01-RI1) )=
H01 = l累计收益率, H02 = WF1*RI1  
WI 是通过计算得来的,通过index_weight 这个表
通过index_weight构建一个行业包含表,28行,然后用行业包含的28同每个日股票做一个交集运算,然后计算学会用交集运算
stockName 是名称     全部股票
stockRate 是收益率   全部股票
%}

%%
%当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)); %某个子基金下的某日的股票和期货
    tstockProp =0;
    tfutureProp =0;
    TsRate =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); %ok
                    if ~isnan(sRate)
                       TsRate=TsRate+sRate*onedaystock{kt,5}/dayStockValue;%此处是股票总值
                    end
                end % if ~isempty(yd)
            end
        end %length(xc)~=0
    end

%行业代码,用来求得说有股票所隶属的的行业,通用
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 = 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 j01 = 1:b
            p=j01+1;                  %第一行已经被占领了
            newSW{p,i}= onedaycode{j01,2};
        end
    end
end         %其中 newSW 就是股票归属列表。


% j= 100  %要先先选 xday
%用上边的求A1 B1 单只股票的市值
%其中RF1 是个组合收益率R1*A1/T1+R2*B1/T1+R3*C/T1+....的,先选 xday 然后选择trade求出value 
%R1 在instrumentCodeUNI 中有名字,1840*1和 rateTable 168*1840 和 dayUni 168*1(xday)
%在这里面一起找
%swRate 就是指数收益率,只要找xday就可以了,再选 xf,t两个数值 ====》 RI1
% WFI1 = T01/F, 
% t 行业下子基金所包含的股票
Ht=0;
for t=1:28 
    % t= 28;                       %第t行业中包含了几只股票,28个行业
    p001 = length(newSW(:,t));     %162
    newS =0;
    for i02=1:p001
        if ~isnan(newSW{i02,t})
           newS = newS +1;
        end
    end
    ap01= newSW(1:newS,t);
    trade = intersect(ap01,onedaystock(:,3));     %第t行业中包含了几只股票
    Tvalue=0;
    if ~isempty(trade)
        tradeSize = length(trade);
        for i01=1:tradeSize
            aa01 =trade{i01,1};
            num01 = find(strcmp(onedaystock(:,3),aa01));
            Tvalue = Tvalue + onedaystock{num01,5};%将这些数值放到一个数组Tvalue01中
        end        
    end    
    % j进行累加 R1*  A1/Tvalue01
    swRt= swRate(xf,t);                %某日某行业下的收益率==RI1
    Ht= Ht +swRt*Tvalue/dayStockValue; % RI*WF
end

AI=0;
for t02=1:28
     swRt = swRate(xf,t02); %RI1
     iWeight = fundNameWeight{t02,2}/100; %WI1
     AI= AI+swRt*iWeight;
end

%% 有对冲的时候看先看PH,后看PN      
     PhValue =PH(j,1);
     selectStockph(j,1) = 0;
     if ~isnan(PhValue*(TsRate-Ht))
         selectStockph(j,1)= PhValue*(TsRate-Ht);      %wf*rf =Tsrate 选股能力 Wf(RF -RI);  Ht= Wf*RI
     end 
     ConfigurationStockph(j,1) = 0;
     if ~isnan(PhValue*(Ht-AI))
         ConfigurationStockph(j,1)=PhValue*(Ht-AI);    % 配置能力 (WF-WI )RI
     end 

%% 没有对冲的时候直接看PN   而且没有对冲的时候还要注意股票范围,这个涉及到RI 的取值范围,目前我使用的是申万的指数
%  当想要精细分析股票的时候,可能要看所选的股票是属于哪个版块的 IC IF 还是中证500 中证300等等,这就涉及到占比问题,中证占有1/3的话
%  那么中证的股票总在总指数的上是不是要占1/3 呢。
%  Pn*Rs - Pn*Ri + Pn*Ri -Po*Ri = Pn*(Rs - Ri) + (Pn- Po)*Ri 
%  Pn*(Rs - Ri) 配置&&选股  
    PnValue = PN(j,1);
    selectStockpn(j,1)= 0;
    if ~isnan(PnValue*(TsRate-Ht))
        selectStockpn(j,1)= PnValue*(TsRate-Ht);      %wf*rf =Tsrate 选股能力 Wf(RF -RI);  Ht= Wf*RI
    end    
    ConfigurationStockpn(j,1)= 0;
    if ~isnan(PnValue*(TsRate-Ht))
        ConfigurationStockpn(j,1)=PnValue*(Ht-AI);    % 配置能力 (WF-WI )RI
    end   
%  (Pn- Po)*Ri 择时能力 
    Po  = 0.6;
    PickTime(j,1) =0;
    if ~isnan(PnValue*(TsRate-Ht))
        PickTime(j,1)=(PnValue-Po)*AI;
    end

end


%{
figure;
hold on;
plot(PickTime(:,1),'g');
%}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值