Choice of data fetching interface

To get the data of stocks, generally speaking, there can be several methods:

  1. tushare, baostock, qstock and other python library interface, the advantage is that the data has been sorted and cleaned, it is more convenient to get, only need a line of command can be obtained, the disadvantage is that there are often a variety of constraints, and the accuracy is not enough, for example, tushare need to register and get points to use, these libraries of the highest free time accuracy is 5 minutes, want to get the granularity of 1 minute data, you need to find a way to do it yourself. The maximum free time precision of these libraries is 5 minutes, so if you want to get the data with a granularity of 1 minute, you need to think of your own way!
  2. directly through the web page to crawl the corresponding data, this way is more free, but the problem is that you can not be broken, once one day your crawler server is down, which means that your data is difficult to pick up, you need to make up from other data sources, the problem is that you have other channels of information, why not use it in the beginning
  3. download the data through stock trading software, and then transform it into your own data.
  4. buy the data directly

Downloading the data is not the end of the matter, it is best to save it in the database, so that it is convenient for multiple terminals and can be called at any time. Here are three ways to do this

Getting historical data via python library.

Install the baostock library.

1
pip install baostock -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn

Get historical A-share K-line data

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
import baostock as bs
import pandas as pd


lg = bs.login()

print('login respond error_code:'+lg.error_code)
print('login respond error_msg:'+lg.error_msg)


start_date = '2017-07-01'
end_date = '2017-12-31'

min_param = "date,time,code,open,high,low,close,volume,amount,adjustflag"

meek_param = "date,code,open,high,low,close,volume,amount,adjustflag,turn,pctChg"

day_param = "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST"

week_rs = bs.query_history_k_data_plus("sh.600000",
day_param,start_date=start_date, end_date=end_date,frequency="w", adjustflag="3")

day_rs = bs.query_history_k_data_plus("sh.600000",
day_param,start_date=start_date, end_date=end_date,frequency="d", adjustflag="3")

min_rs = bs.query_history_k_data_plus("sh.600000",
day_param,start_date=start_date, end_date=end_date,frequency="5", adjustflag="3")

rs = min_rs

print('query_history_k_data_plus respond error_code:'+rs.error_code)
print('query_history_k_data_plus respond error_msg:'+rs.error_msg)


data_list = []
while (rs.error_code == '0') & rs.next():

data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)

result.to_csv("D:\\history_A_stock_k_data.csv", index=False)
print(result)


bs.logout()

Access to valuation indicators (daily frequency)

1
2
3
4
5
6
7
8

rs = bs.query_history_k_data_plus("sh.600000",
"date,code,close,peTTM,pbMRQ,psTTM,pcfNcfTTM",
start_date='2015-01-01', end_date='2017-12-31',
frequency="d", adjustflag="3")
print('query_history_k_data_plus respond error_code:'+rs.error_code)
print('query_history_k_data_plus respond error_msg:'+rs.error_msg)

Ex-rights and ex-dividend information: query_dividend_data()

1
2
3
4
5
6
7
8
9
10

rs_list = []


rs_dividend_2017 = bs.query_dividend_data(code="sh.600000", year="2017", yearType="report")
while (rs_dividend_2017.error_code == '0') & rs_dividend_2017.next():
rs_list.append(rs_dividend_2017.get_row_data())

result_dividend = pd.DataFrame(rs_list, columns=rs_dividend_2017.fields)

Basic information on securities:query_stock_basic()

1
2
3
4
5
6
7
## 登陆过程略
# 获取证券基本资料
rs = bs.query_stock_basic(code="sh.600000")
# rs = bs.query_stock_basic(code_name="浦发银行") # 支持模糊查询
print('query_stock_basic respond error_code:'+rs.error_code)
print('query_stock_basic respond error_msg:'+rs.error_msg)
## 登出过程略

Quarterly frequency company performance snapshot:query_performance_express_report()

1
2
3
4
5
6
7
8
9
10
11

rs = bs.query_performance_express_report("sh.600000", start_date="2015-01-01", end_date="2017-12-31")
print('query_performance_express_report respond error_code:'+rs.error_code)
print('query_performance_express_report respond error_msg:'+rs.error_msg)

