截面持仓数据

来源:互联网 发布:准星软件 编辑:程序博客网 时间:2024/05/01 04:37
%20160907 截面持仓数据 要求,第一股票部分,不包含基金和港股
clear;clc
tic
format long
%% 数据拉取,全部都拉进来就可以了,到时候有交互运算
[data_fund,textdata_fund,raw_fund]=xlsread('实际持仓截面20160906.xlsx','Sheet1');
%取得占比数据。 raw_fund从第二列开始2-533,股票,534-542期货,543-552 商品




ri = 1; %第一列
rt=2;
%% 保留‘600’、‘601’、‘603’、‘000’、‘002’和‘300’打头的股票部分
instrumentCodeAll=raw_fund(:,ri); 
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)); %这个是找到x的位置数值
stock=raw_fund(x,rt);  




%%  期货部分
futuresCodeAll=raw_fund(:,ri); 
y=find(strncmp(futuresCodeAll,'IC',2)|strncmp(futuresCodeAll,'IF',2)...
              |strncmp(futuresCodeAll,'IH',2));
futures=raw_fund(y,rt);  




%% 商品期货
CommodityCodeAll=raw_fund(:,ri); 
y=find(strncmp(CommodityCodeAll,'a1',2)|strncmp(CommodityCodeAll,'i1',2)...
              |strncmp(CommodityCodeAll,'j1',2)|strncmp(CommodityCodeAll,'jm',2)...
              |strncmp(CommodityCodeAll,'m1',2)|strncmp(CommodityCodeAll,'rb',2)...
              |strncmp(CommodityCodeAll,'ru',2));
Commodity=raw_fund(y,rt);  




stockpop =sum(cell2mat(stock(1:end,1))); %股票占比


%寻找期货
%instrumentCodeAll=allInst(:,3); 
%x=find(strncmp(instrumentCodeAll,'IC',2)|strncmp(instrumentCodeAll,'IF',2)...
%              |strncmp(instrumentCodeAll,'IH',2)|);
futurespop =sum(cell2mat(futures(1:end,1))); %期货占比
% 期货空头
xp02 = find(cell2mat(futures)<0);
futuresshort = futures(xp02,:);
futuresshortpop = sum(cell2mat(futuresshort));
% 期货多头
xp03 = find(cell2mat(futures)>0);
futureslong = futures(xp03,:);
futureslongpop = sum(cell2mat(futureslong));


%期货镜头存
futuresNetposition = futureslongpop +futuresshortpop;




%寻找商品
Commoditypop =sum(cell2mat(Commodity(1:end,1))); %商品占比
% 商品空头
xp02 = find(cell2mat(Commodity)<0);
Commodityshort = Commodity(xp02,:);
Commodityshortpop = sum(cell2mat(Commodityshort));
% 商品多头
xp03 = find(cell2mat(Commodity)>0);
Commoditylong = Commodity(xp03,:);
Commoditylongpop = sum(cell2mat(Commoditylong));


%净头寸占比  股票占比+ 期货占比 因为把基金去掉,所以净值变成负数了,少了很多
Netposition = stockpop+futureslongpop+futuresshortpop+Commodityshortpop+Commoditylongpop;






%% 大小盘
stockCode=raw_fund(x,1);  %存储所有股票代码 
instStr01='';
for i=1:1:length(stockCode)
    if i~=length(stockCode)
        str=sprintf('%s,',stockCode{i});
    else
        str=stockCode{i};    
    end
        instStr01=strcat(instStr01,str);
end
%注意此处使用wss 为什么不是wsd
%w=windmatlab;
%stockValue =w.wss(instStr01,'windcode,ev,indexcode_sw,sec_name','2016-06-21','2016-06-21','industryType=1');
%save('stockValue.mat','stockValue');
load('stockValue.mat');


value01 = stockValue(:,2);
aa =value01{1,1};
m12 = length(stockValue);
axiaopan= 0;        % 小于 10 0000 0000 小盘
azhongxiao= 0;      % 10 0000 0000 ----- 50 0000 0000 中小盘
azhongpan=0;        % 50 0000 0000 ----- 100 0000 0000 中盘
adapan=0;           % 100 0000 0000 -----500 0000 0000  大盘
achaodapan=0;       % 500亿  500 0000 0000 超大盘
for i=1:m12
    if value01{i,1}<1000000000
        fprintf('小盘 ');
        axiaopan=axiaopan+value01{i,1};
    elseif  value01{i,1}>1000000000 && value01{i,1}<5000000000
        fprintf('中小盘 ');
        azhongxiao=azhongxiao+value01{i,1};
    elseif  value01{i,1}>5000000000 && value01{i,1}<10000000000
        fprintf('中盘 ');
        azhongpan=azhongpan+value01{i,1};
    elseif  value01{i,1}>10000000000 && value01{i,1}<50000000000    
        fprintf('大盘 ');
        adapan=adapan+value01{i,1};
    elseif  value01{i,1}>50000000000       
        fprintf('超大盘 ');
        achaodapan=achaodapan+value01{i,1};
    end    
end    


