By - admin

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

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

动量/逆叫策略是补进赢家/输家结成。,同时支撑者/赢家投入结成的市策略。次要过程是:

  决定目的份交易作为市客体的改编。

  其次,选择一段工夫作为执行评价期。,表格期或改编期,通常高压地带投入结成。。

  范本有价证券表格期的击穿。

  (4)理由各样品在成形期的成品率主体。,目的交易中领地范本有价证券的升序、降序排列改编,此后将其分聚束。,最赚钱的机构经过叫做赢家投入结成。,报酬率最低的的群体高压地带失败者投入结成。。

  一段工夫后或继,重新选择工夫大量,作为赢家和输家的结婚的持续工夫。。

  6。延续或片刻包围,不时反复 ②一⑤行动。

  ⑦业绩评价。计算每个握住包围的平分报酬率和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股唱片,SZ39300是上海和深圳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;

发表评论

Your email address will not be published.
*
*