90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""创建异动检测所需的数据库表"""
|
|
|
|
import sys
|
|
from sqlalchemy import create_engine, text
|
|
|
|
engine = create_engine('mysql+pymysql://root:Zzl5588161!@222.128.1.157:33060/stock', echo=False)
|
|
|
|
# 删除旧表
|
|
drop_sql1 = 'DROP TABLE IF EXISTS concept_minute_alert'
|
|
drop_sql2 = 'DROP TABLE IF EXISTS index_minute_snapshot'
|
|
|
|
# 创建 concept_minute_alert 表
|
|
# 支持 Z-Score + SVM 智能检测
|
|
sql1 = '''
|
|
CREATE TABLE concept_minute_alert (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
concept_id VARCHAR(32) NOT NULL,
|
|
concept_name VARCHAR(100) NOT NULL,
|
|
alert_time DATETIME NOT NULL,
|
|
alert_type VARCHAR(20) NOT NULL COMMENT 'surge_up=暴涨, surge_down=暴跌, limit_up=涨停增加, rank_jump=排名跃升',
|
|
trade_date DATE NOT NULL,
|
|
change_pct DECIMAL(10,4) COMMENT '当前涨跌幅',
|
|
prev_change_pct DECIMAL(10,4) COMMENT '之前涨跌幅',
|
|
change_delta DECIMAL(10,4) COMMENT '涨跌幅变化',
|
|
limit_up_count INT DEFAULT 0 COMMENT '涨停数',
|
|
prev_limit_up_count INT DEFAULT 0,
|
|
limit_up_delta INT DEFAULT 0,
|
|
limit_down_count INT DEFAULT 0 COMMENT '跌停数',
|
|
rank_position INT COMMENT '当前排名',
|
|
prev_rank_position INT COMMENT '之前排名',
|
|
rank_delta INT COMMENT '排名变化(负数表示上升)',
|
|
index_code VARCHAR(20) DEFAULT '000001.SH',
|
|
index_price DECIMAL(12,4),
|
|
index_change_pct DECIMAL(10,4),
|
|
stock_count INT,
|
|
concept_type VARCHAR(20) DEFAULT 'leaf',
|
|
zscore DECIMAL(8,4) COMMENT 'Z-Score值',
|
|
importance_score DECIMAL(6,4) COMMENT '重要性评分(0-1)',
|
|
extra_info JSON COMMENT '扩展信息(包含zscore,svm_score等)',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
INDEX idx_trade_date (trade_date),
|
|
INDEX idx_alert_time (alert_time),
|
|
INDEX idx_concept_id (concept_id),
|
|
INDEX idx_alert_type (alert_type),
|
|
INDEX idx_trade_date_time (trade_date, alert_time),
|
|
INDEX idx_importance (importance_score)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='概念异动记录表(智能版)'
|
|
'''
|
|
|
|
# 创建 index_minute_snapshot 表
|
|
sql2 = '''
|
|
CREATE TABLE index_minute_snapshot (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
index_code VARCHAR(20) NOT NULL,
|
|
trade_date DATE NOT NULL,
|
|
snapshot_time DATETIME NOT NULL,
|
|
price DECIMAL(12,4),
|
|
open_price DECIMAL(12,4),
|
|
high_price DECIMAL(12,4),
|
|
low_price DECIMAL(12,4),
|
|
prev_close DECIMAL(12,4),
|
|
change_pct DECIMAL(10,4),
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE KEY uk_index_time (index_code, snapshot_time),
|
|
INDEX idx_trade_date (trade_date)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
|
'''
|
|
|
|
if __name__ == '__main__':
|
|
print('正在重建数据库表...\n')
|
|
|
|
with engine.begin() as conn:
|
|
# 先删除旧表
|
|
print('删除旧表...')
|
|
conn.execute(text(drop_sql1))
|
|
print(' - concept_minute_alert 已删除')
|
|
conn.execute(text(drop_sql2))
|
|
print(' - index_minute_snapshot 已删除')
|
|
|
|
# 创建新表
|
|
print('\n创建新表...')
|
|
conn.execute(text(sql1))
|
|
print(' ✅ concept_minute_alert 表创建成功')
|
|
conn.execute(text(sql2))
|
|
print(' ✅ index_minute_snapshot 表创建成功')
|
|
|
|
print('\n✅ 所有表创建完成!')
|