Repository navigation
Expand file tree
/
Copy pathtelegram_states.py
More file actions
583 lines (484 loc) · 32.5 KB
/
Copy pathtelegram_states.py
File metadata and controls
583 lines (484 loc) · 32.5 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
# ==========================================================
# FILE: telegram_states.py
# ==========================================================
# 🚨 VERIFIED: [최종 무결점 판정] 5대 헌법 및 48대 엣지 케이스 완벽 결속 교차 검증 완료.
# 🚨 MODIFIED: [Lost Update 궁극 방어] 파일 물리 삭제 및 덮어쓰기 로직(_hijack_vwap_lock, _process_reset_files, _nuke_assassin_data) 전역에 GlobalThrottle.get_file_lock()을 100% 팩트 래핑 완료.
# 🚨 MODIFIED: [2차 오인 패러독스(False Alarm) 붕괴 차단] EDIT_Q 수동 조작 및 RESET:LOCK 잠금 해제 시, 진행 중인 슬라이싱 지시서(`vrev_slice_state`, `vrev_aftermarket_state`)를 os.remove()로 물리적 삭제하던 맹독성 로직을 영구 소각. 대신 `hijacked=True`인 빈 지시서를 원자적으로 박제하여 VWAP 스케줄러의 오인 에러 타전을 100% 원천 봉쇄 완료.
# ==========================================================
import logging
import datetime
from zoneinfo import ZoneInfo
import os
import json
import asyncio
import tempfile
import html
import math
import glob
from telegram import Update
from telegram.ext import ContextTypes
from telegram.error import BadRequest
from global_throttle import GlobalThrottle
class TelegramStates:
def __init__(self, config, broker, queue_ledger, sync_engine):
self.cfg = config
self.broker = broker
self.queue_ledger = queue_ledger
self.sync_engine = sync_engine
def _safe_float(self, val):
try:
f_val = float(str(val or 0.0).replace(',', ''))
if math.isnan(f_val) or math.isinf(f_val):
return 0.0
return f_val
except Exception:
return 0.0
async def handle_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE, controller):
if not await controller._is_admin(update):
return
chat_id = update.effective_chat.id
text = update.effective_message.text.strip() if update.effective_message and update.effective_message.text else ""
if "통합 지시서" in text or "지시서 조회" in text:
return await controller.cmd_sync(update, context)
elif "장부 동기화" in text or "장부 조회" in text:
return await controller.cmd_record(update, context)
elif "명예의 전당" in text:
return await controller.cmd_history(update, context)
elif "코어 스위칭" in text or "전술 설정" in text or "모드변환" in text or "분할변경" in text:
return await controller.cmd_settlement(update, context)
elif "시드머니" in text or "시드 변경" in text or "시드 관리" in text:
return await controller.cmd_seed(update, context)
elif "종목 선택" in text:
return await controller.cmd_ticker(update, context)
elif "스나이퍼" in text:
return await controller.cmd_mode(update, context)
elif "버전" in text or "업데이트 내역" in text:
return await controller.cmd_version(update, context)
elif "비상 해제" in text:
return await controller.cmd_reset(update, context)
elif "시스템 업데이트" in text or "엔진 업데이트" in text:
return await controller.cmd_update(update, context)
elif "관제탑" in text or "레이더" in text or "avwap" in text.lower():
if hasattr(controller, 'cmd_avwap'):
return await controller.cmd_avwap(update, context)
elif "로그" in text or "에러" in text or "진단" in text:
if hasattr(controller, 'cmd_log'):
return await controller.cmd_log(update, context)
state = controller.user_states.get(chat_id)
if not state:
return
try:
if state.startswith("EDITQ_"):
parts = state.split("_", 2)
ticker = parts[1]
target_date = parts[2]
safe_ticker = html.escape(str(ticker))
input_parts = text.split()
if len(input_parts) != 2:
del controller.user_states[chat_id]
try: await asyncio.wait_for(update.effective_message.reply_text("❌ 입력 형식 오류입니다. 띄어쓰기로 수량과 평단가를 입력해주세요. (수정 취소됨)"), timeout=10.0)
except Exception: pass
return
qty = int(self._safe_float(input_parts[0]))
price = self._safe_float(input_parts[1])
if qty <= 0 or price <= 0.0:
del controller.user_states[chat_id]
try: await asyncio.wait_for(update.effective_message.reply_text("❌ 수량/평단가는 0보다 큰 숫자로 입력하세요. (수정 취소됨)"), timeout=10.0)
except Exception: pass
return
try:
curr_p = 0.0
for attempt in range(3):
try:
curr_p_val = await asyncio.wait_for(
asyncio.to_thread(self.broker.get_current_price, ticker),
timeout=10.0
)
curr_p = self._safe_float(curr_p_val)
break
except Exception:
if attempt == 2: curr_p = 0.0
else: await asyncio.sleep(1.0 * (2 ** attempt))
if curr_p and curr_p > 0 and (price < curr_p * 0.4 or price > curr_p * 1.6):
del controller.user_states[chat_id]
try: await asyncio.wait_for(update.effective_message.reply_text(f"🚨 <b>팻핑거 방어 가동:</b> 입력가(${price:.2f})가 현재가(${curr_p:.2f}) 대비 ±60%를 초과합니다. 다시 시도해주세요.", parse_mode='HTML'), timeout=10.0)
except Exception: pass
return
except Exception:
pass
if getattr(self, 'queue_ledger', None):
try: await asyncio.wait_for(asyncio.to_thread(self.queue_ledger.edit_lot, ticker, target_date, qty, price), timeout=10.0)
except Exception as e: logging.error(f"🚨 지층 수정 파일 I/O 에러: {e}")
# 🚨 MODIFIED: [이중 타격 방어] 낡은 스냅샷(Snapshot), 캐시, 슬라이스/애프터장 지시서 전면 영구 소각
def _nuke_snapshot_and_state_edit():
for f in glob.glob(f"data/daily_snapshot_*_{ticker}.json"):
with GlobalThrottle.get_file_lock(f):
try: os.remove(f)
except OSError: pass
for f in glob.glob(f"data/vwap_state_*_{ticker}.json"):
with GlobalThrottle.get_file_lock(f):
try: os.remove(f)
except OSError: pass
# 🚨 MODIFIED: 2차 오인 패러독스 차단 (물리적 삭제 대신 빈 지시서 박제)
est_now = datetime.datetime.now(ZoneInfo('America/New_York'))
today_str = est_now.strftime('%Y-%m-%d')
empty_state = {"date": today_str, "hijacked": True, "orders": []}
for f_path in [f"data/vrev_slice_state_{ticker}.json", f"data/vrev_aftermarket_state_{ticker}.json"]:
with GlobalThrottle.get_file_lock(f_path):
dir_name = os.path.dirname(f_path) or '.'
try: os.makedirs(dir_name, exist_ok=True)
except OSError: pass
try:
fd, tmp_path = tempfile.mkstemp(dir=dir_name, text=True)
with os.fdopen(fd, 'w', encoding='utf-8') as f_out:
json.dump(empty_state, f_out, ensure_ascii=False, indent=4)
f_out.flush()
os.fsync(f_out.fileno())
os.replace(tmp_path, f_path)
except Exception: pass
await asyncio.wait_for(asyncio.to_thread(_nuke_snapshot_and_state_edit), timeout=10.0)
del controller.user_states[chat_id]
short_date = html.escape(str(target_date[:10]))
try: await asyncio.wait_for(update.effective_message.reply_text(f"✅ <b>[{safe_ticker}] 지층 정밀 수정 완료!</b>\n▫️ {short_date} | {qty}주 | ${price:.2f}\n\n⚠️ <b>[안전 격리 모드]</b>\n수동 다단계 조작 중 시스템 간섭을 막기 위해 <b>자동 동기화가 일시 차단</b>되었습니다.\n삭제 등 남은 수동 작업을 모두 마친 후, 반드시 <code>/record</code>를 눌러 KIS 실원장과 팩트 동기화하십시오.", parse_mode='HTML'), timeout=10.0)
except Exception: pass
return
val = self._safe_float(text)
parts = state.split("_")
if state.startswith("SEED"):
if val <= 0:
try: await asyncio.wait_for(update.effective_message.reply_text("❌ 오류: 시드머니는 0보다 커야 합니다."), timeout=10.0)
except Exception: pass
return
action, ticker = parts[1], parts[-1]
safe_ticker = html.escape(str(ticker))
try: curr = self._safe_float(await asyncio.wait_for(asyncio.to_thread(self.cfg.get_seed, ticker), timeout=10.0))
except Exception: curr = 0.0
new_v = curr + val if action == "ADD" else (max(0.0, curr - val) if action == "SUB" else val)
try: await asyncio.wait_for(asyncio.to_thread(self.cfg.set_seed, ticker, new_v), timeout=10.0)
except Exception as e: logging.error(f"🚨 시드 설정 에러: {e}")
try: await asyncio.wait_for(update.effective_message.reply_text(f"✅ [{safe_ticker}] 시드 변경: ${new_v:,.0f}"), timeout=10.0)
except Exception: pass
if hasattr(controller, 'cmd_seed'):
try:
await controller.cmd_seed(update, context)
except BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
elif state.startswith("CONF_SPLIT"):
if val < 1:
try: await asyncio.wait_for(update.effective_message.reply_text("❌ 오류: 분할 횟수는 1 이상이어야 합니다."), timeout=10.0)
except Exception: pass
return
ticker = parts[-1]
safe_ticker = html.escape(str(ticker))
def _set_split(cfg_obj, t, v):
with GlobalThrottle.get_file_lock(cfg_obj.FILES["SPLIT"]):
d = cfg_obj._load_json(cfg_obj.FILES["SPLIT"], cfg_obj.DEFAULT_SPLIT)
d[t] = v
cfg_obj._save_json(cfg_obj.FILES["SPLIT"], d)
try: await asyncio.wait_for(asyncio.to_thread(_set_split, self.cfg, ticker, val), timeout=10.0)
except Exception as e: logging.error(f"🚨 분할 설정 에러: {e}")
try: await asyncio.wait_for(update.effective_message.reply_text(f"✅ [{safe_ticker}] 분할: {int(val)}회"), timeout=10.0)
except Exception: pass
if hasattr(controller, 'cmd_settlement'):
try:
await controller.cmd_settlement(update, context)
except BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
elif state.startswith("CONF_TARGET"):
ticker = parts[-1]
safe_ticker = html.escape(str(ticker))
def _set_target(cfg_obj, t, v):
with GlobalThrottle.get_file_lock(cfg_obj.FILES["PROFIT_CFG"]):
d = cfg_obj._load_json(cfg_obj.FILES["PROFIT_CFG"], cfg_obj.DEFAULT_TARGET)
d[t] = v
cfg_obj._save_json(cfg_obj.FILES["PROFIT_CFG"], d)
try: await asyncio.wait_for(asyncio.to_thread(_set_target, self.cfg, ticker, val), timeout=10.0)
except Exception as e: logging.error(f"🚨 목표치 설정 에러: {e}")
try: await asyncio.wait_for(update.effective_message.reply_text(f"✅ [{safe_ticker}] 목표 수익률: {val}%"), timeout=10.0)
except Exception: pass
if hasattr(controller, 'cmd_settlement'):
try:
await controller.cmd_settlement(update, context)
except BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
elif state.startswith("CONF_COMPOUND"):
if val < 0:
try: await asyncio.wait_for(update.effective_message.reply_text("❌ 오류: 복리율은 0 이상이어야 합니다."), timeout=10.0)
except Exception: pass
return
ticker = parts[-1]
safe_ticker = html.escape(str(ticker))
try: await asyncio.wait_for(asyncio.to_thread(self.cfg.set_compound_rate, ticker, val), timeout=10.0)
except Exception as e: logging.error(f"🚨 복리 설정 에러: {e}")
try: await asyncio.wait_for(update.effective_message.reply_text(f"✅ [{safe_ticker}] 졸업 시 자동 복리율: {val}%"), timeout=10.0)
except Exception: pass
if hasattr(controller, 'cmd_settlement'):
try:
await controller.cmd_settlement(update, context)
except BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
elif state.startswith("CONF_FEE"):
if val < 0.0 or val > 10.0:
try: await asyncio.wait_for(update.effective_message.reply_text("🚨 <b>오입력 차단:</b> 수수료율은 0.0% ~ 10.0% 사이여야 합니다.", parse_mode='HTML'), timeout=10.0)
except Exception: pass
return
ticker = parts[-1]
safe_ticker = html.escape(str(ticker))
try: await asyncio.wait_for(asyncio.to_thread(self.cfg.set_fee, ticker, val), timeout=10.0)
except Exception as e: logging.error(f"🚨 수수료 설정 에러: {e}")
try: await asyncio.wait_for(update.effective_message.reply_text(f"💳 <b>[{safe_ticker}] 증권사 거래 수수료: {val}% 적용 완료!</b>\n▫️ 다음 명예의 전당 정산부터 수익 연산 시 해당 수수료가 적용됩니다.", parse_mode='HTML'), timeout=10.0)
except Exception: pass
if hasattr(controller, 'cmd_settlement'):
try:
await controller.cmd_settlement(update, context)
except BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
elif state.startswith("CONF_STOCK_SPLIT"):
if val <= 0:
try: await asyncio.wait_for(update.effective_message.reply_text("❌ 오류: 액면 보정 비율은 0보다 커야 합니다."), timeout=10.0)
except Exception: pass
return
ticker = parts[-1]
safe_ticker = html.escape(str(ticker))
try: await asyncio.wait_for(asyncio.to_thread(self.cfg.apply_stock_split, ticker, val), timeout=15.0)
except Exception as e: logging.error(f"🚨 수동 액면보정 에러: {e}")
est = ZoneInfo('America/New_York')
today_str = datetime.datetime.now(est).strftime('%Y-%m-%d')
try: await asyncio.wait_for(asyncio.to_thread(self.cfg.set_last_split_date, ticker, today_str), timeout=10.0)
except Exception as e: logging.error(f"🚨 분할 날짜 기록 에러: {e}")
try: await asyncio.wait_for(update.effective_message.reply_text(f"✅ [{safe_ticker}] 수동 액면 보정 완료\n▫️ 모든 장부 기록이 {val}배 비율로 정밀하게 소급 조정되었습니다."), timeout=10.0)
except Exception: pass
if hasattr(controller, 'cmd_settlement'):
try:
await controller.cmd_settlement(update, context)
except BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
elif state.startswith("CONF_AVWAP_BUDGET"):
if val <= 0.0:
try: await asyncio.wait_for(update.effective_message.reply_text("🚨 <b>오입력 차단:</b> 암살자 예산은 0보다 커야 합니다.", parse_mode='HTML'), timeout=10.0)
except Exception: pass
return
ticker = parts[-1]
safe_ticker = html.escape(str(ticker))
try: await asyncio.wait_for(asyncio.to_thread(self.cfg.set_avwap_budget, ticker, val), timeout=10.0)
except Exception as e: logging.error(f"🚨 암살자 예산 설정 에러: {e}")
try: await asyncio.wait_for(update.effective_message.reply_text(f"🔫 <b>[{safe_ticker}] 암살자 1회 타격 예산: ${val:,.2f} 락온 완료!</b>\n▫️ 다음 소프트웨어 트리거 요격 시부터 해당 예산이 최대치로 캡핑 적용됩니다.", parse_mode='HTML'), timeout=10.0)
except Exception: pass
if hasattr(controller, 'cmd_settlement'):
try:
await controller.cmd_settlement(update, context)
except BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
elif state.startswith("VREV_GAP"):
ticker = parts[-1]
safe_ticker = html.escape(str(ticker))
if val > 0: val = -val
if hasattr(self.cfg, 'set_vrev_gap_threshold'):
try: await asyncio.wait_for(asyncio.to_thread(self.cfg.set_vrev_gap_threshold, ticker, val), timeout=10.0)
except Exception as e: logging.error(f"🚨 VREV 갭 임계치 설정 에러: {e}")
try: await asyncio.wait_for(update.effective_message.reply_text(f"📉 <b>[{safe_ticker}] V-REV 장막판 갭 스위칭 임계치 설정 완료!</b>\n▫️ 팩트 타격선: 기초자산 VWAP 대비 <b>{val}%</b>\n▫️ 다음 타임 슬라이싱 스케줄부터 즉시 적용됩니다.", parse_mode='HTML'), timeout=10.0)
except Exception: pass
if hasattr(controller, 'cmd_settlement'):
try:
await controller.cmd_settlement(update, context)
except BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
except Exception as e:
safe_err = html.escape(str(e))
try: await asyncio.wait_for(update.effective_message.reply_text(f"❌ 알 수 없는 오류 발생: {safe_err}"), timeout=10.0)
except Exception: pass
finally:
if chat_id in controller.user_states:
del controller.user_states[chat_id]
async def _handle_callback_reset(self, query, ticker):
if ticker:
try: await asyncio.wait_for(query.answer("⏳ 매매 잠금 해제 중...", show_alert=False), timeout=5.0)
except Exception: pass
def _hijack_vwap_lock():
est_now = datetime.datetime.now(ZoneInfo('America/New_York'))
today_str = est_now.strftime('%Y-%m-%d')
# 🚨 MODIFIED: 2차 오인 패러독스 차단 (물리적 삭제 대신 빈 지시서 박제)
empty_state = {"date": today_str, "hijacked": True, "orders": []}
for f_path in [f"data/vrev_slice_state_{ticker}.json", f"data/vrev_aftermarket_state_{ticker}.json"]:
with GlobalThrottle.get_file_lock(f_path):
dir_name = os.path.dirname(f_path) or '.'
try: os.makedirs(dir_name, exist_ok=True)
except OSError: pass
try:
fd, tmp_path = tempfile.mkstemp(dir=dir_name, text=True)
with os.fdopen(fd, 'w', encoding='utf-8') as f_out:
json.dump(empty_state, f_out, ensure_ascii=False, indent=4)
f_out.flush()
os.fsync(f_out.fileno())
os.replace(tmp_path, f_path)
except Exception: pass
try:
for snap_prefix in ["REV", "V14", "V14VWAP"]:
snap_file = f"data/daily_snapshot_{snap_prefix}_{today_str}_{ticker}.json"
with GlobalThrottle.get_file_lock(snap_file):
try: os.remove(snap_file)
except OSError: pass
except Exception:
pass
await asyncio.wait_for(asyncio.to_thread(_hijack_vwap_lock), timeout=10.0)
await asyncio.wait_for(asyncio.to_thread(self.cfg.reset_lock_for_ticker, ticker), timeout=10.0)
try:
await asyncio.wait_for(query.edit_message_text(f"✅ <b>[{html.escape(str(ticker))}] 금일 매매 잠금이 해제되었으며, 오염된 슬라이싱 엔진 및 스냅샷도 무효화되었습니다.</b>", parse_mode='HTML'), timeout=10.0)
except telegram.error.BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass
async def _handle_callback_confirm(self, query, ticker, context):
if not ticker: return
try: await asyncio.wait_for(query.answer("🔥 삼위일체 소각 진행 중...", show_alert=False), timeout=5.0)
except Exception: pass
current_ver = str(await asyncio.wait_for(asyncio.to_thread(self.cfg.get_version, ticker), timeout=10.0) or "")
is_rev_active = (current_ver == "V_REV")
await asyncio.wait_for(asyncio.to_thread(self.cfg.set_reverse_state, ticker, is_rev_active, 0, 0.0), timeout=10.0)
ledger = await asyncio.wait_for(asyncio.to_thread(self.cfg.get_ledger), timeout=10.0) or []
ledger_data = [r for r in ledger if isinstance(r, dict) and str(r.get('ticker')) != str(ticker)]
await asyncio.wait_for(asyncio.to_thread(self.cfg._save_json, self.cfg.FILES["LEDGER"], ledger_data), timeout=15.0)
def _process_reset_files():
backup_file = self.cfg.FILES["LEDGER"].replace(".json", "_backup.json")
with GlobalThrottle.get_file_lock(backup_file):
try:
with open(backup_file, 'r', encoding='utf-8') as f:
b_data = json.load(f)
if not isinstance(b_data, list): b_data = []
b_data = [r for r in b_data if isinstance(r, dict) and str(r.get('ticker')) != str(ticker)]
dir_name = os.path.dirname(backup_file) or '.'
try: os.makedirs(dir_name, exist_ok=True)
except OSError: pass
fd = None
tmp_path = None
try:
fd, tmp_path = tempfile.mkstemp(dir=dir_name, text=True)
with os.fdopen(fd, 'w', encoding='utf-8') as f_out:
fd = None
json.dump(b_data, f_out, ensure_ascii=False, indent=4)
f_out.flush()
os.fsync(f_out.fileno())
os.replace(tmp_path, backup_file)
tmp_path = None
except Exception:
if fd is not None:
try: os.close(fd)
except OSError: pass
if tmp_path:
try: os.remove(tmp_path)
except OSError: pass
except OSError: pass
except Exception: pass
await asyncio.wait_for(asyncio.to_thread(_process_reset_files), timeout=10.0)
if getattr(self, 'queue_ledger', None):
await asyncio.wait_for(asyncio.to_thread(self.queue_ledger.clear_queue, ticker), timeout=10.0)
await asyncio.wait_for(asyncio.to_thread(self.queue_ledger.sync_with_broker, ticker, 0, 0.0), timeout=10.0)
def _nuke_assassin_data():
try:
from assassin_ledger import AssassinLedger
a_ledger = AssassinLedger()
a_ledger.clear_ledger(ticker)
except Exception as e:
logging.error(f"🚨 [{ticker}] 암살자 장부 강제 소각 중 에러: {e}")
state_file = f"data/avwap_trade_state_{ticker}.json"
with GlobalThrottle.get_file_lock(state_file):
try:
est_now = datetime.datetime.now(ZoneInfo('America/New_York'))
today_str = est_now.strftime('%Y-%m-%d')
state_data = {}
try:
with open(state_file, 'r', encoding='utf-8') as f:
state_data = json.load(f)
except Exception:
pass
curr_time = est_now.time()
is_pre_market = datetime.time(4, 0) <= curr_time < datetime.time(9, 30)
state_data['date'] = today_str
state_data['qty'] = 0
state_data['buy_odno'] = ""
state_data['sell_odno'] = ""
state_data['shutdown'] = is_pre_market
state_data['dumped'] = is_pre_market
dir_name = os.path.dirname(state_file) or '.'
try: os.makedirs(dir_name, exist_ok=True)
except OSError: pass
fd = None
tmp_path = None
try:
fd, tmp_path = tempfile.mkstemp(dir=dir_name, text=True)
with os.fdopen(fd, 'w', encoding='utf-8') as f_out:
fd = None
json.dump(state_data, f_out, ensure_ascii=False, indent=4)
f_out.flush()
os.fsync(f_out.fileno())
os.replace(tmp_path, state_file)
tmp_path = None
except Exception as inner_e:
if fd is not None:
try: os.close(fd)
except OSError: pass
if tmp_path:
try: os.remove(tmp_path)
except OSError: pass
raise inner_e
except Exception as e:
logging.error(f"🚨 [{ticker}] 암살자 상태 셧다운 주입 에러: {e}")
await asyncio.wait_for(asyncio.to_thread(_nuke_assassin_data), timeout=10.0)
await asyncio.wait_for(asyncio.to_thread(self.cfg.reset_lock_for_ticker, ticker), timeout=10.0)
prev_c = 0.0
for attempt in range(3):
try:
await asyncio.sleep(0.06)
prev_c_val = await asyncio.wait_for(asyncio.to_thread(self.broker.get_previous_close, ticker), timeout=10.0)
prev_c = self._safe_float(prev_c_val)
break
except Exception as e:
if attempt == 2: logging.error(f"🚨 수동 소각 후 전일 종가 스캔 에러: {e}")
else: await asyncio.sleep(1.0 * (2 ** attempt))
if prev_c > 0:
try:
kis_qty = 0
kis_avg = 0.0
async with self.tx_lock:
cash_val = 0.0
for attempt in range(3):
try:
await asyncio.sleep(0.06)
cash_tuple = await asyncio.wait_for(asyncio.to_thread(self.broker.get_account_balance), timeout=10.0)
cash_val = cash_tuple[0] if isinstance(cash_tuple, (list, tuple)) and len(cash_tuple) > 0 else 0.0
holdings = cash_tuple[1] if isinstance(cash_tuple, (list, tuple)) and len(cash_tuple) > 1 else {}
break
except Exception:
if attempt == 2: holdings = {}
else: await asyncio.sleep(1.0 * (2 ** attempt))
cash = self._safe_float(cash_val)
if isinstance(holdings, dict) and ticker in holdings:
kis_qty = int(self._safe_float(holdings[ticker].get('qty', 0)))
kis_avg = self._safe_float(holdings[ticker].get('avg', 0.0))
from scheduler_core import get_budget_allocation
active_tickers_list = await asyncio.wait_for(asyncio.to_thread(self.cfg.get_active_tickers), timeout=10.0) or []
_, alloc_cash_dict = await asyncio.wait_for(asyncio.to_thread(get_budget_allocation, cash, active_tickers_list, self.cfg), timeout=10.0)
alloc_cash_dict = alloc_cash_dict or {}
available_cash = self._safe_float(alloc_cash_dict.get(ticker))
await asyncio.wait_for(
asyncio.to_thread(
self.strategy.get_plan,
ticker, 0.0, kis_avg, kis_qty, prev_c,
ma_5day=0.0, market_type="REG", available_cash=available_cash,
is_simulation=True, is_snapshot_mode=True
), timeout=15.0
)
except Exception as e:
logging.error(f"🚨 0주 강제 스냅샷 오버라이드 에러: {e}")
try:
await asyncio.wait_for(query.edit_message_text(f"✅ <b>[{html.escape(str(ticker))}] 삼위일체 소각(Nuke) 및 초기화 완료!</b>\n▫️ 본장부, 백업장부, 큐(Queue) 찌꺼기 데이터가 100% 영구 삭제되었습니다.\n▫️ 암살자의 재진입(부활)이 전면 차단되었으며, 매매 잠금 해제 및 디커플링 타점 스냅샷 덮어쓰기가 완벽히 집행되었습니다.", parse_mode='HTML'), timeout=10.0)
except telegram.error.BadRequest as e:
if "not modified" not in str(e).lower(): logging.warning(f"⚠️ UI 갱신 예외: {e}")
except Exception: pass