result_list = []
while (rs.error_code == '0') & rs.next():
result_list.append(rs.get_row_data())

result = pd.DataFrame(result_list, columns=rs.fields)

Comparison of libraries

There’s a picture attached. It’s all in the picture.

Crawling data through web pages

Project structure

wKioL1YyHhfTV5ZQAAE6J6--0Gg957.jpg

database connection pool

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## connectionpool.py
#-*- coding: UTF-8 -*-
'''
create a connection pool
'''
from DBUtils import PooledDB
import MySQLdb
import string
maxconn = 30
mincached = 10
maxcached = 20
maxshared = 30
connstring="root#root#127.0.0.1#3307#pystock#utf8"
dbtype = "mysql"
def createConnectionPool(connstring, dbtype):
db_conn = connstring.split("#");
if dbtype=='mysql':
try:
pool = PooledDB.PooledDB(MySQLdb, user=db_conn[0],passwd=db_conn[1],host=db_conn[2],port=string.atoi(db_conn[3]),db=db_conn[4],charset=db_conn[5], mincached=mincached,maxcached=maxcached,maxshared=maxshared,maxconnections=maxconn)
return pool
except Exception, e:
raise Exception,'conn datasource Excepts,%s!!!(%s).'%(db_conn[2],str(e))
return None
pool = createConnectionPool(connstring, dbtype)

database operation

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
## DBOperator.py
#-*- coding: UTF-8 -*-
'''
Created on 2015-3-13
@author: Casey
'''
import MySQLdb
from stockmining.stocks.setting import LoggerFactory
import connectionpool
class DBOperator(object):

def __init__(self):
self.logger = LoggerFactory.getLogger('DBOperator')
#self.conn = None

def connDB(self):
#单连接
#self.conn=MySQLdb.connect(host="127.0.0.1",user="root",passwd="root",db="pystock",port=3307,charset="utf8")
#连接池中获取连接
self.conn=connectionpool.pool.connection()
return self.conn
def closeDB(self):
if(self.conn != None):
self.conn.close()


def insertIntoDB(self, table, dict):
try:
if(self.conn != None):
cursor = self.conn.cursor()
else:
raise MySQLdb.Error('No connection')

sql = "insert into " + table + "("
param = []
for key in dict:
sql += key + ','
param.append(dict.get(key))
param = tuple(param)
sql = sql[:-1] + ") values("
for i in range(len(dict)):
sql += "%s,"
sql = sql[:-1] + ")"

self.logger.debug(sql % param)
n = cursor.execute(sql, param)
self.conn.commit()
cursor.close()
except MySQLdb.Error,e:
self.logger.error("Mysql Error %d: %s" % (e.args[0], e.args[1]))
self.conn.rollback()
def execute(self, sql):
try:
if(self.conn != None):
cursor = self.conn.cursor()
else:
raise MySQLdb.Error('No connection')

n = cursor.execute(sql)
return n
except MySQLdb.Error,e:
self.logger.error("Mysql Error %d: %s" % (e.args[0], e.args[1]))

def findBySQL(self, sql):
try:
if(self.conn != None):
cursor = self.conn.cursor()
else:
raise MySQLdb.Error('No connection')

cursor.execute(sql)
rows = cursor.fetchall()
return rows
except MySQLdb.Error,e:
self.logger.error("Mysql Error %d: %s" % (e.args[0], e.args[1]))

def findByCondition(self, table, fields, wheres):
try:
if(self.conn != None):
cursor = self.conn.cursor()
else:
raise MySQLdb.Error('No connection')

sql = "select "
for field in fields:
sql += field + ","
sql = sql[:-1] + " from " + table + " where "

param = []
values = ''
for where in wheres:
sql += where.key + "='%s' and "
param.append(where.value)
param = tuple(param)
self.logger.debug(sql)

n = cursor.execute(sql[:-5] % param)
self.conn.commit()
cursor.close()
except MySQLdb.Error,e:
self.logger.error("Mysql Error %d: %s" % (e.args[0], e.args[1]))

