Repository navigation
Expand file tree
/
Copy pathposition_manager.py
More file actions
2111 lines (1753 loc) · 101 KB
/
Copy pathposition_manager.py
File metadata and controls
2111 lines (1753 loc) · 101 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
8000
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
持仓管理模块,负责跟踪和管理持仓
优化版本:统一止盈止损判断逻辑,支持模拟交易直接持仓调整
"""
import pandas as pd
import sqlite3
from datetime import datetime
import time
import threading
import sys
import os
import json
import Methods
import config
from logger import get_logger
from data_manager import get_data_manager
from easy_qmt_trader import easy_qmt_trader
# 获取logger
logger = get_logger("position_manager")
class PositionManager:
"""持仓管理类,负责跟踪和管理持仓"""
def __init__(self):
"""初始化持仓管理器"""
self.data_manager = get_data_manager()
self.conn = self.data_manager.conn
self.stock_positions_file = config.STOCK_POOL_FILE
# 持仓监控线程
self.monitor_thread = None
self.stop_flag = False
# 初始化easy_qmt_trader
account_config = config.get_account_config()
self.qmt_trader = easy_qmt_trader(
path= config.QMT_PATH,
account=account_config.get('account_id'),
account_type=account_config.get('account_type', 'STOCK')
)
self.qmt_trader.connect()
# 创建内存数据库
self.memory_conn = sqlite3.connect(":memory:", check_same_thread=False)
self._create_memory_table()
self._sync_db_to_memory()
# 添加模拟交易模式的提示日志
if hasattr(config, 'ENABLE_SIMULATION_MODE') and config.ENABLE_SIMULATION_MODE:
logger.warning("系统以模拟交易模式运行 - 持仓变更只在内存中进行,不会写入数据库")
# 添加缓存机制
self.last_position_update_time = 0
self.position_update_interval = 3 # 3秒更新间隔
self.positions_cache = None
# 新增,持仓数据版本控制
self.data_version = 0
self.data_changed = False
self.version_lock = threading.Lock()
# 新增:全量刷新控制 - 在这里添加缺失的属性
self.last_full_refresh_time = 0
self.full_refresh_interval = 60 # 1分钟全量刷新间隔
# 定时同步线程
self.sync_thread = None
self.sync_stop_flag = False
self.start_sync_thread()
# 添加信号状态管理
self.signal_lock = threading.Lock()
self.latest_signals = {} # 存储最新检测到的信号
self.signal_timestamps = {} # 信号时间戳
def _increment_data_version(self):
"""递增数据版本号"""
with self.version_lock:
self.data_version += 1
self.data_changed = True
logger.debug(f"持仓数据版本更新: v{self.data_version}")
def _create_memory_table(self):
"""创建内存数据库表结构"""
cursor = self.memory_conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS positions (
stock_code TEXT PRIMARY KEY,
stock_name TEXT,
volume REAL,
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
)
''')
self.memory_conn.commit()
logger.info("内存数据库表结构已创建")
def _sync_real_positions_to_memory(self, real_positions_df):
"""将实盘持仓数据同步到内存数据库"""
try:
# 首先检查输入数据
if real_positions_df is None or not isinstance(real_positions_df, pd.DataFrame) or real_positions_df.empty:
logger.warning("传入的实盘持仓数据无效,跳过同步")
return
# 确保必要的列存在
required_columns = ['证券代码', '股票余额', '可用余额', '成本价', '市值']
missing_columns = [col for col in required_columns if col not in real_positions_df.columns]
if missing_columns:
logger.warning(f"实盘持仓数据缺少必要列: {missing_columns},无法同步")
return
# 获取内存数据库中所有持仓的股票代码
cursor = self.memory_conn.cursor()
cursor.execute("SELECT stock_code FROM positions")
memory_stock_codes = {row[0] for row in cursor.fetchall() if row[0] is not None}
current_positions = set()
# 新增:记录更新过程中的错误
update_errors = []
# 遍历实盘持仓数据
for _, row in real_positions_df.iterrows():
try:
# 安全提取并转换数据
stock_code = str(row['证券代码']) if row['证券代码'] is not None else None
if not stock_code:
continue # 跳过无效数据
# 安全提取并转换数值
try:
volume = int(float(row['股票余额'])) if row['股票余额'] is not None else 0
except (ValueError, TypeError):
volume = 0
try:
available = int(float(row['可用余额'])) if row['可用余额'] is not None else 0
except (ValueError, TypeError):
available = 0
try:
cost_price = float(row['成本价']) if row['成本价'] is not None else 0.0
except (ValueError, TypeError):
cost_price = 0.0
try:
market_value = float(row['市值']) if row['市值'] is not None else 0.0
except (ValueError, TypeError):
market_value = 0.0
# 获取当前价格
current_price = cost_price # 默认使用成本价
try:
latest_quote = self.data_manager.get_latest_data(stock_code)
if latest_quote and isinstance(latest_quote, dict) and 'lastPrice' in latest_quote and latest_quote['lastPrice'] is not None:
current_price = float(latest_quote['lastPrice'])
except Exception as e:
logger.warning(f"获取 {stock_code} 的最新价格失败: {str(e)},使用成本价")
# 查询内存数据库中是否已存在该股票的持仓记录
cursor.execute("SELECT profit_triggered, open_date, highest_price, stop_loss_price FROM positions WHERE stock_code=?", (stock_code,))
result = cursor.fetchone()
if result:
# 如果存在,则更新持仓信息,但不修改open_date
profit_triggered = result[0] if result[0] is not None else False
open_date = result[1] if result[1] is not None else datetime.now().strftime('%Y-%m-%d %H:%M:%S')
highest_price = result[2] if result[2] is not None else 0.0
stop_loss_price = result[3] if result[3] is not None else 0.0
# 所有参数都确保有有效值
self.update_position(
stock_code=stock_code,
volume=volume,
cost_price=cost_price,
available=available,
market_value=market_value,
current_price=current_price,
profit_triggered=profit_triggered,
highest_price=highest_price,
open_date=open_date,
stop_loss_price=stop_loss_price
)
else:
# 如果不存在,则新增持仓记录
self.update_position(
stock_code=stock_code,
volume=volume,
cost_price=cost_price,
available=available,
market_value=market_value,
current_price=current_price,
open_date=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
)
# 添加到当前持仓集合
current_positions.add(stock_code)
memory_stock_codes.discard(stock_code)
except Exception as e:
logger.error(f"处理持仓行数据时出错: {str(e)}")
update_errors.append(f"处理 {stock_code if 'stock_code' in locals() else '未知'} 时出错: {str(e)}")
continue # 跳过这一行,继续处理其他行
# 关键修改:只有在没有更新错误且数据完整时才执行删除
if update_errors:
logger.error(f"数据更新过程中出现 {len(update_errors)} 个错误,跳过删除操作以保护数据")
for error in update_errors:
logger.error(f" - {error}")
return
# 数据完整性检查
if len(current_positions) == 0:
logger.warning("外部持仓数据为空,可能是接口异常,跳过删除操作")
return
# 数据量合理性检查
if len(memory_stock_codes) > 0 and len(current_positions) < len(memory_stock_codes) * 0.3:
logger.warning(f"外部持仓数据过少 ({len(current_positions)}) 相比内存数据 ({len(memory_stock_codes)}),可能是接口异常,跳过删除操作")
return
# 修改:在模拟交易模式下,不删除内存中存在但实盘中不存在的持仓记录
if not hasattr(config, 'ENABLE_SIMULATION_MODE') or not config.ENABLE_SIMULATION_MODE:
# 只有通过所有检查后才执行删除
if memory_stock_codes: # 有需要删除的记录
logger.info(f"准备删除 {len(memory_stock_codes)} 个不在外部数据中的持仓: {list(memory_stock_codes)}")
# 逐个删除并记录结果
successfully_deleted = []
failed_deletions = []
for stock_code in memory_stock_codes:
if stock_code:
try:
if self.remove_position(stock_code):
successfully_deleted.append(stock_code)
else:
failed_deletions.append(stock_code)
except Exception as e:
logger.error(f"删除 {stock_code} 时出错: {str(e)}")
failed_deletions.append(stock_code)
if successfully_deleted:
logger.info(f"成功删除持仓: {successfully_deleted}")
if failed_deletions:
logger.error(f"删除失败的持仓: {failed_deletions}")
else:
logger.info(f"模拟交易模式:保留内存中的模拟持仓记录,不与实盘同步删除")
# 更新 stock_positions.json
self._update_stock_positions_file(current_positions)
except Exception as e:
logger.error(f"同步实盘持仓数据到内存数据库时出错: {str(e)}")
self.memory_conn.rollback()
def _sync_db_to_memory(self):
"""将数据库数据同步到内存数据库"""
try:
db_positions = pd.read_sql_query("SELECT * FROM positions", self.conn)
if not db_positions.empty:
# 确保stock_name字段存在,如果不存在则添加默认值
if 'stock_name' not in db_positions.columns:
db_positions['stock_name'] = db_positions['stock_code'] # 使用股票代码作为默认名称
logger.warning("SQLite数据库中缺少stock_name字段,使用股票代码作为默认值")
db_positions.to_sql("positions", self.memory_conn, if_exists="replace", index=False)
self.memory_conn.commit()
logger.info("数据库数据已同步到内存数据库")
except Exception as e:
logger.error(f"数据库数据同步到内存数据库时出错: {str(e)}")
def _sync_memory_to_db(self):
"""将内存数据库数据同步到数据库"""
try:
# 添加模拟交易模式检查,模拟模式下不同步到SQLite
if hasattr(config, 'ENABLE_SIMULATION_MODE') and config.ENABLE_SIMULATION_MODE:
logger.debug("模拟交易模式:跳过内存数据库到SQLite数据库的同步")
return
# 添加交易时间检查 - 非交易时间不同步到SQLite
if not config.is_trade_time():
logger.debug("非交易时间,跳过内存数据库到SQLite的同步")
return
# 获取内存数据库中的所有股票代码
# memory_positions = pd.read_sql_query("SELECT stock_code, stock_name, open_date, profit_triggered, highest_price, stop_loss_price FROM positions", self.memory_conn)
memory_positions = pd.read_sql_query("SELECT * FROM positions", self.memory_conn)
memory_stock_codes = set(memory_positions['stock_code'].tolist()) if not memory_positions.empty else set()
# 获取SQLite数据库中的所有股票代码
cursor = self.conn.cursor()
cursor.execute("SELECT stock_code FROM positions")
sqlite_stock_codes = {row[0] for row in cursor.fetchall() if row[0] is not None}
# 删除SQLite中存在但内存数据库中不存在的记录
stocks_to_delete = sqlite_stock_codes - memory_stock_codes
if stocks_to_delete:
deleted_count = 0
for stock_code in stocks_to_delete:
try:
cursor.execute("DELETE FROM positions WHERE stock_code=?", (stock_code,))
if cursor.rowcount > 0:
deleted_count += 1
logger.info(f"从SQLite删除持仓记录: {stock_code}")
except Exception as e:
logger.error(f"删除SQLite中的 {stock_code} 记录时出错: {str(e)}")
if deleted_count > 0:
logger.info(f"SQLite同步:删除了 {deleted_count} 个过期的持仓记录")
if not memory_positions.empty:
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for _, row in memory_positions.iterrows():
stock_code = row['stock_code']
stock_name = row['stock_name']
volume = row['volume']
available = row['available']
cost_price = row['cost_price']
open_date = row['open_date']
profit_triggered = row['profit_triggered']
highest_price = row['highest_price']
stop_loss_price = row['stop_loss_price']
# 查询数据库中的对应记录
cursor = self.conn.cursor()
cursor.execute("SELECT stock_name, open_date, profit_triggered, highest_price, stop_loss_price FROM positions WHERE stock_code=?", (stock_code,))
db_row = cursor.fetchone()
if db_row:
db_stock_name, db_open_date, db_profit_triggered, db_highest_price, db_stop_loss_price = db_row
# 比较字段是否不同
if (db_stock_name != stock_name) or (db_open_date != open_date) or (db_profit_triggered != profit_triggered) or (db_highest_price != highest_price) or (db_stop_loss_price != stop_loss_price):
# 如果内存数据库中的 open_date 与 SQLite 数据库中的不一致,则使用 SQLite 数据库中的值
if db_open_date != open_date:
open_date = db_open_date
row['open_date'] = open_date # 更新内存数据库中的 open_date
# 更新数据库,确保所有字段都得到更新
cursor.execute("""
UPDATE positions
SET stock_name=?, volume=?, available=?, cost_price=?, current_price=?, market_value=?, profit_ratio=?, open_date=?, profit_triggered=?, highest_price=?, stop_loss_price=?, last_update=?
WHERE stock_code=?
""", (stock_name, volume, available, cost_price, row['current_price'], row['market_value'], row['profit_ratio'], open_date, profit_triggered, highest_price, stop_loss_price, now, stock_code))
logger.info(f"更新内存数据库的 {stock_code} 到sql数据库")
else:
# 插入新记录,使用当前日期作为 open_date
current_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute("""
INSERT INTO positions (stock_code, stock_name, volume, available, cost_price, current_price, market_value, profit_ratio, open_date, profit_triggered, highest_price, stop_loss_price, last_update)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (stock_code, stock_name, volume, available, cost_price, row['current_price'], row['market_value'], row['profit_ratio'], current_date, profit_triggered, highest_price, stop_loss_price, now))
# 插入新记录后,立即从数据库读取 open_date,以确保内存数据库与数据库一致
cursor.execute("SELECT open_date FROM positions WHERE stock_code=?", (stock_code,))
open_date = cursor.fetchone()[0]
row['open_date'] = open_date # 更新内存数据库中的 open_date
logger.info(f"在数据库中插入新的 {stock_code} 记录,使用当前日期 {current_date} 作为 open_date")
self.conn.commit()
except Exception as e:
logger.error(f"内存数据库数据同步到数据库时出错: {str(e)}")
self.conn.rollback()
def start_sync_thread(self):
"""启动定时同步线程"""
self.sync_stop_flag = False
self.sync_thread = threading.Thread(target=self._sync_loop)
self.sync_thread.daemon = True
self.sync_thread.start()
logger.info("定时同步线程已启动")
def stop_sync_thread(self):
"""停止定时同步线程"""
if self.sync_thread and self.sync_thread.is_alive():
self.sync_stop_flag = True
self.sync_thread.join(timeout=5)
logger.info("定时同步线程已停止")
# position_manager.py:_sync_loop() 方法修改
def _sync_loop(self):
"""定时同步循环 - 增强版"""
while not self.sync_stop_flag:
try:
# 原有的数据库同步
self._sync_memory_to_db()
# 新增:每1分钟执行一次全量刷新
current_time = time.time()
if (current_time - self.last_full_refresh_time) >= self.full_refresh_interval:
if hasattr(config, 'ENABLE_SIMULATION_MODE') and config.ENABLE_SIMULATION_MODE:
logger.info("执行模拟交易全量数据刷新")
self._full_refresh_simulation_data()
self.last_full_refresh_time = current_time
# 新增:模拟交易模式下的价格更新
if hasattr(config, 'ENABLE_SIMULATION_MODE') and config.ENABLE_SIMULATION_MODE:
# 在交易时间内更频繁地更新价格
if config.is_trade_time():
logger.debug("模拟交易模式:更新持仓价格和指标")
self.update_all_positions_price() # 更新价格
self._increment_data_version() # 触发版本更新
# 调整休眠时间
sleep_time = 3 if (hasattr(config, 'ENABLE_SIMULATION_MODE') and
config.ENABLE_SIMULATION_MODE and
config.is_trade_time()) else 5
for _ in range(sleep_time):
if self.sync_stop_flag:
break
time.sleep(1)
except Exception as e:
logger.error(f"定时同步循环出错: {str(e)}")
time.sleep(60) # 出错后等待一分钟再继续
def get_all_positions(self):
"""获取所有持仓"""
try:
current_time = time.time()
# 只在时间间隔到达后更新数据
if (current_time - self.last_position_update_time) >= self.position_update_interval:
# 获取实盘持仓数据
try:
real_positions_df = self.qmt_trader.position()
# 检查实盘数据
if real_positions_df is None:
logger.warning("实盘持仓数据获取失败,返回None")
real_positions_df = pd.DataFrame() # 使用空DataFrame而不是None
elif not isinstance(real_positions_df, pd.DataFrame):
logger.warning(f"实盘持仓数据类型错误: {type(real_positions_df)},将转换为DataFrame")
try:
# 尝试转换为DataFrame
real_positions_df = pd.DataFrame(real_positions_df)
except:
real_positions_df = pd.DataFrame() # 转换失败则使用空DataFrame
# 同步实盘持仓数据到内存数据库
if not real_positions_df.empty:
self._sync_real_positions_to_memory(real_positions_df)
# 更新缓存和时间戳
query = "SELECT * FROM positions"
self.positions_cache = pd.read_sql_query(query, self.memory_conn)
# 确保所有列都有合适的默认值
if not self.positions_cache.empty:
# 确保数值列为数值类型
numeric_columns = ['volume', 'available', 'cost_price', 'current_price',
'market_value', 'profit_ratio', 'highest_price', 'stop_loss_price']
for col in numeric_columns:
if col in self.positions_cache.columns:
# 转换为数值,无效值替换为0
self.positions_cache[col] = pd.to_numeric(self.positions_cache[col], errors='coerce').fillna(0)
# 确保布尔列为布尔类型
if 'profit_triggered' in self.positions_cache.columns:
self.positions_cache['profit_triggered'] = self.positions_cache['profit_triggered'].fillna(False)
self.last_position_update_time = current_time
logger.debug(f"更新持仓缓存,共 {len(self.positions_cache)} 条记录")
except Exception as e:
logger.error(f"获取和处理持仓数据时出错: {str(e)}")
# 如果出错,返回上次的缓存,或者空DataFrame
if self.positions_cache is None:
self.positions_cache = pd.DataFrame()
# 返回缓存数据的副本
return self.positions_cache.copy() if self.positions_cache is not None else pd.DataFrame()
except Exception as e:
logger.error(f"获取所有持仓信息时出错: {str(e)}")
return pd.DataFrame() # 出错时返回空DataFrame
def get_position(self, stock_code):
"""获取指定股票的持仓"""
try:
# 从缓存获取所有持仓
all_positions = self.get_all_positions()
# 从缓存中筛选指定股票
position_row = all_positions[all_positions['stock_code'] == stock_code]
if position_row.empty:
return None
# 转换为字典
position = position_row.iloc[0].to_dict()
# 确保数值字段转换为浮点数
numeric_fields = ['volume', 'available', 'cost_price', 'current_price', 'market_value', 'profit_ratio', 'highest_price', 'stop_loss_price']
for field in numeric_fields:
if field in position and position[field] is not None:
try:
position[field] = float(position[field])
except ValueError:
position[field] = 0.0
return position
except Exception as e:
logger.error(f"获取 {stock_code} 的持仓信息时出错: {str(e)}")
return None
def _is_test_environment(self):
"""判断是否为测试环境"""
# 可以根据需要修改判断逻辑
return 'unittest' in sys.modules
def _update_stock_positions_file(self, current_positions):
"""
更新 stock_positions.json 文件,如果内容有变化则写入。
参数:
current_positions (set): 当前持仓的股票代码集合
"""
try:
if os.path.exists(self.stock_positions_file):
with open(self.stock_positions_file, "r") as f:
try:
existing_positions = set(json.load(f))
except json.JSONDecodeError:
logger.warning(f"Error decoding JSON from {self.stock_positions_file}. Overwriting with current positions.")
existing_positions = set()
else:
existing_positions = set()
if existing_positions != current_positions:
with open(self.stock_positions_file, "w") as f:
json.dump(sorted(list(current_positions)), f, indent=4, ensure_ascii=False) # Sort for consistency
logger.info(f"更新 {self.stock_positions_file} with new positions.")
except Exception as e:
logger.error(f"更新出错 {self.stock_positions_file}: {str(e)}")
def update_position(self, stock_code, volume, cost_price, available=None, market_value=None, current_price=None, profit_triggered=False, highest_price=None, open_date=None, stop_loss_price=None, stock_name=None):
"""更新持仓信息"""
# Convert inputs to appropriate numeric types at the beginning
try:
# 确保stock_code有效
if stock_code is None or stock_code == "":
logger.error("股票代码不能为空")
return False
if stock_name is None:
try:
# 使用data_manager获取股票名称
from data_manager import get_data_manager
data_manager = get_data_manager()
stock_name = data_manager.get_stock_name(stock_code)
except Exception as e:
logger.warning(f"获取股票 {stock_code} 名称时出错: {str(e)}")
stock_name = stock_code # 如果无法获取名称,使用代码代替
# volume is typically int, but float conversion is safer for general arithmetic
p_volume = int(float(volume)) if volume is not None else 0
p_cost_price = float(cost_price) if cost_price is not None else 0.0
# current_price can be None if it needs to be fetched
p_current_price = float(current_price) if current_price is not None else None
# available defaults to volume if not provided
p_available = int(float(available)) if available is not None else p_volume
# highest_price and stop_loss_price can be None
p_highest_price = float(highest_price) if highest_price is not None else None
p_stop_loss_price = float(stop_loss_price) if stop_loss_price is not None else None
# profit_triggered 布尔值转换
if isinstance(profit_triggered, str):
p_profit_triggered = profit_triggered.lower() in ['true', '1', 't', 'y', 'yes']
else:
p_profit_triggered = bool(profit_triggered)
except (ValueError, TypeError) as e:
logger.error(f"Error converting inputs for {stock_code} to float: {e}. volume='{volume}', cost_price='{cost_price}', current_price='{current_price}'")
self.memory_conn.rollback() # Ensure rollback on early error
return False
try:
# 如果当前价格为None,获取最新行情
if p_current_price is None:
# 获取最新数据
latest_data = self.data_manager.get_latest_data(stock_code)
if latest_data and isinstance(latest_data, dict) and 'lastPrice' in latest_data and latest_data['lastPrice'] is not None:
p_current_price = float(latest_data['lastPrice'])
else:
logger.debug(f"未能获取 {stock_code} 的最新价格,使用成本价")
p_current_price = p_cost_price
# Ensure p_current_price is a float, default to cost_price if it's still None
if p_current_price is None:
p_current_price = p_cost_price if p_cost_price is not None else 0.0
# 计算市值和收益率
# Use the converted variables (p_volume, p_current_price, p_cost_price)
p_market_value = round(p_volume * p_current_price, 2)
# 防止除零错误
if p_cost_price > 0:
p_profit_ratio = round(100 * (p_current_price - p_cost_price) / p_cost_price, 2)
else:
p_profit_ratio = 0.0
# Round the final values before storing or using in DB operations
final_cost_price = round(p_cost_price, 2)
final_current_price = round(p_current_price, 2)
# 处理最高价
if p_highest_price is not None:
final_highest_price = round(p_highest_price, 2)
else:
final_highest_price = final_current_price # 默认使用当前价格
# 处理止损价格
if p_stop_loss_price is not None:
final_stop_loss_price = round(p_stop_loss_price, 2)
else:
# 计算默认止损价格
calculated_slp = self.calculate_stop_loss_price(final_cost_price, final_highest_price, p_profit_triggered)
final_stop_loss_price = round(calculated_slp, 2) if calculated_slp is not None else None
# 获取当前时间
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 处理open_date
if open_date is None:
open_date = now
# 检查是否已有持仓记录
cursor = self.memory_conn.cursor()
cursor.execute("SELECT open_date, profit_triggered, highest_price, stop_loss_price FROM positions WHERE stock_code=?", (stock_code,))
result = cursor.fetchone()
if result:
# 更新持仓
if open_date is None:
open_date = result[0] # 获取已有的open_date
old_db_highest_price = float(result[2]) if result[2] is not None else None # from DB
if final_highest_price is None: # if not passed or calculated yet
final_highest_price = max(old_db_highest_price, final_current_price) if old_db_highest_price is not None else final_current_price
# 修复:如果最高价发生变化,强制重新计算止损价格
if old_db_highest_price != final_highest_price:
logger.info(f"{stock_code} 最高价变化:{old_db_highest_price} -> {final_highest_price},重新计算止损价格")
calculated_slp = self.calculate_stop_loss_price(final_cost_price, final_highest_price, profit_triggered)
final_stop_loss_price = round(calculated_slp, 2) if calculated_slp is not None else None
elif final_stop_loss_price is None:
calculated_slp = self.calculate_stop_loss_price(final_cost_price, final_highest_price, profit_triggered)
final_stop_loss_price = round(calculated_slp, 2) if calculated_slp is not None else None
else:
calculated_slp = self.calculate_stop_loss_price(final_cost_price, final_highest_price, profit_triggered)
if calculated_slp is not None:
final_stop_loss_price = min(final_stop_loss_price, calculated_slp)
final_stop_loss_price = round(final_stop_loss_price, 2)
cursor.execute("""
UPDATE positions
SET volume=?, cost_price=?, current_price=?, market_value=?, available=?,
profit_ratio=?, last_update=?, highest_price=?, stop_loss_price=?, profit_triggered=?, stock_name=?
WHERE stock_code=?
""", (int(p_volume), final_cost_price, final_current_price, p_market_value, int(p_available),
p_profit_ratio, now, final_highest_price, final_stop_loss_price, profit_triggered, stock_name, stock_code))
if profit_triggered != result[1]:
logger.info(f"更新 {stock_code} 持仓: 首次止盈触发: 从 {result[1]} 到 {profit_triggered}")
elif final_highest_price != (float(result[2]) if result[2] is not None else None):
logger.info(f"更新 {stock_code} 持仓: 最高价: 从 {result[2]} 到 {final_highest_price}")
elif final_stop_loss_price != (float(result[3]) if result[3] is not None else None):
logger.info(f"更新 {stock_code} 持仓: 止损价: 从 {result[3]} 到 {final_stop_loss_price}")
else:
# 新增持仓
if open_date is None:
open_date = now # 新建仓时记录当前时间为open_date
profit_triggered = False
if final_highest_price is None:
final_highest_price = final_current_price
# 计算止损价格
calculated_slp = self.calculate_stop_loss_price(final_cost_price, final_highest_price, profit_triggered)
final_stop_loss_price = round(calculated_slp, 2) if calculated_slp is not None else None
cursor.execute("""
INSERT INTO positions
(stock_code, stock_name, volume, cost_price, current_price, market_value, available, profit_ratio, last_update, open_date, profit_triggered, highest_price, stop_loss_price)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (stock_code, stock_name, int(p_volume), final_cost_price, final_current_price, p_market_value,
int(p_available), p_profit_ratio, now, open_date, profit_triggered, final_highest_price, final_stop_loss_price))
self.memory_conn.commit()
# 触发持仓数据版本更新
self._increment_data_version()
return True
except Exception as e:
logger.error(f"更新 {stock_code} 持仓Error: {str(e)}")
self.memory_conn.rollback()
return False
def remove_position(self, stock_code):
"""
删除持仓记录
参数:
stock_code (str): 股票代码
返回:
bool: 是否删除成功
"""
try:
position = self.get_position(stock_code)
if position:
profit_triggered = position.get('profit_triggered', False)
profit_ratio = position.get('profit_ratio', 0)
if profit_triggered:
logger.warning(f"⚠️ 删除已触发止盈的持仓 {stock_code},盈亏率: {profit_ratio:.2f}%")
else:
logger.info(f"删除持仓 {stock_code},盈亏率: {profit_ratio:.2f}%")
cursor = self.memory_conn.cursor()
cursor.execute("DELETE FROM positions WHERE stock_code=?", (stock_code,))
self.memory_conn.commit()
if cursor.rowcount > 0:
# 触发持仓数据版本更新
self._increment_data_version()
logger.info(f"已删除 {stock_code} 的持仓记录")
return True
else:
logger.warning(f"未找到 {stock_code} 的持仓记录,无需删除")
return False
except Exception as e:
logger.error(f"删除 {stock_code} 的持仓记录时出错: {str(e)}")
self.memory_conn.rollback()
return False
def get_data_version_info(self):
"""获取持仓数据版本信息"""
with self.version_lock:
return {
'version': self.data_version,
'changed': self.data_changed,
'timestamp': datetime.now().isoformat()
}
def mark_data_consumed(self):
"""标记持仓数据已被消费"""
with self.version_lock:
self.data_changed = False
def update_all_positions_highest_price(self):
"""更新所有持仓的最高价"""
try:
positions = self.get_all_positions()
if positions.empty:
logger.debug("当前没有持仓,无需更新最高价")
return
for _, position in positions.iterrows():
stock_code = position['stock_code']
# 安全获取最高价,确保不为None
current_highest_price = 0.0
if position['highest_price'] is not None:
try:
current_highest_price = float(position['highest_price'])
except (ValueError, TypeError):
current_highest_price = 0.0
# 安全获取开仓日期
open_date_str = position['open_date']
try:
if isinstance(open_date_str, str):
open_date = datetime.strptime(open_date_str, '%Y-%m-%d %H:%M:%S')
else:
open_date = datetime.now()
open_date_formatted = open_date.strftime('%Y-%m-%d')
except (ValueError, TypeError):
open_date_formatted = datetime.now().strftime('%Y-%m-%d')
# Format open_date to YYYY-MM-DD for getStockData
open_date_formatted = open_date.strftime('%Y-%m-%d')
# Get today's date for getStockData
today_formatted = datetime.now().strftime('%Y-%m-%d')
# 获取从开仓日期到今天的历史数据
try:
# Get the latest data
history_data = Methods.getStockData(
code=stock_code,
fields="high",
start_date=open_date_formatted,
freq= 'd', # 日线
adjustflag= '2'
)
except Exception as e:
logger.error(f"获取 {stock_code} 从 {open_date_formatted} 到 {today_formatted} 的历史数据时出错: {str(e)}")
continue
if history_data is not None and not history_data.empty:
# 找到开仓后日线数据最高价
highest_price = history_data['high'].astype(float).max()
else:
highest_price = 0.0
logger.warning(f"未能获取 {stock_code} 从 {open_date_formatted} 到 {today_formatted} 的历史数据,跳过更新最高价")
# 开盘时间,获取最新tick数据
if config.is_trade_time:
latest_data = self.data_manager.get_latest_data(stock_code)
if latest_data:
current_price = latest_data.get('lastPrice')
current_high_price = latest_data.get('high')
if current_high_price and current_high_price > highest_price:
highest_price = current_high_price
if highest_price > current_highest_price:
# 更新持仓"最高价"信息
self.update_position(
stock_code=stock_code,
volume=position['volume'],
cost_price=position['cost_price'],
current_price=position['current_price'],
profit_triggered=position['profit_triggered'],
highest_price=highest_price,
open_date=position['open_date'],
stop_loss_price=position['stop_loss_price']
)
logger.info(f"更新 {stock_code} 的最高价为 {highest_price:.2f}")
except Exception as e:
logger.error(f"更新所有持仓的最高价时出错: {str(e)}")
def update_all_positions_price(self):
"""更新所有持仓的最新价格"""
try:
# 首先检查是否有持仓数据
positions = self.get_all_positions()
# 检查positions是否为None或空DataFrame
if positions is None or positions.empty:
logger.debug("当前没有持仓,无需更新价格")
return
# 检查positions是否含有必要的列
required_columns = ['stock_code', 'volume', 'cost_price', 'current_price', 'highest_price']
missing_columns = [col for col in required_columns if col not in positions.columns]
if missing_columns:
logger.warning(f"持仓数据缺少必要列: {missing_columns},无法更新价格")
return
for _, position in positions.iterrows():
try:
# 提取数据并安全转换
stock_code = position['stock_code']
if stock_code is None:
continue # 跳过无效数据
# 安全提取和转换所有数值
safe_numeric_values = {}
for field in ['volume', 'cost_price', 'current_price', 'highest_price', 'profit_triggered', 'available', 'market_value', 'stop_loss_price']:
if field in position:
value = position[field]
# 布尔值特殊处理
if field == 'profit_triggered':
safe_numeric_values[field] = bool(value) if value is not None else False
# 数值处理
elif field in ['volume', 'available']:
safe_numeric_values[field] = int(float(value)) if value is not None else 0
else:
safe_numeric_values[field] = float(value) if value is not None else 0.0
else:
# 设置默认值
if field == 'profit_triggered':
safe_numeric_values[field] = False
elif field in ['volume', 'available']:
safe_numeric_values[field] = 0
else:
safe_numeric_values[field] = 0.0
# 安全处理open_date
open_date = position.get('open_date')
if open_date is None:
open_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 获取最新价格
try:
latest_quote = self.data_manager.get_latest_data(stock_code)
if latest_quote and isinstance(latest_quote, dict) and 'lastPrice' in latest_quote and latest_quote['lastPrice'] is not None:
current_price = float(latest_quote['lastPrice'])
# 只有价格有显著变化时才更新
old_price = safe_numeric_values['current_price']
if abs(current_price - old_price) / max(old_price, 0.01) > 0.003: # 防止除零
# 使用安全转换后的值来更新
self.update_position(
stock_code=stock_code,
volume=safe_numeric_values['volume'],
cost_price=safe_numeric_values['cost_price'],
available=safe_numeric_values['available'],
market_value=safe_numeric_values['market_value'],
current_price=current_price, # 使用最新价格
profit_triggered=safe_numeric_values['profit_triggered'],
highest_price=safe_numeric_values['highest_price'],
open_date=open_date,
stop_loss_price=safe_numeric_values['stop_loss_price']
)
logger.debug(f"更新 {stock_code} 的最新价格为 {current_price:.2f}")
except Exception as e:
logger.error(f"获取 {stock_code} 最新价格时出错: {str(e)}")
continue # 跳过这只股票,继续处理其他股票
except Exception as e:
logger.error(f"处理 {position.get('stock_code', 'unknown')} 持仓数据时出错: {str(e)}")
continue # 跳过这只股票,继续处理其他股票
except Exception as e:
logger.error(f"更新所有持仓价格时出错: {str(e)}")
def get_account_info(self):
"""获取账户信息"""
try:
# 如果是模拟交易模式,直接返回模拟账户信息(由trading_executor模块管理)
if hasattr(config, 'ENABLE_SIMULATION_MODE
6BD0
039;) and config.ENABLE_SIMULATION_MODE:
logger.debug(f"返回模拟账户信息,余额: {config.SIMULATION_BALANCE}")
# 计算持仓市值
positions = self.get_all_positions()
market_value = 0
if not positions.empty:
for _, pos in positions.iterrows():
pos_market_value = pos.get('market_value')
if pos_market_value is not None:
try:
market_value += float(pos_market_value)
except (ValueError, TypeError):
# 忽略无效值
pass
# 计算总资产
available = float(config.SIMULATION_BALANCE)
total_asset = available + market_value # 可用资金 + 持仓市值
return {
'account_id': 'SIMULATION',
'account_type': 'SIMULATION',
'available': available,
'market_value': float(market_value),
'total_asset': total_asset, # 添加总资产字段
'profit_loss': 0.0,
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
# 使用qmt_trader获取账户信息
account_df = self.qmt_trader.balance()
if account_df.empty:
return None
# 转换为字典格式
account_info = {
'account_id': account_df['资金账户'].iloc[0] if '资金账户' in account_df.columns and not account_df['资金账户'].empty else '--',
'account_type': account_df['账号类型'].iloc[0] if '账号类型' in account_df.columns and not account_df['账号类型'].empty else '--',
'available': float(account_df['可用金额'].iloc[0]) if '可用金额' in account_df.columns and not account_df['可用金额'].empty else 0.0,
'frozen_cash': float(account_df['冻结金额'].iloc[0]) if '冻结金额' in account_df.columns and not account_df['冻结金额'].empty else 0.0,
'market_value': float(account_df['持仓市值'].iloc[0]) if '持仓市值' in account_df.columns and not account_df['持仓市值'].empty else 0.0,
'total_asset': float(account_df['总资产'].iloc[0]) if '总资产' in account_df.columns and not account_df['总资产'].empty else 0.0,
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
return account_info
except Exception as e:
logger.error(f"获取账户信息时出错: {str(e)}")
return None
def get_grid_trades(self, stock_code, status=None):
"""
获取网格交易记录
参数:
stock_code (str): 股票代码