feat: add OI derived features (oi_change_ma5, oi_price_spread) to dataset builder and ML features
Add two new OI-derived features to improve ML model's market microstructure understanding: - oi_change_ma5: 5-candle moving average of OI change rate (short-term trend) - oi_price_spread: z-scored OI minus z-scored price return (divergence signal) Both features use 96-candle rolling z-score window. FEATURE_COLS expanded from 24 to 26. Existing tests updated to reflect new feature counts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -287,8 +287,18 @@ def _calc_features_vectorized(
|
||||
else:
|
||||
fr_raw = np.full(len(d), np.nan)
|
||||
|
||||
result["oi_change"] = _rolling_zscore(oi_raw.astype(np.float64))
|
||||
result["funding_rate"] = _rolling_zscore(fr_raw.astype(np.float64))
|
||||
oi_z = _rolling_zscore(oi_raw.astype(np.float64), window=96)
|
||||
result["oi_change"] = oi_z
|
||||
result["funding_rate"] = _rolling_zscore(fr_raw.astype(np.float64), window=96)
|
||||
|
||||
# --- OI 파생 피처 ---
|
||||
# 1. oi_change_ma5: OI 변화율의 5캔들 이동평균 (단기 추세)
|
||||
oi_series = pd.Series(oi_raw.astype(np.float64))
|
||||
oi_ma5_raw = oi_series.rolling(window=5, min_periods=1).mean().values
|
||||
result["oi_change_ma5"] = _rolling_zscore(oi_ma5_raw, window=96)
|
||||
|
||||
# 2. oi_price_spread: z-scored OI - z-scored 가격 수익률 (연속값)
|
||||
result["oi_price_spread"] = oi_z - ret_1_z
|
||||
|
||||
return result
|
||||
|
||||
@@ -384,7 +394,7 @@ def generate_dataset_vectorized(
|
||||
feat_all = _calc_features_vectorized(d, signal_arr, btc_df=btc_df, eth_df=eth_df)
|
||||
|
||||
# 신호 발생 + NaN 없음 + 미래 데이터 충분한 인덱스만
|
||||
OPTIONAL_COLS = {"oi_change", "funding_rate"}
|
||||
OPTIONAL_COLS = {"oi_change", "funding_rate", "oi_change_ma5", "oi_price_spread"}
|
||||
available_cols_for_nan_check = [
|
||||
c for c in FEATURE_COLS
|
||||
if c in feat_all.columns and c not in OPTIONAL_COLS
|
||||
|
||||
@@ -9,8 +9,9 @@ FEATURE_COLS = [
|
||||
"eth_ret_1", "eth_ret_3", "eth_ret_5",
|
||||
"xrp_btc_rs", "xrp_eth_rs",
|
||||
# 시장 미시구조: OI 변화율(z-score), 펀딩비(z-score)
|
||||
# parquet에 oi_change/funding_rate 컬럼이 없으면 dataset_builder에서 0으로 채움
|
||||
"oi_change", "funding_rate",
|
||||
# OI 파생 피처
|
||||
"oi_change_ma5", "oi_price_spread",
|
||||
"adx",
|
||||
]
|
||||
|
||||
@@ -37,12 +38,14 @@ def build_features(
|
||||
eth_df: pd.DataFrame | None = None,
|
||||
oi_change: float | None = None,
|
||||
funding_rate: float | None = None,
|
||||
oi_change_ma5: float | None = None,
|
||||
oi_price_spread: float | None = None,
|
||||
) -> pd.Series:
|
||||
"""
|
||||
기술 지표가 계산된 DataFrame의 마지막 행에서 ML 피처를 추출한다.
|
||||
btc_df, eth_df가 제공되면 24개 피처를, 없으면 16개 피처를 반환한다.
|
||||
btc_df, eth_df가 제공되면 26개 피처를, 없으면 18개 피처를 반환한다.
|
||||
signal: "LONG" | "SHORT"
|
||||
oi_change, funding_rate: 실제 값이 제공되면 사용, 없으면 0.0으로 채운다.
|
||||
oi_change, funding_rate, oi_change_ma5, oi_price_spread: 실제 값이 제공되면 사용, 없으면 0.0으로 채운다.
|
||||
"""
|
||||
last = df.iloc[-1]
|
||||
close = last["close"]
|
||||
@@ -132,8 +135,10 @@ def build_features(
|
||||
})
|
||||
|
||||
# 실시간에서 실제 값이 제공되면 사용, 없으면 0으로 채운다
|
||||
base["oi_change"] = float(oi_change) if oi_change is not None else 0.0
|
||||
base["funding_rate"] = float(funding_rate) if funding_rate is not None else 0.0
|
||||
base["oi_change"] = float(oi_change) if oi_change is not None else 0.0
|
||||
base["funding_rate"] = float(funding_rate) if funding_rate is not None else 0.0
|
||||
base["oi_change_ma5"] = float(oi_change_ma5) if oi_change_ma5 is not None else 0.0
|
||||
base["oi_price_spread"] = float(oi_price_spread) if oi_price_spread is not None else 0.0
|
||||
base["adx"] = float(last.get("adx", 0))
|
||||
|
||||
return pd.Series(base)
|
||||
|
||||
Reference in New Issue
Block a user