Spaces:
Sleeping
Sleeping
Delete features_t5.py
Browse files- features_t5.py +0 -167
features_t5.py
DELETED
|
@@ -1,167 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Feature extraction for T+5 model from 1-min OHLCV DataFrames.
|
| 3 |
-
Uses the 09:15-09:20 candle window to predict the day's close.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import numpy as np
|
| 7 |
-
import pandas as pd
|
| 8 |
-
|
| 9 |
-
def _safe_fill(arr):
|
| 10 |
-
return pd.Series(arr).ffill().bfill().values
|
| 11 |
-
|
| 12 |
-
def extract_semantic_features_t5(df):
|
| 13 |
-
df = df.copy()
|
| 14 |
-
df["time"] = df.index.time
|
| 15 |
-
df["date_only"] = df.index.date
|
| 16 |
-
required_times = (
|
| 17 |
-
pd.date_range("09:15", "09:20", freq="min").time.tolist()
|
| 18 |
-
+ [pd.to_datetime("15:10").time()]
|
| 19 |
-
)
|
| 20 |
-
df = df[~df.index.duplicated(keep="first")]
|
| 21 |
-
|
| 22 |
-
daily_close = df.groupby("date_only")["close"].last()
|
| 23 |
-
prev_daily_close = daily_close.shift(1)
|
| 24 |
-
|
| 25 |
-
time_0921 = pd.to_datetime("09:21").time()
|
| 26 |
-
time_1200 = pd.to_datetime("12:00").time()
|
| 27 |
-
df_morning = df[(df["time"] >= time_0921) & (df["time"] <= time_1200)]
|
| 28 |
-
morning_low_per_date = df_morning.groupby("date_only")["low"].min()
|
| 29 |
-
morning_high_per_date = df_morning.groupby("date_only")["high"].max()
|
| 30 |
-
|
| 31 |
-
df_filtered = df[df["time"].isin(required_times)].copy()
|
| 32 |
-
pivot_close = df_filtered.pivot(index="date_only", columns="time", values="close")
|
| 33 |
-
pivot_open = df_filtered.pivot(index="date_only", columns="time", values="open")
|
| 34 |
-
pivot_high = df_filtered.pivot(index="date_only", columns="time", values="high")
|
| 35 |
-
pivot_low = df_filtered.pivot(index="date_only", columns="time", values="low")
|
| 36 |
-
pivot_vol = df_filtered.pivot(index="date_only", columns="time", values="volume")
|
| 37 |
-
|
| 38 |
-
time_0920 = pd.to_datetime("09:20").time()
|
| 39 |
-
time_1510 = pd.to_datetime("15:10").time()
|
| 40 |
-
|
| 41 |
-
if time_0920 not in pivot_close.columns:
|
| 42 |
-
return None, None, None
|
| 43 |
-
|
| 44 |
-
pivot_close = pivot_close.dropna(subset=[time_0920])
|
| 45 |
-
valid_dates = pivot_close.index
|
| 46 |
-
times_6m = pd.date_range("09:15", "09:20", freq="min").time
|
| 47 |
-
|
| 48 |
-
feature_dicts = []
|
| 49 |
-
metadata = {}
|
| 50 |
-
|
| 51 |
-
for date in valid_dates:
|
| 52 |
-
f = {}
|
| 53 |
-
c_series = _safe_fill(pivot_close.loc[date, times_6m].values.astype(float))
|
| 54 |
-
o_series = _safe_fill(pivot_open.loc[date, times_6m].values.astype(float))
|
| 55 |
-
h_series = _safe_fill(pivot_high.loc[date, times_6m].values.astype(float))
|
| 56 |
-
l_series = _safe_fill(pivot_low.loc[date, times_6m].values.astype(float))
|
| 57 |
-
v_series = pd.Series(pivot_vol.loc[date, times_6m].values.astype(float)).fillna(0).values
|
| 58 |
-
|
| 59 |
-
o_915 = o_series[0]
|
| 60 |
-
c_920 = c_series[-1]
|
| 61 |
-
|
| 62 |
-
pdc = prev_daily_close.get(date, np.nan)
|
| 63 |
-
f["gap"] = 0 if (pd.isna(pdc) or pdc == 0) else (o_915 / pdc) - 1.0
|
| 64 |
-
f["return_6m"] = (c_920 / o_915) - 1.0 if o_915 != 0 else 0
|
| 65 |
-
f["std_dev"] = np.std(c_series / (o_915 + 1e-8))
|
| 66 |
-
|
| 67 |
-
max_h = np.max(h_series)
|
| 68 |
-
min_l = np.min(l_series)
|
| 69 |
-
f["range_pct"] = (max_h - min_l) / (o_915 + 1e-8)
|
| 70 |
-
f["upper_shadow"] = (max_h - max(o_915, c_920)) / (o_915 + 1e-8)
|
| 71 |
-
f["lower_shadow"] = (min(o_915, c_920) - min_l) / (o_915 + 1e-8)
|
| 72 |
-
f["total_vol"] = np.sum(v_series)
|
| 73 |
-
|
| 74 |
-
vwap = np.sum(((h_series + l_series + c_series) / 3.0) * v_series) / (np.sum(v_series) + 1e-8)
|
| 75 |
-
f["vwap_dev"] = (c_920 / vwap) - 1.0 if vwap != 0 else 0
|
| 76 |
-
f["price_momentum"] = (c_series[-1] - c_series[0]) / (c_series[0] + 1e-8)
|
| 77 |
-
f["morning_trend"] = np.polyfit(np.arange(len(c_series)), c_series, 1)[0]
|
| 78 |
-
|
| 79 |
-
feature_dicts.append(f)
|
| 80 |
-
|
| 81 |
-
metadata[date] = {
|
| 82 |
-
"c_0920": c_920,
|
| 83 |
-
"h_0920": float(pivot_high.loc[date, time_0920]) if time_0920 in pivot_high.columns else c_920,
|
| 84 |
-
"l_0920": float(pivot_low.loc[date, time_0920]) if time_0920 in pivot_low.columns else c_920,
|
| 85 |
-
"v_0920": float(pivot_vol.loc[date, time_0920]) if time_0920 in pivot_vol.columns else 0,
|
| 86 |
-
"c_1510": float(pivot_close.loc[date, time_1510]) if time_1510 in pivot_close.columns else c_920,
|
| 87 |
-
"h_1510": float(pivot_high.loc[date, time_1510]) if time_1510 in pivot_high.columns else c_920,
|
| 88 |
-
"l_1510": float(pivot_low.loc[date, time_1510]) if time_1510 in pivot_low.columns else c_920,
|
| 89 |
-
"dip_low": float(morning_low_per_date.get(date, c_920)),
|
| 90 |
-
"peak_high": float(morning_high_per_date.get(date, c_920)),
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
X = pd.DataFrame(feature_dicts, index=valid_dates).fillna(0)
|
| 94 |
-
|
| 95 |
-
if time_1510 in pivot_close.columns:
|
| 96 |
-
target = (pivot_close[time_1510] > pivot_close[time_0920]).astype(int)
|
| 97 |
-
else:
|
| 98 |
-
target = pd.Series(0, index=valid_dates)
|
| 99 |
-
|
| 100 |
-
return X, target, metadata
|
| 101 |
-
|
| 102 |
-
def extract_sequential_features_t5(df):
|
| 103 |
-
df = df.copy()
|
| 104 |
-
df["time"] = df.index.time
|
| 105 |
-
df["date_only"] = df.index.date
|
| 106 |
-
required_times = (
|
| 107 |
-
pd.date_range("09:15", "09:20", freq="min").time.tolist()
|
| 108 |
-
+ [pd.to_datetime("15:10").time()]
|
| 109 |
-
)
|
| 110 |
-
df = df[~df.index.duplicated(keep="first")]
|
| 111 |
-
|
| 112 |
-
time_0921 = pd.to_datetime("09:21").time()
|
| 113 |
-
time_1200 = pd.to_datetime("12:00").time()
|
| 114 |
-
df_morning = df[(df["time"] >= time_0921) & (df["time"] <= time_1200)]
|
| 115 |
-
morning_low_per_date = df_morning.groupby("date_only")["low"].min()
|
| 116 |
-
morning_high_per_date = df_morning.groupby("date_only")["high"].max()
|
| 117 |
-
|
| 118 |
-
df_filtered = df[df["time"].isin(required_times)].copy()
|
| 119 |
-
pivot_close = df_filtered.pivot(index="date_only", columns="time", values="close")
|
| 120 |
-
pivot_high = df_filtered.pivot(index="date_only", columns="time", values="high")
|
| 121 |
-
pivot_low = df_filtered.pivot(index="date_only", columns="time", values="low")
|
| 122 |
-
pivot_vol = df_filtered.pivot(index="date_only", columns="time", values="volume")
|
| 123 |
-
|
| 124 |
-
time_0920 = pd.to_datetime("09:20").time()
|
| 125 |
-
time_1510 = pd.to_datetime("15:10").time()
|
| 126 |
-
|
| 127 |
-
if time_0920 not in pivot_close.columns:
|
| 128 |
-
return None, None, None
|
| 129 |
-
|
| 130 |
-
pivot_close = pivot_close.dropna(subset=[time_0920])
|
| 131 |
-
valid_dates = pivot_close.index
|
| 132 |
-
times_6m = pd.date_range("09:15", "09:20", freq="min").time
|
| 133 |
-
|
| 134 |
-
feature_dicts = []
|
| 135 |
-
metadata = {}
|
| 136 |
-
|
| 137 |
-
for date in valid_dates:
|
| 138 |
-
f = {}
|
| 139 |
-
c_series = _safe_fill(pivot_close.loc[date, times_6m].values.astype(float))
|
| 140 |
-
v_series = pd.Series(pivot_vol.loc[date, times_6m].values.astype(float)).fillna(0).values
|
| 141 |
-
c_ref = c_series[-1]
|
| 142 |
-
|
| 143 |
-
for i, t in enumerate(times_6m):
|
| 144 |
-
f[f"ret_c_{i}"] = (c_series[i] / (c_ref + 1e-8)) - 1.0
|
| 145 |
-
f[f"raw_vol_{i}"] = v_series[i]
|
| 146 |
-
feature_dicts.append(f)
|
| 147 |
-
|
| 148 |
-
metadata[date] = {
|
| 149 |
-
"c_0920": c_ref,
|
| 150 |
-
"h_0920": float(pivot_high.loc[date, time_0920]) if time_0920 in pivot_high.columns else c_ref,
|
| 151 |
-
"l_0920": float(pivot_low.loc[date, time_0920]) if time_0920 in pivot_low.columns else c_ref,
|
| 152 |
-
"v_0920": float(pivot_vol.loc[date, time_0920]) if time_0920 in pivot_vol.columns else 0,
|
| 153 |
-
"c_1510": float(pivot_close.loc[date, time_1510]) if time_1510 in pivot_close.columns else c_ref,
|
| 154 |
-
"h_1510": float(pivot_high.loc[date, time_1510]) if time_1510 in pivot_high.columns else c_ref,
|
| 155 |
-
"l_1510": float(pivot_low.loc[date, time_1510]) if time_1510 in pivot_low.columns else c_ref,
|
| 156 |
-
"dip_low": float(morning_low_per_date.get(date, c_ref)),
|
| 157 |
-
"peak_high": float(morning_high_per_date.get(date, c_ref)),
|
| 158 |
-
}
|
| 159 |
-
|
| 160 |
-
X = pd.DataFrame(feature_dicts, index=valid_dates).fillna(0)
|
| 161 |
-
|
| 162 |
-
if time_1510 in pivot_close.columns:
|
| 163 |
-
target = (pivot_close[time_1510] > pivot_close[time_0920]).astype(int)
|
| 164 |
-
else:
|
| 165 |
-
target = pd.Series(0, index=valid_dates)
|
| 166 |
-
|
| 167 |
-
return X, target, metadata
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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