首先利用通达信下载盘后数据,然后可以利用Python编程提取相关交易数据,实例如下:
import struct
import datetime
def stock2csv(filepath,name):
data = []
with open(filepath, 'rb') as f:
file_object_path = 'D:/new_haitong/vipdoc/' + 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]) + ""
print(list)
file_object.writelines(list)
file_object.close()
stock2csv('D:/new_haitong/vipdoc/sh/lday/sh000001.day', '1')
如果您想运行上面的代码,还请根据你系统环境修改其中的数据存储位置,例如上面程序中提到的’D:/new_haitong/vipdoc/’ + name + ‘.csv’ 运行之后内容如下:
2015-09-02,3027.68,3194.48,3019.09,3160.17,438170153
2015-09-07,3149.38,3217.58,3066.3,3080.42,296468114
2015-09-08,3054.44,3174.71,3011.12,3170.45,255415465
2015-09-09,3182.55,3256.74,3165.7,3243.09,375327978
2015-09-10,3190.55,3243.28,3178.9,3197.89,273261759
2015-09-11,3189.48,3223.76,3163.45,3200.23,224557822
2015-09-14,3221.17,3229.48,3049.23,3114.8,346631158
2015-09-15,3043.8,3081.7,2983.92,3005.17,249194445
2015-09-16,2998.04,3182.93,2983.54,3152.26,277524524
2015-09-17,3131.98,3204.7,3085.31,3086.06,317602892
2015-09-18,3100.28,3122.05,3070.34,3097.92,209175398
2015-09-21,3072.09,3159.88,3060.86,3156.54,239897354
2015-09-22,3161.32,3213.48,3152.48,3185.62,274786154
2015-09-23,3137.72,3164.04,3104.74,3115.89,236322670
2015-09-24,3126.49,3151.16,3109.69,3142.69,212887723
2015-09-25,3130.85,3149.95,3063.0,3092.35,236263871