动量策略&反转策略初探sas

MBAlib百科对动量策略和掉换策略作了大好的解说:

动量/掉换策略是价格看涨而买入赢家/输家结成。,同时,吹嘘过度者/赢家结成的市策略。首要过程如次:

  ①决定目的证券便宜货作为市宾语的漫游。

  选择一任一某一时间一定尺寸的作为保释金业绩评价时间,结构期或结构期,通常高气压装饰结成。。

  范本保释金在结构期的报酬率。

  (4)由于…的范本保释金的报酬率的一定尺寸的,目的义卖中自己的事物范本保释金的继承挨次、降序达成协议达成协议,于是掉进专有的组,一组称为赢家结成的最大投降,回转最小的一组称为输家结成。

  或反省一段时间后,拿一段时间,便宜货赢家和输家的限期。

  延续或停顿,不竭反复 一任一某一行动。。

  ⑦业绩评价。每个持有期报酬率的动量/掉换策略计算,假使T统计资料显示,动量/掉换策略报酬率的意思,业界称动量/掉换策略成,学会称之为动量/掉换的在。,不同的亦反。

我尝试应用SAS分步使完满开刀过程:国际A股的全部的漫游,P周结构期说,同盟国用桩区分的时间是在问周表达,会有P,Q都简略的取1,2,3。后续t受试验有待使完善。

在这里test记载系包括2000多只A股的日线记载。

* 考验证券体系的日常记载,选择2009-2012年度日常记载
data test1;
   set test;
       year=年份(日期);
       month=month(date);
       week=week(date);
   keep id date year month week close ; 
   if    2009<=year<=2012 then output;
run;
*计算两年期间证券的日线记载一定尺寸的,剔除较短记载;
proc freq data=test1 noprint ;
   table id /out=test2(keep=id count)   ;
run;
data test3;
   merge test1 test2;
   by id;
   if count<360  then delete ;
   if id eq 滞后(ID)于是 do;
   ret_1=log(close)-log(lag(close));
   end;
run;

proc sort data=test3;by id;run;
*计算每支证券的周累计进项;
proc sql;
   create table test4 as
      select *,
	         sum(ret_1) as sum_ret
		 from test3	
		 group by id,year,month,week;
quit;
*计算日均报酬率,对其停止排序,进而对2000多只证券停止分组;
proc sql;
   create table test5 as
      select distinct id,year,month,week,sum_ret
	     from test4;
quit;


*选定一任一某一考验时点,例如以2011年1月第一周开始,找到该时点前p周到记载,
进而按照前P周到累计报酬率停止排序,在这里p取1,2,3;
data test6;
   set test5;
   if id eq lag(id) then do;
      ret_lag1=lag(sum_ret);
      ret_lag2=lag(sum_ret)+lag2(sum_ret);
      ret_lag3=ret_lag2+lag3(sum_ret);
   end;
run;
*考虑lag函数产生缺失值的问题,我们选取观测时间点位2011年1月第一周;
data test7;
   set test6;
   if year=2011 & month=1 & week=1 then output;
run;

*对观测时间点停止前P周到累计进项停止排序:升序:轮转策略;降序达成协议:动量策略;
%macro rank(num);
%do i=1 %to #
proc rank data=test7 out=test7_&i group=14;
   var ret_lag&i;
   ranks group;
run;
data test_dong&i test_fan&i;
   set test7_&i;
   if group=0 then output test_fan&i;
   if group=13 then output test_dong&i;
run;

*从待选证券中选出动量结成证券,从观测日开始,计算持有结成内证券Q周的累计进项;

data new;
   set test6;
   if year<2011 then delete;
run;
*计算动量策略结构期为1周,持有期为1,2,3周到进项,同理可以计算
结构期为2/3周到动量策略;


data merge_d&i;
   merge test_dong&i(in=a) new(in=b);
   by id;
   if  a=1 & b=1 then output;
run;
proc expand data=merge_d&i out=d_&i;
   convert ret_lag1=ret1 / transform=(lead 2);
   convert ret_lag2=ret2 / transform=(lead 3);
   convert ret_lag3=ret3 / transform=(lead 4);
run;
data d_&i;
   set d_&i;
   if year=2011 & month=1 & week=1 then output;
run;