log module

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
## LoggerFactory.py
#-*- coding: UTF-8 -*-
'''
Created on 2015-3-11
@author: Casey
'''
import logging
import time
'''
传入名称
'''
def getLogger(name):
now = time.strftime('%Y-%m-%d %H:%M:%S')

logging.basicConfig(
level = logging.DEBUG,
format = now +" : " + name + ' LINE %(lineno)-4d %(levelname)-8s %(message)s',
datefmt = '%m-%d %H:%M',
filename = "d:\\stocks\stock.log",
filemode = 'w');

console = logging.StreamHandler();
console.setLevel(logging.DEBUG);
formatter = logging.Formatter(name + ': LINE %(lineno)-4d : %(levelname)-8s %(message)s');
console.setFormatter(formatter);

logger = logging.getLogger(name)
logger.addHandler(console);
return logger

if __name__ == '__main__':
getLogger("www").debug("www")

Getting Stock Historical Data - Single Threaded Mode

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
81
82
83
84
85
86
87
88
89
90
## stockyahoo.py
#-*- coding: UTF-8 -*-
'''
Created on 2015-3-1
@author: Casey
'''
import urllib
import re
import sys
from setting import params
import urllib2
from db import *
dbOperator = DBOperator()
table = "stock_quote_yahoo"
'''查找指定日期股票流量'''
def isStockExitsInDate(table, stock, date):
sql = "select * from " + table + " where code = '%d' and date='%s'" % (stock, date)
n = dbOperator.execute(sql)
if n >= 1:
return True

def getHistoryStockData(code, dataurl):
try:
r = urllib2.Request(dataurl)
try:
stdout = urllib2.urlopen(r, data=None, timeout=3)
except Exception,e:
print ">>>>>> Exception: " +str(e)
return None

stdoutInfo = stdout.read().decode(params.codingtype).encode('utf-8')
tempData = stdoutInfo.replace('"', '')
stockQuotes = []
if tempData.find('404') != -1: stockQuotes = tempData.split("\n")

stockDetail = {}
for stockQuote in stockQuotes:
stockInfo = stockQuote.split(",")
if len(stockInfo) == 7 and stockInfo[0]!='Date':
if not isStockExitsInDate(table, code, stockInfo[0]):
stockDetail["date"] = stockInfo[0]
stockDetail["open"] = stockInfo[1] #开盘
stockDetail["high"] = stockInfo[2] #最高
stockDetail["low"] = stockInfo[3] #最低
stockDetail["close"] = stockInfo[4] #收盘
stockDetail["volume"] = stockInfo[5] #交易量
stockDetail["adj_close"] = stockInfo[6] #收盘adj价格
stockDetail["code"] = code #代码
dbOperator.insertIntoDB(table, stockDetail)
result = tempData
except Exception as err:
print ">>>>>> Exception: " + str(dataurl) + " " + str(err)
else:
return result
finally:
None

def get_stock_history():
#沪市2005-2015历史数据
for code in range(601999, 602100):
dataUrl = "http://ichart.yahoo.com/table.csv?s=%d.SS&a=01&b=01&c=2005&d=01&e=01&f=2015&g=d" % code
print getHistoryStockData(code, dataUrl )


#深市2005-2015历史数据
for code in range(1, 1999):
dataUrl = "http://ichart.yahoo.com/table.csv?s=%06d.SZ&a=01&b=01&c=2005&d=01&e=01&f=2015&g=d" % code
print getHistoryStockData(code, dataUrl)

#中小板股票
for code in range(2001, 2999):
dataUrl = "http://ichart.yahoo.com/table.csv?s=%06d.SZ&a=01&b=01&c=2005&d=01&e=01&f=2015&g=d" % code
print getHistoryStockData(code, dataUrl)


#创业板股票
for code in range(300001, 300400):
dataUrl = "http://ichart.yahoo.com/table.csv?s=%d.SZ&a=01&b=01&c=2005&d=01&e=01&f=2015&g=d" % code
print getHistoryStockData(code, dataUrl)