allpan = axiaopan+azhongxiao+azhongpan+adapan+achaodapan;
bxiaopanpop = 100*axiaopan/allpan;
bzhongxiaopop = 100*azhongxiao/allpan;
bzhongpanpop = 100*azhongpan/allpan;
bdapanpop = 100*adapan/allpan;
bchaodapanpop = 100*achaodapan/allpan;
%%  行业占比,行业占比 前十名
%b=sortrows(a,-3)
%将矩阵a按照第3列的降序排列,其他列将跟随着第3列 的变化,保持一一对应。
%sortrows(data,1) 将矩阵a按照第1列的升序排列
allinfo(:,1)= stockValue(:,4);    % 名称
allinfo(:,2)= stockValue(:,2);    % 市值
allinfo(:,3)= stock(:,1);         % 占比


%% 行业占比 前十名
allinfoTen= sortrows(allinfo,-3);
%save('allinfoTen.mat','allinfoTen');
load('allinfoTen.mat');
%%


%swdata 是深万的指数,应该用中证的指数。这里面有申万的行业代码。
[data_hangye,textdata_hangye,raw_hangye]=xlsread('行业权重.xlsx','Sheet1');


% data_fund 股票权重占比,stockValue 同
% textdata_hangye 对比这个进行行业权重计算
k= length(stockValue);
fundweight=[];
weight= 0;
for i= 1:28
    j = i+1; %2-29
    raw = textdata_hangye{j,1};   
    for p =1:k
        if find(strcmp(raw,stockValue{p,3}))
           pp = p+1; %下移一行
           data_fund01 = data_fund(:,1);
           weight = weight+ data_fund01(pp,1); %第一列之中
        end
    end
    fundweight{i,1}=weight;
    weight= 0;
end




%中证800 的拉取,在wset下面,找到指数权重,并将wind下面的名称改为 中证800的,确定当日日期。
[w_wset_data,w_wset_codes,w_wset_fields,w_wset_times,w_wset_errorid,w_wset_reqid]=w.wset('indexconstituent','date=2016-06-21;windcode=000906.SH')
%通过已经有的代码和数据,拉取行业代码 申万的行业代码


tradeCode=w_wset_data(:,2);  %存储中证800的股票代码 
instStr02='';
for i=1:1:length(tradeCode)
    if i~=length(tradeCode)
        str=sprintf('%s,',tradeCode{i});
    else
        str=tradeCode{i};    
    end
        instStr02=strcat(instStr02,str);
end
%注意此处使用wss 为什么不是wsd
w=windmatlab;
%indexValue =w.wss(instStr02,'windcode,ev,indexcode_sw,sec_name','2016-06-21','2016-06-21','industryType=1');
%save('indexValue.mat','indexValue');
load('indexValue.mat');


k01= length(indexValue);
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,indexValue{p,3}))
           data_fund01 = w_wset_data(:,4);
           aa03 = data_fund01{p,1};
           if ~isnan(aa03)
              weight = weight+ aa03; %第一列之中
           end  
        end
    end
    indexweight{i,1}=weight;
    weight= 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'};
dateStr='20151222';  %日期,可随时调整
dateEnd='20160621'; 
instStr='';
for i=1:1:length(swCode)
    if i~=length(swCode)
    str=sprintf('%s,',swCode{i});
    else
    str=swCode{i};    
    end
    instStr=strcat(instStr,str);    
end
%w=windmatlab;


%[w_wsd_data,w_wsd_codes,w_wsd_fields,w_wsd_times,w_wsd_errorid,w_wsd_reqid]...
% =w.wsd(instStr,'pct_chg',dateStr,dateEnd);%不用收盘价close,用涨跌幅'pct_chg'
%swRate=w_wsd_data/100;  %获得申万一级行业的收益率序列
%save('swRate.mat','swRate');
load('swRate.mat');




%求28个行业的协方差个矩阵
%将深万指数转化为收益序列,然后用收益序列求得协方差,左侧第一行和第二行已经求得,目前就是利用协方差求得右侧的第一行和第二行
data_all = swData;%
[ms,ns] = size(data_all);
for k=1:ns
    TotalData = data_all(:,k); %净值序列
    TRG01(:,k) = tick2ret(TotalData); %收益率序列
end 
%求得协方差
for a=1:28
    for b=1:28
        R02{a,b} = corr(TRG01(:,a),TRG01(:,b));
    end
end


%用这个第一列乘以 这个R02然后,用这个乘以第一列的转置,求出每个数值就可以了。
%跟dataw 进行交互运算就可以了。




%求基金风险占比
for k=1:28
    aa=0;
    for i=1:28
        aa=aa + fundweight{i,1}*R02{i,k} %第一次转置相乘
    end
    dataA{k,1}=aa
end


for t=1:28
    j=t+1;
    phangye{t,1}=dataA{t,1}*dataw{2,j} %第二次转置相乘
end 


%求中证指数风险占比
for k=1:28
    aa=0;
    for i=1:28
        j=i+1;
        aa=aa + dataw{3,j}*R02{i,k} %第一次转置相乘
    end
    dataA{k,1}=aa
end


for t=1:28
    j=t+1;
    pindex{t,1}=dataA{t,1}*dataw{3,j}
end









0 0
原创粉丝点击