%end;
%mend;
%rank(3);

试着到达是你这么说的嘛!编码和上海和深圳300的结成。,反省报酬率。上面需求计算确切的时间量子的沪深300目录报酬率,再差的关系上地。上海和深圳300目录记载需先下载。

对上海和深圳300目录报酬率的计算
data sz399300_1;
   set sz399300;
   year=year(var1);
   month=month(var1);
   week=week(var1);
   ret=dif(log(var5));
   drop var: ;
run;
   
proc sql;
   create table sz399300_2 as
      select distinct year,month,week,
	                  和(RET) as sum_ret
	     from sz399300_1
		 group by year,month,week ;
quit;

data sz399300_3;
   set sz399300_2;
   ret_lag1=lag(sum_ret);
   ret_lag2=lag(sum_ret)+lag2(sum_ret);
   ret_lag3=ret_lag2+lag3(sum_ret);
run;

proc expand data=sz399300_3 out=sz399300_4;
   convert ret_lag1=ret1 / transform=(lead 2);
   convert ret_lag2=ret2 / transform=(lead 3);
   convert ret_lag3=ret3 / transform=(lead 4);
run;
data sz399300_4;
   set sz399300_4;
   if year=2011 & month=1 & week=1 then output;
run;


相抵上海,深圳300,合成的进项
%macro diff_hs(num);
%do i= 1 %to #
proc sql;
   create table d&i_need as
      select distinct a.year,a.month,a.week,
	         mean() as ret1,
			 mean() as ret2,
			 mean() as ret3
	  from d_&i as a
	  join sz399300_4 as b
	  on a.year=b.year;
quit;

proc sql;
   create table f&i_need as
      select distinct a.year,a.month,a.week,
	         mean() as ret1,
			 mean() as ret2,
			 mean() as ret3
	  from f_&i as a
	  join sz399300_4 as b
	  on a.year=b.year;
quit;

%end;
%mend;

%diff_hs(3);

结构和保持不变期的结成(P,Q)编码在一任一某一月内表达:

libname yu E:TMP
* 考验证券体系的日常记载,选择2009-2012年度日常记载
data test1;
   set YU.test;
       year=年份(日期);
       month=month(date);
   keep id date year month  close ; 
   if    2009<=year<=2012 then output;
run;
*计算两年期间证券的日线记载一定尺寸的,剔除较短记载;
proc freq data=test1 noprint ;
   table id /out=test2(keep=id count)   ;
run;
data test3;
   merge test1 test2;
   by id;
   if count<360  then delete ;
   if id eq 滞后(ID)于是 do;
   ret_1=log(close)-log(lag(close));
   end;
run;

proc sort data=test3;by id;run;
*计算每支证券的月累计进项;
proc sql;
   create table test4 as
      select distinct id,year,month,
	         sum(ret_1) as sum_ret
		 from test3	
		 group by id,year,month;
quit;

*选定一任一某一考验时点,例如以2011年1月1日开始,找到该时点前p月到记载,
进而按照前P月到累计报酬率停止排序,在这里p取1,2,3,4,5,6;
data test5;
   set test4;
   if id eq lag(id) then do;
      ret_lag1=lag(sum_ret);
      ret_lag2=lag(sum_ret)+lag2(sum_ret);
      ret_lag3=ret_lag2+lag3(sum_ret);
	  ret_lag4=ret_lag3+lag4(sum_ret);
	  ret_lag5=ret_lag4+lag5(sum_ret);
	  ret_lag6=ret_lag5+lag6(sum_ret);
   end;
run;

*考虑lag函数产生缺失值的问题,我们选取观测时间点位2011年1月第一周;
data test6;
   set test5;
   if year=2011 & month=10  then output;
run;

*对观测时间点停止前P周到累计进项停止排序:升序:轮转策略;降序达成协议:动量策略;
%macro rank(num);
%do i=1 %to #
proc rank data=test6 out=test6_&i group=14;
   var ret_lag&i;
   ranks group;
run;
data test_dong&i test_fan&i;
   set test6_&i;
   if group=0 then output test_fan&i;
   if group=13 then output test_dong&i;
run;

*从待选证券中选出动量结成证券,从观测日开始,计算持有结成内证券Q周的累计进项;

