一、介紹
python獲取股票數據的方法很多,其中 Tushare 財經數據接口包很好用,當然,也可以通過通達信本地的數據獲取,這樣更為方便。
日線數據存在這路徑下 D:\通達信\vipdoc\sh\lday(我的通達信安裝目錄是D盤)
接著我們需要的就是解析這些數據,在分別存為 csv 格式的數據就行了,這樣我們可以方便的用 pandas 或其他方法讀取和分析。
通達信的日線數據格式如下:
- 每32個字節為一天數據
- 每4個字節為一個字段,每個字段內低字節在前
- 00 ~ 03 字節:年月日, 整型
- 04 ~ 07 字節:開盤價*100, 整型
- 08 ~ 11 字節:最高價*100, 整型
- 12 ~ 15 字節:最低價*100, 整型
- 16 ~ 19 字節:收盤價*100, 整型
- 20 ~ 23 字節:成交額(元),float型
- 24 ~ 27 字節:成交量(股),整型
- 28 ~ 31 字節:(保留)
打開一個 .day 的文件,發現是亂碼,以二進制格式存儲,那么我們只需按照上面存的字節數解析下就可以了。
先讀取一天的數據
>>> f = open('D:/通達信/vipdoc/sh/lday/sh000001.day', 'rb')
>>> f.read(32)
b'\xa2\xde2\x01\x14\x9b\x03\x00\x0f\x9d\x03\x00\x8d\x91\x03\x00\xef\x93\x03\x00\xcb\xbc\x14Q\x9a\xfb|\x02-\x01Z\x02'
這應該就是一天的數據了,我們對這個數據進行解析,這里需要用到 struct 模塊中的 unpack 方法
>>> import struct
>>> f = open('D:/通達信/vipdoc/sh/lday/sh000001.day', 'rb')
>>> li = f.read(32)
>>> data = struct.unpack('lllllfll', li)
>>> data
(20111010, 236308, 236815, 233869, 234479, 39926411264.0, 41745306, 39452973)
# 分別為日期,開盤,最高,最低,收盤,成交額,成交量,保留值
unpack用法: 前一個參數是格式,'lllllfii' 就是一個浮點數格式(f,這里對應日線數據中的成交額是float格式)和其他整形格式(i,這里對應日線數據中的其他數據是 int 格式)
那么剩下的問題不大了
二、完整代碼
在 sh 目錄下新建了個 pythondata 文件夾,注意文件路徑分隔符是 /
import struct
import datetime
def stock_csv(filepath, name):
data = []
with open(filepath, 'rb') as f:
file_object_path = 'D:/通達信/vipdoc/sh/pythondata/' + name +'.csv'
file_object = open(file_object_path, 'w+')
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字節 如20091229
stock_open = struct.unpack("l", stock_open) #開盤價*100
stock_high = struct.unpack("l", stock_high) #最高價*100
stock_low= struct.unpack("l", stock_low) #最低價*100
stock_close = struct.unpack("l", stock_close) #收盤價*100
stock_amount = struct.unpack("f", stock_amount) #成交額
stock_vol = struct.unpack("l", stock_vol) #成交量
stock_reservation = struct.unpack("l", stock_reservation) #保留值
date_format = datetime.datetime.strptime(str(stock_date[0]),'%Y%M%d') #格式化日期
list= date_format.strftime('%Y-%M-%d')+","+str(stock_open[0]/100)+","+str(stock_high[0]/100.0)+","+str(stock_low[0]/100.0)+","+str(stock_close[0]/100.0)+","+str(stock_vol[0])+"\r\n"
file_object.writelines(list)
file_object.close()
stock_csv('D:/通達信/vipdoc/sh/lday/sh000001.day', '1')
運行下,打開 1.CSV 文件
OK
三、批量解析
import os
import struct
import datetime
def stock_csv(filepath, name):
data = []
with open(filepath, 'rb') as f:
file_object_path = 'D:/通達信/vipdoc/sh/pythondata/' + name +'.csv'
file_object = open(file_object_path, 'w+')
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字節 如20091229
stock_open = struct.unpack("l", stock_open) #開盤價*100
stock_high = struct.unpack("l", stock_high) #最高價*100
stock_low= struct.unpack("l", stock_low) #最低價*100
stock_close = struct.unpack("l", stock_close) #收盤價*100
stock_amount = struct.unpack("f", stock_amount) #成交額
stock_vol = struct.unpack("l", stock_vol) #成交量
stock_reservation = struct.unpack("l", stock_reservation) #保留值
date_format = datetime.datetime.strptime(str(stock_date[0]),'%Y%M%d') #格式化日期
list= date_format.strftime('%Y-%M-%d')+","+str(stock_open[0]/100)+","+str(stock_high[0]/100.0)+","+str(stock_low[0]/100.0)+","+str(stock_close[0]/100.0)+","+str(stock_vol[0])+"\r\n"
file_object.writelines(list)
file_object.close()
path = 'D:/通達信/vipdoc/sh/lday/'
listfile = os.listdir('D:/通達信/vipdoc/sh/lday/')
for i in listfile:
stock_csv(path+i, i[:-4])
運行下
完美