-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler_trade.py
More file actions
1337 lines (1097 loc) · 81.4 KB
/
Copy pathscheduler_trade.py
File metadata and controls
1337 lines (1097 loc) · 81.4 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
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
# ==========================================================
# [scheduler_trade.py] - 🌟 100% 통합 전투 사령부 (V29.21) 🌟 (Part 1/2)
# ⚠️ 이 주석 및 파일명 표기는 절대 지우지 마세요.
# 🚨 [V27.13 그랜드 수술] 코파일럿 합작 5대 엣지 케이스 완벽 수술 완료
# 🚀 [V28.01 그랜드 수술] 서머타임 데드락 방어 윈도우 65분 확장, 결측치 락온 차단, tx_lock 런타임 붕괴 가드 완비
# 🚀 [V28.02 그랜드 수술] 이중 매도 방어, 큐 증발 Fallback, UX 모순 팩트 교정
# 🚀 [V28.04 그랜드 수술] KIS API LOC 중복 응답 뻥튀기 맹점 원천 차단 (odno 기반 병합 엔진)
# 🚀 [V28.05 그랜드 수술] VWAP 앵커 영속화(Lock-on) 및 기억상실(Amnesia) 오발탄 방어막 이식
# 🚀 [V28.07 그랜드 수술] 스냅샷 강제 은폐 적출 및 VWAP 디커플링 무결성 확보
# 🚀 [V28.13 그랜드 수술] 애프터마켓 스냅샷 소각 맹점 적출 및 24시간 디커플링 보존
# 🚀 [V28.30 그랜드 수술] 애프터마켓 로터리 덫 휴장일 오발탄(False Fire) 원천 차단 쉴드 이식
# 🚀 [V28.31 그랜드 수술] V14 상방 스나이퍼 코어 100% 이식 및 V-REV 락다운 복 복원 완료
# 🚀 [V28.37 그랜드 수술] 스윕 피니셔 발화 후 '잔고 증발' 오발탄 원천 차단: sweep_msg_sent 플래그 교차 참조 바이패스 가드 이식
# MODIFIED: [V28.41] U_CURVE_WEIGHTS 배열 합산 불일치(0.9596)로 인한 예산 누수 버그 완벽 수술
# 🚨 [V28.50 NEW] AVWAP 조기퇴근 모드(Early Exit) 파이프라인 배선 개통 및 타겟 수익률 팩트 캐스팅
# 🚨 [V28.51 팩트 수술] 정규장 스케줄러 통신 지연(가짜 에러) 진단망 이식
# 🚨 [V29.03 팩트 수술] AVWAP 영속성(Persistence) 듀얼 캐시 동기화 이식 완료
# MODIFIED: [V29.08 핫픽스] AVWAP 암살자 런타임 라우팅 누수(AttributeError) 팩트 교정 완료
# MODIFIED: [V29.09 핫픽스] 0주 새출발 예방적 LOC 덫 타점 역배선(Swap) 팩트 교정 완료
# MODIFIED: [V29.10 핫픽스] 스나이퍼 모니터링 들여쓰기(Indentation) 붕괴 복구 및 SyntaxError 원천 차단
# MODIFIED: [V29.11 핫픽스] AVWAP 엔진 get_decision() 매개변수 불일치(TypeError) 팩트 교정 완료
# MODIFIED: [V29.12 핫픽스] 스나이퍼 모니터링 AttributeError(get_master_switch) Safe Casting 방어막 이식
# MODIFIED: [V29.13 핫픽스] 스나이퍼 모니터링 이중 락(Nested tx_lock) 데드락 붕괴 현상 원천 적출 완료
# MODIFIED: [V29.14 핫픽스] InfiniteStrategy check_sniper_condition 모듈 누락(AttributeError) Safe Bypass 방어막 이식
# MODIFIED: [V29.15 핫픽스] AVWAP 데이터 기아(Data Starvation) 원천 차단 및 필수 파라미터 배선 개통
# MODIFIED: [V29.17 그랜드 수술] AVWAP 딕셔너리 맵핑 불일치(Zero-Price), 스레드 마비(은닉메서드 오호출) 및 L1 기억상실 이중타격 맹점 완벽 적출 수술
# MODIFIED: [V29.20 팩트 수술] 미체결 스캔 문자열 참조 런타임 붕괴(AttributeError) 완벽 적출 및 시가 갭하락 시맨틱 오판 방어망(yfinance Open 팩트 파싱) 개통 완료
# MODIFIED: [V29.21 그랜드 수술] 미체결 스캔 시 예방적 LOC 덫(34) 혼선에 의한 스나이퍼 마비를 원천 차단하기 위해 순수 지정가(00) 팩트 필터링 도입.
# MODIFIED: [V29.21 그랜드 수술] AVWAP 및 스나이퍼 타격 후 8초 다중 교차 검증(ODNO 기반 팩트 스캔) 엔진을 전면 이식하고 미체결 잔량 취소(Nuke) 및 실체결량(ccld_qty) 듀얼 캐싱 처리 완료.
# MODIFIED: [V29.21 핫픽스] get_execution_history 호출 시 날짜 파라미터 누락 버그를 today_est_str 팩트 주입으로 완벽 교정.
# ==========================================================
import logging
import datetime
import pytz
import asyncio
import traceback
import math
import os
import time
import json
import random
import glob
import yfinance as yf
import pandas_market_calendars as mcal
from scheduler_core import is_market_open, get_budget_allocation, get_target_hour
# ==========================================================
# 1. 🔫 스나이퍼 모니터링 (하이브리드 AVWAP 및 V14 상방 스나이퍼)
# ==========================================================
# 🚨 [AI 에이전트(Copilot/Claude) 절대 주의 - 환각(Hallucination) 방어막]
# 코파일럿 등이 제안하는 외부 KIS API 래퍼(동기식 블로킹) 모듈을 무지성으로 병합하지 마세요.
# asyncio 루프 내에서 requests나 동기 함수를 직접 호출하면 전체 스케줄러가 100% 교착(Deadlock)에 빠집니다.
# 반드시 기존의 asyncio.to_thread 래핑과 내부 broker.py 엔진만을 사용해 비동기 무결성을 유지하세요.
async def scheduled_sniper_monitor(context):
if not is_market_open(): return
est = pytz.timezone('US/Eastern')
now_est = datetime.datetime.now(est)
if context.job.data.get('tx_lock') is None:
logging.warning("⚠️ [sniper_monitor] tx_lock 미초기화. 이번 사이클 스킵.")
return
try:
nyse = mcal.get_calendar('NYSE')
schedule = nyse.schedule(start_date=now_est.date(), end_date=now_est.date())
if schedule.empty: return
market_open = schedule.iloc[0]['market_open'].astimezone(est)
market_close = schedule.iloc[0]['market_close'].astimezone(est)
except Exception:
if now_est.weekday() < 5:
market_open = now_est.replace(hour=9, minute=30, second=0, microsecond=0)
market_close = now_est.replace(hour=16, minute=0, second=0, microsecond=0)
else: return
pre_start = market_open - datetime.timedelta(hours=5, minutes=30)
start_monitor = pre_start + datetime.timedelta(minutes=1)
end_monitor = market_close - datetime.timedelta(minutes=1)
if not (start_monitor <= now_est <= end_monitor):
return
is_regular_session = market_open <= now_est <= market_close
app_data = context.job.data
cfg, broker, strategy, tx_lock = app_data['cfg'], app_data['broker'], app_data['strategy'], app_data['tx_lock']
base_map = app_data.get('base_map', {'SOXL': 'SOXX', 'TQQQ': 'QQQ'})
chat_id = context.job.chat_id
tracking_cache = app_data.setdefault('sniper_tracking', {})
today_est_str = now_est.strftime('%Y%m%d')
if tracking_cache.get('date') != today_est_str:
tracking_cache.clear()
tracking_cache['date'] = today_est_str
try:
for _f in glob.glob("data/sniper_cache_*.json"): os.remove(_f)
except: pass
async def _do_sniper():
async with tx_lock:
cash, holdings = await asyncio.to_thread(broker.get_account_balance)
if holdings is None: return
safe_holdings = holdings if isinstance(holdings, dict) else {}
avwap_free_cash = cash
for t in cfg.get_active_tickers():
version = cfg.get_version(t)
if version == "V_REV":
h = safe_holdings.get(t) or {}
actual_qty = int(float(h.get('qty', 0)))
q_ledger = app_data.get('queue_ledger')
if q_ledger:
q_data = q_ledger.get_queue(t)
total_q = sum(item.get("qty", 0) for item in q_data)
if actual_qty == 0 and total_q > 0:
_vwap_cache_ref = app_data.get('vwap_cache', {})
if _vwap_cache_ref.get(f"REV_{t}_sweep_msg_sent"):
continue
if not tracking_cache.get(f"REV_{t}_panic_sell_warn"):
tracking_cache[f"REV_{t}_panic_sell_warn"] = True
await context.bot.send_message(
chat_id=chat_id,
text=f"🚨 <b>[비상] [{t}] 뇌동매매로 인한 잔고 증발이 감지되었습니다.</b>\n"
f"▫️ 봇의 매매가 일시 정지됩니다.\n"
f"▫️ 시드 오염을 막기 위해 즉시 <code>/reset</code> 커맨드를 실행하여 장부를 소각하십시오.",
parse_mode='HTML'
)
continue
if version == "V_REV":
if not cfg.get_avwap_hybrid_mode(t): continue
if not tracking_cache.get(f"AVWAP_INIT_{t}"):
try:
saved_state = strategy.v_avwap_plugin.load_state(t, now_est)
if saved_state:
tracking_cache[f"AVWAP_BOUGHT_{t}"] = saved_state.get('bought', False)
tracking_cache[f"AVWAP_SHUTDOWN_{t}"] = saved_state.get('shutdown', False)
tracking_cache[f"AVWAP_QTY_{t}"] = saved_state.get('qty', 0)
tracking_cache[f"AVWAP_AVG_{t}"] = saved_state.get('avg_price', 0.0)
except Exception as e:
logging.error(f"AVWAP 상태 복구 실패: {e}")
tracking_cache[f"AVWAP_INIT_{t}"] = True
if tracking_cache.get(f"AVWAP_SHUTDOWN_{t}"): continue
target_base = base_map.get(t, t)
if f"AVWAP_CTX_{t}" not in tracking_cache or tracking_cache[f"AVWAP_CTX_{t}"] is None:
ctx_data = await asyncio.to_thread(strategy.fetch_avwap_macro, target_base)
if ctx_data is not None:
tracking_cache[f"AVWAP_CTX_{t}"] = ctx_data
else:
continue
ctx_data = tracking_cache.get(f"AVWAP_CTX_{t}")
avwap_qty = tracking_cache.get(f"AVWAP_QTY_{t}", 0)
avwap_avg = tracking_cache.get(f"AVWAP_AVG_{t}", 0.0)
exec_curr_p = float(await asyncio.to_thread(broker.get_current_price, t) or 0.0)
if exec_curr_p <= 0: continue
base_curr_p = float(await asyncio.to_thread(broker.get_current_price, target_base) or 0.0)
if base_curr_p <= 0: continue
def _fetch_open(tkr):
try:
st = yf.Ticker(tkr)
h = st.history(period="1d", interval="1m", prepost=False, timeout=5)
if not h.empty: return float(h['Open'].dropna().iloc[0])
except: pass
return 0.0
base_day_open = float(await asyncio.to_thread(_fetch_open, target_base) or 0.0)
if base_day_open <= 0:
continue
df_1min_base = None
try: df_1min_base = await asyncio.to_thread(broker.get_1min_candles_df, target_base)
except: pass
early_exit_mode = cfg.get_avwap_early_exit_mode(t)
early_target_profit = cfg.get_avwap_early_target(t) / 100.0
decision = strategy.v_avwap_plugin.get_decision(
base_ticker=target_base,
exec_ticker=t,
base_curr_p=base_curr_p,
exec_curr_p=exec_curr_p,
base_day_open=base_day_open,
avwap_avg_price=avwap_avg,
avwap_qty=avwap_qty,
avwap_alloc_cash=avwap_free_cash,
context_data=ctx_data,
df_1min_base=df_1min_base,
now_est=now_est,
early_exit_mode=early_exit_mode,
early_target_profit=early_target_profit
)
action = decision.get("action")
reason = decision.get("reason", "")
if action == "BUY":
alloc_cash = decision.get("alloc_cash", 0)
price = float(decision.get("target_price", decision.get("price", 0.0)))
qty = int(decision.get("qty", 0))
if qty > 0 and price > 0:
# MODIFIED: [V29.21 그랜드 수술] 예방적 LOC 덫(34) 혼선 차단을 위해 순수 지정가(00) 미체결만 바이패스 검증
has_unfilled = False
for _ in range(4):
unfilled = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
if isinstance(unfilled, list) and any(
o.get('sll_buy_dvsn_cd') == '02' and str(o.get('ord_dvsn_cd') or o.get('ord_dvsn') or '').strip().zfill(2) == '00'
for o in unfilled
):
has_unfilled = True
break
await asyncio.sleep(2.0)
if has_unfilled:
continue
res = await asyncio.to_thread(broker.send_order, t, "BUY", qty, price, "LIMIT")
odno = res.get('odno', '') if isinstance(res, dict) else ''
if res and res.get('rt_cd') == '0' and odno:
# MODIFIED: [V29.21 그랜드 수술] 8초(4회) 다중 교차 검증 및 미체결 잔여 강제 Nuke 로직 이식
ccld_qty = 0
for _ in range(4):
await asyncio.sleep(2.0)
unfilled_check = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
safe_unfilled = unfilled_check if isinstance(unfilled_check, list) else []
my_order = next((ox for ox in safe_unfilled if ox.get('odno') == odno), None)
if my_order:
ccld_qty = int(float(my_order.get('tot_ccld_qty') or 0))
else:
ccld_qty = qty
break
if ccld_qty < qty:
try:
await asyncio.to_thread(broker.cancel_order, t, odno)
await asyncio.sleep(0.5)
except Exception as e_cancel:
logging.warning(f"⚠️ [{t}] AVWAP 매수 잔여 취소 실패: {e_cancel}")
if ccld_qty > 0:
msg = f"⚔️ <b>[AVWAP] 단타 암살자 딥매수 타격 성공!</b>\n▫️ 타겟: {t}\n▫️ 타점: ${price}\n▫️ 팩트 체결수량: {ccld_qty}주 (목표 {qty}주)\n▫️ 사유: {reason}"
if ccld_qty < qty:
msg += f"\n▫️ 미체결 {qty - ccld_qty}주는 안전을 위해 즉각 취소(Nuke)되었습니다."
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML')
old_qty = tracking_cache.get(f"AVWAP_QTY_{t}", 0)
old_avg = tracking_cache.get(f"AVWAP_AVG_{t}", 0.0)
new_qty = old_qty + ccld_qty
new_avg = ((old_qty * old_avg) + (ccld_qty * price)) / new_qty if new_qty > 0 else 0.0
tracking_cache[f"AVWAP_BOUGHT_{t}"] = True
tracking_cache[f"AVWAP_SHUTDOWN_{t}"] = False
tracking_cache[f"AVWAP_QTY_{t}"] = new_qty
tracking_cache[f"AVWAP_AVG_{t}"] = round(new_avg, 4)
state_data = {
"bought": True,
"shutdown": False,
"qty": new_qty,
"avg_price": round(new_avg, 4)
}
await asyncio.to_thread(strategy.v_avwap_plugin.save_state, t, now_est, state_data)
elif action == "SELL":
price = float(decision.get("target_price", decision.get("price", 0.0)))
qty = int(decision.get("qty", 0))
exec_price = price
if exec_price <= 0.0:
bid_price = float(await asyncio.to_thread(broker.get_bid_price, t) or 0.0)
exec_price = bid_price if bid_price > 0 else exec_curr_p
if qty > 0:
# MODIFIED: [V29.21 그랜드 수술] 예방적 LOC 덫 혼선 차단
has_unfilled = False
for _ in range(4):
unfilled = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
if isinstance(unfilled, list) and any(
o.get('sll_buy_dvsn_cd') == '01' and str(o.get('ord_dvsn_cd') or o.get('ord_dvsn') or '').strip().zfill(2) == '00'
for o in unfilled
):
has_unfilled = True
break
await asyncio.sleep(2.0)
if has_unfilled:
continue
res = await asyncio.to_thread(broker.send_order, t, "SELL", qty, exec_price, "LIMIT")
odno = res.get('odno', '') if isinstance(res, dict) else ''
if res and res.get('rt_cd') == '0' and odno:
# MODIFIED: [V29.21 그랜드 수술] 8초(4회) 다중 교차 검증 및 미체결 즉각 Nuke 로직 이식
ccld_qty = 0
for _ in range(4):
await asyncio.sleep(2.0)
unfilled_check = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
safe_unfilled = unfilled_check if isinstance(unfilled_check, list) else []
my_order = next((ox for ox in safe_unfilled if ox.get('odno') == odno), None)
if my_order:
ccld_qty = int(float(my_order.get('tot_ccld_qty') or 0))
else:
ccld_qty = qty
break
if ccld_qty < qty:
try:
await asyncio.to_thread(broker.cancel_order, t, odno)
await asyncio.sleep(0.5)
except Exception as e_cancel:
logging.warning(f"⚠️ [{t}] AVWAP 매도 잔여 취소 실패: {e_cancel}")
if ccld_qty > 0:
msg = f"⚔️ <b>[AVWAP] 암살자 덤핑 타격!</b>\n▫️ 타겟: {t}\n▫️ 타점: ${exec_price}\n▫️ 팩트 체결수량: {ccld_qty}주 (목표 {qty}주)\n▫️ 사유: {reason}"
old_qty = tracking_cache.get(f"AVWAP_QTY_{t}", 0)
new_qty = max(0, old_qty - ccld_qty)
if new_qty == 0:
msg += "\n🛡️ 금일 해당 종목의 100% 청산 완료, 단타 작전을 영구 셧다운합니다."
shutdown_flag = True
new_avg = 0.0
else:
msg += f"\n⚠️ 잔량 {new_qty}주 발생 (미체결 강제 취소됨, 다음 1분봉 루프에서 재시도)"
shutdown_flag = tracking_cache.get(f"AVWAP_SHUTDOWN_{t}", False)
new_avg = tracking_cache.get(f"AVWAP_AVG_{t}", 0.0)
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML')
tracking_cache[f"AVWAP_BOUGHT_{t}"] = (new_qty > 0)
tracking_cache[f"AVWAP_SHUTDOWN_{t}"] = shutdown_flag
tracking_cache[f"AVWAP_QTY_{t}"] = new_qty
tracking_cache[f"AVWAP_AVG_{t}"] = new_avg
state_data = {
'bought': tracking_cache[f"AVWAP_BOUGHT_{t}"],
'shutdown': shutdown_flag,
'qty': new_qty,
'avg_price': new_avg
}
await asyncio.to_thread(strategy.v_avwap_plugin.save_state, t, now_est, state_data)
elif action == "SHUTDOWN":
if not tracking_cache.get(f"AVWAP_SHUTDOWN_{t}"):
tracking_cache[f"AVWAP_SHUTDOWN_{t}"] = True
state_data = {
"bought": tracking_cache.get(f"AVWAP_BOUGHT_{t}", False),
"shutdown": True,
"qty": tracking_cache.get(f"AVWAP_QTY_{t}", 0),
"avg_price": tracking_cache.get(f"AVWAP_AVG_{t}", 0.0)
}
await asyncio.to_thread(strategy.v_avwap_plugin.save_state, t, now_est, state_data)
msg = f"🛡️ <b>[AVWAP] 암살자 작전 영구 셧다운(동결)</b>\n▫️ 타겟: {t}\n▫️ 사유: {reason}"
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML')
master_switch = getattr(cfg, 'get_master_switch', lambda x: "ALL")(t)
sniper_buy_locked = getattr(cfg, 'get_sniper_buy_locked', lambda x: False)(t)
sniper_sell_locked = getattr(cfg, 'get_sniper_sell_locked', lambda x: False)(t)
curr_p = await asyncio.to_thread(broker.get_current_price, t)
if curr_p is None or float(curr_p) <= 0:
continue
sniper_func = getattr(strategy, 'check_sniper_condition', None)
if sniper_func:
res = await asyncio.to_thread(sniper_func, t, cfg, broker, chat_id)
else:
res = {"action": "HOLD", "reason": "스나이퍼 모듈 누락(Bypass)", "limit_price": 0.0}
action = res.get("action")
reason = res.get("reason", "")
limit_p = res.get("limit_price", 0.0)
is_rev = (cfg.get_version(t) == "V_REV")
if action == "BUY" and not is_rev and not sniper_buy_locked and master_switch != "UP_ONLY":
qty = res.get("qty", 0)
if qty > 0:
cancelled = await asyncio.to_thread(broker.cancel_targeted_orders, t, "02", "03")
await asyncio.sleep(1.0)
# MODIFIED: [V29.21 그랜드 수술] 지정가(00) 주문 필터링으로 LOC 덫 혼선 차단
has_unfilled = False
for _ in range(4):
unfilled = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
if isinstance(unfilled, list) and any(
o.get('sll_buy_dvsn_cd') == '02' and str(o.get('ord_dvsn_cd') or o.get('ord_dvsn') or '').strip().zfill(2) == '00'
for o in unfilled
):
has_unfilled = True
break
await asyncio.sleep(2.0)
if has_unfilled:
continue
order_res = await asyncio.to_thread(broker.send_order, t, "BUY", qty, limit_p, "LIMIT")
odno = order_res.get('odno', '') if isinstance(order_res, dict) else ''
if order_res and order_res.get('rt_cd') == '0' and odno:
ccld_qty = 0
for _ in range(4):
await asyncio.sleep(2.0)
unfilled_check = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
safe_unfilled = unfilled_check if isinstance(unfilled_check, list) else []
my_order = next((ox for ox in safe_unfilled if ox.get('odno') == odno), None)
if my_order:
ccld_qty = int(float(my_order.get('tot_ccld_qty') or 0))
else:
ccld_qty = qty
break
if ccld_qty < qty:
try:
await asyncio.to_thread(broker.cancel_order, t, odno)
await asyncio.sleep(1.0)
except: pass
if ccld_qty > 0:
if hasattr(cfg, 'set_sniper_buy_locked'):
cfg.set_sniper_buy_locked(t, True)
exec_history = await asyncio.to_thread(broker.get_execution_history, t, today_est_str, today_est_str)
def get_actual_execution_price(history, side_code, target_odno):
if not history: return 0.0
for ex in history:
if ex.get('sll_buy_dvsn_cd') == side_code and ex.get('odno') == target_odno:
p = float(ex.get('ft_ccld_unpr3', '0'))
if p > 0: return p
target_recs = [ex for ex in history if ex.get('sll_buy_dvsn_cd') == side_code]
for ex in target_recs:
p = float(ex.get('ft_ccld_unpr3', '0'))
if p > 0: return p
return 0.0
actual_exec_price = get_actual_execution_price(exec_history, "02", odno)
display_price = actual_exec_price if actual_exec_price > 0 else limit_p
msg = f"🚨 <b>[{t}] 스나이퍼 딥-매수(Intercept) 명중!</b>\n▫️ 타겟가: ${limit_p}\n▫️ 팩트 단가: ${display_price}\n▫️ 체결수량: {ccld_qty}주 (요청: {qty}주)\n▫️ 사유: {reason}\n▫️ 하방 방어망이 잠깁니다 (상방 독립 유지)."
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML')
upward_mode = getattr(cfg, 'get_upward_sniper_mode', lambda x: False)(t)
is_upward_active = upward_mode and not is_rev and not sniper_sell_locked and master_switch != "DOWN_ONLY"
if is_upward_active and action in ["SELL_QUARTER", "SELL_JACKPOT"]:
qty = res.get("qty", 0)
if qty > 0:
cancelled = await asyncio.to_thread(broker.cancel_targeted_orders, t, "01", "03")
await asyncio.sleep(1.0)
# MODIFIED: [V29.21 그랜드 수술] 지정가(00) 주문 필터링으로 LOC 덫 혼선 차단
has_unfilled = False
for _ in range(4):
unfilled = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
if isinstance(unfilled, list) and any(
o.get('sll_buy_dvsn_cd') == '01' and str(o.get('ord_dvsn_cd') or o.get('ord_dvsn') or '').strip().zfill(2) == '00'
for o in unfilled
):
has_unfilled = True
break
await asyncio.sleep(2.0)
if has_unfilled:
continue
order_res = await asyncio.to_thread(broker.send_order, t, "SELL", qty, limit_p, "LIMIT")
odno = order_res.get('odno', '') if isinstance(order_res, dict) else ''
if order_res and order_res.get('rt_cd') == '0' and odno:
ccld_qty = 0
for _ in range(4):
await asyncio.sleep(2.0)
unfilled_check = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
safe_unfilled = unfilled_check if isinstance(unfilled_check, list) else []
my_order = next((ox for ox in safe_unfilled if ox.get('odno') == odno), None)
if my_order:
ccld_qty = int(float(my_order.get('tot_ccld_qty') or 0))
else:
ccld_qty = qty
break
if ccld_qty < qty:
try:
await asyncio.to_thread(broker.cancel_order, t, odno)
await asyncio.sleep(1.0)
except: pass
if ccld_qty > 0:
if hasattr(cfg, 'set_sniper_sell_locked'):
cfg.set_sniper_sell_locked(t, True)
exec_history = await asyncio.to_thread(broker.get_execution_history, t, today_est_str, today_est_str)
def get_actual_execution_price(history, side_code, target_odno):
if not history: return 0.0
for ex in history:
if ex.get('sll_buy_dvsn_cd') == side_code and ex.get('odno') == target_odno:
p = float(ex.get('ft_ccld_unpr3', '0'))
if p > 0: return p
target_recs = [ex for ex in history if ex.get('sll_buy_dvsn_cd') == side_code]
for ex in target_recs:
p = float(ex.get('ft_ccld_unpr3', '0'))
if p > 0: return p
return 0.0
actual_exec_price = get_actual_execution_price(exec_history, "01", odno)
display_price = actual_exec_price if actual_exec_price > 0 else limit_p
msg = f"🦇 <b>[{t}] 스나이퍼 상방 기습({action}) 명중!</b>\n▫️ 타겟가: ${limit_p}\n▫️ 팩트 단가: ${display_price}\n▫️ 체결수량: {ccld_qty}주 (요청: {qty}주)\n▫️ 사유: {reason}\n▫️ 상방 감시망이 잠깁니다 (하방 독립 유지)."
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML')
try:
await asyncio.wait_for(_do_sniper(), timeout=45.0)
except Exception as e:
logging.error(f"🚨 스나이퍼 타임아웃 에러: {e}", exc_info=True)
# ==========================================================
# [scheduler_trade.py] - 🌟 100% 통합 전투 사령부 (V29.21) 🌟 (Part 2/2)
# ==========================================================
# ==========================================================
# 2. 🛡️ Fail-Safe: 선제적 LOC 취소 (VWAP 엔진 기상과 동기화 완료)
# ==========================================================
async def scheduled_vwap_init_and_cancel(context):
if not is_market_open(): return
est = pytz.timezone('US/Eastern')
now_est = datetime.datetime.now(est)
try:
nyse = mcal.get_calendar('NYSE')
schedule = nyse.schedule(start_date=now_est.date(), end_date=now_est.date())
if schedule.empty: return
market_close = schedule.iloc[0]['market_close'].astimezone(est)
except Exception:
market_close = now_est.replace(hour=16, minute=0, second=0, microsecond=0)
vwap_start_time = market_close - datetime.timedelta(minutes=33)
vwap_end_time = market_close
if not (vwap_start_time <= now_est <= vwap_end_time):
return
app_data = context.job.data
cfg, broker, tx_lock = app_data['cfg'], app_data['broker'], app_data['tx_lock']
chat_id = context.job.chat_id
vwap_cache = app_data.setdefault('vwap_cache', {})
today_str = now_est.strftime('%Y%m%d')
if vwap_cache.get('date') != today_str:
vwap_cache.clear()
vwap_cache['date'] = today_str
async def _do_init():
async with tx_lock:
for t in cfg.get_active_tickers():
version = cfg.get_version(t)
is_manual_vwap = getattr(cfg, 'get_manual_vwap_mode', lambda x: False)(t)
if version == "V_REV" and is_manual_vwap:
continue
if version == "V_REV" or (version == "V14" and is_manual_vwap):
if not vwap_cache.get(f"REV_{t}_nuked"):
try:
await asyncio.to_thread(broker.cancel_all_orders_safe, t, "BUY")
await asyncio.to_thread(broker.cancel_all_orders_safe, t, "SELL")
vwap_cache[f"REV_{t}_nuked"] = True
msg = f"🌅 <b>[{t}] 장 마감 33분 전 엔진 기상 (Fail-Safe 전환)</b>\n"
msg += f"▫️ 프리장에 선제 전송해둔 '예방적 양방향 LOC 덫'을 전량 취소(Nuke)했습니다.\n"
msg += f"▫️ 1분 단위 정밀 타격(VWAP 슬라이싱) 모드로 교전 수칙을 변경합니다. ⚔️"
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML', disable_notification=True)
await asyncio.sleep(1.0)
except Exception as e:
logging.error(f"🚨 Fail-Safe 초기화(Nuke) 에러: {e}", exc_info=True)
vwap_cache[f"REV_{t}_nuked"] = False
try:
await asyncio.wait_for(_do_init(), timeout=45.0)
except Exception as e:
logging.error(f"🚨 Fail-Safe 타임아웃 에러: {e}", exc_info=True)
# ==========================================================
# 3. 🎯 VWAP 1분 단위 정밀 타격 엔진
# ==========================================================
async def scheduled_vwap_trade(context):
if not is_market_open(): return
est = pytz.timezone('US/Eastern')
now_est = datetime.datetime.now(est)
if context.job.data.get('tx_lock') is None:
logging.warning("⚠️ [vwap_trade] tx_lock 미초기화. 이번 사이클 스킵.")
return
try:
nyse = mcal.get_calendar('NYSE')
schedule = nyse.schedule(start_date=now_est.date(), end_date=now_est.date())
if schedule.empty: return
market_close = schedule.iloc[0]['market_close'].astimezone(est)
except Exception:
market_close = now_est.replace(hour=16, minute=0, second=0, microsecond=0)
vwap_start_time = market_close - datetime.timedelta(minutes=33)
vwap_end_time = market_close
if not (vwap_start_time <= now_est <= vwap_end_time):
return
app_data = context.job.data
cfg, broker, strategy, tx_lock = app_data['cfg'], app_data['broker'], app_data['strategy'], app_data['tx_lock']
chat_id = context.job.chat_id
vwap_cache = app_data.setdefault('vwap_cache', {})
today_str = now_est.strftime('%Y%m%d')
if vwap_cache.get('date') != today_str:
vwap_cache.clear()
vwap_cache['date'] = today_str
U_CURVE_WEIGHTS = [
0.0308, 0.0220, 0.0190, 0.0228, 0.0179, 0.0191, 0.0199, 0.0190, 0.0187, 0.0213,
0.0216, 0.0234, 0.0231, 0.0210, 0.0205, 0.0252, 0.0225, 0.0228, 0.0238, 0.0229,
0.0259, 0.0284, 0.0331, 0.0385, 0.0400, 0.0461, 0.0553, 0.0620, 0.0750, 0.1584
]
minutes_to_close = int(max(0, (market_close - now_est).total_seconds()) / 60)
min_idx = 33 - minutes_to_close
if min_idx < 0: min_idx = 0
if min_idx > 29: min_idx = 29
current_weight = U_CURVE_WEIGHTS[min_idx]
async def _do_vwap():
async with tx_lock:
cash, holdings = await asyncio.to_thread(broker.get_account_balance)
if holdings is None: return
safe_holdings = holdings if isinstance(holdings, dict) else {}
for t in cfg.get_active_tickers():
version = cfg.get_version(t)
is_manual_vwap = getattr(cfg, 'get_manual_vwap_mode', lambda x: False)(t)
if version == "V_REV" and is_manual_vwap:
continue
if version == "V_REV" or (version == "V14" and is_manual_vwap):
if not vwap_cache.get(f"REV_{t}_nuked"):
try:
await asyncio.to_thread(broker.cancel_all_orders_safe, t, "BUY")
await asyncio.to_thread(broker.cancel_all_orders_safe, t, "SELL")
vwap_cache[f"REV_{t}_nuked"] = True
msg = f"🌅 <b>[{t}] 하이브리드 타임 슬라이싱 기상 (자가 치유 가동)</b>\n"
msg += f"▫️ 장 마감 33분 전 진입을 확인하여 기존 LOC 덫 강제 취소(Nuke)했습니다.\n"
msg += f"▫️ 스케줄러 누락을 완벽히 극복하고 1분 단위 정밀 타격을 즉각 개시합니다. ⚔️"
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML', disable_notification=True)
await asyncio.sleep(1.0)
except Exception as e:
logging.error(f"🚨 자가 치유 Nuke 실패: {e}", exc_info=True)
continue
curr_p = float(await asyncio.to_thread(broker.get_current_price, t) or 0.0)
if not vwap_cache.get(f"REV_{t}_anchor_prev_c"):
prev_c_live = float(await asyncio.to_thread(broker.get_previous_close, t) or 0.0)
if prev_c_live > 0:
vwap_cache[f"REV_{t}_anchor_prev_c"] = prev_c_live
prev_c = float(vwap_cache.get(f"REV_{t}_anchor_prev_c") or 0.0)
if curr_p <= 0 or prev_c <= 0: continue
if version == "V_REV":
strategy_rev = app_data.get('strategy_rev')
queue_ledger = app_data.get('queue_ledger')
if not strategy_rev or not queue_ledger: continue
h = safe_holdings.get(t) or {}
actual_qty = int(float(h.get('qty', 0)))
q_data = queue_ledger.get_queue(t)
total_q = sum(item.get("qty", 0) for item in q_data)
if actual_qty == 0 and total_q > 0:
if vwap_cache.get(f"REV_{t}_sweep_msg_sent"):
continue
if not vwap_cache.get(f"REV_{t}_panic_sell_warn"):
vwap_cache[f"REV_{t}_panic_sell_warn"] = True
await context.bot.send_message(
chat_id=chat_id,
text=f"🚨 <b>[비상] [{t}] 수동매매로 인한 잔고 증발이 감지되었습니다.</b>\n"
f"▫️ 봇의 매매가 일시 정지됩니다.\n"
f"▫️ 시드 오염을 막기 위해 즉시 <code>/reset</code> 커맨드를 실행하여 장부를 소각하십시오.",
parse_mode='HTML'
)
continue
cached_plan = strategy_rev.load_daily_snapshot(t)
is_zero_start = (cached_plan and cached_plan.get("total_q", -1) == 0)
virtual_q_data = [] if is_zero_start else q_data
strategy_rev._load_state_if_needed(t)
held_in_cache = vwap_cache.get(f"REV_{t}_was_holding", False)
held_in_file = strategy_rev.was_holding.get(t, False)
if (held_in_cache or held_in_file) and total_q == 0:
continue
if total_q > 0:
vwap_cache[f"REV_{t}_was_holding"] = True
if not strategy_rev.was_holding.get(t, False):
strategy_rev.was_holding[t] = True
strategy_rev._save_state(t)
if total_q > 0:
avg_price = sum(item.get("qty", 0) * item.get("price", 0.0) for item in q_data) / total_q
jackpot_trigger = avg_price * 1.010
else:
avg_price = 0.0
jackpot_trigger = float('inf')
dates_in_queue = sorted(list(set(item.get('date') for item in q_data if item.get('date'))), reverse=True)
layer_1_qty = 0
layer_1_trigger = round(prev_c * 1.006, 2)
if dates_in_queue:
lots_for_date = [item for item in q_data if item.get('date') == dates_in_queue[0]]
layer_1_qty = sum(item.get('qty', 0) for item in lots_for_date)
if layer_1_qty > 0:
layer_1_price = sum(item.get('qty', 0) * item.get('price', 0.0) for item in lots_for_date) / layer_1_qty
layer_1_trigger = round(layer_1_price * 1.006, 2)
if not is_zero_start and minutes_to_close <= 3:
target_sweep_qty = 0
sweep_type = ""
if total_q > 0 and curr_p >= jackpot_trigger:
target_sweep_qty = total_q
sweep_type = "잭팟 전량"
elif layer_1_qty > 0 and curr_p >= layer_1_trigger:
target_sweep_qty = layer_1_qty
sweep_type = "1층 잔여물량"
if target_sweep_qty > 0:
await asyncio.to_thread(broker.cancel_all_orders_safe, t, "SELL")
await asyncio.sleep(0.5)
_, live_holdings = await asyncio.to_thread(broker.get_account_balance)
safe_live_holdings = live_holdings if isinstance(live_holdings, dict) else {}
if safe_live_holdings and t in safe_live_holdings:
h_live = safe_live_holdings[t]
sellable_qty = int(float(h_live.get('ord_psbl_qty', h_live.get('qty', 0))))
actual_sweep_qty = min(target_sweep_qty, sellable_qty)
if actual_sweep_qty > 0:
bid_price = float(await asyncio.to_thread(broker.get_bid_price, t) or 0.0)
exec_price = bid_price if bid_price > 0 else curr_p
# MODIFIED: [V29.21 핫픽스] 동기 함수 블로킹에 의한 루프 마비 방지 (asyncio.to_thread 래핑)
res = await asyncio.to_thread(broker.send_order, t, "SELL", actual_sweep_qty, exec_price, "LIMIT")
odno = res.get('odno', '') if isinstance(res, dict) else ''
if res and res.get('rt_cd') == '0' and odno:
if not vwap_cache.get(f"REV_{t}_sweep_msg_sent"):
msg = f"🌪️ <b>[{t}] V-REV 본대 {sweep_type} 3분 가속 스윕(Sweep) 개시!</b>\n"
if "잭팟" in sweep_type:
msg += f"▫️ 장 마감 3분 전 데드존 철거. 잭팟 커트라인({jackpot_trigger:.2f}) 돌파를 확인했습니다.\n"
else:
msg += f"▫️ 장 마감 3분 전 데드존 철거. 1층 앵커({layer_1_trigger:.2f}) 방어를 확인했습니다.\n"
msg += f"▫️ 매도 가능 잔량이 0이 될 때까지 매 1분마다 지속 덤핑합니다! (현재 <b>{actual_sweep_qty}주</b> 매수호가 폭격) 🏆"
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML')
vwap_cache[f"REV_{t}_sweep_msg_sent"] = True
ccld_qty = 0
for _ in range(4):
await asyncio.sleep(2.0)
unfilled_check = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
safe_unfilled = unfilled_check if isinstance(unfilled_check, list) else []
my_order = next((ox for ox in safe_unfilled if ox.get('odno') == odno), None)
if my_order:
ccld_qty = int(float(my_order.get('tot_ccld_qty') or 0))
else:
ccld_qty = actual_sweep_qty
break
if ccld_qty < actual_sweep_qty:
try:
await asyncio.to_thread(broker.cancel_order, t, odno)
await asyncio.sleep(0.5)
except Exception as e_cancel:
logging.warning(f"⚠️ [{t}] 스윕 잔여 주문 취소 실패: {e_cancel}")
if ccld_qty > 0:
strategy_rev.record_execution(t, "SELL", ccld_qty, exec_price)
q_snap_before_pop = list(q_data)
queue_ledger.pop_lots(t, ccld_qty)
remaining_after_pop = queue_ledger.get_queue(t)
remaining_qty_after = sum(item.get('qty', 0) for item in remaining_after_pop)
if remaining_qty_after == 0 and total_q > 0:
try:
pending_file = f"data/pending_grad_{t}.json"
pending_data = {
"q_data_before": q_snap_before_pop,
"exec_price": exec_price,
"total_q": total_q
}
with open(pending_file, 'w', encoding='utf-8') as _pf:
json.dump(pending_data, _pf)
except Exception as pg_e:
logging.error(f"🚨 [{t}] pending_grad 마커 파일 저장 실패: {pg_e}")
else:
if not vwap_cache.get(f"REV_{t}_sweep_skip_msg"):
msg = f"⚠️ <b>[{t}] 스윕 피니셔 덤핑 생략 (MOC 락다운 감지)</b>\n▫️ 조건이 달성되었으나, 대상 물량이 수동 긴급 수혈(MOC) 등 취소 불가 상태로 미국 거래소에 묶여 있어 스윕 덤핑을 자동 스킵합니다."
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML')
vwap_cache[f"REV_{t}_sweep_skip_msg"] = True
if target_sweep_qty > 0 or (total_q > 0 and curr_p >= jackpot_trigger):
continue
try:
df_1min = await asyncio.to_thread(broker.get_1min_candles_df, t)
vwap_status = strategy.analyze_vwap_dominance(df_1min)
except Exception:
vwap_status = {"vwap_price": 0.0, "is_strong_up": False, "is_strong_down": False}
current_regime = "BUY" if is_zero_start else ("SELL" if curr_p > prev_c else "BUY")
last_regime = vwap_cache.get(f"REV_{t}_regime")
if not is_zero_start and last_regime and last_regime != current_regime:
await context.bot.send_message(
chat_id=chat_id,
text=f"🔄 <b>[{t}] 실시간 공수 교대 발동!</b>\n"
f"▫️ <b>[{last_regime} ➡️ {current_regime}]</b> 모드로 두뇌를 전환하며 궤도를 수정합니다.",
parse_mode='HTML', disable_notification=True
)
try:
await asyncio.to_thread(broker.cancel_all_orders_safe, t, "BUY")
await asyncio.to_thread(broker.cancel_all_orders_safe, t, "SELL")
strategy_rev.reset_residual(t)
except Exception as e:
err_msg = f"🛑 <b>[FATAL ERROR] {t} 공수 교대 중 기존 덫 취소 실패!</b>\n▫️ 2중 예산 소진 방어를 위해 당일 남은 V-REV 교전을 강제 중단(Hard-Lock)합니다.\n▫️ 상세 오류: {e}"
await context.bot.send_message(chat_id=chat_id, text=err_msg, parse_mode='HTML')
continue
vwap_cache[f"REV_{t}_regime"] = current_regime
if vwap_cache.get(f"REV_{t}_loc_fired"):
continue
rev_daily_budget = float(cfg.get_seed(t) or 0.0) * 0.15
rev_plan = None
try:
rev_plan = strategy_rev.get_dynamic_plan(
ticker=t, curr_p=curr_p, prev_c=prev_c,
current_weight=current_weight, vwap_status=vwap_status,
min_idx=min_idx, alloc_cash=rev_daily_budget, q_data=virtual_q_data,
is_snapshot_mode=False
)
except Exception as plan_e:
logging.error(f"🚨 [{t}] get_dynamic_plan 실행 에러 (해당 티커 건너뜀): {plan_e}")
if rev_plan is None:
continue
if not is_zero_start and rev_plan.get('trigger_loc') and minutes_to_close >= 15:
vwap_cache[f"REV_{t}_loc_fired"] = True
msg = f"🛡️ <b>[{t}] 60% 거래량 지배력 감지 (추세장 전환)</b>\n"
msg += f"▫️ 기관급 자금 쏠림으로 인해 위험한 1분 단위 타임 슬라이싱(VWAP)을 전면 중단합니다.\n"
msg += f"▫️ <b>잔여 할당량 전량을 양방향 LOC 방어선으로 전환 배치 완료!</b>\n"
await context.bot.send_message(chat_id=chat_id, text=msg, parse_mode='HTML', disable_notification=True)
for o in rev_plan.get('orders', []):
if o['qty'] > 0:
# MODIFIED: [V29.21 핫픽스] 동기 함수 블로킹에 의한 루프 마비 방지 (asyncio.to_thread 래핑)
await asyncio.to_thread(broker.send_order, t, o['side'], o['qty'], o['price'], "LOC")
await asyncio.sleep(0.2)
continue
target_orders = rev_plan.get('orders', [])
elif version == "V14":
h = safe_holdings.get(t, {'qty':0, 'avg':0.0})
actual_qty = int(h.get('qty', 0))
actual_avg = float(h.get('avg', 0.0))
v14_vwap_plugin = strategy.v14_vwap_plugin
plan = v14_vwap_plugin.get_dynamic_plan(
ticker=t, current_price=curr_p, prev_c=prev_c,
current_weight=current_weight, min_idx=min_idx,
alloc_cash=0.0, qty=actual_qty, avg_price=actual_avg
)
target_orders = plan.get('orders', [])
for o in target_orders:
slice_qty = o['qty']
if slice_qty <= 0: continue
target_price = o['price']
side = o['side']
ask_price = float(await asyncio.to_thread(broker.get_ask_price, t) or 0.0)
bid_price = float(await asyncio.to_thread(broker.get_bid_price, t) or 0.0)
exec_price = ask_price if side == "BUY" else bid_price
if exec_price <= 0: exec_price = curr_p
if side == "BUY" and exec_price > target_price: continue
if side == "SELL" and exec_price < target_price: continue
# MODIFIED: [V29.21 핫픽스] 동기 함수 블로킹에 의한 루프 마비 방지 (asyncio.to_thread 래핑)
res = await asyncio.to_thread(broker.send_order, t, side, slice_qty, exec_price, "LIMIT")
odno = res.get('odno', '') if isinstance(res, dict) else ''
if res and res.get('rt_cd') == '0' and odno:
ccld_qty = 0
for _ in range(4):
await asyncio.sleep(2.0)
unfilled_check = await asyncio.to_thread(broker.get_unfilled_orders_detail, t)
safe_unfilled = unfilled_check if isinstance(unfilled_check, list) else []
my_order = next((ox for ox in safe_unfilled if ox.get('odno') == odno), None)
if my_order:
ccld_qty = int(float(my_order.get('tot_ccld_qty') or 0))
else:
ccld_qty = slice_qty
break
if ccld_qty < slice_qty:
try:
await asyncio.to_thread(broker.cancel_order, t, odno)
await asyncio.sleep(1.0)
except: pass
if ccld_qty > 0:
if version == "V_REV":
strategy_rev.record_execution(t, side, ccld_qty, exec_price)
if side == "BUY": queue_ledger.add_lot(t, ccld_qty, exec_price, "VWAP_BUY")
elif side == "SELL": queue_ledger.pop_lots(t, ccld_qty)
elif version == "V14":
v14_vwap_plugin.record_execution(t, side, ccld_qty, exec_price)
await asyncio.sleep(0.2)
try:
await asyncio.wait_for(_do_vwap(), timeout=45.0)
except Exception as e:
logging.error(f"🚨 VWAP 스케줄러 에러: {e}", exc_info=True)
# ==========================================================
# 4. 🌅 정규장 오픈 (17:05) 전송 (V14 통합 & V-REV 예방 방어선)
# ==========================================================
async def scheduled_regular_trade(context):
kst = pytz.timezone('Asia/Seoul')
now = datetime.datetime.now(kst)
target_hour, _ = get_target_hour()
chat_id = context.job.chat_id
now_minutes = now.hour * 60 + now.minute
target_minutes = target_hour * 60 + 5
if abs(now_minutes - target_minutes) > 65 and abs(now_minutes - target_minutes) < (24*60 - 65):
return
if not is_market_open():
return
app_data = context.job.data
cfg, broker, strategy, tx_lock = app_data['cfg'], app_data['broker'], app_data['strategy'], app_data['tx_lock']
strategy_rev = app_data.get('strategy_rev')
queue_ledger = app_data.get('queue_ledger')
if tx_lock is None:
logging.warning("⚠️ [regular_trade] tx_lock 미초기화. 이번 사이클 스킵.")
await context.bot.send_message(chat_id=chat_id, text="⚠️ <b>[시스템 경고]</b> tx_lock 미초기화로 정규장 주문을 1회 스킵합니다.", parse_mode='HTML')