-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathqueue_ledger.py
More file actions
496 lines (419 loc) · 23.4 KB
/
Copy pathqueue_ledger.py
File metadata and controls
496 lines (419 loc) · 23.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
# ==========================================================
# FILE: queue_ledger.py
# ==========================================================
# 🚨 VERIFIED: [최종 무결점 판정] 5대 헌법 및 48대 엣지 케이스 완벽 결속 교차 검증 완료.
# 🚨 MODIFIED: [Lost Update 궁극 방어] 인스턴스 레벨 Lock 소각 및 시스템 전역 파일 Mutex(GlobalThrottle) 락온 (Case 47)
# 🚨 MODIFIED: [수수료 트랩 원천 차단] 1층 매도 총액(Gross)에서 왕복 수수료 및 슬리피지 버퍼(0.6%)를 선차감한 '순수 회수금(Net Cash)'만을 원가 차감에 반영하여 전체 사이클 마진 붕괴 패러독스 방어 유지.
# 🚨 MODIFIED: [Insight 14] 콤마(,) 및 NaN/Inf 맹독성 유입 시 ValueError 즉사 방어를 위한 `_safe_float` 쉴드 전면 내재화.
# 🚨 VERIFIED: [Case 16] 원자적 쓰기(Atomic Write) 실패 시 임시 파일 스코프 고아화 방어 100% 사수 완료.
# 🚨 VERIFIED: [제4헌법 절대 사수] 메인 장부뿐만 아니라 백업 파일(.bak) 생성 시에도 임시 파일(.bak.tmp)을 거치는 원자적 복사(Atomic Copy)를 강제하여 OS 커널 패닉 시 백업본 오염 원천 차단.
# 🚨 NEW: [Case 48: 2-Tier Auto-Merge Protocol 락온] 3개 이상의 지층 생성 시 즉각 상위 지층을 통합하여 최대 2개 지층(2-Bucket)만을 유지하도록 `_enforce_two_tier_limit` 헬퍼 메서드 팩트 주입.
# 🚨 NEW: [지층 단가 역전 방어막 (Price Inversion Shield) 락온] 1회분 초과 지층 분할(Tier Split) 시, 전체 평단가(new_pure_price)가 1층 앵커가 될 정규장 종가(prev_close)보다 낮을 경우 2층의 단가가 1층보다 낮아지는 맹독성 역전 패러독스가 발생함. 이를 원천 차단하기 위해 `new_pure_price > prev_close` 일 때만 분할을 허용하고, 반대의 경우 분할을 포기하고 저점 단일 지층으로 보존하도록 안전장치 100% 결속 완료.
# ==========================================================
import os
import json
import time
import math
import shutil
import tempfile
from zoneinfo import ZoneInfo
from datetime import datetime
import logging
from global_throttle import GlobalThrottle # 🚨 전역 락 엔진
class QueueLedger:
def __init__(self, file_path="data/queue_ledger.json"):
self.file_path = file_path
self._ensure_file()
def _safe_float(self, value):
try:
val = float(str(value or 0.0).replace(',', ''))
if math.isnan(val) or math.isinf(val):
return 0.0
return val
except Exception:
return 0.0
def _ensure_file(self):
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
try:
dir_name = os.path.dirname(self.file_path) or '.'
os.makedirs(dir_name, exist_ok=True)
except OSError:
pass
try:
with open(self.file_path, 'r', encoding='utf-8') as f:
pass
except FileNotFoundError:
self._save_unsafe_no_lock({})
def _get_trading_date_str(self):
est = ZoneInfo('America/New_York')
return datetime.now(est).strftime("%Y-%m-%d")
def _load_unsafe(self):
last_exc = None
for attempt in range(3):
try:
with open(self.file_path, 'r', encoding='utf-8') as f:
content = f.read()
if not content.strip():
return {}
return json.loads(content)
except json.JSONDecodeError as e:
last_exc = e
break
except FileNotFoundError:
return {}
except Exception as e:
last_exc = e
time.sleep(1.0 * (2 ** attempt))
backup_path = self.file_path + ".bak"
try:
with open(backup_path, 'r', encoding='utf-8') as f:
data = json.load(f)
logging.warning(f"🚨 [QueueLedger] JSON 손상 감지. 백업 파일({backup_path}) 복원 완료. 손상된 메인 장부를 즉시 자가 치유합니다.")
try:
self._save_unsafe_no_lock(data)
except Exception as heal_e:
logging.error(f"🚨 [QueueLedger] 자가 치유 I/O 통신 에러: {heal_e}")
return data
except FileNotFoundError:
pass
except Exception as be:
logging.error(f"🚨 [QueueLedger] 백업 복원도 실패: {be}")
raise RuntimeError(f"🚨 [FATAL ERROR] {self.file_path} 장부 파일 읽기 실패. 데이터 유실 방지를 위해 시스템을 중단합니다. 원인: {last_exc}")
def _save_unsafe_no_lock(self, data):
dir_name = os.path.dirname(self.file_path) or '.'
for attempt in range(3):
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:
fd = None
json.dump(data, f, ensure_ascii=False, indent=4)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, self.file_path)
tmp_path = None
bak_path = self.file_path + ".bak"
bak_tmp_path = bak_path + ".tmp"
try:
shutil.copy2(self.file_path, bak_tmp_path)
os.replace(bak_tmp_path, bak_path)
except Exception:
try: os.remove(bak_tmp_path)
except OSError: pass
return
except Exception as e:
logging.warning(f"⚠️ [QueueLedger] 장부 저장 재시도 ({attempt+1}/3): {e}")
if fd is not None:
try: os.close(fd)
except OSError: pass
if tmp_path:
try: os.remove(tmp_path)
except OSError: pass
time.sleep(1.0 * (2 ** attempt))
logging.error(f"🚨 [QueueLedger] 장부 저장 최종 실패: {self.file_path} — 데이터 유실 위험!")
def _enforce_two_tier_limit(self, q):
if len(q) >= 3:
upper_layers = q[:-1]
merged_qty = sum(int(self._safe_float(l.get("qty", 0))) for l in upper_layers)
merged_invested = sum(int(self._safe_float(l.get("qty", 0))) * self._safe_float(l.get("price", 0.0)) for l in upper_layers)
merged_price = merged_invested / merged_qty if merged_qty > 0 else 0.0
merged_date = upper_layers[0].get("date", "")
merged_layer = {
"qty": merged_qty,
"price": round(merged_price, 4),
"date": merged_date,
"type": "AUTO_MERGED_UPPER"
}
return [merged_layer, q[-1]]
return q
def split_single_layer_if_needed(self, ticker, prev_close, portion_budget):
prev_close_f = self._safe_float(prev_close)
portion_budget_f = self._safe_float(portion_budget)
if prev_close_f <= 0.0 or portion_budget_f <= 0.0:
return False
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
q = data.get(ticker, [])
q = [lot for lot in q if int(self._safe_float(lot.get("qty"))) > 0]
if len(q) == 1:
lot = q[0]
qty = int(self._safe_float(lot.get("qty")))
price = self._safe_float(lot.get("price"))
total_invested = qty * price
# 🚨 MODIFIED: [지층 단가 역전 패러독스 방어] price(현재 평단가)가 prev_close_f 보다 커야만 분할 진행
if total_invested > portion_budget_f * 1.1 and price > prev_close_f:
new_l1_qty = math.floor(portion_budget_f / prev_close_f)
if 0 < new_l1_qty < qty:
new_l1_invested = new_l1_qty * prev_close_f
rem_qty = qty - new_l1_qty
rem_invested = total_invested - new_l1_invested
rem_price = round(max(0.01, rem_invested / rem_qty), 4)
lot_date = lot.get("date", self._get_trading_date_str())
new_l2 = {
"qty": rem_qty,
"price": rem_price,
"date": lot_date,
"type": "AUTO_SPLIT_UPPER"
}
new_l1 = {
"qty": new_l1_qty,
"price": round(prev_close_f, 4),
"date": lot_date,
"type": "AUTO_SPLIT_L1"
}
data[ticker] = [new_l2, new_l1]
self._save_unsafe_no_lock(data)
logging.info(f"🔪 [{ticker}] 큐 장부 단일 지층 수동 분할 완료: 1층({new_l1_qty}주 @ ${prev_close_f:.2f}), 상위층({rem_qty}주 @ ${rem_price:.2f})")
return True
return False
def apply_stock_split(self, ticker, ratio):
if ratio <= 0: return
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
q = data.get(ticker, [])
changed = False
for lot in q:
old_qty = int(self._safe_float(lot.get("qty", 0)))
raw_new_qty = old_qty * ratio
new_qty = math.floor(raw_new_qty + 0.5)
lot["qty"] = new_qty if new_qty > 0 else (1 if old_qty > 0 else 0)
old_price = self._safe_float(lot.get("price", 0.0))
lot["price"] = round(old_price / ratio, 4)
changed = True
if changed:
data[ticker] = q
self._save_unsafe_no_lock(data)
def get_queue(self, ticker):
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
q = data.get(ticker, [])
return [lot for lot in q if int(self._safe_float(lot.get("qty"))) > 0]
def add_lot(self, ticker, qty, price, lot_type="NORMAL"):
qty = int(self._safe_float(qty))
if qty <= 0: return
price_f = self._safe_float(price)
if price_f <= 0.0:
logging.error(f"🚨 [QueueLedger] add_lot 중단: {ticker} — 유효하지 않은 매수 가격 (price={price}). 로트 추가 취소.")
return
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
q = data.get(ticker, [])
q = [lot for lot in q if int(self._safe_float(lot.get("qty"))) > 0]
today_str = self._get_trading_date_str()
if q and str(q[-1].get("date", "")).startswith(today_str):
old_qty = int(self._safe_float(q[-1].get("qty")))
old_price = self._safe_float(q[-1].get("price"))
new_qty = old_qty + qty
new_price = ((old_qty * old_price) + (qty * price_f)) / new_qty if new_qty > 0 else 0.0
q[-1]["qty"] = new_qty
q[-1]["price"] = round(new_price, 4)
q[-1]["date"] = datetime.now(ZoneInfo('America/New_York')).strftime("%Y-%m-%d %H:%M:%S")
else:
q.append({
"qty": qty,
"price": price_f,
"date": datetime.now(ZoneInfo('America/New_York')).strftime("%Y-%m-%d %H:%M:%S"),
"type": lot_type
})
q = self._enforce_two_tier_limit(q)
data[ticker] = q
self._save_unsafe_no_lock(data)
def pop_lots(self, ticker, target_qty, sold_price=0.0, prev_close=None, portion_budget=None):
original_target = int(self._safe_float(target_qty))
target_qty = original_target
if target_qty <= 0: return 0
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
q = data.get(ticker, [])
q = [lot for lot in q if int(self._safe_float(lot.get("qty"))) > 0]
if not q: return 0
vrev_total_invested = sum(int(self._safe_float(item.get('qty'))) * self._safe_float(item.get('price')) for item in q)
popped_total = 0
realized_cash = 0.0
while q and target_qty > 0:
last_lot = q[-1]
lot_qty = int(self._safe_float(last_lot.get("qty")))
lot_price = self._safe_float(last_lot.get("price"))
cp = sold_price if sold_price > 0 else lot_price
if lot_qty == 0:
q.pop()
continue
if lot_qty <= target_qty:
popped = q.pop()
popped_qty = int(self._safe_float(popped.get("qty")))
popped_total += popped_qty
realized_cash += popped_qty * cp
target_qty -= popped_qty
else:
last_lot["qty"] = lot_qty - target_qty
popped_total += target_qty
realized_cash += target_qty * cp
target_qty = 0
remaining_qty = sum(int(self._safe_float(item.get('qty'))) for item in q)
if remaining_qty > 0 and popped_total > 0:
if len(q) == 1:
net_realized_cash = realized_cash * 0.994
remaining_invested = vrev_total_invested - net_realized_cash
new_pure_price = round(max(0.01, remaining_invested / remaining_qty), 4)
# 🚨 MODIFIED: [지층 단가 역전 패러독스 방어] new_pure_price > prev_close 일 때만 분할
if prev_close is not None and portion_budget is not None and remaining_invested > portion_budget * 1.1 and new_pure_price > prev_close:
new_l1_qty = math.floor(portion_budget / prev_close)
if 0 < new_l1_qty < remaining_qty:
new_l1_invested = new_l1_qty * prev_close
rem_qty = remaining_qty - new_l1_qty
rem_invested = remaining_invested - new_l1_invested
rem_price = round(max(0.01, rem_invested / rem_qty), 4)
q[0]["qty"] = rem_qty
q[0]["price"] = rem_price
q[0]["type"] = "AUTO_SPLIT_UPPER"
q.append({
"qty": new_l1_qty,
"price": round(prev_close, 4),
"date": q[0]["date"],
"type": "AUTO_SPLIT_L1"
})
logging.info(f"🔪 [{ticker}] 단일 지층 마법 및 초과분 분할(Split) 완료 (1층: {new_l1_qty}주 @ ${prev_close:.2f} / 상위층: {rem_qty}주 @ ${rem_price:.2f})")
else:
q[0]["price"] = new_pure_price
else:
q[0]["price"] = new_pure_price
if popped_total < original_target:
logging.error(f"🚨 [QueueLedger] pop_lots 미달: {ticker} — 요청 {original_target}주 중 {popped_total}주만 차감. 즉시 sync_with_broker 실행 권고.")
data[ticker] = q
self._save_unsafe_no_lock(data)
return popped_total
def sync_with_broker(self, ticker, actual_qty, actual_avg=0.0, clear_price=0.0, prev_close=None, portion_budget=None):
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
q = data.get(ticker, [])
q = [lot for lot in q if int(self._safe_float(lot.get("qty"))) > 0]
current_q_qty = sum(int(self._safe_float(item.get("qty"))) for item in q)
actual_qty = int(self._safe_float(actual_qty))
if current_q_qty == actual_qty:
return False
today_str = self._get_trading_date_str()
if current_q_qty < actual_qty:
diff = actual_qty - current_q_qty
calib_price = self._safe_float(actual_avg)
if calib_price <= 0.0:
calib_price = self._safe_float(q[-1].get("price")) if q else 0.0
if calib_price <= 0.0:
logging.error(f"🚨 [QueueLedger] sync_with_broker CALIB_ADD 중단: {ticker} — 실제 평단가 불명 (actual_avg={actual_avg}). $0 로트 주입 방지.")
data[ticker] = q
self._save_unsafe_no_lock(data)
return True
if q and str(q[-1].get("date", "")).startswith(today_str):
old_qty = int(self._safe_float(q[-1].get("qty")))
old_price = self._safe_float(q[-1].get("price"))
new_qty = old_qty + diff
new_price = ((old_qty * old_price) + (diff * calib_price)) / new_qty if new_qty > 0 else 0.0
q[-1]["qty"] = new_qty
q[-1]["price"] = round(new_price, 4)
q[-1]["date"] = datetime.now(ZoneInfo('America/New_York')).strftime("%Y-%m-%d %H:%M:%S")
else:
q.append({
"qty": diff,
"price": round(calib_price, 4),
"date": datetime.now(ZoneInfo('America/New_York')).strftime("%Y-%m-%d %H:%M:%S"),
"type": "CALIB_ADD"
})
else:
diff = current_q_qty - actual_qty
popped_total = 0
realized_cash = 0.0
vrev_total_invested = sum(int(self._safe_float(item.get('qty'))) * self._safe_float(item.get('price')) for item in q)
while q and diff > 0:
last_lot = q[-1]
lot_qty = int(self._safe_float(last_lot.get("qty")))
lot_price = self._safe_float(last_lot.get("price"))
cp = clear_price if clear_price > 0 else lot_price
if lot_qty == 0:
q.pop()
continue
if lot_qty <= diff:
q.pop()
diff -= lot_qty
popped_total += lot_qty
realized_cash += lot_qty * cp
else:
last_lot["qty"] = lot_qty - diff
popped_total += diff
realized_cash += diff * cp
diff = 0
remaining_qty = actual_qty
if remaining_qty > 0 and popped_total > 0:
if len(q) == 1:
net_realized_cash = realized_cash * 0.994
remaining_invested = vrev_total_invested - net_realized_cash
new_pure_price = round(max(0.01, remaining_invested / remaining_qty), 4)
# 🚨 MODIFIED: [지층 단가 역전 패러독스 방어] new_pure_price > prev_close 일 때만 분할
if prev_close is not None and portion_budget is not None and remaining_invested > portion_budget * 1.1 and new_pure_price > prev_close:
new_l1_qty = math.floor(portion_budget / prev_close)
if 0 < new_l1_qty < remaining_qty:
new_l1_invested = new_l1_qty * prev_close
rem_qty = remaining_qty - new_l1_qty
rem_invested = remaining_invested - new_l1_invested
rem_price = round(max(0.01, rem_invested / rem_qty), 4)
q[0]["qty"] = rem_qty
q[0]["price"] = rem_price
q[0]["type"] = "AUTO_SPLIT_UPPER"
q.append({
"qty": new_l1_qty,
"price": round(prev_close, 4),
"date": q[0]["date"],
"type": "AUTO_SPLIT_L1"
})
logging.info(f"🔪 [{ticker}] 동기화 단일 지층 마법 및 초과분 분할 완료 (1층: {new_l1_qty}주 @ ${prev_close:.2f})")
else:
q[0]["price"] = new_pure_price
else:
q[0]["price"] = new_pure_price
q = self._enforce_two_tier_limit(q)
if diff > 0:
logging.warning(f"⚠️ [QueueLedger] sync_with_broker CALIB_SUB 미달: {ticker} 큐 물량이 브로커보다 {diff}주 부족합니다. 큐가 초기화되었습니다.")
data[ticker] = q
self._save_unsafe_no_lock(data)
return True
def delete_lot(self, ticker, target_date):
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
q = data.get(ticker, [])
new_q = [lot for lot in q if str(lot.get('date', '')) != str(target_date)]
data[ticker] = new_q
self._save_unsafe_no_lock(data)
def edit_lot(self, ticker, target_date, qty, price):
qty_int = int(self._safe_float(qty))
price_f = self._safe_float(price)
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
q = data.get(ticker, [])
for lot in q:
if str(lot.get('date', '')) == str(target_date):
lot['qty'] = qty_int
lot['price'] = round(price_f, 4)
break
data[ticker] = q
self._save_unsafe_no_lock(data)
def clear_queue(self, ticker):
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
data[ticker] = []
self._save_unsafe_no_lock(data)
def overwrite_queue(self, ticker, q_data):
lock = GlobalThrottle.get_file_lock(self.file_path)
with lock:
data = self._load_unsafe()
sorted_q = sorted(q_data, key=lambda x: str(x.get('date', '0000-00-00')))
sorted_q = self._enforce_two_tier_limit(sorted_q)
data[ticker] = sorted_q
self._save_unsafe_no_lock(data)