一些網(wǎng)友收集的excel vba ,,特別是如果抓取網(wǎng)頁股票數(shù)據(jù)的文章,。
http://blog.sina.com.cn/s/articlelist_2127818045_10_1.html
http://blog.sina.com.cn/s/articlelist_1576572380_12_1.html
目前市場上有很多股票行情交易軟件,各種軟件提供了豐富的分析和展示功能,而且基本上是免費的,。
但有時我們還是想取得股票歷史數(shù)據(jù)自己用Excel等工具進行查看或分析,。如何獲得股票歷史數(shù)據(jù)呢?
最復(fù)雜也是最靈活的方法是編程實現(xiàn),,就是用程序到新浪搜狐的財經(jīng)頻道或其他財經(jīng)類網(wǎng)站抓取并分析網(wǎng)頁,,這種方法對于大部分來說可操作性不強。
最直觀的方法是直接到網(wǎng)站查看股票歷史數(shù)據(jù)然后復(fù)制下來,,比如到谷歌財經(jīng)(http://www./finance)輸入相應(yīng)股票代碼,,點擊“歷史價格”,就可以看到該股票歷史數(shù)據(jù),。
接下來可以直接復(fù)制數(shù)據(jù)了,。缺點是每頁最多顯示200行數(shù)據(jù),需要手工翻頁,。
最方便快捷省時省力的方法就是雅虎財經(jīng)網(wǎng)站,,它提供的接口可以直接把股票歷史數(shù)據(jù)導(dǎo)成Excel,真實太方便了!
直接在瀏覽器地址中數(shù)據(jù)網(wǎng)址即可
http://table.finance.yahoo.com/table.csv?s=股票代碼
上證股票是股票代碼后面加上.ss,,深證股票是股票代碼后面加上.sz
深市數(shù)據(jù)鏈接:http://table.finance.yahoo.com/table.csv?s=000001.sz
上市數(shù)據(jù)鏈接:http://table.finance.yahoo.com/table.csv?s=600000.ss
另外,,上證綜指代碼:000001.ss,,深證成指代碼:399001.SZ,滬深300代碼:000300.ss
例如查詢中國石油的歷史數(shù)據(jù),,直接在瀏覽器中輸入:http://table.finance.yahoo.com/table.csv?s=601857.ss
網(wǎng)站自動返回一個csv格式的文件,,保存到本地即可??梢灾苯佑肊xcel打開分析,,也可以導(dǎo)入SAS、SPSS等軟件進行分析,。
得到的文件包括如下幾個字段:
Date Open High Low Close Volume Adj
Close
分別是:日期,、開盤價、最高價,、最低價,、收盤價、收盤價,、成交量,、復(fù)權(quán)收盤價
好了,有時間我將在以后的文章介紹如何進行股票價格的回歸分析,。
【例子】 取 2012年1月1日 至 2012年4月19日的數(shù)據(jù)
http://table.finance.yahoo.com/table.csv?a=0&b=1&c=2012&d=3&e=19&f=2012&s=600000.ss
private string CreatUrl(string _Symbol)
{
DateTime dt = DateTime.Today.AddYears(-1);
dt.AddDays(-10);
int a = dt.Month -1 ;
int b = dt.Day;
int c = dt.Year;
int d = DateTime.Today.Month -1 ;
int e = DateTime.Today.Day;
int f = DateTime.Today.Year;
return @"http://table.finance.yahoo.com/table.csva="
+ a + @"&b=" + b + @"&c=" + c + @"&d=" + d + @"&e="
+ e + @"&f=" + f + @"&s=" + _Symbol +
@"&y=0&g=d&ignore=.csv";
}
通過Yahoo Finance API獲取股票數(shù)據(jù)
最近要做的一個網(wǎng)站需要在首頁顯示世界各大股票交易場所的綜合指數(shù),,于是搜集了點這方面的資料,今天把研究的結(jié)果放出來,。
提供財經(jīng)方面訊息,,做的比較好的應(yīng)該是Google財經(jīng)和Yahoo財經(jīng)了,綜合考慮還是Yahoo的接口比較好用
API使用方法比較簡單,,舉個例子:
http://finance.yahoo.com/d/quotes.csv?s=^XAU&f=snd1l1c6
返回數(shù)據(jù):
”^XAU”,”PHLX Gold/Silver “,”11/24/2010″,213.51,”+1.01″
很典型的CSV數(shù)據(jù),,比較好處理?!皊”指的是股票的代碼(Symbol),,“f”是要獲取的數(shù)據(jù)選項,上面例子返回的數(shù)據(jù)分別是“代碼”,,“股票
名稱”,,“最后更新時間”,“指數(shù)”,,“變動”,。完整的參數(shù)列表參見下面表格(對財經(jīng)方面知道不多,所以不翻譯了,,以免誤導(dǎo)別人):
a |
Ask |
a2 |
Average Daily Volume |
a5 |
Ask Size |
b |
Bid |
b2 |
Ask (Real-time) |
b3 |
Bid (Real-time) |
b4 |
Book Value |
b6 |
Bid Size |
c |
Change & Percent Change |
c1 |
Change |
c3 |
Commission |
c6 |
Change (Real-time) |
c8 |
After Hours Change (Real-time) |
d |
Dividend/Share |
d1 |
Last Trade Date |
d2 |
Trade Date |
e |
Earnings/Share |
e1 |
Error Indication (returned for symbol changed / invalid) |
e7 |
EPS Estimate Current Year |
e8 |
EPS Estimate Next Year |
e9 |
EPS Estimate Next Quarter |
f6 |
Float Shares |
g |
Day’s Low |
h |
Day’s High |
j |
52-week Low |
k |
52-week High |
g1 |
Holdings Gain Percent |
g3 |
Annualized Gain |
g4 |
Holdings Gain |
g5 |
Holdings Gain Percent (Real-time) |
g6 |
Holdings Gain (Real-time) |
i |
More Info |
i5 |
Order Book (Real-time) |
j1 |
Market Capitalization |
j3 |
Market Cap (Real-time) |
j4 |
EBITDA |
j5 |
Change From 52-week Low |
j6 |
Percent Change From 52-week Low |
k1 |
Last Trade (Real-time) With Time |
k2 |
Change Percent (Real-time) |
k3 |
Last Trade Size |
k4 |
Change From 52-week High |
k5 |
Percebt Change From 52-week High |
l |
Last Trade (With Time) |
l1 |
Last Trade (Price Only) |
l2 |
High Limit |
l3 |
Low Limit |
m |
Day’s Range |
m2 |
Day’s Range (Real-time) |
m3 |
50-day Moving Average |
m4 |
200-day Moving Average |
m5 |
Change From 200-day Moving Average |
m6 |
Percent Change From 200-day Moving Average |
m7 |
Change From 50-day Moving Average |
m8 |
Percent Change From 50-day Moving Average |
n |
Name 股票名稱 |
n4 |
Notes |
o |
Open |
p |
Previous Close |
p1 |
Price Paid |
p2 |
Change in Percent |
p5 |
Price/Sales |
p6 |
Price/Book |
q |
Ex-Dividend Date |
r |
P/E Ratio |
r1 |
Dividend Pay Date |
r2 |
P/E Ratio (Real-time) |
r5 |
PEG Ratio |
r6 |
Price/EPS Estimate Current Year |
r7 |
Price/EPS Estimate Next Year |
s |
Symbol 股票代碼 |
s1 |
Shares Owned |
s7 |
Short Ratio |
t1 |
Last Trade Time |
t6 |
Trade Links |
t7 |
Ticker Trend |
t8 |
1 yr Target Price |
v |
Volume |
v1 |
Holdings Value |
v7 |
Holdings Value (Real-time) |
w |
52-week Range |
w1 |
Day’s Value Change |
w4 |
Day’s Value Change (Real-time) |
x |
Stock Exchange |
y |
Dividend Yield |
|
|
|
|
上面接口雖然好用,,不過根據(jù)項目需求,在進行了進一步封裝,自己寫了個Widget類,,方便在模板里調(diào)用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
class FinanceWidget { public $stocks = array(); public $htmlOptions = array(); public$data = array(); public function __construct($data = array(),$htmlOptions = array()){$this->stocks = $data; $this->htmlOptions = $htmlOptions; } private function_renderHead($htmlOptions){ $options = ''; foreach($htmlOptions as $option => $value){$options .= "$option=\"$value\" "; } echo "
-
$options>\n"; } private function_renderBody($data){ foreach($data as $row){ echo '
-
'.$row[3].''.$row[1].''.$row[2].'
- '."\n"; } } private function_renderFoot(){ echo "
\n"; } private function getAPI(){ $IDs = join(',',$this->stocks); return "http://finance.yahoo.com/d/quotes.csv?s=$IDs&f=nl1p2"; } publicfunction run(){ $this->getStocks(); $this->_renderHead($this->htmlOptions); $this->_renderBody($this->data); $this->_renderFoot(); } public function getStocks(){ $row =0; $stocks_name = array_keys($this->stocks); if (($handle = fopen($this->getAPI(), 'r'))!== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {array_push($data,$stocks_name[$row]); array_push($this->data,$data); $row++; }fclose($handle); } } }
|
調(diào)用的時候只需要定義你需要展示的股票,,傳給FinanceWidget類:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
$stock = array( '上證指數(shù)'=>'000001.ss', '深圳成指'=>'399001.sz', '香港恒生'=>'0011.hk', '日經(jīng)指數(shù)'=>'^N225', '英國FTSE'=>'^FTSE', '法國CAC'=>'^FCHI', '德國DAX'=>'^GDAXI', '納斯達克'=>'^IXIC','道瓊指數(shù)'=>'INDU', ); $widget = new FinanceWidget($metals); $widget->run();
|
輸出結(jié)果如下:
-
XAU指數(shù)213.51+0.48%
-
HUI指數(shù)548.979+0.01%
-
JSE黃金2701.89+0.11%
-
GOX指數(shù)246.62+0.31%
-
TSX指數(shù)411.840.00%
當(dāng)然這個類支持指定ul的html選項,作為第二個參數(shù)傳遞給Widget類即可,,這里不多介紹,大家根據(jù)自己需要擴展即可
|