-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstockdata.py
80 lines (65 loc) · 2.27 KB
/
stockdata.py
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
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# -------------------------------------------------------------------------------
# FileName: [stockdata.py]
# Purpose: [module for handle stock data]
# Author: [Zhou Ke]
# Created: [2015-2-10]
# Copyright: (c) ZhouKe 2015
# Licence: <GPL>
# -------------------------------------------------------------------------------
"""
import struct
import os
# 字典保存设置
settings = {}
settings['syspath'] = r'D:\MyApplications\new_gdzq_v6'
settings['daydata_sh'] = 'vipdoc\sh'
settings['daydata_sz'] = 'vipdoc\sz'
settings['chuquandata'] = 'T0002\hq_cache\gbbq'
# 日线数据格式, 每32个字节为一天数据
# 每4个字节为一个字段,每个字段内低字节在前
# 00 ~ 03 字节:年月日, 整型
# 04 ~ 07 字节:开盘价*1000, 整型
# 08 ~ 11 字节:最高价*1000, 整型
# 12 ~ 15 字节:最低价*1000, 整型
# 16 ~ 19 字节:收盘价*1000, 整型
# 20 ~ 23 字节:成交额(元),float型
# 24 ~ 27 字节:成交量(手),整型
# 28 ~ 31 字节:上日收盘*1000, 整型
# define a dict to store single day data
s_day = {'date':'1900-01-01',
'open': 0.0,
'high': 0.0,
'low': 0.0,
'close': 0.0,
'amount': 0.0,
'vol': 0.0,
'reserved': ''}
def get_filename(stockcode):
"""
Purpose / Usage: 转换股票代码为一个对应的日线数据文件, 返回全路径文件名
Parameter(s): stockcode string
"""
if stockcode[0] == '6':
stockfile = os.path.join(settings['syspath'], 'vipdoc', 'sh', 'lday', 'sh'+ stockcode + '.day')
else:
stockfile = os.path.join(settings['syspath'], 'vipdoc', 'sz', 'lday', 'sz'+ stockcode + '.day')
return stockfile
def load_stock_data(stockfile):
"""
Purpose / Usage: 从日线数据文件加载单支股票数据, 返回list
@parameter stockfile
"""
lst = []
with open(stockfile, 'rb') as df:
while True:
block = df.read(32)
if len(block) < 32:
break
else:
sd = struct.unpack('iiiiifii', block)
lst.append(sd)
return lst
print(load_stock_data(get_filename('600100'))[0])