def main():
"main function"

dbOperator.connDB()
get_stock_history()
dbOperator.closeDB()

if __name__ == '__main__':
main()

Get stock history data - multi-threaded mode

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#-*- coding: UTF-8 -*- 
'''
Created on 2015年3月2日
@author: Casey
'''
import time
import threading
'''
上证编码:'600001' .. '602100'
深圳编码:'000001' .. '001999'
中小板:'002001' .. '002999'
创业板:'300001' .. '300400'
'''
import urllib2
from datetime import date
from db import *
from setting import *
class StockTencent(object):
#数据库表
__stockTables = {'cash':'stock_cash_tencent','quotation':'stock_quotation_tencent'}
'''初始化'''
def __init__(self):
self.__logger = LoggerFactory.getLogger('StockTencent')
self.__dbOperator = DBOperator()

def main(self):
self.__dbOperator.connDB()
threading.Thread(target = self.getStockCash).start()
threading.Thread(target = self.getStockQuotation).start()
self.__dbOperator.closeDB()

'''查找指定日期股票流量'''
def __isStockExitsInDate(self, table, stock, date):
sql = "select * from " + table + " where code = '%s' and date='%s'" % (stock, date)
n = self.__dbOperator.execute(sql)
if n >= 1:
return True

'''获取股票资金流明细'''
def __getStockCashDetail(self, dataUrl):
#读取数据
tempData = self.__getDataFromUrl(dataUrl)

if tempData == None:
time.sleep(10)
tempData = self.__getDataFromUrl(dataUrl)
return False

#解析资金流向数据
stockCash = {}
stockInfo = tempData.split('~')
if len(stockInfo) < 13: return
if len(stockInfo) != 0 and stockInfo[0].find('pv_none') == -1:
table = self.__stockTables['cash']
code = stockInfo[0].split('=')[1][2:]
date = stockInfo[13]
if not self.__isStockExitsInDate(table, code, date):
stockCash['code'] = stockInfo[0].split('=')[1][2:]
stockCash['main_in_cash'] = stockInfo[1]
stockCash['main_out_cash'] = stockInfo[2]
stockCash['main_net_cash'] = stockInfo[3]
stockCash['main_net_rate'] = stockInfo[4]
stockCash['private_in_cash'] = stockInfo[5]
stockCash['private_out_cash'] = stockInfo[6]
stockCash['private_net_cash'] = stockInfo[7]
stockCash['private_net_rate'] = stockInfo[8]
stockCash['total_cash'] = stockInfo[9]
stockCash['name'] = stockInfo[12].decode('utf8')
stockCash['date'] = stockInfo[13]
#插入数据库
self.__dbOperator.insertIntoDB(table, stockCash)

'''获取股票交易信息明细'''
def getStockQuotationDetail(self, dataUrl):
tempData = self.__getDataFromUrl(dataUrl)

if tempData == None:
time.sleep(10)
tempData = self.__getDataFromUrl(dataUrl)
return False

