SAS中文论坛

标题: 请教SAS下载股票数据 [打印本页]

作者: lianlinchi    时间: 2015-5-18 23:56
标题: 请教SAS下载股票数据
请问用SAS怎么下载A股股票的交易数据,每天的开盘最高最低收盘和交易量数据,如果有分钟数据更好了,google finance 或者 yahoo finance 有数据,但是不知道怎么下载。

作者: BioSas    时间: 2016-5-18 00:26

proc format;
   value $TckName
    '^DJI'='Dow Jones'
    '^IXIC'='NASDAQ'
    '000001.SS'='Shanghai'
        '399001.SZ'='Shenzhen'
        ;
run;
%macro ReadStockData(Ticker, FromDate, ToDate);
   %local ImpNum;
   %let Ticker=%sysfunc(upcase(&Ticker));
    /** Add a proxyserver= option here if you need to run this from behind a firewall **/
   FileName YaHoo url "http://ichart.finance.yahoo.com/table.csv?s=&Ticker";
   data Temp;
   label Index_Name='Index Name'
             Date='Date'
             Open='Open'
             High='High'
             Low='Low'
             Close='Close'
         Volume='Volume'
             Adj_Close='Adjusted Close'
         ;
   attrib
      Index_Name length=$11
      Date length=8 informat=yymmdd10. format=mmddyy10.
      High Low Close Volume Adj_Close length=8 format=best25.
      ;
   Index_Name=put("&Ticker",$TckName.);
   infile YaHoo dlm=',' missover firstobs=2 dsd ;
   /** The ?? modifier suppresses error/notes when reading invalid data **/
   input
    Date       : ?? yymmdd10.
    Open       : ?? comma15.
    High       : ?? comma15.
    Low        : ?? comma15.
    Close      : ?? comma15.
    Volume     : ?? comma15.
    Adj_Close  : ?? comma15.
     ;
    if &FromDate <= Date <= &ToDate and Adj_Close>0 then output;  
  run;
  FileName YaHoo clear;
  proc sql noprint;
   select count(*) into :ImpNum
   from Temp;
  quit;

%if %sysevalf(&ImpNum >0) %then %do;
  proc sort data=Temp;
   by Date;
  run;
  data Temp (drop=PreClose);
   label Index_Name='Index Name'
         Year='Year'
             Month='Month'
         Date="Date"
                 Day='Week Day'
         Adj_Close='Adjusted Close Price'
             Return='Return'
        ;
   format Date mmddyy10. Adj_Close comma15.4 Return percentn9.2;
        ;
  set Temp (keep=Index_Name Date Adj_Close);
   retain PreClose 0;
   Year=Year(Date);
   Month=Month(Date);
   Day=strip(put(Date,downame.));
   if _n_=1 then do;
     PreClose=Adj_Close;
     Return=.;
   end;
   else do;
    Return=(Adj_Close-PreClose)/PreClose;
    PreClose=Adj_Close;
   end;
   if Return ne . then output;
   run;
   proc append base = Commbined_Stock_Data
            data = Temp;
   run;
   quit;
  %end;
    proc sql;
    drop table Temp;
    quit;
  %mend ReadStockData;

  %ReadStockData(000001.SS, '01JAN2015'd, '16MAY2016'd)
  %ReadStockData(399001.SZ, '01JAN2015'd, '16MAY2016'd)
  %ReadStockData(^DJI, '01JAN2015'd, '16MAY2016'd)
  %ReadStockData(^IXIC,'01JAN2015'd, '16MAY2016'd)
作者: BioSas    时间: 2016-5-18 00:28
You can use Python to do the same:

'''
Import Shanghai Adjusted Close Prices.py

Shanghai Composite Index: 000001.SS

If returning errors, open the website
    http://finance.yahoo.com/
and run again

To adjuste to Chinese Date:
Output Date - 1 Day
'''

from pandas.io.data import DataReader
import pandas as pd
from datetime import datetime
import numpy as np
try:
    Ticker_List = ['000001.SS']
    Firm_List = ['Shanghai']
    ls_key = 'Adj Close'
    StartDt = datetime(2000,1,1)
    EndDt = datetime.today().strftime("%Y,%m,%d")  
    InStream = DataReader(Ticker_List, 'yahoo', StartDt, EndDt)
    SelectedData = pd.DataFrame(InStream.ix[ls_key])
    SelectedData.columns = Firm_List
    SelectedData.columns.values[-1] = 'Adjusted Close'
    SelectedData['Return']=SelectedData['Adjusted Close'].pct_change()
    print ("Max Index:",SelectedData.max(axis=0))
    print ("\n")
    print (SelectedData[:10])  # print the first 10 rows
    print ("")                # print an empty row
    print (SelectedData[-10:]) # print the last 10 rows
    # output to a CSV file: SelectedData.to_csv(file_name, sep='\t', encoding='utf-8')
    SelectedData.to_csv("C:\Shanghai.csv")
except pandas.io.data.RemoteDataError:
            print("\nThe website is blocked! Go to http://finance.yahoo.com/ to make sure it is available.")




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2