套利組合收益的節假日統計
需求:統計節假日效應:節前一週內開倉和節後一週內開倉的收益情況
其中,套利組合在開平倉訊號內,按照週一-週五分別開倉。
解決步驟:
1.資料處理
1.1匯入週一-週五組合的市值資料,組合資料包含開倉時間(start),平倉時間(end),
開倉市值(open),平倉市值(close),平倉滬深300市值(hs_close)。
PROC IMPORT OUT= WORK.e
DATAFILE= "C:\Documents and Settings\Administrator\My Docume
nts\My SAS Files\9.2\彙總表.xls"
DBMS=EXCEL REPLACE;
RANGE="週五$";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
data e;*e為週五組合資料,類似可以得到週四(d)週三(c)週二(b)週一(a)資料;
set e;
ret=(close-hs_close)/open;*ret為組合收益;
run;
......................
1.2匯入滬深300指數日線資料並計算漲幅
PROC IMPORT OUT= WORK.hs
DATAFILE= "C:\Documents and Settings\Administrator\My Docume
nts\My SAS Files\9.2\彙總表.xls"
DBMS=EXCEL REPLACE;
RANGE="sheet1$";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
data hs;
set hs;
ret1=dif(close_hs)/lag(close_hs);*ret為漲幅;
date=input(compress(start),yymmdd10.);
format date date9.;
drop start;
run;
1.3將滬深300指數資料與組合收益資料合併為一張表
%macro zhangfu(datafile);
proc sql noprint;
create table hs_&datafile as
select *
from &datafile
left join hs
on &datafile..start=hs.date ;
quit;
%mend;
%zhangfu(a);%zhangfu(b);%zhangfu(c);%zhangfu(d);%zhangfu(e);
1.4利用2010年至今資料統計節假日收益情況,首先需要明確2010-2012期間的
法定節假日情況,然後進行篩選出節假日前後開平倉的組合收益(節前7天內)
%macro holiday(datafiles);
data holiday_&datafiles ;
set &datafiles;
h_1 = '01jan2010'd ;
h_2 = '03jan2010'd ;
h_3 = '13feb2010'd ;
h_4 = '19feb2010'd ;
h_5 = '03apr2010'd ;
h_6 = '05apr2010'd ;
h_7 = '01may2010'd ;
h_8 = '03may2010'd ;
h_9 = '14jun2010'd ;
h_10= '16jun2010'd;
h_11= '22sep2010'd;
h_12= '24sep2010'd;
h_13= '01oct2010'd;
h_14= '07oct2010'd;
h_15= '01jan2011'd;
h_16= '03jan2011'd;
h_17= '02feb2011'd;
h_18= '08feb2011'd;
h_19= '03apr2011'd;
h_20= '05apr2011'd;
h_21= '30apr2011'd;
h_22= '02may2011'd;
h_23= '04jun2011'd;
h_24= '06jun2011'd;
h_25= '10sep2011'd;
h_26= '12sep2011'd;
h_27= '01oct2011'd;
h_28= '07oct2011'd;
h_29= '01jan2012'd;
h_30= '03jan2012'd;
h_31= '22jan2012'd;
h_32= '28jan2012'd;
h_33= '02apr2012'd;
h_34= '02apr2012'd;
h_35= '29apr2012'd;
h_36= '01may2012'd;
h_37= '22jun2012'd;
h_38= '24jun2012'd;
h_39= '30sep2012'd;
h_40= '07oct2012'd;
format h_: date9. ret percentn10.2;
select ;
when (abs(start - h_1) <7 ) output;
when (abs(start - h_2) <7 ) output;
when (abs(start - h_3) <7 ) output;
when (abs(start - h_4) <7 ) output;
when (abs(start - h_5) <7 ) output;
when (abs(start - h_6) <7 ) output;
when (abs(start - h_7) <7 ) output;
when (abs(start - h_8) <7 ) output;
when (abs(start - h_9) <7 ) output;
when (abs(start - h_10) <7 ) output;
when (abs(start - h_11) <7 ) output;
when (abs(start - h_12) <7 ) output;
when (abs(start - h_13) <7 ) output;
when (abs(start - h_14) <7 ) output;
when (abs(start - h_15) <7 ) output;
when (abs(start - h_16) <7 ) output;
when (abs(start - h_17) <7 ) output;
when (abs(start - h_18) <7 ) output;
when (abs(start - h_19) <7 ) output;
when (abs(start - h_20) <7 ) output;
when (abs(start - h_21) <7 ) output;
when (abs(start - h_22) <7 ) output;
when (abs(start - h_23) <7 ) output;
when (abs(start - h_24) <7 ) output;
when (abs(start - h_25) <7 ) output;
when (abs(start - h_26) <7 ) output;
when (abs(start - h_27) <7 ) output;
when (abs(start - h_28) <7 ) output;
when (abs(start - h_29) <7 ) output;
when (abs(start - h_30) <7 ) output;
when (abs(start - h_31) <7 ) output;
when (abs(start - h_32) <7 ) output;
when (abs(start - h_33) <7 ) output;
when (abs(start - h_34) <7 ) output;
when (abs(start - h_35) <7 ) output;
when (abs(start - h_36) <7 ) output;
when (abs(start - h_37) <7 ) output;
when (abs(start - h_38) <7 ) output;
when (abs(start - h_39) <7 ) output;
when (abs(start - h_40) <7 ) output;
otherwise delete;
end;
keep start end ret ret1;
run;
%mend;
%holiday(hs_a);%holiday(hs_b);%holiday(hs_c);%holiday(hs_d);%holiday(hs_e);
1.4剔除節前節後的組合另存為新的資料
%macro noholiday(dataset1=,dataset2=);
proc sql noprint;
create table noholiday_&dataset1 as
select *
from &dataset1
where start not in
(select start from &dataset2);
quit;
%mend;
%noholiday(dataset1=hs_a,dataset2=holiday_hs_a);
%noholiday(dataset1=hs_b,dataset2=holiday_hs_b);
%noholiday(dataset1=hs_c,dataset2=holiday_hs_c);
%noholiday(dataset1=hs_d,dataset2=holiday_hs_d);
%noholiday(dataset1=hs_e,dataset2=holiday_hs_e);
1.5將週一、週二、週三、週四、週五的資料合併為一張表,下面是對節假日資料的合併
,noholiday(去節假日資料)hs_abcd(完整的資料)合併方法類似。
data holiday;*holiday系所有節前節後資料;
set holiday_hs_a(in=a)
holiday_hs_b(in=b)
holiday_hs_c(in=c)
holiday_hs_d(in=d)
holiday_hs_e(in=e)
;
if a=1 then group='週一';
if b=1 then group='週二';
if c=1 then group='週三';
if d=1 then group='週四';
if e=1 then group='週五';
run;
2.進行收益的描述性統計計算。
2.1proc means計算出常規統計,下面統計了節假日前後一週開倉的組合收益情況,其他
情況可以將holiday替換。
proc means data=holiday sum mean std max min;
by group notsorted;
var ret;
class flag;
output out=holiday1;
run;
2.2統計組合收益分佈比例情況
data test;
set holiday;
by end notsorted;
if first.end then cnt=1;
else cnt+1;
run;
proc format;
value ret
low - -0.05 = '< -5% '
-0.05 - -0.02 = '-5% ~ -2%'
-0.02 - 0 = '-2% ~ 0% '
0 - 0.02 = ' 0% ~ 2% '
0.02 - 0.05 = ' 2% ~5% '
0.05 - 0.10 = ' 5% ~10% '
other= ' >10% '
;
proc freq data=test;
tables cnt*ret ;
format ret ret.;
ods output CrossTabFreqs=need;
run;