我逆向工程解析了某对冲基金的交易策略,并将该模型开源。
这是一个 LightGBM 回归器,用于预测 150 只美国股票的次日对数收益率,同时搭配一条简单的状态规则来决定是否根据预测采取行动。该模型在 MacBook M1 上完成训练,无需云 GPU,除了单一的 FMP API 订阅外,无需其他付费数据。
作者:@jc_builds

| 市场状态 | 策略 vs 买入持有 | 策略 vs SPY |
|---|---|---|
| 2010 年牛市 | −0.1% | +10.9% ✓ |
| 2018 年熊市 | +11.9% ✓ | +18.1% ✓ |
| 2020 年 COVID | +22.0% ✓ | +38.8% ✓ |
| 2022 年熊市 | −0.0% | −3.8% |
| 2025–26 年实盘 | −3.2% | −6.2% |
| 5 个区间平均 | +6.1% | +11.6% |
该模型在市场崩盘期间(2018 年、2020 年)表现优于买入持有策略,而在牛市和震荡熊市(2010 年、2022 年、实盘期间)表现基本持平。它是一个“崩盘防护盾”,而非选股工具。

整个系统由五个常规部分组成:
t 日已知数据,用于预测 t+1 日。无卖空,无杠杆,无期权,无日内交易。

原始模型已经相当不错,但真正提升指标的是一行代码的状态门控:
# Pseudocode — see src/strategy.py for the real version
bull = spy_price > spy_200_day_moving_average
threshold = bull_threshold if bull else bear_threshold # bull: -0.003, bear: tuned
position = 1 if pred > threshold else 0正是这一个 if 语句,将 2018 年的收益从持平提升至相对买入并持有策略 +11.85%。

训练采用经典的滚动前向方式:对于每个测试年份 Y,使用 2005 年至 Y-1 年的所有数据进行训练,完全不接触 Y 年的数据。对于实盘阶段,训练数据截止到 2025 年 4 月 22 日,测试从该时间点开始向前进行。
策略表现优异的时期(市场暴跌):
策略勉强获胜或失利的时期(牛市和缓慢熊市):
在测试的 675 个股票年中,平均相对 SPY 超额收益为 +11.57%,相对个股买入并持有策略超额收益为 +6.11%。

共尝试了八个模型版本。七个版本表现倒退,一个版本成功上线。最终的优胜模型并不复杂——它是最简单有效的方法,再加上一条市场阶段规则。
| 版本 | 思路 | 结果 |
|---|---|---|
| v1 | 基础 LightGBM,MSE 损失,验证集调优阈值 | 有效的基准模型 |
| v2 | 非对称 y 加权损失 | 表现倒退——在第 1 轮迭代早停 |
| v3 | 基于幅度加权的训练 | 表现倒退——平均化后丢失了暴跌信号 |
| v4 | v1 + 市场阶段感知阈值 | 优胜模型——已上线 |
| v4b | v4 采用固定阈值 | 略有倒退 |
| v5 | 5 种子集成 | 表现倒退——平均化后丢失了高确信度的暴跌选股 |
| v6 | 大幅下跌概率分类器 | 表现倒退——目标事件过于罕见 |
| v7 | 基于 Apple GPU 的 MLX MLP | 表现倒退——校准失败 |
| v8 | v4 仅使用 2015 年以后的数据训练 | 表现倒退——最佳迭代次数从 120 降至 5,失去正则化效果 |
| v9 | 多模态前 10 轮换 | 单独上线,收益波动剧烈 |

本仓库包含:
stockprediction-ai/
├── README.md this file
├── config.json hyperparameters, feature columns, regime thresholds, splits
├── results.json per-split metrics (strat final, B&H final, excess)
├── boosters/
│ ├── 2010.txt LightGBM text-format booster, trained on 2005–2009
│ ├── 2018.txt trained on 2005–2017
│ ├── 2020.txt trained on 2005–2019
│ ├── 2022.txt trained on 2005–2021
│ └── live.txt trained on 2005-01-01 through 2025-04-22
└── images/ charts used in this cardBooster 是纯 LightGBM 文本文件,可使用任何 LightGBM 运行时加载,无需 Python。
import json
import lightgbm as lgb
import numpy as np
import pandas as pd
from huggingface_hub import hf_hub_download
REPO = "jc-builds/stockprediction-ai"
# 1. Grab the live booster and config
booster_path = hf_hub_download(REPO, "boosters/live.txt")
config_path = hf_hub_download(REPO, "config.json")
booster = lgb.Booster(model_file=booster_path)
config = json.loads(open(config_path).read())
feature_cols = config["feature_columns"]
# 2. Build a feature row for one stock on one trading day
# (see github.com/jc-builds/stockprediction/blob/main/src/features.py
# for the full 47-feature builder — the code is open-source).
features: pd.DataFrame = build_features_for_today(symbol="AAPL")
x = features[feature_cols].astype(float).values
# 3. Predict next-day log return
pred = booster.predict(x)[0]
# 4. Apply the regime rule
bull = spy_close_today > spy_200_day_ma_today
thr_bull = config["regime_rule"]["bull_threshold"] # -0.003
thr_bear = config["splits"]["live"]["bear_threshold"]
threshold = thr_bull if bull else thr_bear
position = 1 if pred > threshold else 0 # 1=long, 0=cashbest_iter=1意味着模型“放弃了”——它返回了近乎恒定的预测值。任何实际部署都需要检测到这种情况(例如,当验证损失趋于平稳时拒绝交易),而不是默认转为全仓做多。git clone https://github.com/jc-builds/stockprediction
cd stockprediction
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
# Set FMP_API_KEY in .env, then:
PYTHONPATH=src python src/run.py --model v4如果本项目对您的研究工作或教学有所帮助,请引用:
@misc{jcbuilds_stockprediction_2026,
author = {Jared Cassoutt (@jc_builds)},
title = {Stock Prediction AI: Regime-Aware LightGBM},
year = {2026},
howpublished = {\url{https://huggingface.co/jc-builds/stockprediction-ai}},
}MIT 许可协议。你可以随意使用,但如果你因此亏损,请勿起诉我。