用SAS获取雅虎股票数据以及均线计算源码.docx
《用SAS获取雅虎股票数据以及均线计算源码.docx》由会员分享,可在线阅读,更多相关《用SAS获取雅虎股票数据以及均线计算源码.docx(11页珍藏版)》请在冰豆网上搜索。
![用SAS获取雅虎股票数据以及均线计算源码.docx](https://file1.bdocx.com/fileroot1/2022-11/25/38be572c-db73-4457-8527-b05c44ab0616/38be572c-db73-4457-8527-b05c44ab06161.gif)
用SAS获取雅虎股票数据以及均线计算源码
用SAS获取雅虎股票数据以及均线的计算
第一要有一张股票基础表,包含股票代码等,用来读数。
如下
第二步:
建立stock永久SAS库
第三步:
运行程序
这个过程说明读取了数据,会在stock库下形成对应文件。
每个表有以下数据
下面是源代码:
获取历史数据以及五日、十日均线、十五日均线的计算
%macrogeturl(stockcode,fromdate,todate);
%leta=%sysevalf(%sysfunc(month(%sysfunc(inputn(&fromdate,yymmdd10.))))-1);
%letb=%sysfunc(day(%sysfunc(inputn(&fromdate,yymmdd10.))));
%letc=%sysfunc(year(%sysfunc(inputn(&fromdate,yymmdd10.))));
%letd=%sysevalf(%sysfunc(month(%sysfunc(inputn(&todate,yymmdd10.))))-1);
%lete=%sysfunc(day(%sysfunc(inputn(&todate,yymmdd10.))));
%letf=%sysfunc(year(%sysfunc(inputn(&todate,yymmdd10.))));
"
%mendgeturl;
%macrogetprice();
%letdsid=%sysfunc(open(hua.stock));
%put&dsid;
%letnum=%sysfunc(attrn(&dsid,nobs));
%if&num>0%then%do;
%doj=1690%to1890;
%letrc=%sysfunc(fetchobs(&dsid,&j));
%letstockcode=%sysfunc(getvarc(&dsid,5));
%letfromdate=1900-01-01;
%lettodate=2014-09-30;
%letdataset=s%sysfunc(substr(&stockcode,1,6));
filenamereadweburl%geturl(&stockcode,&fromdate,&todate);
DATAstock.&dataset;
FORMATrqyymmdd10.;
INFILEreadwebLRECL=32767DELIMITER=","firstobs=2;
INPUTrqyymmdd10.openhighlowclosevolumeadjclose@@;
range=high-low;
price=(high+low)/2;
if(volume>0);
procsort;
bydescendingrq;
RUN;
%end;
%end;
%letdsid=%sysfunc(close(&dsid));
%mendgetprice;
%getprice();
以上为历史数据获取,以下是均线的计算
%letstockcode=600033.ss;/*股票代码;*/
%letfromdate=2000-01-01;
%lettodate=%sysfunc(intnx(Day,"&sysdate"d,-1),yymmdd10);
%macrogeturl(stockcode,fromdate,todate);
%leta=%sysevalf(%sysfunc(month(%sysfunc(inputn(&fromdate,yymmdd10.))))-1);
%letb=%sysfunc(day(%sysfunc(inputn(&fromdate,yymmdd10.))));
%letc=%sysfunc(year(%sysfunc(inputn(&fromdate,yymmdd10.))));
%letd=%sysevalf(%sysfunc(month(%sysfunc(inputn(&todate,yymmdd10.))))-1);
%lete=%sysfunc(day(%sysfunc(inputn(&todate,yymmdd10.))));
%letf=%sysfunc(year(%sysfunc(inputn(&todate,yymmdd10.))));
"
*"
*"
%mendgeturl;
%put%geturl(&stockcode,&fromdate,&todate);
filenamereadweburl%geturl(&stockcode,&fromdate,&todate);
DATAstocktemp;
FORMATrqyymmdd10.;
INFILEreadwebLRECL=32767DELIMITER=","firstobs=2;
INPUTrqyymmdd10.openhighlowclosevolumeadjclose@@;
range=high-low;
price=(high+low)/2;
if(volume>0);
procsort;
bydescendingrq;
RUN;
dataday5;
setstocktemp(obs=5);
run;
dataday10;
setstocktemp(obs=10);
run;
dataday30;
setstocktemp(obs=30);
run;
dataday60;
setstocktemp(obs=60);
run;
dataday120;
setstocktemp(obs=120);
run;
dataday360;
setstocktemp(obs=360);
run;
procsqlnoprint;
selectmax(high),min(low),mean(price),mean(range),var(price)into:
highest,:
lowest,:
mprice,:
mrange,:
vpricefromstocktemp;
selectmax(high),min(low),mean(price),mean(range),var(price)into:
high5,:
low5,:
mprice5,:
mrange5,:
vprice5fromday5;
selectmax(high),min(low),mean(price),mean(range),var(price)into:
high10,:
low10,:
mprice10,:
mrange10,:
vprice10fromday10;
selectmean(price),mean(range),var(price)into:
mprice30,:
mrange30,:
vprice30fromday30;
selectmean(price),mean(range),var(price)into:
mprice60,:
mrange60,:
vprice60fromday60;
selectmean(price),mean(range),var(price)into:
mprice120,:
mrange120,:
vprice120fromday120;
selectmean(price),mean(range),var(price)into:
mprice360,:
mrange360,:
vprice360fromday360;
quit;
datalow;
formatstockcode$6.;
stockcode="&stockcode";
high5=&high5;
low5=&low5;
mprice5=&mprice5;
mrange5=&mrange5;
vprice5=&vprice5;
highest=&highest;
lowest=&lowest;
mprice=&mprice;
mrange=&mrange;
vprice=&vprice;
high10=&high10;
low10=&low10;
mprice10=&mprice10;
mrange10=&mrange10;
vprice10=&vprice10;
mprice30=&mprice30;
mrange30=&mrange30;
vprice30=&vprice30;
mprice60=&mprice60;
mrange60=&mrange60;
vprice60=&vprice60;
mprice120=&mprice120;
mrange120=&mrange120;
vprice120=&vprice120;
mprice360=&mprice360;
mrange360=&mrange360;
vprice360=&vprice360;
run;
procappendbase=hua.lowdatadata=low;
quit;
下面是附加的简单股票统计分析
procsqlnoprint;
createtablehua.yahooas
selectstock.yahoocodefromhua.lowpriceleftjoinhua.stockonlowprice.stockcode=stock.numcode;
delete*fromhua.lowdata;
quit;
%macrogeturl(stockcode,fromdate,todate);
%leta=%sysevalf(%sysfunc(month(%sysfunc(inputn(&fromdate,yymmdd10.))))-1);
%letb=%sysfunc(day(%sysfunc(inputn(&fromdate,yymmdd10.))));
%letc=%sysfunc(year(%sysfunc(inputn(&fromdate,yymmdd10.))));
%letd=%sysevalf(%sysfunc(month(%sysfunc(inputn(&todate,yymmdd10.))))-1);
%lete=%sysfunc(day(%sysfunc(inputn(&todate,yymmdd10.))));
%letf=%sysfunc(year(%sysfunc(inputn(&todate,yymmdd10.))));
"
%mendgeturl;
%macrogetdata(stockcode);
%letfromdate=2000-01-01;
%lettodate=%sysfunc(intnx(Day,"&sysdate"d,-1),yymmdd10);
filenamereadweburl%geturl(&stockcode,&fromdate,&todate);
DATAstocktemp;
FORMATrqyymmdd10.;
INFILEreadwebLRECL=32767DELIMITER=","firstobs=2;
INPUTrqyymmdd10.openhighlowclosevolumeadjclose@@;
range=high-low;
price=(high+low)/2;
if(volume>0);
procsort;
bydescendingrq;
RUN;
dataday5;
setstocktemp(obs=5);
run;
dataday10;
setstocktemp(obs=10);
run;
dataday30;
setstocktemp(obs=30);
run;
dataday60;
setstocktemp(obs=60);
run;
dataday120;
setstocktemp(obs=120);
run;
dataday360;
setstocktemp(obs=360);
run;
procsqlnoprint;
selectmax(high),min(low),mean(price),mean(range),var(price)into:
highest,:
lowest,:
mprice,:
mrange,:
vpricefromstocktemp;
selectmax(high),min(low),mean(price),mean(range),var(price)into:
high5,:
low5,:
mprice5,:
mrange5,:
vprice5fromday5;
selectmax(high),min(low),mean(price),mean(range),var(price)into:
high10,:
low10,:
mprice10,:
mrange10,:
vprice10fromday10;
selectmean(price),mean(range),var(price)into:
mprice30,:
mrange30,:
vprice30fromday30;
selectmean(price),mean(range),var(price)into:
mprice60,:
mrange60,:
vprice60fromday60;
selectmean(price),mean(range),var(price)into:
mprice120,:
mrange120,:
vprice120fromday120;
selectmean(price),mean(range),var(price)into:
mprice360,:
mrange360,:
vprice360fromday360;
quit;
datalow;
formatstockcode$6.;
stockcode="&stockcode";
high5=&high5;
low5=&low5;
mprice5=&mprice5;
mrange5=&mrange5;
vprice5=&vprice5;
highest=&highest;
lowest=&lowest;
mprice=&mprice;
mrange=&mrange;
vprice=&vprice;
high10=&high10;
low10=&low10;
mprice10=&mprice10;
mrange10=&mrange10;
vprice10=&vprice10;
mprice30=&mprice30;
mrange30=&mrange30;
vprice30=&vprice30;
mprice60=&mprice60;
mrange60=&mrange60;
vprice60=&vprice60;
mprice120=&mprice120;
mrange120=&mrange120;
vprice120=&vprice120;
mprice360=&mprice360;
mrange360=&mrange360;
vprice360=&vprice360;
run;
%mendgetdata;
%macroanalyse();
%letdsid=%sysfunc(open(hua.yahoo));
%letnum=%sysfunc(attrn(&dsid,nobs));
%if&num>0%then%do;
%doi=1%to#
%letrc=%sysfunc(fetchobs(&dsid,&i));
%letstockcode=%sysfunc(getvarc(&dsid,1));
%getdata(&stockcode);
%if&syserr^=0%then%do;
%do%while(&syserr^=0);
%put---------------------------------------------------------------------------&stockcode;
%getdata(&stockcode);
%end;
%end;
procappendbase=hua.lowdatadata=low;
quit;
%end;
%end;
%mendanalyse;
%analyse;
procsqlnoprint;
droptablehua.result;
createtablehua.resultas
selectlowprice.stockname,lowprice.price,lowprice.zongshizhi,lowdata.*fromhua.lowpriceleftjoinhua.lowdataonlowprice.stockcode=lowdata.stockcode;
quit;