update get data code
This commit is contained in:
@@ -1,23 +1,21 @@
|
||||
#
|
||||
# 使用筛选器来更新股票列表
|
||||
#
|
||||
|
||||
from futu import *
|
||||
import time
|
||||
import logging
|
||||
from typing import List, Dict
|
||||
from MySQLHelper import MySQLHelper # 假设您已有MySQLHelper类
|
||||
from LogHelper import LogHelper
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.FileHandler('Debug.log', encoding='utf-8'), # 关键在这里
|
||||
logging.StreamHandler()
|
||||
]
|
||||
)
|
||||
# 基本用法(自动创建日期日志+控制台输出)
|
||||
logger = LogHelper().setup()
|
||||
|
||||
class FutuDirectDataImporter:
|
||||
def __init__(self, db_config: Dict):
|
||||
class FutuStockFilter:
|
||||
def __init__(self, db_config:dict):
|
||||
self.db_config = db_config
|
||||
self.table_name = "conditionalselection"
|
||||
self.table_name = "stock_filter" # 筛选后的股票信息
|
||||
self.quote_ctx = None
|
||||
|
||||
def connect_to_futu(self) -> bool:
|
||||
@@ -34,29 +32,15 @@ class FutuDirectDataImporter:
|
||||
if self.quote_ctx:
|
||||
self.quote_ctx.close()
|
||||
|
||||
# 准备数据结构,后面可以根据实际需要再增加,
|
||||
# stock_filter 表格中的数据每日更新
|
||||
def prepare_db_structure(self) -> bool:
|
||||
"""准备数据库表结构"""
|
||||
create_table_sql = f"""
|
||||
CREATE TABLE IF NOT EXISTS `{self.table_name}` (
|
||||
`stock_code` varchar(255) NOT NULL,
|
||||
`stock_name` varchar(255) DEFAULT NULL,
|
||||
`cur_price` float DEFAULT NULL,
|
||||
`price` float DEFAULT NULL,
|
||||
`high_price_to_highest_52weeks_ratio` float DEFAULT NULL,
|
||||
`low_price_to_lowest_52weeks_ratio` float DEFAULT NULL,
|
||||
`volume` float DEFAULT NULL,
|
||||
`turnover` float DEFAULT NULL,
|
||||
`turnover_rate` float DEFAULT NULL,
|
||||
`change_rate` float DEFAULT NULL,
|
||||
`amplitude` float DEFAULT NULL,
|
||||
`pe_ttm` float DEFAULT NULL,
|
||||
`pb_rate` float DEFAULT NULL,
|
||||
`market_val` float DEFAULT NULL,
|
||||
`total_share` float DEFAULT NULL,
|
||||
`float_share` float DEFAULT NULL,
|
||||
`float_market_val` float DEFAULT NULL,
|
||||
`basic_eps` float DEFAULT NULL,
|
||||
`diluted_eps` float DEFAULT NULL,
|
||||
UNIQUE KEY `idx_stock_code` (`stock_code`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
"""
|
||||
@@ -79,40 +63,7 @@ class FutuDirectDataImporter:
|
||||
'stock_code': item.stock_code,
|
||||
'stock_name': item.stock_name,
|
||||
'float_share': item.float_share
|
||||
}
|
||||
# data = {
|
||||
# 'stock_code': item.stock_code,
|
||||
# 'stock_name': item.stock_name,
|
||||
# 'cur_price': item.cur_price,
|
||||
# 'price': item.cur_price,
|
||||
# 'high_price_to_highest_52weeks_ratio': item.high_price_to_highest_52weeks_ratio,
|
||||
# 'low_price_to_lowest_52weeks_ratio': item.low_price_to_lowest_52weeks_ratio,
|
||||
# 'volume': item.volume,
|
||||
# 'turnover': item.turnover,
|
||||
# 'turnover_rate': item.turnover_rate,
|
||||
# 'change_rate': item.change_rate,
|
||||
# 'amplitude': item.amplitude,
|
||||
# 'pe_ttm': item.pe_ttm,
|
||||
# 'pb_rate': item.pb,
|
||||
# 'market_val': item.market_val,
|
||||
# 'total_share': item.total_shares,
|
||||
# 'float_share': item.float_shares,
|
||||
# 'float_market_val': item.float_market_val,
|
||||
# 'basic_eps': item.basic_eps,
|
||||
# 'diluted_eps': item.diluted_eps
|
||||
# }
|
||||
|
||||
# # 计算衍生字段
|
||||
# if data['cur_price'] and data['high_price_to_highest_52weeks_ratio']:
|
||||
# data['cur_price_to_highest_52weeks_ratio'] = (
|
||||
# data['cur_price'] / data['high_price_to_highest_52weeks_ratio']
|
||||
# )
|
||||
|
||||
# if data['cur_price'] and data['low_price_to_lowest_52weeks_ratio']:
|
||||
# data['cur_price_to_lowest_52weeks_ratio'] = (
|
||||
# data['cur_price'] / data['low_price_to_lowest_52weeks_ratio']
|
||||
# )
|
||||
|
||||
}
|
||||
processed_data.append(data)
|
||||
except Exception as e:
|
||||
logging.error(f"处理股票 {getattr(item, 'stock_code', '未知')} 数据失败: {str(e)}")
|
||||
@@ -126,12 +77,7 @@ class FutuDirectDataImporter:
|
||||
|
||||
# 准备SQL
|
||||
columns = [
|
||||
'stock_code', 'stock_name', 'cur_price', 'price',
|
||||
'high_price_to_highest_52weeks_ratio', 'low_price_to_lowest_52weeks_ratio',
|
||||
'volume', 'turnover', 'turnover_rate', 'change_rate', 'amplitude',
|
||||
'pe_ttm', 'pb_rate', 'market_val', 'total_share', 'float_share',
|
||||
'float_market_val', 'basic_eps', 'diluted_eps',
|
||||
'cur_price_to_highest_52weeks_ratio', 'cur_price_to_lowest_52weeks_ratio'
|
||||
'stock_code', 'stock_name', 'float_share'
|
||||
]
|
||||
|
||||
placeholders = ', '.join(['%s'] * len(columns))
|
||||
@@ -167,19 +113,13 @@ class FutuDirectDataImporter:
|
||||
self.disconnect()
|
||||
return False
|
||||
|
||||
logging.info(f"================= start ===================")
|
||||
simple_filter = SimpleFilter()
|
||||
simple_filter.filter_min = 0
|
||||
simple_filter.filter_max = 100000000000000
|
||||
simple_filter.stock_field = StockField.FLOAT_SHARE
|
||||
simple_filter.is_no_filter = False
|
||||
|
||||
# simple_filter1 = SimpleFilter()
|
||||
# simple_filter1.filter_min = 0
|
||||
# simple_filter1.filter_max = 100000000000000000000
|
||||
# simple_filter1.stock_field = StockField.CUR_PRICE
|
||||
# simple_filter1.is_no_filter = False
|
||||
|
||||
|
||||
nBegin = 0
|
||||
last_page = False
|
||||
total_imported = 0
|
||||
@@ -210,25 +150,11 @@ class FutuDirectDataImporter:
|
||||
time.sleep(3) # 避免触发限频
|
||||
|
||||
logging.info(f"导入完成! 共导入 {total_imported} 条记录")
|
||||
logging.info(f"================= end ===================")
|
||||
return total_imported > 0
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"导入过程中发生错误: {str(e)}")
|
||||
return False
|
||||
finally:
|
||||
self.disconnect()
|
||||
|
||||
# 使用示例
|
||||
if __name__ == "__main__":
|
||||
|
||||
# 数据库配置
|
||||
db_config = {
|
||||
'host': 'localhost',
|
||||
'user': 'root',
|
||||
'password': 'bzskmysql',
|
||||
'database': 'fullmarketdata_hk'
|
||||
}
|
||||
|
||||
# 创建导入器并运行
|
||||
importer = FutuDirectDataImporter(db_config)
|
||||
importer.run_direct_import()
|
||||
self.disconnect()
|
||||
Reference in New Issue
Block a user