data new;
   set test6;
   if year<2011 then delete;
run;
*计算动量策略结构期为1周,持有期为1,2,3周到进项,同理可以计算
结构期为2/3周到动量策略;


data merge_d&i;
   merge test_dong&i(in=a) new(in=b);
   by id;
   if  a=1 & b=1 then output;
run;
proc expand data=merge_d&i out=d_&i;
   convert ret_lag1=ret1 / transform=(lead 2);
   convert ret_lag2=ret2 / transform=(lead 3);
   convert ret_lag3=ret3 / transform=(lead 4);
   convert ret_lag4=ret4 / transform=(lead 5);
   convert ret_lag5=ret5 / transform=(lead 6);
   convert ret_lag6=ret6 / transform=(lead 7);
run;
data d_&i;
   set d_&i;
   if year=2011 & month=10  then output;
run;

data merge_f&i;
   merge test_fan&i(in=a) new(in=b);
   by id;
   if  a=1 & b=1 then output;
run;
proc expand data=merge_f&i out=f_&i;
   convert ret_lag1=ret1 / transform=(lead 2);
   convert ret_lag2=ret2 / transform=(lead 3);
   convert ret_lag3=ret3 / transform=(lead 4);
   convert ret_lag4=ret4 / transform=(lead 5);
   convert ret_lag5=ret5 / transform=(lead 6);
   convert ret_lag6=ret6 / transform=(lead 7);
run;
data f_&i;
   set f_&i;
   if year=2011 & month=10  then output;
run;
%end;
%mend;
%rank(6);

对上海和深圳300目录报酬率的计算
data sz399300_1;
   set YU.sz399300;
   year=year(var1);
   month=month(var1);
   ret=dif(log(var5));
   drop var: ;
run;
   
proc sql;
   create table sz399300_2 as
      select distinct year,month,
	                  和(RET) as sum_ret
	     from sz399300_1
		 group by year,month ;
quit;

data sz399300_3;
   set sz399300_2;
   ret_lag1=lag(sum_ret);
   ret_lag2=lag(sum_ret)+lag2(sum_ret);
   ret_lag3=ret_lag2+lag3(sum_ret);
   ret_lag4=ret_lag3+lag4(sum_ret);
   ret_lag5=ret_lag4+lag5(sum_ret);
   ret_lag6=ret_lag5+lag6(sum_ret);
run;

proc expand data=sz399300_3 out=sz399300_4;
   convert ret_lag1=ret1 / transform=(lead 2);
   convert ret_lag2=ret2 / transform=(lead 3);
   convert ret_lag3=ret3 / transform=(lead 4);
   convert ret_lag4=ret4 / transform=(lead 5);
   convert ret_lag5=ret5 / transform=(lead 6);
   convert ret_lag6=ret6 / transform=(lead 7);
run;
data sz399300_4;
   set sz399300_4;
   if year=2011 & month=10  then output;
run;


%macro diff_hs(num);
%do i= 1 %to #
proc sql;
   create table d_need&i as
      select distinct a.year,a.month,
	         mean() as ret1,
			 mean() as ret2,
			 mean() as ret3,
	         mean() as ret4,
			 mean() as ret5,
			 mean() as ret6
	  from d_&i as a
	  join sz399300_4 as b
	  on a.year=b.year;
quit;

proc sql;
   create table f_need&i as
      select distinct a.year,a.month,
	         mean() as ret1,
			 mean() as ret2,
			 mean() as ret3,
			 mean() as ret4,
			 mean() as ret5,
			 mean() as ret6
	  from f_&i as a
	  join sz399300_4 as b
	  on a.year=b.year;
quit;

%end;
%mend;

%diff_hs(6);


data final_d9; 
   set d_need:;
run;
data final_f9;
   set f_need:;
run;
proc datasets library=work;
   delete d_: f_: merge_: Sz399300: test: new;
run;
quit;

假使每天都停止考验,会较体贴的改建:

E:\发烧\考验国际A股记载,sz399300上海和深圳300目录记载部
libname gao e
data test1;
   set gao.test;
   if id eq 滞后(ID)于是 do;
      ret = DIF(关)/滞后(合拢)
   end;
   if 年份(日期)<2009 then delete; 
   keep id date ret close;
run;

