Files
stock/stockapp/bak_get_hs300_his.py

104 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pymysql
import time
import logging
from futu import *
from datetime import datetime, timedelta
# 配置日志
logging.basicConfig(filename='./log/hs300_fetch.log', level=logging.INFO,
format='%(asctime)s %(levelname)s: %(message)s')
# MySQL 配置
db_config = {
'host': '172.18.0.2',
'user': 'root',
'password': 'mysqlpw',
'database': 'stockdb'
}
# 连接 MySQL
connection = pymysql.connect(**db_config)
# 获取当前日期
end_date = datetime.now().strftime('%Y-%m-%d')
# 计算 start_date 为当前日期减去10年再加一天
start_date = (datetime.now() - timedelta(days=365*10-1)).strftime('%Y-%m-%d')
# 定义插入数据的函数
def insert_data(connection, data):
try:
with connection.cursor() as cursor:
for index, row in data.iterrows():
sql = """
INSERT INTO hs300_qfq_his
(code, name, time_key, open, close, high, low, pe_ratio, turnover_rate, volume, turnover, change_rate, last_close)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
name = VALUES(name),
open = VALUES(open),
close = VALUES(close),
high = VALUES(high),
low = VALUES(low),
pe_ratio = VALUES(pe_ratio),
turnover_rate = VALUES(turnover_rate),
volume = VALUES(volume),
turnover = VALUES(turnover),
change_rate = VALUES(change_rate),
last_close = VALUES(last_close)
"""
cursor.execute(sql, (
row['code'], row['name'], row['time_key'], row['open'], row['close'],
row['high'], row['low'], row['pe_ratio'], row['turnover_rate'],
row['volume'], row['turnover'], row['change_rate'], row['last_close']
))
connection.commit()
except pymysql.MySQLError as e:
logging.error(f"Error occurred while inserting data: {e}")
print(f"Error occurred while inserting data: {e}")
# 获取 hs300 表中的所有股票代码
def get_hs300_codes():
with connection.cursor() as cursor:
cursor.execute("SELECT code FROM hs300")
return cursor.fetchall()
# 初始化 futu 行情连接
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
try:
hs300_codes = get_hs300_codes()
for code_row in hs300_codes:
code = code_row[0] # 从数据库行中提取 code
# 获取历史 K 线数据,设置分页请求
ret, data, page_req_key = quote_ctx.request_history_kline(code, start=start_date, end=end_date, max_count=500)
if ret == RET_OK:
logging.info(f"成功获取 {code} 的第一页数据,共 {len(data)}")
print(f"成功获取 {code} 的第一页数据,共 {len(data)}")
# 插入第一页数据
insert_data(connection, data)
else:
logging.error(f"获取 {code} 的数据失败: {data}")
print(f"获取 {code} 的数据失败: {data}")
# 分页拉取
while page_req_key is not None:
time.sleep(5) # 休眠 5 秒
ret, data, page_req_key = quote_ctx.request_history_kline(code, start=start_date, end=end_date, max_count=500, page_req_key=page_req_key)
if ret == RET_OK:
logging.info(f"成功获取 {code} 的分页数据,共 {len(data)}")
print(f"成功获取 {code} 的分页数据,共 {len(data)}")
# 插入分页数据
insert_data(connection, data)
else:
logging.error(f"分页数据获取失败: {data}")
print(f"分页数据获取失败: {data}")
# 每次获取完一个股票的数据后,休眠 5 秒
time.sleep(5)
finally:
quote_ctx.close() # 关闭 futu 连接
connection.close() # 关闭 MySQL 连接