容器化改造
This commit is contained in:
parent
d70ecc3d6c
commit
0bbd19f14d
30
factors.py
30
factors.py
|
|
@ -93,24 +93,20 @@ def build_upside(start, end):
|
||||||
cons = cons[cons["k"].isin(set(price["k"]))]
|
cons = cons[cons["k"].isin(set(price["k"]))]
|
||||||
if cons.empty:
|
if cons.empty:
|
||||||
return _EMPTY
|
return _EMPTY
|
||||||
cons["asof_date"] = pd.to_datetime(cons["asof_date"])
|
# 强制两侧键同分辨率 datetime64[ns](pandas 2.x 不同来源可能 us/ns 混,
|
||||||
price["trade_date"] = pd.to_datetime(price["trade_date"])
|
# merge_asof 会报 incompatible merge keys)
|
||||||
out = []
|
cons["asof_date"] = pd.to_datetime(cons["asof_date"]).astype("datetime64[ns]")
|
||||||
cons_sorted = cons.sort_values("asof_date")
|
price["trade_date"] = pd.to_datetime(price["trade_date"]).astype("datetime64[ns]")
|
||||||
for k, pg in price.groupby("k"):
|
left = price[["trade_date", "k", "close"]].sort_values("trade_date")
|
||||||
cg = cons_sorted[cons_sorted["k"] == k]
|
right = cons[["asof_date", "k", "target_mid_avg"]].sort_values("asof_date")
|
||||||
if cg.empty:
|
m = pd.merge_asof(left, right, left_on="trade_date", right_on="asof_date",
|
||||||
continue
|
by="k", direction="backward") # 每股取 asof<=当日最新目标价
|
||||||
m = pd.merge_asof(pg.sort_values("trade_date"),
|
m = m.dropna(subset=["target_mid_avg", "close"])
|
||||||
cg[["asof_date", "target_mid_avg"]],
|
|
||||||
left_on="trade_date", right_on="asof_date", direction="backward")
|
|
||||||
m = m.dropna(subset=["target_mid_avg"])
|
|
||||||
if m.empty:
|
if m.empty:
|
||||||
continue
|
return _EMPTY
|
||||||
m["factor_value"] = m["target_mid_avg"].astype(float) / m["close"] - 1.0
|
m["factor_value"] = m["target_mid_avg"].astype(float) / m["close"] - 1.0
|
||||||
m["stock_code"] = k
|
m = m.rename(columns={"k": "stock_code"})
|
||||||
out.append(m[["trade_date", "stock_code", "factor_value"]])
|
return m[["trade_date", "stock_code", "factor_value"]]
|
||||||
return pd.concat(out) if out else _EMPTY
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 事件
|
# ---------------------------------------------------------------- 事件
|
||||||
|
|
@ -132,6 +128,8 @@ def build_event(start, end):
|
||||||
ev = ev[ev["ts_code"].isin(uni)].copy()
|
ev = ev[ev["ts_code"].isin(uni)].copy()
|
||||||
if ev.empty:
|
if ev.empty:
|
||||||
return _EMPTY
|
return _EMPTY
|
||||||
|
ev["event_type"] = ev["event_type"].fillna("")
|
||||||
|
ev["direction"] = ev["direction"].fillna("") # 多数事件无 direction(NULL→NaN),先填空防 .strip 崩
|
||||||
ev["pol"] = [_polarity(t, d) for t, d in zip(ev["event_type"], ev["direction"])]
|
ev["pol"] = [_polarity(t, d) for t, d in zip(ev["event_type"], ev["direction"])]
|
||||||
ev = ev[ev["pol"] != 0.0]
|
ev = ev[ev["pol"] != 0.0]
|
||||||
if ev.empty:
|
if ev.empty:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue