动量策略&反转策略初探sas | 学步园

MBAlib百科对动量策略和加速策略作了地租的解说:

动量/加速策略指的是补进成的东西的结成。,熊者/赢家的市策略。它的次要行动是:

  决定目的的股本交易情况作为市女朋友的射程。

  其次,选择一段工夫作为表演评价期。,通常指投资额结成的长期或排序期。。

  三。计算各样品在长期的成品率。。

  (4)着陆各样品在成形期的成品率上浆。,目的交易情况中缠住范本文件的上扬、递减次序陈列,而且把它们堕入分类。,里面的,最大岔道组高压地带赢家结成。,岔道极小值的一组称为失败者结成。。

  长期后或一段工夫后。,选择另独一工夫上涂料。,作为赢家和输家的嫁的持续工夫。。

  独一延续的或下料的时间。,不休反复 (1)行动。

  ⑦业绩评价。计算每个保留圈子的几何平均生利和t人口财产调查量。,条件T人口财产调查量标示动量/加速策略的生利是S,知情人表现,动量/反向战术是成的。,学术环境在动量/加速气象。,倒地亦反。

我试着逐渐应用SAS来使筋疲力尽越过作用。:的股本的射程是总数A股在奇纳。,长期以P周表现。,结成留在心中期用Q周表现。,在这里是P.,Q仅占1。,2,3。随访t考查需求完美的。。

在这里,检验唱片零碎包括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商标I的生利。,而且停止种差比力。。上海和深圳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股唱片,上海和深圳300商标SZ399 300唱片
libname gao E:TMP
data test1;
   set gao.test;
   if id eq 滞后(ID) do;
      RIt = 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));
   RIt = 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;

Add a Comment

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