Fix portfolio construction NaN handling

This commit is contained in:
Yuxuan Yan
2026-06-10 14:58:06 +08:00
parent 94ab679a75
commit 98a4f99300
7 changed files with 109 additions and 20 deletions
+7 -4
View File
@@ -82,7 +82,8 @@ def round_to_valid_lot(
def _exposures(q: np.ndarray, price: np.ndarray) -> tuple[np.ndarray, float, float]:
v = q.astype(np.float64) * price
safe_price = np.where(np.isfinite(price) & (price > 0), price, 0.0)
v = q.astype(np.float64) * safe_price
return v, float(v.sum()), float(np.abs(v).sum())
@@ -129,7 +130,7 @@ def repair_exposure(
``int64`` repaired positions, length N.
"""
q = np.asarray(q_round, dtype=np.int64).copy()
price = np.asarray(price, dtype=np.float64)
raw_price = np.asarray(price, dtype=np.float64)
increment = np.asarray(increment, dtype=np.int64).astype(np.float64)
min_open = np.asarray(min_open, dtype=np.int64).astype(np.float64)
qt = np.asarray(q_target, dtype=np.float64)
@@ -137,8 +138,10 @@ def repair_exposure(
if n == 0:
return q
vt = np.where(np.isfinite(qt), qt, 0.0) * price # v_target, NaN-safe
tradable = np.isfinite(price) & (price > 0)
tradable = np.isfinite(raw_price) & (raw_price > 0)
price = np.where(tradable, raw_price, 0.0)
qt_safe = np.where(np.isfinite(qt), qt, 0.0)
vt = np.where(tradable, qt_safe * price, 0.0) # v_target, NaN-safe
step = np.where(tradable, increment * price, np.inf) # dollar per increment
if max_iters is None: