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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Enterprise ML ensemble: H2O.ai, DataRobot-style, Alteryx-style.
|
| 3 |
+
Provides 75% of final prediction blend (dominant signal source).
|
| 4 |
+
Quality-weighted: H2O 40% + DataRobot 40% + Alteryx 20%.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from sklearn.ensemble import ExtraTreesRegressor
|
| 13 |
+
from sklearn.linear_model import Ridge
|
| 14 |
+
from sklearn.preprocessing import StandardScaler
|
| 15 |
+
|
| 16 |
+
# Optional: H2O AutoML (pip install h2o)
|
| 17 |
+
H2O_AVAILABLE = False
|
| 18 |
+
try:
|
| 19 |
+
import h2o
|
| 20 |
+
from h2o.automl import H2OAutoML
|
| 21 |
+
H2O_AVAILABLE = True
|
| 22 |
+
except ImportError:
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
# Optional: DataRobot API (pip install datarobot)
|
| 26 |
+
DATAROBOT_AVAILABLE = False
|
| 27 |
+
try:
|
| 28 |
+
import datarobot as dr
|
| 29 |
+
DATAROBOT_AVAILABLE = True
|
| 30 |
+
except ImportError:
|
| 31 |
+
pass
|
| 32 |
+
|
| 33 |
+
FEATURE_COLS = ["Close", "SMA_20", "EMA_20", "RSI", "MACD", "Volume",
|
| 34 |
+
"BB_Upper", "BB_Lower", "BB_Width", "ATR"]
|
| 35 |
+
PREDICT_DAYS = 5
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _build_tabular_dataset(df: pd.DataFrame, target_shift: int = PREDICT_DAYS):
|
| 39 |
+
"""Build tabular X, y for regression. Target = close at t+target_shift."""
|
| 40 |
+
available = [c for c in FEATURE_COLS if c in df.columns]
|
| 41 |
+
if len(available) < 2:
|
| 42 |
+
available = ["Close"]
|
| 43 |
+
|
| 44 |
+
df = df.dropna(subset=available)
|
| 45 |
+
if len(df) < 80:
|
| 46 |
+
return None, None, None
|
| 47 |
+
|
| 48 |
+
# Add returns and volatility features
|
| 49 |
+
df = df.copy()
|
| 50 |
+
df["ret_1d"] = df["Close"].pct_change(1)
|
| 51 |
+
df["ret_3d"] = df["Close"].pct_change(3)
|
| 52 |
+
df["ret_5d"] = df["Close"].pct_change(5)
|
| 53 |
+
df["ret_10d"] = df["Close"].pct_change(10)
|
| 54 |
+
df["vol_5d"] = df["ret_1d"].rolling(5).std()
|
| 55 |
+
df["vol_10d"] = df["ret_1d"].rolling(10).std()
|
| 56 |
+
df["momentum_5d"] = df["Close"] / df["Close"].shift(5) - 1
|
| 57 |
+
if "SMA_20" in df.columns:
|
| 58 |
+
df["dist_sma20"] = (df["Close"] - df["SMA_20"]) / df["SMA_20"].replace(0, np.nan)
|
| 59 |
+
if "SMA_50" in df.columns:
|
| 60 |
+
df["dist_sma50"] = (df["Close"] - df["SMA_50"]) / df["SMA_50"].replace(0, np.nan)
|
| 61 |
+
if "EMA_20" in df.columns:
|
| 62 |
+
df["dist_ema20"] = (df["Close"] - df["EMA_20"]) / df["EMA_20"].replace(0, np.nan)
|
| 63 |
+
|
| 64 |
+
feature_cols = [c for c in available + [
|
| 65 |
+
"ret_1d", "ret_3d", "ret_5d", "ret_10d", "vol_5d", "vol_10d",
|
| 66 |
+
"momentum_5d", "dist_sma20", "dist_sma50", "dist_ema20",
|
| 67 |
+
] if c in df.columns]
|
| 68 |
+
df = df.dropna(subset=feature_cols)
|
| 69 |
+
|
| 70 |
+
# Target: close price at t+target_shift
|
| 71 |
+
df["target"] = df["Close"].shift(-target_shift)
|
| 72 |
+
df = df.dropna(subset=["target"])
|
| 73 |
+
|
| 74 |
+
if len(df) < 50:
|
| 75 |
+
return None, None, None
|
| 76 |
+
|
| 77 |
+
X = df[feature_cols].values.astype(np.float64)
|
| 78 |
+
y = df["target"].values.astype(np.float64)
|
| 79 |
+
return X, y, df[feature_cols]
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def _quadratic_interpolate(current_price: float, pred_day3: float, pred_day5: float) -> list[float]:
|
| 83 |
+
"""
|
| 84 |
+
Generate a 5-day curve using quadratic interpolation through
|
| 85 |
+
(day0=current, day3=pred_day3, day5=pred_day5).
|
| 86 |
+
Produces realistic acceleration instead of straight lines.
|
| 87 |
+
"""
|
| 88 |
+
# Fit quadratic: p(t) = at^2 + bt + c through t=0,3,5
|
| 89 |
+
# p(0) = current, p(3) = pred_day3, p(5) = pred_day5
|
| 90 |
+
c = current_price
|
| 91 |
+
# Solving: 9a + 3b = pred_day3 - c, 25a + 5b = pred_day5 - c
|
| 92 |
+
d3 = pred_day3 - current_price
|
| 93 |
+
d5 = pred_day5 - current_price
|
| 94 |
+
# From linear algebra: a = (5*d3 - 3*d5) / (9*5 - 3*25) = (5*d3 - 3*d5) / (-30)
|
| 95 |
+
denom = (9 * 5 - 3 * 25) # = -30
|
| 96 |
+
if denom == 0:
|
| 97 |
+
denom = -30 # safety (always -30)
|
| 98 |
+
a = (5 * d3 - 3 * d5) / denom
|
| 99 |
+
b = (d3 - 9 * a) / 3 if abs(3) > 1e-9 else d5 / 5
|
| 100 |
+
|
| 101 |
+
prices = []
|
| 102 |
+
for t in range(1, PREDICT_DAYS + 1):
|
| 103 |
+
p = a * t * t + b * t + c
|
| 104 |
+
prices.append(round(p, 2))
|
| 105 |
+
return prices
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def _dual_target_predict(df, current_price, fit_fn):
|
| 109 |
+
"""
|
| 110 |
+
Predict day-3 and day-5 targets separately, then produce a
|
| 111 |
+
quadratic interpolated 5-day curve. Much more realistic than linear.
|
| 112 |
+
fit_fn(X_train, y_train) -> model with .predict(X) method.
|
| 113 |
+
"""
|
| 114 |
+
X3, y3, _ = _build_tabular_dataset(df, target_shift=3)
|
| 115 |
+
X5, y5, _ = _build_tabular_dataset(df, target_shift=5)
|
| 116 |
+
if X3 is None or X5 is None or len(X3) < 50 or len(X5) < 50:
|
| 117 |
+
return None
|
| 118 |
+
|
| 119 |
+
split3 = int(len(X3) * 0.85)
|
| 120 |
+
split5 = int(len(X5) * 0.85)
|
| 121 |
+
|
| 122 |
+
model3 = fit_fn(X3[:split3], y3[:split3])
|
| 123 |
+
model5 = fit_fn(X5[:split5], y5[:split5])
|
| 124 |
+
|
| 125 |
+
pred_day3 = float(model3.predict(X3[-1:])[0])
|
| 126 |
+
pred_day5 = float(model5.predict(X5[-1:])[0])
|
| 127 |
+
|
| 128 |
+
return _quadratic_interpolate(current_price, pred_day3, pred_day5)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def _predict_h2o_local(df: pd.DataFrame, current_price: float) -> list[float] | None:
|
| 132 |
+
"""H2O AutoML local prediction (fallback if h2o not installed uses sklearn)."""
|
| 133 |
+
from sklearn.ensemble import GradientBoostingRegressor
|
| 134 |
+
|
| 135 |
+
def make_model(X_train, y_train):
|
| 136 |
+
m = GradientBoostingRegressor(
|
| 137 |
+
n_estimators=120, max_depth=5, learning_rate=0.04,
|
| 138 |
+
subsample=0.85, random_state=42,
|
| 139 |
+
)
|
| 140 |
+
m.fit(X_train, y_train)
|
| 141 |
+
return m
|
| 142 |
+
|
| 143 |
+
return _dual_target_predict(df, current_price, make_model)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def predict_h2o(df: pd.DataFrame, current_price: float) -> list[float] | None:
|
| 147 |
+
"""H2O.ai AutoML prediction. Uses H2O if available, else sklearn fallback."""
|
| 148 |
+
if H2O_AVAILABLE:
|
| 149 |
+
try:
|
| 150 |
+
X, y, frame = _build_tabular_dataset(df)
|
| 151 |
+
if X is None or len(X) < 80:
|
| 152 |
+
return _predict_h2o_local(df, current_price)
|
| 153 |
+
|
| 154 |
+
h2o.init(verbose=False)
|
| 155 |
+
train_df = pd.DataFrame(X, columns=frame.columns)
|
| 156 |
+
train_df["target"] = y
|
| 157 |
+
hf = h2o.H2OFrame(train_df)
|
| 158 |
+
x_cols = list(frame.columns)
|
| 159 |
+
aml = H2OAutoML(max_models=5, max_runtime_secs=60, seed=42)
|
| 160 |
+
aml.train(x=x_cols, y="target", training_frame=hf)
|
| 161 |
+
|
| 162 |
+
last_row = pd.DataFrame(X[-1:], columns=x_cols)
|
| 163 |
+
hf_pred = h2o.H2OFrame(last_row)
|
| 164 |
+
pred = aml.predict(hf_pred)
|
| 165 |
+
pred_day5 = float(pred.as_data_frame().iloc[0, 0])
|
| 166 |
+
h2o.shutdown(prompt=False)
|
| 167 |
+
|
| 168 |
+
# H2O only predicts day-5, so we use linear interpolation for now.
|
| 169 |
+
# TODO: Implement dual-target for H2O if possible.
|
| 170 |
+
return [round(current_price + (i / PREDICT_DAYS) * (pred_day5 - current_price), 2)
|
| 171 |
+
for i in range(1, PREDICT_DAYS + 1)]
|
| 172 |
+
except Exception:
|
| 173 |
+
pass
|
| 174 |
+
return _predict_h2o_local(df, current_price)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def predict_datarobot(df: pd.DataFrame, current_price: float) -> list[float] | None:
|
| 178 |
+
"""
|
| 179 |
+
DataRobot-style prediction. Uses DataRobot API if configured,
|
| 180 |
+
else local XGBoost/RandomForest ensemble.
|
| 181 |
+
"""
|
| 182 |
+
dr_url = os.environ.get("DATAROBOT_API_URL")
|
| 183 |
+
dr_key = os.environ.get("DATAROBOT_API_TOKEN")
|
| 184 |
+
deployment_id = os.environ.get("DATAROBOT_DEPLOYMENT_ID")
|
| 185 |
+
|
| 186 |
+
if DATAROBOT_AVAILABLE and dr_url and dr_key and deployment_id:
|
| 187 |
+
try:
|
| 188 |
+
dr.Client(token=dr_key, endpoint=dr_url)
|
| 189 |
+
X, y, frame = _build_tabular_dataset(df)
|
| 190 |
+
if X is None:
|
| 191 |
+
return None
|
| 192 |
+
last_row = pd.DataFrame(X[-1:], columns=frame.columns)
|
| 193 |
+
pred = dr.PredictionEndpoint.predict(deployment_id, last_row)
|
| 194 |
+
pred_day5 = float(pred.loc[0, "prediction"])
|
| 195 |
+
return [round(current_price + (i / PREDICT_DAYS) * (pred_day5 - current_price), 2)
|
| 196 |
+
for i in range(1, PREDICT_DAYS + 1)]
|
| 197 |
+
except Exception:
|
| 198 |
+
pass
|
| 199 |
+
|
| 200 |
+
# Local DataRobot-style: diverse 3-model ensemble (RF + GBM + HistGBM)
|
| 201 |
+
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
|
| 202 |
+
from sklearn.ensemble import HistGradientBoostingRegressor
|
| 203 |
+
|
| 204 |
+
def make_model(X_train, y_train):
|
| 205 |
+
rf = RandomForestRegressor(n_estimators=150, max_depth=7, random_state=43)
|
| 206 |
+
gbm = GradientBoostingRegressor(n_estimators=100, max_depth=5, learning_rate=0.04, random_state=44)
|
| 207 |
+
hgbm = HistGradientBoostingRegressor(max_iter=120, max_depth=6, learning_rate=0.05, random_state=47)
|
| 208 |
+
rf.fit(X_train, y_train)
|
| 209 |
+
gbm.fit(X_train, y_train)
|
| 210 |
+
hgbm.fit(X_train, y_train)
|
| 211 |
+
|
| 212 |
+
class TripleEnsemble:
|
| 213 |
+
def predict(self, X):
|
| 214 |
+
return 0.35 * rf.predict(X) + 0.35 * gbm.predict(X) + 0.30 * hgbm.predict(X)
|
| 215 |
+
return TripleEnsemble()
|
| 216 |
+
|
| 217 |
+
return _dual_target_predict(df, current_price, make_model)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def predict_alteryx(df: pd.DataFrame, current_price: float) -> list[float] | None:
|
| 221 |
+
"""
|
| 222 |
+
Alteryx-style prediction. Blends ExtraTrees + Ridge regression
|
| 223 |
+
with dual-target (day-3 + day-5) quadratic interpolation.
|
| 224 |
+
"""
|
| 225 |
+
_scaler = StandardScaler()
|
| 226 |
+
|
| 227 |
+
def make_model(X_train, y_train):
|
| 228 |
+
X_train_scaled = _scaler.fit_transform(X_train)
|
| 229 |
+
et = ExtraTreesRegressor(n_estimators=120, max_depth=8, random_state=45)
|
| 230 |
+
ridge = Ridge(alpha=1.0)
|
| 231 |
+
et.fit(X_train, y_train)
|
| 232 |
+
ridge.fit(X_train_scaled, y_train)
|
| 233 |
+
|
| 234 |
+
class TreeLinearBlend:
|
| 235 |
+
def predict(self, X):
|
| 236 |
+
X_s = _scaler.transform(X)
|
| 237 |
+
return 0.6 * et.predict(X) + 0.4 * ridge.predict(X_s)
|
| 238 |
+
return TreeLinearBlend()
|
| 239 |
+
|
| 240 |
+
return _dual_target_predict(df, current_price, make_model)
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def get_enterprise_predictions(
|
| 244 |
+
df: pd.DataFrame,
|
| 245 |
+
current_price: float,
|
| 246 |
+
) -> tuple[list[float] | None, list[float] | None, list[float] | None]:
|
| 247 |
+
"""Return (h2o_pred, datarobot_pred, alteryx_pred). Any may be None on failure."""
|
| 248 |
+
h2o_p = predict_h2o(df, current_price)
|
| 249 |
+
dr_p = predict_datarobot(df, current_price)
|
| 250 |
+
alt_p = predict_alteryx(df, current_price)
|
| 251 |
+
return h2o_p, dr_p, alt_p
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
# Quality weights: H2O and DataRobot are stronger models, Alteryx is supplementary
|
| 255 |
+
_MODEL_WEIGHTS = {
|
| 256 |
+
"h2o": 0.40,
|
| 257 |
+
"datarobot": 0.40,
|
| 258 |
+
"alteryx": 0.20,
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def blend_enterprise_predictions(
|
| 263 |
+
h2o_pred: list[float] | None,
|
| 264 |
+
datarobot_pred: list[float] | None,
|
| 265 |
+
alteryx_pred: list[float] | None,
|
| 266 |
+
) -> list[float] | None:
|
| 267 |
+
"""
|
| 268 |
+
Blend H2O, DataRobot, Alteryx with quality-based weights (40/40/20).
|
| 269 |
+
If one model is missing, redistribute its weight proportionally.
|
| 270 |
+
Returns None if no models succeeded.
|
| 271 |
+
"""
|
| 272 |
+
available = []
|
| 273 |
+
if h2o_pred is not None and len(h2o_pred) == PREDICT_DAYS:
|
| 274 |
+
available.append(("h2o", h2o_pred))
|
| 275 |
+
if datarobot_pred is not None and len(datarobot_pred) == PREDICT_DAYS:
|
| 276 |
+
available.append(("datarobot", datarobot_pred))
|
| 277 |
+
if alteryx_pred is not None and len(alteryx_pred) == PREDICT_DAYS:
|
| 278 |
+
available.append(("alteryx", alteryx_pred))
|
| 279 |
+
|
| 280 |
+
if not available:
|
| 281 |
+
return None
|
| 282 |
+
if len(available) == 1:
|
| 283 |
+
return available[0][1]
|
| 284 |
+
|
| 285 |
+
# Normalize weights for available models
|
| 286 |
+
raw_weights = {name: _MODEL_WEIGHTS[name] for name, _ in available}
|
| 287 |
+
total_w = sum(raw_weights.values())
|
| 288 |
+
norm_weights = {name: w / total_w for name, w in raw_weights.items()}
|
| 289 |
+
|
| 290 |
+
blended = []
|
| 291 |
+
for i in range(PREDICT_DAYS):
|
| 292 |
+
val = sum(norm_weights[name] * pred[i] for name, pred in available)
|
| 293 |
+
blended.append(round(val, 2))
|
| 294 |
+
return blended
|