Repository navigation
Expand file tree
/
Copy pathdata_manager.py
More file actions
849 lines (706 loc) · 30.6 KB
/
Copy pathdata_manager.py
File metadata and controls
849 lines (706 loc) · 30.6 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
"""
数据管理模块,负责历史数据的获取与存储
"""
import os
import pandas as pd
import sqlite3
import time
from datetime import datetime, timedelta
import threading
import warnings
# 忽略pandas的FutureWarning警告(来自mootdx库)
warnings.filterwarnings('ignore', category=FutureWarning, module='mootdx')
warnings.filterwarnings('ignore', message='.*fillna.*method.*deprecated.*')
warnings.filterwarnings('ignore', message='.*Downcasting object dtype arrays.*')
import xtquant.xtdata as xt
import Methods
import config
from logger import get_logger
# from realtime_data_manager import get_realtime_data_manager
# 获取logger
logger = get_logger("data_manager")
class DataManager:
"""数据管理类,处理历史行情数据的获取与存储"""
def __init__(self):
"""初始化数据管理器"""
# 创建数据目录
if not os.path.exists(config.DATA_DIR):
os.makedirs(config.DATA_DIR)
# 连接数据库
self.conn = self._connect_db()
# 创建表结构
self._create_tables()
# 已订阅的股票代码列表
self.subscribed_stocks = []
# # 初始化行情接口
self._init_xtquant()
# self.realtime_manager = get_realtime_data_manager()
# 数据更新线程
self.update_thread = None
self.stop_flag = False
def _init_xtquant(self):
"""初始化迅投行情接口 - 使用共享连接"""
try:
import xtquant.xtdata as xt
self.xt = xt
if xt.connect():
logger.info("xtquant行情服务连接成功")
else:
logger.error("xtquant行情服务连接失败")
self.xt = None
return
# 验证连接状态
self._verify_connection()
except Exception as e:
logger.error(f"初始化迅投行情接口出错: {str(e)}")
self.xt = None
def _verify_connection(self):
"""验证连接状态"""
try:
# 使用一个简单的测试来验证连接
test_codes = ['000001.SZ'] # 测试股票
test_data = self.xt.get_full_tick(test_codes)
if test_data:
logger.debug("xtquant连接状态验证成功")
return True
else:
logger.warning("xtquant连接状态验证失败")
return False
except Exception as e:
logger.warning(f"xtquant连接验证出错: {str(e)}")
return False
def _connect_db(self):
"""连接SQLite数据库"""
try:
conn = sqlite3.connect(config.DB_PATH, check_same_thread=False)
logger.info(f"已连接数据库: {config.DB_PATH}")
return conn
except Exception as e:
logger.error(f"连接数据库失败: {str(e)}")
raise
def _create_tables(self):
"""创建数据表结构"""
cursor = self.conn.cursor()
# 创建股票历史数据表
cursor.execute('''
CREATE TABLE IF NOT EXISTS stock_daily_data (
stock_code TEXT,
stock_name TEXT,
date TEXT,
open REAL,
high REAL,
low REAL,
close REAL,
volume REAL,
amount REAL,
PRIMARY KEY (stock_code, date)
)
''')
# 创建指标数据表
cursor.execute('''
CREATE TABLE IF NOT EXISTS stock_indicators (
stock_code TEXT,
date TEXT,
ma10 REAL,
ma20 REAL,
ma30 REAL,
ma60 REAL,
macd REAL,
macd_signal REAL,
macd_hist REAL,
PRIMARY KEY (stock_code, date)
)
''')
# 创建交易记录表
cursor.execute('''
CREATE TABLE IF NOT EXISTS trade_records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
stock_code TEXT,
stock_name TEXT,
trade_time TIMESTAMP,
trade_type TEXT, -- BUY, SELL
price REAL,
volume INTEGER,
amount REAL,
trade_id TEXT,
commission REAL,
strategy TEXT
)
''')
# 创建持仓表
cursor.execute('''
CREATE TABLE IF NOT EXISTS positions (
stock_code TEXT PRIMARY KEY,
stock_name TEXT,
volume INTEGER,
available REAL,
cost_price REAL,
current_price REAL,
market_value REAL,
profit_ratio REAL,
last_update TIMESTAMP,
open_date TIMESTAMP,
profit_triggered BOOLEAN DEFAULT FALSE,
highest_price REAL,
stop_loss_price REAL
)
''')
# 创建网格交易表
cursor.execute('''
CREATE TABLE IF NOT EXISTS grid_trades (
id INTEGER PRIMARY KEY AUTOINCREMENT,
stock_code TEXT,
grid_level INTEGER,
buy_price REAL,
sell_price REAL,
volume INTEGER,
status TEXT, -- PENDING, ACTIVE, COMPLETED
create_time TIMESTAMP,
update_time TIMESTAMP
)
''')
self.conn.commit()
logger.info("数据表结构已创建")
# def _init_xtquant(self):
# """初始化迅投行情接口"""
# try:
# # 根据文档,首先调用connect连接到行情服务器
# if not xt.connect():
# logger.error("行情服务连接失败")
# return
# logger.info("行情服务连接成功")
# # 根据测试结果,我们不使用subscribe_quote方法(会失败)
# # 改为验证股票代码是否可以通过get_full_tick获取数据
# valid_stocks = []
# for stock_code in config.STOCK_POOL:
# try:
# stock_code = self._adjust_stock(stock_code)
# # 尝试adjust_stock(stock_code)
# # 尝试获取Tick数据验证股票代码有效性
# tick_data = xt.get_full_tick([stock_code])
# if tick_data and stock_code in tick_data:
# valid_stocks.append(stock_code)
# logger.info(f"股票 {stock_code} 数据获取成功")
# else:
# logger.warning(f"无法获取 {stock_code} 的Tick数据")
# except Exception as e:
# logger.warning(f"获取 {stock_code} 的Tick数据失败: {str(e)}")
# self.subscribed_stocks = valid_stocks
# if self.subscribed_stocks:
# logger.info(f"成功验证 {len(self.subscribed_stocks)} 只股票可获取数据")
# else:
# logger.warning("没有有效的股票,请检查股票代码格式")
# except Exception as e:
# logger.error(f"初始化迅投行情接口出错: {str(e)}")
# # 股票代码转换
# def _select_data_type(self, stock='600031'):
# '''
# 选择数据类型
# '''
# return Methods.select_data_type(stock)
def _adjust_stock(self, stock='600031.SH'):
'''
调整代码
'''
return Methods.add_xt_suffix(stock)
def download_history_data(self, stock_code, period=None, start_date=None, end_date=None):
"""
下载股票历史数据 (使用Mootdx)
参数:
stock_code (str): 股票代码
period (str): 周期,默认为日线 'day'
start_date (str): 开始日期,格式为'2022-01-01'
end_date (str): 结束日期,格式为'2022-01-01'
返回:
pandas.DataFrame: 历史数据,若失败则返回None
"""
try:
import Methods # Import the Methods module
# Determine frequency code for Mootdx
if period == 'day':
freq = 9 # 日线
elif period == 'week':
freq = 5 # 周线
elif period == 'mon':
freq = 6 # 月线
elif period == '5m':
freq = 0 # 5分钟
elif period == '15m':
freq = 1 # 15分钟
elif period == '30m':
freq = 2 # 30分钟
elif period == '1h':
freq = 3 # 小时线
else:
freq = 9 # Default to 日线
# Adjust stock code if necessary
if stock_code.endswith((".SH", ".SZ")):
stock_code = stock_code[:-3] # Remove suffix
# Call getStockData
df = Methods.getStockData(
code=stock_code,
offset = 60,
freq=freq,
adjustflag='qfq' # 前复权
)
if df is None or df.empty:
logger.warning(f"使用Mootdx获取 {stock_code} 的历史数据为空")
return None
# Rename columns to match expected format
df = df.rename(columns={
'datetime': 'date',
'open': 'open',
'high': 'high',
'low': 'low',
'close': 'close',
'volume': 'volume',
'amount': 'amount'
})
# Ensure date column is in the correct format
if 'date' in df.columns:
df['date'] = pd.to_datetime(df['date']).dt.strftime('%Y-%m-%d')
# Ensure 'close' column is numeric
df['close'] = pd.to_numeric(df['close'], errors='coerce')
# Add stock_code column
df['stock_code'] = stock_code
# logger.info(f"成功使用Mootdx获取 {stock_code} 的历史数据, 共 {len(df)} 条记录")
return df
except Exception as e:
# 检查是否是长度不匹配错误
if "Length mismatch" in str(e):
logger.warning(f"下载 {stock_code} 数据时发生长度不匹配错误,使用默认数据")
# 创å»
8000
º包含默认值的DataFrame
df = pd.DataFrame({
'date': [datetime.now().strftime('%Y-%m-%d')],
'open': [0.0],
'high': [0.0],
'low': [0.0],
'close': [0.0],
'volume': [0],
'amount': [0],
'stock_code': [stock_code]
})
else:
# 其他错误,记录并返回None
logger.error(f"下载 {stock_code} 的历史数据时出错: {str(e)}")
return None
def download_history_xtdata(self, stock_code, period=None, start_date=None, end_date=None):
"""
下载股票历史数据
参数:
stock_code (str): 股票代码
period (str): 周期,默认为日线 'day'
start_date (str): 开始日期,格式为'20220101'
end_date (str): 结束日期,格式为'20220101'
返回:
pandas.DataFrame: 历史数据,若失败则返回None
"""
if not period:
period = 'day'
if not start_date:
start_date = '20200101' # 默认从2020年开始
if not end_date:
# 默认到今天
end_date = datetime.now().strftime('%Y%m%d')
logger.info(f"下载 {stock_code} 的历史数据, 周期: {period}, 从 {start_date} 到 {end_date}")
try:
# 首先使用XtQuant API下载数据到本地
xt.download_history_data(
stock_code,
period=period,
start_time=start_date,
end_time=end_date,
incrementally=True # 使用增量下载
)
# 等待数据下载完成
time.sleep(0.5)
# 使用get_market_data_ex从本地获取下载的数据
# 注意第一个参数是字段列表,可以为空
result = xt.get_market_data_ex(
[], # 空字段列表表示获取所有可用字段
[stock_code],
period=period,
start_time=start_date,
end_time=end_date
)
if not result:
logger.warning(f"获取 {stock_code} 的历史数据为空")
return None
if stock_code in result:
stock_data = result[stock_code]
df = pd.DataFrame(stock_data)
else:
logger.warning(f"获取的数据中没有 {stock_code}, 可用的键: {list(result.keys())}")
if result:
first_key = list(result.keys())[0]
stock_data = result[first_key]
df = pd.DataFrame(stock_data)
else:
return None
# 确保日期列格式正确
if 'date' in df.columns:
try:
df['date'] = pd.to_datetime(df['date']).dt.strftime('%Y-%m-%d')
except Exception as e:
logger.warning(f"转换日期格式失败: {str(e)}")
elif 'time' in df.columns:
try:
df = df.rename(columns={'time': 'date'})
df['date'] = pd.to_datetime(df['date']).dt.strftime('%Y-%m-%d')
except Exception as e:
logger.warning(f"转换time列为日期格式失败: {str(e)}")
if not df.empty:
logger.info(f"成功下载 {stock_code} 的历史数据, 共 {len(df)} 条记录")
return df
else:
logger.warning(f"下载的 {stock_code} 数据为空")
return None
except Exception as e:
logger.error(f"下载 {stock_code} 的历史数据时出错: {str(e)}")
return None
def get_stock_name(self, stock_code):
"""
获取股票名称
参数:
stock_code (str): 股票代码
返回:
str: 股票名称,如果未找到则返回股票代码
"""
try:
# 初始化名称缓存(如果不存在)
if not hasattr(self, 'stock_names_cache'):
self.stock_names_cache = {}
# 尝试从缓存获取名称
if stock_code in self.stock_names_cache:
return self.stock_names_cache[stock_code]
# 从QMT交易接口获取
try:
from position_manager import get_position_manager
position_manager = get_position_manager()
if hasattr(position_manager, 'qmt_trader') and position_manager.qmt_trader:
positions_df = position_manager.qmt_trader.position()
if not positions_df.empty and '证券代码' in positions_df.columns and '证券名称' in positions_df.columns:
# 简化股票代码以匹配
stock_code_simple = stock_code.split('.')[0] if '.' in stock_code else stock_code
stock_info = positions_df[positions_df['证券代码'] == stock_code_simple]
if not stock_info.empty:
stock_name = stock_info.iloc[0]['证券名称']
# 保存到缓存
self.stock_names_cache[stock_code] = stock_name
return stock_name
except Exception as e:
logger.debug(f"通过qmt_trader获取股票名称出错: {str(e)}")
# 尝试使用baostock查询
try:
import baostock as bs
lg = bs.login()
if lg.error_code != '0':
logger.warning(f"baostock登录失败: {lg.error_msg}")
return stock_code
# 调整股票代码格式
if '.' in stock_code:
formatted_code = stock_code # 假设已经是bs格式
else:
# 转换为baostock格式
if stock_code.startswith(('600', '601', '603', '688', '510')):
formatted_code = f"sh.{stock_code}"
else:
formatted_code = f"sz.{stock_code}"
# 查询股票基本信息
rs = bs.query_stock_basic(code=formatted_code)
if rs.error_code != '0':
logger.warning(f"查询股票基本信息失败: {rs.error_msg}")
bs.logout()
return stock_code
# 获取结果
data_list = []
while (rs.error_code == '0') & rs.next():
data_list.append(rs.get_row_data())
bs.logout()
if data_list:
# 股票名称通常是结果的第二列
stock_name = data_list[0][1] if len(data_list[0]) > 1 else stock_code
# 保存到缓存
self.stock_names_cache[stock_code] = stock_name
return stock_name
return stock_code
except ImportError:
logger.warning("未安装baostock,无法获取股票名称")
return stock_code
except Exception as e:
logger.error(f"获取股票 {stock_code} 名称时出错: {str(e)}")
return stock_code
except Exception as e:
logger.error(f"获取股票 {stock_code} 名称时出错: {str(e)}")
return stock_code
def save_history_data(self, stock_code, data_df):
"""
保存历史数据到数据库
参数:
stock_code (str): 股票代码
data_df (pandas.DataFrame): 历史数据
"""
if data_df is None or data_df.empty:
logger.warning(f"没有 {stock_code} 的数据可保存")
return
try:
# 立即创建工作副本
work_df = data_df.copy()
# 数据验证和清理
required_columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'amount']
missing_cols = [col for col in required_columns if col not in work_df.columns]
if missing_cols:
logger.error(f"{stock_code} 缺少必要列: {missing_cols}")
return
# 清理空数据
initial_count = len(work_df)
work_df = work_df.dropna(subset=['date'])
final_count = len(work_df)
if initial_count != final_count:
logger.warning(f"{stock_code} 过滤了 {initial_count - final_count} 行空date数据")
if work_df.empty:
logger.warning(f"{stock_code} 无有效数据可保存")
return
# 数据处理
work_df['stock_code'] = stock_code
work_df['close'] = pd.to_numeric(work_df['close'], errors='coerce')
# Delete existing data for the stock_code
cursor = self.conn.cursor()
cursor.execute("DELETE FROM stock_daily_data WHERE stock_code=?", (stock_code,))
self.conn.commit()
# logger.info(f"已清除 {stock_code} 在 stock_daily_data 表中的原有数据")
# 保存到数据库
work_df[['stock_code', 'date', 'open', 'high', 'low', 'close', 'volume', 'amount']].to_sql(
'stock_daily_data',
self.conn,
if_exists='append',
index=False,
method='multi' # 使用批量插入提高性能
)
self.conn.commit()
# logger.info(f"已保存 {stock_code} 的历史数据到数据库, 共 {len(data_df)} 条记录")
except Exception as e:
logger.error(f"保存 {stock_code} 的历史数据时出错: {str(e)}")
self.conn.rollback()
def get_latest_data(self, stock_code):
"""
获取最新行情数据 (使用Mootdx)
参数:
stock_code (str): 股票代码
返回:
dict: 最新行情数据
"""
try:
# 在交易时间内,优先使用实时数据管理器
if config.is_trade_time():
# # 添加频率控制,避免过于频繁调用
# if not hasattr(self, '_last_realtime_call_time'):
# self._last_realtime_call_time = {}
# current_time = time.time()
# last_call_time = self._last_realtime_call_time.get(stock_code, 0)
# # 限制调用频率:每只股票最多每秒调用一次
# if current_time - last_call_time >= 1.0:
# self._last_realtime_call_time[stock_code] = current_time
try:
# realtime_data = self.realtime_manager.get_realtime_data(stock_code)
realtime_data = self.get_latest_xtdata(stock_code)
if realtime_data and realtime_data.get('lastPrice', 0) > 0:
logger.debug(f"XT获取 {stock_code} 实时数据 {realtime_data.get('lastPrice')}")
return realtime_data
except Exception as e:
logger.debug(f"实时数据管理器获取{stock_code}失败,降级到Mootdx: {str(e)}")
# 继续尝试从Mootdx获取数据
# Adjust stock code if necessary
if stock_code.endswith((".SH", ".SZ")):
stock_code = stock_code[:-3] # Remove suffix
# Get the latest data using Mootdx (e.g., get last 1 day)
df = Methods.getStockData(
code=stock_code,
offset=2, # Get only the latest data
freq=9, # 日线
adjustflag='qfq'
)
if df is None or df.empty:
logger.warning(f"使用Mootdx获取 {stock_code} 的最新行情为空")
return None
# Extract the latest data
latest_data = df.iloc[-1].to_dict()
lastday_data = df.iloc[-2].to_dict()
# Rename columns to match expected format
latest_data = {
'lastPrice': float(latest_data.get('close', 0)),
'lastClose': float(lastday_data.get('close', 0)),
'volume': float(latest_data.get('volume', 0)),
'amount': float(latest_data.get('amount', 0)),
'date': latest_data.get('datetime', None)
}
logger.debug(f"Mootdx:{stock_code} 最新行情: {latest_data}")
return latest_data
except Exception as e:
logger.error(f"获取 {stock_code} 的latest_data出错: {str(e)}")
return None
def get_latest_xtdata(self, stock_code):
"""获取最新行情数据"""
stock_code = self._adjust_stock(stock_code)
try:
# 测试已证明get_full_tick方法可用
latest_quote = xt.get_full_tick([stock_code])
if not latest_quote or stock_code not in latest_quote:
logger.warning(f"xtdata:未获取到 {stock_code} 的tick行情,返回值: {latest_quote}")
return {} # 返回空字典而不是None
quote_data = latest_quote[stock_code]
logger.debug(f"xtdata: {stock_code} 最新行情: {quote_data}")
return quote_data
except Exception as e:
logger.error(f"xtdata: 获取 {stock_code} 的最新行情时出错: {str(e)}", exc_info=True)
return {} # 返回空字典而不是None
def get_history_data_from_db(self, stock_code, start_date=None, end_date=None):
"""
从数据库获取历史数据
参数:
stock_code (str): 股票代码
start_date (str): 开始日期,如 '2021-01-01'
end_date (str): 结束日期,如 '2021-03-31'
返回:
pandas.DataFrame: 历史数据
"""
query = "SELECT * FROM stock_daily_data WHERE stock_code=?"
params = [stock_code]
if start_date:
query += " AND date>=?"
params.append(start_date)
if end_date:
query += " AND date<=?"
params.append(end_date)
query += " ORDER BY date"
try:
df = pd.read_sql_query(query, self.conn, params=params)
logger.debug(f"从数据库获取 {stock_code} 的历史数据, 共 {len(df)} 条记录")
return df
except Exception as e:
logger.error(f"从数据库获取 {stock_code} 的历史数据时出错: {str(e)}")
return pd.DataFrame()
def get_market_data(self, stock_code, period='1d', count=1):
"""
获取市场数据(兼容sell_strategy调用)
参数:
stock_code (str): 股票代码
period (str): 周期,默认'1d'
count (int): 获取数据条数
返回:
pandas.DataFrame: 市场数据
"""
try:
# 调整股票代码格式
adjusted_code = self._adjust_stock(stock_code)
# 使用Mootdx获取数据
df = Methods.getStockData(
code=adjusted_code.split('.')[0] if '.' in adjusted_code else adjusted_code,
offset=count,
freq=9, # 日线
adjustflag='qfq'
)
if df is not None and not df.empty:
# 重命名列以匹配期望格式
df = df.rename(columns={
'datetime': 'date',
'open': 'open',
'high': 'high',
'low': 'low',
'close': 'close',
'volume': 'volume'
})
logger.debug(f"获取 {stock_code} 市场数据成功,共 {len(df)} 条记录")
return df
else:
logger.warning(f"获取 {stock_code} 市场数据为空")
return pd.DataFrame()
except Exception as e:
logger.error(f"获取 {stock_code} 市场数据时出错: {str(e)}")
return pd.DataFrame()
def update_all_stock_data(self):
"""更新所有股票的历史数据"""
for stock_code in config.STOCK_POOL:
self.update_stock_data(stock_code)
# 避免请求过于频繁
time.sleep(1)
def update_stock_data(self, stock_code):
"""
更新单只股票的数据
参数:
stock_code (str): 股票代码
"""
# 从数据库获取最新的数据日期
latest_date_query = "SELECT MAX(date) FROM stock_daily_data WHERE stock_code=?"
cursor = self.conn.cursor()
cursor.execute(latest_date_query, (stock_code,))
result = cursor.fetchone()
if result and result[0]:
latest_date = result[0]
# 从最新日期的下一天开始获取
start_date = (datetime.strptime(latest_date, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y%m%d')
# logger.info(f"更新 {stock_code} 的数据,从 {start_date} 开始")
else:
# 如果没有历史数据,获取完整的历史数据
start_date = None
logger.info(f"获取 {stock_code} 的完整历史数据")
# 下载并保存数据
data_df = self.download_history_data(stock_code, start_date=start_date)
if data_df is not None and not data_df.empty:
self.save_history_data(stock_code, data_df)
def start_data_update_thread(self):
"""启动数据更新线程"""
if not config.ENABLE_DATA_SYNC:
logger.info("数据同步功能已关闭,不启动更新线程")
return
if self.update_thread and self.update_thread.is_alive():
logger.warning("数据更新线程已在运行")
return
self.stop_flag = False
self.update_thread = threading.Thread(target=self._data_update_loop)
self.update_thread.daemon = True
self.update_thread.start()
logger.info("数据更新线程已启动")
def stop_data_update_thread(self):
"""停止数据更新线程"""
if self.update_thread and self.update_thread.is_alive():
self.stop_flag = True
self.update_thread.join(timeout=5)
logger.info("数据更新线程已停止")
def _data_update_loop(self):
"""数据更新循环"""
while not self.stop_flag:
try:
# 判断是否在交易时间
if config.is_trade_time():
logger.info("开始更新所有股票数据")
self.update_all_stock_data()
logger.info("股票数据更新完成")
# 等待下一次更新
for _ in range(config.UPDATE_INTERVAL):
if self.stop_flag:
break
time.sleep(1)
except Exception as e:
logger.error(f"数据更新循环出
4875
错: {str(e)}")
time.sleep(60) # 出错后等待一分钟再继续
def close(self):
"""关闭数据管理器"""
self.stop_data_update_thread()
if self.conn:
self.conn.close()
logger.info("数据库连接已关闭")
# 断开行情连接
try:
xt.disconnect()
logger.info("已断开行情连接")
except Exception as e:
logger.error(f"断开行情连接出错: {str(e)}")
# 单例模式
_instance = None
def get_data_manager():
"""获取DataManager单例"""
global _instance
if _instance is None:
_instance = DataManager()
return _instance