stockQuotation = {}
stockInfo = tempData.split('~')
if len(stockInfo) < 45: return
if len(stockInfo) != 0 and stockInfo[0].find('pv_none') ==-1 and stockInfo[3].find('0.00') == -1:
table = self.__stockTables['quotation']
code = stockInfo[2]
date = stockInfo[30]
if not self.__isStockExitsInDate(table, code, date):
stockQuotation['code'] = stockInfo[2]
stockQuotation['name'] = stockInfo[1].decode('utf8')
stockQuotation['price'] = stockInfo[3]
stockQuotation['yesterday_close'] = stockInfo[4]
stockQuotation['today_open'] = stockInfo[5]
stockQuotation['volume'] = stockInfo[6]
stockQuotation['outer_sell'] = stockInfo[7]
stockQuotation['inner_buy'] = stockInfo[8]
stockQuotation['buy_one'] = stockInfo[9]
stockQuotation['buy_one_volume'] = stockInfo[10]
stockQuotation['buy_two'] = stockInfo[11]
stockQuotation['buy_two_volume'] = stockInfo[12]
stockQuotation['buy_three'] = stockInfo[13]
stockQuotation['buy_three_volume'] = stockInfo[14]
stockQuotation['buy_four'] = stockInfo[15]
stockQuotation['buy_four_volume'] = stockInfo[16]
stockQuotation['buy_five'] = stockInfo[17]
stockQuotation['buy_five_volume'] = stockInfo[18]
stockQuotation['sell_one'] = stockInfo[19]
stockQuotation['sell_one_volume'] = stockInfo[20]
stockQuotation['sell_two'] = stockInfo[22]
stockQuotation['sell_two_volume'] = stockInfo[22]
stockQuotation['sell_three'] = stockInfo[23]
stockQuotation['sell_three_volume'] = stockInfo[24]
stockQuotation['sell_four'] = stockInfo[25]
stockQuotation['sell_four_volume'] = stockInfo[26]
stockQuotation['sell_five'] = stockInfo[27]
stockQuotation['sell_five_volume'] = stockInfo[28]
stockQuotation['datetime'] = stockInfo[30]
stockQuotation['updown'] = stockInfo[31]
stockQuotation['updown_rate'] = stockInfo[32]
stockQuotation['heighest_price'] = stockInfo[33]
stockQuotation['lowest_price'] = stockInfo[34]
stockQuotation['volume_amout'] = stockInfo[35].split('/')[2]
stockQuotation['turnover_rate'] = stockInfo[38]
stockQuotation['pe_rate'] = stockInfo[39]
stockQuotation['viberation_rate'] = stockInfo[42]
stockQuotation['circulated_stock'] = stockInfo[43]
stockQuotation['total_stock'] = stockInfo[44]
stockQuotation['pb_rate'] = stockInfo[45]
self.__dbOperator.insertIntoDB(table, stockQuotation)
'''读取信息'''
def __getDataFromUrl(self, dataUrl):
r = urllib2.Request(dataUrl)
try:
stdout = urllib2.urlopen(r, data=None, timeout=3)
except Exception,e:
self.__logger.error(">>>>>> Exception: " +str(e))
return None

stdoutInfo = stdout.read().decode(params.codingtype).encode('utf-8')
tempData = stdoutInfo.replace('"', '')
self.__logger.debug(tempData)
return tempData

'''获取股票现金流量'''
def getStockCash(self):
self.__logger.debug("开始:收集股票现金流信息")
try:
#沪市股票
for code in range(600001, 602100):
dataUrl = "http://qt.gtimg.cn/q=ff_sh%d" % code
self.__getStockCashDetail(dataUrl)

#深市股票
for code in range(1, 1999):
dataUrl = "http://qt.gtimg.cn/q=ff_sz%06d" % code
self.__getStockCashDetail(dataUrl)

#中小板股票
for code in range(2001, 2999):
dataUrl = "http://qt.gtimg.cn/q=ff_sz%06d" % code
self.__getStockCashDetail(dataUrl)

#'300001' .. '300400'
#创业板股票
for code in range(300001, 300400):
dataUrl = "http://qt.gtimg.cn/q=ff_sz%d" % code
self.__getStockCashDetail(dataUrl)

except Exception as err:
self.__logger.error(">>>>>> Exception: " +str(code) + " " + str(err))
finally:
None
self.__logger.debug("结束:股票现金流收集")

'''获取股票交易行情数据'''
def getStockQuotation(self):
self.__logger.debug("开始:收集股票交易行情数据")
try:
#沪市股票
for code in range(600001, 602100):
dataUrl = "http://qt.gtimg.cn/q=sh%d" % code
self.getStockQuotationDetail(dataUrl)

#深市股票
for code in range(1, 1999):
dataUrl = "http://qt.gtimg.cn/q=sz%06d" % code
self.getStockQuotationDetail(dataUrl)

#中小板股票
for code in range(2001, 2999):
dataUrl = "http://qt.gtimg.cn/q=sz%06d" % code
self.getStockQuotationDetail(dataUrl)

#'300001' .. '300400'
# 创业板股票
for code in range(300001, 300400):
dataUrl = "http://qt.gtimg.cn/q=sz%d" % code
self.getStockQuotationDetail(dataUrl)

