import struct
import os
import math
import string
import pandas as pd
pd.set_option('expand_frame_repr', False)
__sz_minute_dir = r"C:\jcb_hczq\vipdoc\sz\minline" #定义深市日交易数据路径
__sh_minute_dir = r"C:\jcb_hczq\vipdoc\sh\minline" #定义沪市日交易数据路径
__sz_day_dir = r"C:\jcb_hczq\vipdoc\sz\lday" #定义深市日交易数据路径
__sh_day_dir = r"C:\jcb_hczq\vipdoc\sh\lday" #定义沪市日交易数据路径
__stock_name_file_sz = r"D:\Stock\深交所股票代码及名称.txt"
__stock_name_file_sh = r"D:\Stock\上交所股票代码及名称.txt"
#根据数据结构按块读取文件中的交易信息(日)
def get_stock_day(block_day):
trade_day = struct.unpack('i',block_day[0:4])[0] #交易日期
open_price = struct.unpack('i',block_day[4:8])[0] #开盘价
highest_price = struct.unpack('i', block_day[8:12])[0] #最高价
lowest_price = struct.unpack('i', block_day[12:16])[0] #最低价
close_price = struct.unpack('i', block_day[16:20])[0] #收盘价
traded_charge = struct.unpack('f', block_day[20:24])[0] #交易金额
traded_volum = struct.unpack('i', block_day[24:28])[0] #交易量
note = struct.unpack('i', block_day[28:])[0] #备注(未使用)
day_result = [trade_day, open_price, highest_price, lowest_price, close_price, traded_charge, traded_volum, note] #形成列表返回
return day_result #返回日交易信息
#分钟数据中获取日期
def get_day(num):
year = math.floor(num/2048) + 2004
month = math.floor(math.fmod(num,2048)/100)
day = math.fmod(math.fmod(num,2048),100)
date_result = int(year * 10000 + month * 100 + day)
return date_result
#根据数据结构按块读取文件中的交易信息(分钟)
def get_stock_minute(block_day):
trade_day = get_day(struct.unpack('h', block_day[0:2])[0]) # 交易日期
trade_min = struct.unpack('h', block_day[2:4])[0]
open_price = round(struct.unpack('f', block_day[4:8])[0],2) # 开盘价
highest_price = round(struct.unpack('f', block_day[8:12])[0],2) # 最高价
lowest_price = round(struct.unpack('f', block_day[12:16])[0],2) # 最低价
close_price = round(struct.unpack('f', block_day[16:20])[0],2) # 收盘价
traded_charge = struct.unpack('f', block_day[20:24])[0] # 交易金额
traded_volum = struct.unpack('i', block_day[24:28])[0] # 交易量
note = struct.unpack('i', block_day[28:])[0] # 备注(未使用)
minute_result = [trade_day, trade_min, open_price, highest_price, lowest_price, close_price, traded_charge, traded_volum, note] #形成列表返回
return minute_result #返回日交易信息
#取目录下所有股票名编码对应的文件名称(返回列表数据)
def get_stock_filenames(path):
file_list = os.listdir(path) #读取目录下所有的文件信息
li_files=[] #定义文件列表
for item in file_list:
if((item[0:2] == "sz") or (item[0:2] == "sh")): li_files.append(item[0:8])
return li_files #返回文件列表
file_list = get_stock_filenames(__sz_day_dir)
print(file_list)
#将文件夹中的股票信息(名称与代码相分离)取出到列表中
def get_stock_code_and_name(path):
item = ""
re_list = []
with open(path,'r') as file_name:
while True:
item = file_name.readline()
if item == "":
break;
tuple_t = tuple([item[:item.find("(")],item[item.find("(")+1:item.find(")")]])
re_list.append(tuple_t)
return re_list
#获取股票日数据,得到每日交易情况列表
def get_stock_data_day(path, file_item):
stock_code = file_item[2:8]
with open(path + "\\" + file_item,'rb') as v_temp_file_name:
stock_data_day = []
loop_flag = True
while loop_flag:
block_day = v_temp_file_name.read(32)
if block_day == b'':
break
day_item = get_stock_day(block_day)
day_item.insert(1, stock_code)
stock_data_day.append(day_item)
#将List转换
stock_data_day = pd.DataFrame(stock_data_day,columns=['trade_day', 'stock_code', 'open_price', 'highest_price', 'lowest_price', 'close_price', 'traded_charge', 'traded_volum', 'note'])
stock_data_day.sort_values(by='trade_day', ascending=1, inplace=True)
# stock_data_day.reset_index(inplace=True)
# stock_data_day.__delitem__('index')
return (stock_data_day) #, avg_40_line, avg_60_line
#获取股票分钟数据,得到每分钟交易情况列表
def get_stock_data_minute(path, file_item):
stock_code = file_item[2:8]
serial_id = 0
with open(path + "\\" + file_item,'rb') as v_temp_file_name:
stock_data_minute = []
loop_flag = True
while loop_flag:
block_day = v_temp_file_name.read(32)
if block_day == b'':
break
serial_id += 1
day_item = get_stock_minute(block_day)
day_item.insert(2,stock_code)
stock_data_minute.append(day_item)
stock_data_minute = pd.DataFrame(stock_data_minute,columns=['trade_day', 'trade_min', 'stock_code', 'open_price', 'highest_price', 'lowest_price', 'close_price', 'traded_charge', 'traded_volum', 'note'])
stock_data_minute.sort_values(by=['trade_day','trade_min'], ascending=[1,1], inplace=True)
# stock_data_minute.reset_index(inplace=True)
# stock_data_minute.__delitem__('index')
return stock_data_minute
评论7