-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathstrategy_reversion.py
More file actions
465 lines (386 loc) ยท 21.7 KB
/
Copy pathstrategy_reversion.py
File metadata and controls
465 lines (386 loc) ยท 21.7 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
# ==========================================================
# FILE: strategy_reversion.py
# ==========================================================
# ๐จ MODIFIED: [๋จ์ผ ์ง์ธต ์ค์ธ ํจ๋ฌ๋
์ค ์ฌ์] get_dynamic_plan ๋ด๋ถ์ ๋ ์ง ๋ฌธ์์ด(Date String) ๊ธฐ๋ฐ ๊ทธ๋ฃนํ์ ์ ๋ฉด ์๊ฐํ๊ณ , ํ ์ฅ๋ถ์ LIFO ์ธ๋ฑ์ค(`[-1]`)๋ฅผ 1์ง์ธต์ผ๋ก ์ ๋ ๋ฝ์จํ์ฌ ๋ค์ค ์ง์ธต ํตํฉ ์ค๋ฅ ์์ฒ ๋ด์ ์๋ฃ.
# ๐จ MODIFIED: [์ ์ก์ ๊ธฐ๋ฐ ์ ๋์ 100% ๋ฝ์จ] 1ํ๋ถ ํ ๋น ์์ฐ(15%)์ ํ์ ์ผ๋ก ๋๋์ด '๋น์ผ ๋งค์ ์ ๋(Fixed-Quantity)'์ ์ฐ์ถํ๊ณ ์๊ตฌ ๊ณ ์ . ํญ๋ฝ ์ ์์ฐ์ ์ต์ง๋ก ์์งํ์ง ์๊ณ ๋จ์ ์ฐจ์ต์ ์๋ ํ๋ก ๋ณด์กดํ์ฌ ๋งค์ ๊ฐ๋ฅ ํ์(Runway)๋ฅผ ๋ฌดํ ํ์ฅํจ.
# ๐จ MODIFIED: [Lost Update ๊ถ๊ทน ๋ฐฉ์ด] ๋ชจ๋ ์ํ ๋ฐ ์ค๋
์ท ํ์ผ(JSON) ์ฝ๊ธฐ/์ฐ๊ธฐ ์ฐ์ฐ์ GlobalThrottle.get_file_lock()์ 100% ๊ฒฐ์ํ์ฌ ๋ํฐ ๋ฆฌ๋(Dirty Read) ๋ฐ ๋์์ฑ ํ๊ดด ์์ฒ ์ฐจ๋จ.
# ๐จ MODIFIED: [์ 4ํ๋ฒ ์์์ ์ฐ๊ธฐ ์ ๋ ์ฌ์] tempfile ์์ฑ โ flush โ fsync โ os.replace์ 100% ์์์ ํ์ดํ๋ผ์ธ ๊ฐ์ ์ฃผ์
.
# ๐จ MODIFIED: [๋ฉ๋ชจ๋ฆฌ ์ ๋ นํ(Ghost Memory) ๋ถ๊ดด ๊ถ๊ทน ์์ ] ๋์คํฌ ํ์ผ์ ์กด์ฌ ์ฌ๋ถ ๋ฐ ์ ํจ์ฑ์ ๊ต์ฐจ ๊ฒ์ฆํ์ฌ, ํ์ผ์ด ์๊ฐ๋์์ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ์ ๋น์ผ ๋งค๋ ์๋(SELL_QTY)์ ์ฆ๊ฐ 0์ผ๋ก ์์์ ์ด๊ธฐํ.
# ๐จ MODIFIED: [Case 46] 0์ฃผ ์ค๋
์ท ์ค์ผ ๊ฐ์ง ์ YF ๊ณต์ ์ข
๊ฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ ์ ์ ๋ฐํ๊ฒ ์๊ฐ ์น์ (Self-Healing)ํ๋ ๋ฐฉ์ด๋ง ๊ฒฐ์ ์๋ฃ.
# ==========================================================
import math
import os
import json
import tempfile
import logging
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from global_throttle import GlobalThrottle # ๐จ NEW: ์ค์ ํต์ ์ ๊ฒฐ์
class ReversionStrategy:
def __init__(self, config):
self.cfg = config
self.executed = {"BUY_BUDGET": {}, "SELL_QTY": {}}
self.state_loaded = {}
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 _get_logical_date_str(self):
now_est = datetime.now(ZoneInfo('America/New_York'))
if now_est.hour < 4 or (now_est.hour == 4 and now_est.minute < 4):
target_date = now_est - timedelta(days=1)
elif now_est.hour >= 16:
target_date = now_est + timedelta(days=1)
else:
target_date = now_est
if target_date.weekday() == 5:
target_date += timedelta(days=2)
elif target_date.weekday() == 6:
target_date += timedelta(days=1)
return target_date.strftime("%Y-%m-%d")
def _get_state_file(self, ticker):
today_str = self._get_logical_date_str()
return f"data/vwap_state_REV_{today_str}_{ticker}.json"
def _get_snapshot_file(self, ticker):
today_str = self._get_logical_date_str()
return f"data/daily_snapshot_REV_{today_str}_{ticker}.json"
def _load_state_if_needed(self, ticker):
today_str = self._get_logical_date_str()
state_file = self._get_state_file(ticker)
is_disk_valid = False
# ๐จ MODIFIED: File Mutex ๊ฒฐ์
with GlobalThrottle.get_file_lock(state_file):
try:
with open(state_file, 'r', encoding='utf-8') as f:
data = json.load(f)
if data.get("date") == today_str:
for k in self.executed.keys():
raw_val = (data.get("executed") or {}).get(k, 0)
self.executed[k][ticker] = int(self._safe_float(raw_val)) if k == "SELL_QTY" else self._safe_float(raw_val)
is_disk_valid = True
except Exception:
pass
if not is_disk_valid:
self.executed["BUY_BUDGET"][ticker] = 0.0
self.executed["SELL_QTY"][ticker] = 0
self._save_state(ticker)
self.state_loaded[ticker] = today_str
def _save_state(self, ticker):
today_str = self._get_logical_date_str()
state_file = self._get_state_file(ticker)
data = {
"date": today_str,
"executed": {
"BUY_BUDGET": self._safe_float((self.executed.get("BUY_BUDGET") or {}).get(ticker, 0.0)),
"SELL_QTY": int(self._safe_float((self.executed.get("SELL_QTY") or {}).get(ticker, 0)))
}
}
# ๐จ MODIFIED: File Mutex ๊ฒฐ์ ๋ฐ ์ 4ํ๋ฒ ์์์ ์ฐ๊ธฐ 100% ๋ฝ์จ
with GlobalThrottle.get_file_lock(state_file):
fd = None
temp_path = None
try:
dir_name = os.path.dirname(state_file)
if dir_name:
try: os.makedirs(dir_name, exist_ok=True)
except OSError: pass
fd, temp_path = tempfile.mkstemp(dir=dir_name or '.', 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(temp_path, state_file)
temp_path = None
except Exception as e:
if fd is not None:
try: os.close(fd)
except OSError: pass
if temp_path:
try: os.remove(temp_path)
except OSError: pass
logging.error(f"๐จ [{ticker}] V-REV ์ํ ํ์ผ ์์์ ์ฐ๊ธฐ ์คํจ: {e}")
def save_daily_snapshot(self, ticker, plan_data):
snap_file = self._get_snapshot_file(ticker)
today_str = self._get_logical_date_str()
data = {
"date": today_str,
"plan": plan_data
}
# ๐จ MODIFIED: File Mutex ๊ฒฐ์ ๋ฐ ์ 4ํ๋ฒ ์์์ ์ฐ๊ธฐ 100% ๋ฝ์จ
with GlobalThrottle.get_file_lock(snap_file):
fd = None
temp_path = None
try:
dir_name = os.path.dirname(snap_file)
if dir_name:
try: os.makedirs(dir_name, exist_ok=True)
except OSError: pass
fd, temp_path = tempfile.mkstemp(dir=dir_name or '.', 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(temp_path, snap_file)
temp_path = None
except Exception as e:
if fd is not None:
try: os.close(fd)
except OSError: pass
if temp_path:
try: os.remove(temp_path)
except OSError: pass
logging.error(f"๐จ [{ticker}] V-REV ์ค๋
์ท ํ์ผ ์์์ ์ฐ๊ธฐ ์คํจ: {e}")
def load_daily_snapshot(self, ticker):
snap_file = self._get_snapshot_file(ticker)
# ๐จ MODIFIED: File Mutex ๊ฒฐ์
with GlobalThrottle.get_file_lock(snap_file):
try:
with open(snap_file, 'r', encoding='utf-8') as f:
data = json.load(f)
return data.get("plan")
except Exception:
pass
return None
def ensure_failsafe_snapshot(self, ticker, curr_p, prev_c, alloc_cash, q_data, total_kis_qty, avwap_qty, actual_avg=0.0):
curr_p = self._safe_float(curr_p)
prev_c = self._safe_float(prev_c)
alloc_cash = self._safe_float(alloc_cash)
actual_avg = self._safe_float(actual_avg)
snap = self.load_daily_snapshot(ticker)
if snap is not None:
return snap
today_str_est = self._get_logical_date_str()
legacy_lots = [item for item in (q_data or []) if isinstance(item, dict) and not str(item.get("date", "")).startswith(today_str_est)]
logging.warning(f"๐จ [{ticker}] V_REV ์ค๋
์ท ์ฆ๋ฐ ๊ฐ์ง! ํ์ผ์ธ์ดํ ๊ธด๊ธ ๋ณต์ ๊ฐ๋")
return self.get_dynamic_plan(
ticker=ticker,
curr_p=curr_p,
prev_c=prev_c,
current_weight=0.0,
vwap_status={},
min_idx=-1,
alloc_cash=alloc_cash,
q_data=legacy_lots,
is_snapshot_mode=True,
market_type="REG",
actual_qty=total_kis_qty,
actual_avg=actual_avg
)
def record_execution(self, ticker, side, qty, exec_price):
self._load_state_if_needed(ticker)
safe_qty = int(self._safe_float(qty))
safe_price = self._safe_float(exec_price)
if side == "BUY":
spent = safe_qty * safe_price
self.executed["BUY_BUDGET"][ticker] = self._safe_float((self.executed.get("BUY_BUDGET") or {}).get(ticker, 0.0)) + spent
else:
self.executed["SELL_QTY"][ticker] = int(self._safe_float((self.executed.get("SELL_QTY") or {}).get(ticker, 0))) + safe_qty
self._save_state(ticker)
def get_dynamic_plan(self, ticker, curr_p, prev_c, current_weight, vwap_status, min_idx, alloc_cash, q_data, is_snapshot_mode=False, market_type="REG", actual_qty=0, actual_avg=0.0):
curr_p = self._safe_float(curr_p)
prev_c = self._safe_float(prev_c)
current_weight = self._safe_float(current_weight)
alloc_cash = self._safe_float(alloc_cash)
self._load_state_if_needed(ticker)
cached_plan = self.load_daily_snapshot(ticker)
if cached_plan:
is_zero_val = cached_plan.get("is_zero_start")
if is_zero_val is None:
tot_q_snap = int(self._safe_float(cached_plan.get("snapshot_total_q", cached_plan.get("total_q", -1))))
is_zero_snap = (tot_q_snap == 0)
else:
is_zero_snap = str(is_zero_val).lower() == 'true'
# ๐จ MODIFIED: [Case 46 ์๊ฐ์น์ ๊ฒฐ์] 0์ฃผ ์ค๋
์ท ์ค์ผ ๊ฐ์ง ์ YF ๋ฌด๊ฒฐ์ฑ ์ข
๊ฐ ๊ธฐ๋ฐ ์ ๋ฐ ๋ณด์
if is_zero_snap and prev_c > 0.0:
orders = cached_plan.get("orders", [])
buy_orders = [o for o in orders if isinstance(o, dict) and str(o.get("side")) == "BUY"]
target_p1 = round(prev_c * 1.15, 2)
target_p2 = round(prev_c * 0.999, 2)
is_polluted = False
for o in buy_orders:
p = self._safe_float(o.get("price"))
desc = str(o.get("desc", ""))
if p > 0:
if "Buy1" in desc or "1" in desc:
if abs(p - target_p1) / target_p1 >= 0.01: is_polluted = True
elif "Buy2" in desc or "2" in desc:
if abs(p - target_p2) / target_p2 >= 0.01: is_polluted = True
if is_polluted:
logging.warning(f"๐จ [{ticker}] 0์ฃผ ์ค๋
์ท ์ค์ผ ๊ฐ์ง! YF ๋ฌด๊ฒฐ์ฑ ์ข
๊ฐ(${prev_c}) ๊ธฐ๋ฐ์ผ๋ก ํ์ ์ ์ฆ๊ฐ ์๊ฐ ์น์ (Self-Healing)ํฉ๋๋ค.")
seed_val = self._safe_float(self.cfg.get_seed(ticker))
daily_limit = seed_val * 0.15
safe_alloc_cash = alloc_cash if alloc_cash > 0.0 else daily_limit
safe_alloc_cash = min(safe_alloc_cash, daily_limit) if daily_limit > 0 else safe_alloc_cash
total_spent = self._safe_float((self.executed.get("BUY_BUDGET") or {}).get(ticker, 0.0))
rem_budget = max(0.0, safe_alloc_cash - total_spent)
b1_budget = rem_budget * 0.5
b2_budget = rem_budget * 0.5
# ๐จ MODIFIED: ์ ์ก์ ๊ธฐ๋ฐ ์ ๋์ (Fixed-Amount based Fixed-Quantity) ํต์ฌ ํฉํธ
new_q1 = math.floor(b1_budget / target_p1) if target_p1 > 0 else 0
new_q2 = math.floor(b2_budget / target_p2) if target_p2 > 0 else 0
if new_q1 == 0 and new_q2 == 0:
if target_p1 > 0 and rem_budget >= target_p1: new_q1 = math.floor(rem_budget / target_p1)
elif target_p2 > 0 and rem_budget >= target_p2: new_q2 = math.floor(rem_budget / target_p2)
elif new_q1 == 0 and new_q2 > 0:
new_q2 = math.floor(rem_budget / target_p2) if target_p2 > 0 else 0
elif new_q2 == 0 and new_q1 > 0:
new_q1 = math.floor(rem_budget / target_p1) if target_p1 > 0 else 0
for o in cached_plan.get("orders", []):
if str(o.get("side")) == "BUY":
desc = str(o.get("desc", ""))
if "Buy1" in desc or "1" in desc:
o["price"] = target_p1
o["qty"] = new_q1
elif "Buy2" in desc or "2" in desc:
o["price"] = target_p2
o["qty"] = new_q2
self.save_daily_snapshot(ticker, cached_plan)
if not is_snapshot_mode and cached_plan:
return cached_plan
valid_q_data = [item for item in (q_data or []) if isinstance(item, dict) and self._safe_float(item.get('price')) > 0]
total_q = sum(int(self._safe_float(item.get("qty"))) for item in valid_q_data)
total_inv = sum(self._safe_float(item.get('qty')) * self._safe_float(item.get('price')) for item in valid_q_data)
l1_qty, l1_price = 0, 0.0
if valid_q_data:
# ๐จ MODIFIED: [๋จ์ผ ์ง์ธต ์ค์ธ ํจ๋ฌ๋
์ค ์ฌ์] ๋ ์ง ๋ฌธ์์ด(Date String) ๊ธฐ๋ฐ ๊ทธ๋ฃนํ์ ์ ๋ฉด ์๊ฐํ๊ณ , LIFO ํ์ ์ต์ ๋กํธ([-1])๋ฅผ 1์ง์ธต์ผ๋ก ํฉํธ ๋ฝ์จ.
last_lot = valid_q_data[-1]
l1_qty = int(self._safe_float(last_lot.get('qty')))
l1_price = self._safe_float(last_lot.get('price'))
upper_qty = total_q - l1_qty
pure_l1_qty = l1_qty
pure_upper_qty = upper_qty
trigger_l1 = round(l1_price * 1.006, 2)
# ๐จ MODIFIED: dates_in_queue ์์กด์ฑ ์๊ฐ (len(valid_q_data) ์ ์ฉ)
if pure_upper_qty > 0 and len(valid_q_data) >= 2:
upper_inv = max(0.0, total_inv - (l1_price * l1_qty))
upper_price = upper_inv / pure_upper_qty if pure_upper_qty > 0 else 0.0
trigger_upper = round(upper_price * 1.010, 2)
else:
trigger_upper = 0.0
if cached_plan:
is_zero_val = cached_plan.get("is_zero_start")
if is_zero_val is None:
tot_q_snap = int(self._safe_float(cached_plan.get("snapshot_total_q", cached_plan.get("total_q", -1))))
is_zero_start_session = (tot_q_snap == 0)
else:
is_zero_start_session = str(is_zero_val).lower() == 'true'
else:
is_zero_start_session = (total_q == 0)
if is_zero_start_session and total_q > 0:
is_zero_start_session = False
elif not is_zero_start_session and total_q == 0:
is_zero_start_session = True
if is_zero_start_session and prev_c <= 0.0:
error_plan = {
"orders": [], "trigger_loc": False, "total_q": total_q, "is_zero_start": True, "process_status": "โ๊ฐ๊ฒฉ์ค๋ฅ"
}
if is_snapshot_mode:
self.save_daily_snapshot(ticker, error_plan)
return error_plan
if is_zero_start_session:
p1_trigger = round(prev_c * 1.15, 2)
p2_trigger = round(prev_c * 0.999, 2)
else:
safe_anchor = l1_price if l1_price > 0.0 else prev_c
p1_trigger = round(safe_anchor * 0.998, 2)
p2_trigger = round(safe_anchor * 0.993, 2)
rem_qty_total = max(0, int(pure_l1_qty + pure_upper_qty) - int(self._safe_float((self.executed.get("SELL_QTY") or {}).get(ticker, 0))))
available_l1 = min(pure_l1_qty, rem_qty_total) if rem_qty_total > 0 else 0
available_upper = min(pure_upper_qty, rem_qty_total - available_l1) if rem_qty_total > 0 else 0
if rem_qty_total > 0:
active_sells = []
if available_l1 > 0 and trigger_l1 > 0:
active_sells.append(trigger_l1)
if available_upper > 0 and trigger_upper >= 0.01:
active_sells.append(trigger_upper)
# ๐จ MODIFIED: [์์ ๊ฑฐ๋ ๋ฐฉ์ด๋ง ์บกํ ๊ฒฐ์] ๋งค์๊ฐ๊ฒฉ์ด ๋งค๋๊ฐ๊ฒฉ ์ด์์ผ ๊ฒฝ์ฐ ์ฆ๊ฐ ํํฅ ์กฐ์น
if active_sells:
min_sell = min(active_sells)
if p1_trigger >= min_sell:
p1_trigger = max(0.01, round(min_sell - 0.01, 2))
if p2_trigger >= min_sell:
p2_trigger = max(0.01, round(min_sell - 0.01, 2))
orders = []
est_zone = ZoneInfo('America/New_York')
kst_zone = ZoneInfo('Asia/Seoul')
now_est = datetime.now(est_zone)
base_start_est = now_est.replace(hour=15, minute=26, second=0, microsecond=0)
shifted_start_est = now_est + timedelta(minutes=3)
actual_start_est = max(base_start_est, shifted_start_est)
base_end_est = now_est.replace(hour=15, minute=56, second=0, microsecond=0)
start_dt_kst = actual_start_est.astimezone(kst_zone)
end_dt_kst = base_end_est.astimezone(kst_zone)
start_t = start_dt_kst.strftime("%H%M%S")
end_t = end_dt_kst.strftime("%H%M%S")
total_spent = 0.0 if is_snapshot_mode else self._safe_float((self.executed.get("BUY_BUDGET") or {}).get(ticker, 0.0))
# ๐จ MODIFIED: [์ ์ก์ ๊ธฐ๋ฐ ์ ๋์ 100% ํฉํธ ์ฐ์ฐ] ์ด ์๋์ 15%๋ก 1ํ ์์ฐ(Budget) ํ๋ ์ ๋ ๊ณ ์
seed_val = self._safe_float(self.cfg.get_seed(ticker))
daily_limit = seed_val * 0.15
if alloc_cash <= 0.0:
alloc_cash = daily_limit
safe_alloc_cash = min(float(alloc_cash), daily_limit) if daily_limit > 0 else float(alloc_cash)
rem_budget = max(0.0, safe_alloc_cash - total_spent)
if rem_budget > 0:
b1_budget = rem_budget * 0.5
b2_budget = rem_budget * 0.5
# ๐จ MODIFIED: [๋ชฉํ ์๋ ์๊ตฌ ๊ณ ์ ] ์ด ์์ ์ ์ฐ์ถ๋ ์ ๋(q1, q2)์ ๋น์ผ ์ ๋ ๋ณ๊ฒฝ๋์ง ์์ผ๋ฉฐ, ํญ๋ฝ ์ ์ฐจ์ต์ ๊ทธ๋๋ก ์๋ ํ๋ก ์ธ์ด๋ธ๋จ
q1 = math.floor(b1_budget / p1_trigger) if p1_trigger > 0 else 0
q2 = math.floor(b2_budget / p2_trigger) if p2_trigger > 0 else 0
if q1 == 0 and q2 == 0:
if p1_trigger > 0 and rem_budget >= p1_trigger:
q1 = math.floor(rem_budget / p1_trigger)
elif p2_trigger > 0 and rem_budget >= p2_trigger:
q2 = math.floor(rem_budget / p2_trigger)
elif q1 == 0 and q2 > 0:
q2 = math.floor(rem_budget / p2_trigger) if p2_trigger > 0 else 0
elif q2 == 0 and q1 > 0:
q1 = math.floor(rem_budget / p1_trigger) if p1_trigger > 0 else 0
if q1 > 0:
ord_type = "VWAP"
desc_str = "VWAP๋งค์(Buy1)"
orders.append({"side": "BUY", "qty": q1, "price": p1_trigger, "type": ord_type, "start_time": start_t, "end_time": end_t, "desc": desc_str})
if q2 > 0:
ord_type = "VWAP"
desc_str = "VWAP๋งค์(Buy2)"
orders.append({"side": "BUY", "qty": q2, "price": p2_trigger, "type": ord_type, "start_time": start_t, "end_time": end_t, "desc": desc_str})
if rem_qty_total > 0:
sell_dict = {}
if available_l1 > 0 and trigger_l1 > 0:
sell_dict[trigger_l1] = sell_dict.get(trigger_l1, 0) + available_l1
if available_upper > 0 and trigger_upper >= 0.01:
sell_dict[trigger_upper] = sell_dict.get(trigger_upper, 0) + available_upper
for price in sorted(sell_dict.keys()):
s_qty = sell_dict[price]
ord_type = "VWAP"
if price == trigger_l1 and price == trigger_upper:
desc_str = "ํตํฉํ์ถ"
elif price == trigger_l1:
desc_str = "1์ธตํ์ถ"
elif price == trigger_upper:
desc_str = "์์์ธตํ์ถ"
else:
desc_str = "์์ฌํ์ถ"
orders.append({
"side": "SELL", "qty": s_qty, "price": price, "type": ord_type,
"start_time": start_t,
"end_time": end_t,
"desc": desc_str
})
plan_result = {
"orders": orders,
"trigger_loc": False,
"total_q": total_q,
"is_zero_start": is_zero_start_session,
"process_status": "์ ์์ฐ์ฐ"
}
if is_zero_start_session and market_type != "AFTER":
plan_result["orders"] = [o for o in plan_result.get("orders", []) if o.get("side") != "SELL"]
if is_snapshot_mode:
self.save_daily_snapshot(ticker, plan_result)
self._save_state(ticker)
return plan_result