2016-9-3 使用Yahoo的接口也能夠獲取到數(shù)據(jù) https://github.com/lukaszbanasiak/yahoo-finance
通達(dá)信網(wǎng)站為券商系統(tǒng)提供交易數(shù)據(jù)下載,如:上證所有證券日線shlday.zip,深證所有證券日線szlday.zip。shlday.zip下載加壓后會(huì)得到sh000001.day、sh000002.day等312個(gè)獨(dú)立的數(shù)據(jù)文件,sh000001.day代表了上證股票000001的所有日線數(shù)據(jù)。
1.日線數(shù)據(jù)格式
1-4字節(jié) Date:LongInt; //日期
5-8字節(jié) OPen:LongInt; //開(kāi)盤*100(元)
9-12字節(jié) High:LongInt; //最高價(jià)*100(元)
13-16字節(jié) Low:LongInt; //最低價(jià)*100(元)
17-20字節(jié) Close:LongInt; //收盤*100(元)
21-24字節(jié) Amount;//成交額
25-28字節(jié) Volume:LongInt; //Volume 成交量(股)
29-32字節(jié) // Reserved 保留值
2.python程序
本程序代碼將給定日線數(shù)據(jù)文件內(nèi)容輸出到csv文件以供后續(xù)程序分析用。
2.1.代碼
# -*- coding:utf-8 -*-
import os
import os.path
import struct
import time,datetime
time_original='17/Sep/2012:11:40:00'
time_format=datetime.datetime.strptime(time_original,'%d/%b/%Y:%H:%M:%S')
def stockOutput(path):
file_object = open('D:\dev\data.csv', 'w+')
with open(path,"rb") as f:
while True:
stock_date = f.read(4)
stock_open = f.read(4)
stock_high = f.read(4)
stock_low= f.read(4)
stock_close = f.read(4)
stock_amount = f.read(4)
stock_vol = f.read(4)
stock_reservation = f.read(4)
# date,open,high,low,close,amount,vol,reservation
if not stock_date:
break
stock_date = struct.unpack("l", stock_date) # 4字節(jié) 如20091229
stock_open = struct.unpack("l", stock_open) #開(kāi)盤價(jià)
stock_high = struct.unpack("l", stock_high) #最高價(jià)
stock_low= struct.unpack("l", stock_low) #最低價(jià)
stock_close = struct.unpack("l", stock_close) #收盤價(jià)
stock_amount = struct.unpack("l", stock_amount) #成交額
stock_vol = struct.unpack("l", stock_vol) #成交量
stock_reservation = struct.unpack("l", stock_reservation) #保留值
# print "PROCESS:", stock_date[0],stock_open,stock_high,stock_low,stock_close,stock_amount,stock_vol,stock_reservation
#print "PROCESS:", stock_date[0],stock_close[0]
date_format=datetime.datetime.strptime(str(stock_date[0]),'%Y%M%d')
list= "CYBZ,"+date_format.strftime('%Y-%M-%d')+",,"+str(stock_open[0])+","+str(stock_high[0])+","+str(stock_low[0])+","+str(stock_close[0])+","+str(stock_vol[0])+"\r\n"#,stock_high[0],stock_low[0],stock_close[0],stock_vol[0]
#[股票代碼0,日期1,開(kāi)盤價(jià)2,最高價(jià)3,最低價(jià)4,收盤價(jià)5,成交量6]
file_object.writelines(list)
#file_object.truncate()
file_object.close()
stockOutput("D:\\projects\\data\\tdx\\szlday\\sz399006.day")
2.2.說(shuō)明
- 函數(shù) open(path,”rb”) 參數(shù)’rb’ 指定只讀二進(jìn)制打開(kāi)
- struct.unpack(“l(fā)”, stock_date) 將二進(jìn)制數(shù)據(jù)解碼為integer類型
3.參考:
- 通達(dá)信數(shù)據(jù)格式:http://blog.163.com/da7_1@126/blog/static/1040726782012017856371/
- Python使用struct處理二進(jìn)制:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html
|