except Exception as err:
self.__logger.error(">>>>>> Exception: " +str(code) + " " + str(err))
finally:
None
self.__logger.debug("结束:收集股票交易行情数据")

if __name__ == '__main__':
StockTencent(). main()

A few commonly used interfaces for getting stock data

Brief Information

  1. Sending address

    1
    http://qt.gtimg.cn/q=s_sh600519 // sh600519是贵州茅台的股票代码,根据自己的关键字拼接URL
  2. Message received

    1
    v_s_sh600519="1~贵州茅台~600519~2075.95~42.95~2.11~38930~794942~~26078.04~GP-A";
  3. Here represents the summary information, which is the stock code we spliced in, followed in quotes by the summary information of the specific stock found, separated by the “~” sign, the specific information of the stock

    序号 返回值 含义
    1 1 Representative exchanges, 200-US (us), 100-HK (hk), 51-SZ (sz), 1-SH (sh)
    2 贵州茅台 Stock Names
    3 600519 stock code (computing)
    4 2075.95 current price
    5 42.95 rise or fall in price
    6 2.11 % increase/decrease
    7 38930 turnover
    8 794942 turnover
    9 26078.04 -
    10 GP-A total market value

Daily K-line data Returns json format

1
2
3
4
5
6
7
8
9

https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param=usAAPL.OQ,day,2020-3-1,2021-3-1,500,qfq

https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param=sh600519,day,2020-3-1,2021-3-1,500,qfq
https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param=sh600519,day,2020-3-1,2021-3-1,500,hfq
https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param=sh600519,day,2020-3-1,2021-3-1,500,bfq


https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param=hk01810,day,2020-3-1,2021-3-1,500,qfq

Minute data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

http://web.ifzq.gtimg.cn/appstock/app/minute/query?code=sh600103


http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sz000413,m1,,32000&_var=m1_today

http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sz000413,m5,,32000&_var=m5_today

http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sz000413,m15,,32000&_var=m15_today

http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sz000413,m30,,32000&_var=m30_today

http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sz000413,m60,,32000&_var=m60_today


http://web.ifzq.gtimg.cn/appstock/app/minute/query?_var=min_data_sh600103&code=sh600103&r=0.12174363108800135

Getting Historical Data via Stock Software

Download data to local via Tom Tom Tom

I don’t need to tell you more about this.

Run the following code

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
# coding: UTF-8
import os
from struct import *

def day2csv_data(dirname, fname, targetDir):
ofile = open(dirname + os.sep + fname, 'rb')
buf = ofile.read()
ofile.close()
ifile = open(targetDir + os.sep + fname + '.csv', 'w')
num = len(buf)
no = num // 32
b = 0
e = 32
line = ''
linename = str('date') + ',' + str('open') + ', ' + str('high') + ' ,' + str('low') + ', ' + str('close') + ' ,' + str('amount') + ', ' + str('vol') + ' ,' + str('str07') + '' + '\n'
ifile.write(linename)
for i in range(int(no)):
a = unpack('IIIIIfII', buf[b:e])
line = str(a[0]) + ',' + str(a[1] / 100.0) + ', ' + str(a[2] / 100.0) + ' ,' + str(a[3] / 100.0) + ', ' + str(a[4] / 100.0) + ' ,' + str(a[5]) + ', ' + str(a[6]) + ' ,' + str(a[7]) + '' + '\n'
ifile.write(line)
b = b + 32
e = e + 32
ifile.close()

pathdir = 'X:\\股票\\解析通达信day日线数据\\day'
targetDir = 'X:\\股票\\解析通达信day日线数据\\day'
listfile = os.listdir(pathdir)
for f in listfile:
day2csv_data(pathdir, f, targetDir)
else:
print('The for ' + pathdir + ' to ' + targetDir + ' loop is over')

Buying direct

Direct purchase is to do without technical content is also the most efficient, but still the words do not forget the beginning of the always. Some people’s goal is Rome, some people were born in Rome, this is not comparable, may we soon reach the end of the heart.