*计算两年期间证券的日线记载一定尺寸的,剔除较短记载;
proc freq data=test1 noprint ;
   table id /out=test2(keep=id count)   ;
run;
data test3;
   merge test1 test2;
   by id;
   if count<360  then delete ;
run;

*选定一任一某一考验时点,例如以2011年1月5日,计算前P日累计报酬率并停止排序,
在这里p取21,42;
proc expand data=test3 out=test4 method=none;
   by id;
   convert ret =ret_lag1 / transformout=(movsum 21);
   convert ret =ret_lag2 / transformout=(movsum 42);   
run;
*选定考验日;
data test5;
   set test4;
   if date="05jan2012"d then output;
run;

*以05jan2011日前21/42日累计报酬率停止排序,生产动量结成和掉换结成;
%macro rank(num);
%do i=1 %to #
proc rank data=test5 out=test5_&i group=10;
   var ret_lag&i;
   ranks group;
run;
data test_dong&i test_fan&i;
   set test5_&i;
   if group=0 then output test_fan&i;
   if group=9 then output test_dong&i;
run;

*从待选证券中选出动量结成证券,从观测日开始,计算持有结成内证券Q日的累
计进项;

data new;
   set test4;
   if date<"05jan2012"d then delete;
run;
*计算动量策略结构期为1,持有期为1,2,3进项,同理可以计算
结构期为2/3到动量策略;

data merge_d&i;
   merge test_dong&i(in=a) new(in=b);
   by id;
   if  a=1 & b=1 then output;
run;
proc expand data=merge_d&i out=d_&i;
   convert ret_lag1=ret1 / transform=(lead 21);
   convert close=close21 / transform=(lead 21);
   convert ret_lag2=ret2 / transform=(lead 42);
   convert close=close42 / transform=(lead 42);

run;

data d_&i;
   set d_&i;
   if date="05jan2012"d  then output;
run;

data merge_f&i;
   merge test_fan&i(in=a) new(in=b);
   by id;
   if  a=1 & b=1 then output;
run;
proc expand data=merge_f&i out=f_&i;
   convert ret_lag1=ret1 / transform=(lead 21);
   convert close=close21 / transform=(lead 21);
   convert ret_lag2=ret2 / transform=(lead 42);
   convert close=close42 / transform=(lead 42);
run;
data f_&i;
   set f_&i;
   if date="05jan2012"d then output;
run;
%end;
%mend;
%rank(2);


对上海和深圳300目录报酬率的计算

data sz399300_1;
   set gao.sz399300(rename=(var1=date var5=close));
   ret = DIF(关)/滞后(合拢)
   drop var: ;
run;

proc expand data=sz399300_1 out=sz399300_2 method=none;
   convert ret =ret_lag1 / transformout=(sum 21);
   convert ret =ret_lag2 / transformout=(sum 42); 
run;

proc expand data=sz399300_2 out=sz399300_3;
   convert ret_lag1=ret1 / transform=(lead 21);
   convert close=close21 / transform=(lead 21);
   convert ret_lag2=ret2 / transform=(lead 42);
   convert close=close42 / transform=(lead 42);
run;
data sz399300_4;
   set sz399300_3;
   c0=close*300;
   c21=close21*300;
   C42=close42*300;
   if date="05jan2012"d  then output;
run;


%macro diff_hs(num);
%do i= 1 %to #
proc sql;
   create table d_need&i as
      select distinct a.date,
	         mean() as ret1,
			 mean() as ret2,
            (sum((ceil()/)*21) -c21)/c0 as r21,	
            (sum((ceil()/)*42) -c42)/c0 as r42

	  from d_&i as a
	  join sz399300_4 as b
	  on a.date=b.date;
quit;

proc sql;
   create table f_need&i as
      select distinct a.date,
	         mean() as ret1,
			 mean() as ret2,
            (sum((ceil()/)*21) -c21)/c0 as r21,	
            (sum((ceil()/)*42) -c42)/c0 as r42
	  from f_&i as a
	  join sz399300_4 as b
	  on a.date=b.date;
quit;

%end;
%mend;

%diff_hs(2);



data final_d1; 
   set d_need:;
run;
data final_f1;
   set f_need:;
run;

发表评论

电子邮件地址不会被公开。 必填项已用*标注