Compare commits
7 Commits
9acb3460a1
...
340d1dd91b
| Author | SHA1 | Date | |
|---|---|---|---|
| 340d1dd91b | |||
| 87849a66a7 | |||
| 38a728c00b | |||
| c463be1741 | |||
| 5d4de011b2 | |||
| 062440fac2 | |||
| 6d816b21ad |
@@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
import _bootstrap # noqa: F401
|
||||
from trader_training.io_utils import add_common_args, setup_logging
|
||||
from trader_training.state_continue_experiment import run_state_continue_experiment
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
add_common_args(parser)
|
||||
parser.add_argument("--baseline-run-id", required=True)
|
||||
parser.add_argument("--ages-minutes", default="5,15,30")
|
||||
parser.add_argument("--max-rows-per-split", type=int, default=0)
|
||||
parser.add_argument("--regressor-kind", choices=["huber", "ridge"], default="huber")
|
||||
parser.add_argument("--ridge-alpha", type=float, default=10.0)
|
||||
parser.add_argument("--huber-alpha", type=float, default=0.001)
|
||||
parser.add_argument("--huber-epsilon", type=float, default=1.35)
|
||||
parser.add_argument("--huber-max-iter", type=int, default=1000)
|
||||
parser.add_argument("--regression-target-clip-bps", type=float, default=0.0)
|
||||
args = parser.parse_args()
|
||||
setup_logging()
|
||||
run_state_continue_experiment(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,23 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
import _bootstrap # noqa: F401
|
||||
from trader_training.io_utils import add_common_args, setup_logging
|
||||
from trader_training.ofi_feature_experiment import run_ofi_feature_experiment
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
add_common_args(parser)
|
||||
parser.add_argument("--baseline-run-id", required=True)
|
||||
parser.add_argument("--raw-root", type=Path)
|
||||
parser.add_argument("--max-rows-per-split", type=int, default=0)
|
||||
args = parser.parse_args()
|
||||
setup_logging()
|
||||
run_ofi_feature_experiment(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,98 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from argparse import Namespace
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd
|
||||
|
||||
TRAINING_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(TRAINING_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(TRAINING_ROOT))
|
||||
|
||||
from trader_training.io_utils import write_json
|
||||
from trader_training.labels import build_continue_exit_risk_labels
|
||||
|
||||
|
||||
class ContinueLabelsTest(unittest.TestCase):
|
||||
def test_continue_label_uses_first_price_plan_barrier_not_later_mae(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
data_root = Path(tmp)
|
||||
run_root = data_root / "trader-v4" / "runs" / "unit-continue"
|
||||
feature_path = run_root / "feature" / "feature_frame.parquet"
|
||||
replay_path = run_root / "replay" / "replay_1m.parquet"
|
||||
plan_path = run_root / "label" / "price_plan_context.json"
|
||||
config_path = data_root / "label_config.json"
|
||||
feature_path.parent.mkdir(parents=True)
|
||||
replay_path.parent.mkdir(parents=True)
|
||||
plan_path.parent.mkdir(parents=True)
|
||||
|
||||
times = pd.date_range("2026-01-01", periods=5, freq="min", tz="UTC")
|
||||
pd.DataFrame(
|
||||
{
|
||||
"sample_id": ["s0"],
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"event_time": [times[0]],
|
||||
"open_time_ms": [0],
|
||||
"split_id": ["fit_inner"],
|
||||
"walk_forward_fold": [0],
|
||||
"data_quality_flag": ["OK"],
|
||||
"spread_bps": [1.0],
|
||||
"spread_rank_24h_pct": [0.1],
|
||||
"realized_vol_15m_bps": [2.0],
|
||||
}
|
||||
).to_parquet(feature_path, index=False)
|
||||
pd.DataFrame(
|
||||
{
|
||||
"event_time": times,
|
||||
"open_time_ms": [0, 60_000, 120_000, 180_000, 240_000],
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"open": [100.0, 100.0, 100.0, 100.0, 100.0],
|
||||
"high": [100.0, 100.30, 100.0, 100.0, 100.0],
|
||||
"low": [100.0, 100.0, 99.80, 100.0, 100.0],
|
||||
"close": [100.0, 100.10, 100.0, 100.0, 100.0],
|
||||
"spread_bps": [1.0, 1.1, 1.2, 1.3, 1.4],
|
||||
}
|
||||
).to_parquet(replay_path, index=False)
|
||||
write_json(
|
||||
config_path,
|
||||
{
|
||||
"continue": {"horizon_minutes": 3, "min_expected_continue_edge_bps": 5.0},
|
||||
"entry": {"target_bps": 20.0, "stop_bps": 8.0, "max_hold_minutes": 3},
|
||||
},
|
||||
)
|
||||
write_json(
|
||||
plan_path,
|
||||
{
|
||||
"pricePlanId": "unit-plan",
|
||||
"pricePlanConfigHash": "unit-hash",
|
||||
"targetDistanceBps": 20.0,
|
||||
"stopDistanceBps": 8.0,
|
||||
"maxHoldMinutes": 3,
|
||||
"costBps": 6.5,
|
||||
"entryLabelMethod": "PRICE_PLAN_OUTCOME_V1",
|
||||
},
|
||||
)
|
||||
|
||||
build_continue_exit_risk_labels(
|
||||
Namespace(
|
||||
data_root=data_root,
|
||||
run_id="unit-continue",
|
||||
feature_path=feature_path,
|
||||
replay_path=replay_path,
|
||||
label_config_path=config_path,
|
||||
cost_config_path=None,
|
||||
price_plan_context_path=plan_path,
|
||||
)
|
||||
)
|
||||
|
||||
labels = pd.read_parquet(run_root / "label" / "continue_labels.parquet")
|
||||
row = labels.iloc[0]
|
||||
self.assertEqual(1, int(row["long_continue_target"]))
|
||||
self.assertAlmostEqual(13.5, float(row["long_expected_continue_edge_bps"]), places=6)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,173 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
TRAINING_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(TRAINING_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(TRAINING_ROOT))
|
||||
|
||||
from trader_training.labels import DEFAULT_LABEL_CONFIG, _path_stats_for_group
|
||||
from trader_training.pm import _probability_implied_edge, _simulate_open_trades, _threshold_candidates, default_pm_config
|
||||
|
||||
|
||||
class RiskPmFixTest(unittest.TestCase):
|
||||
def test_path_stats_never_writes_negative_adverse_or_favorable_move(self) -> None:
|
||||
frame = pd.DataFrame(
|
||||
{
|
||||
"event_time": pd.date_range("2026-01-01", periods=4, freq="min", tz="UTC"),
|
||||
"open_time_ms": np.arange(4, dtype=np.int64) * 60_000,
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"close": [100.0, 101.0, 102.0, 103.0],
|
||||
"high": [100.0, 101.0, 102.0, 103.0],
|
||||
"low": [100.0, 101.0, 102.0, 103.0],
|
||||
"spread_bps": [1.0, 1.0, 1.0, 1.0],
|
||||
}
|
||||
)
|
||||
|
||||
long_stats = _path_stats_for_group(frame, "LONG", horizon=2, target_bps=500.0, stop_bps=500.0)
|
||||
short_stats = _path_stats_for_group(frame, "SHORT", horizon=2, target_bps=500.0, stop_bps=500.0)
|
||||
|
||||
self.assertGreaterEqual(float(long_stats["mae_bps"].min()), 0.0)
|
||||
self.assertGreaterEqual(float(long_stats["mfe_bps"].min()), 0.0)
|
||||
self.assertGreaterEqual(float(short_stats["mae_bps"].min()), 0.0)
|
||||
self.assertGreaterEqual(float(short_stats["mfe_bps"].min()), 0.0)
|
||||
|
||||
def test_default_risk_labels_match_design_thresholds(self) -> None:
|
||||
self.assertEqual(45, DEFAULT_LABEL_CONFIG["continue"]["horizon_minutes"])
|
||||
self.assertEqual(60.0, DEFAULT_LABEL_CONFIG["risk"]["market_drawdown_bps"])
|
||||
self.assertEqual(35.0, DEFAULT_LABEL_CONFIG["risk"]["position_path_risk_bps"])
|
||||
self.assertEqual(80.0, DEFAULT_LABEL_CONFIG["risk"]["spike_bps"])
|
||||
self.assertEqual(1.8, DEFAULT_LABEL_CONFIG["risk"]["vol_expansion_ratio"])
|
||||
|
||||
def test_pm_search_covers_low_entry_probability_without_allowing_negative_edge(self) -> None:
|
||||
candidates = _threshold_candidates()
|
||||
|
||||
self.assertTrue(candidates)
|
||||
self.assertLessEqual(max(item["max_market_risk_prob"] for item in candidates), 0.98)
|
||||
self.assertLessEqual(min(item["min_entry_prob"] for item in candidates), 0.03)
|
||||
self.assertGreaterEqual(min(item["min_expected_edge_bps"] for item in candidates), 0.0)
|
||||
|
||||
def test_probability_implied_edge_uses_price_plan_payoff(self) -> None:
|
||||
edge = _probability_implied_edge(
|
||||
pd.Series([0.10, 0.50]),
|
||||
{"targetDistanceBps": 120.0, "stopDistanceBps": 2.0, "costBps": 6.5},
|
||||
)
|
||||
|
||||
self.assertAlmostEqual(3.7, float(edge.iloc[0]), places=6)
|
||||
self.assertAlmostEqual(52.5, float(edge.iloc[1]), places=6)
|
||||
|
||||
def test_pm_backtest_sizing_uses_position_manager_formula_not_fixed_floor(self) -> None:
|
||||
frame = pd.DataFrame(
|
||||
{
|
||||
"sample_id": ["s0"],
|
||||
"symbol": ["BTC-USDT-PERP"],
|
||||
"event_time": pd.to_datetime(["2026-01-01T00:00:00Z"]),
|
||||
"split_id": ["tune_inner"],
|
||||
"long_prob": [0.70],
|
||||
"short_prob": [0.10],
|
||||
"neutral_prob": [0.20],
|
||||
"long_entry_prob": [0.80],
|
||||
"short_entry_prob": [0.20],
|
||||
"market_risk_prob": [0.20],
|
||||
"long_position_risk_prob": [0.10],
|
||||
"short_position_risk_prob": [0.10],
|
||||
"pred_long_expected_net_edge_bps": [40.0],
|
||||
"pred_short_expected_net_edge_bps": [1.0],
|
||||
"actual_long_expected_net_edge_bps": [30.0],
|
||||
"actual_short_expected_net_edge_bps": [-10.0],
|
||||
"long_trade_net_edge_bps": [11.0],
|
||||
"short_trade_net_edge_bps": [-14.5],
|
||||
"long_target_hit": [1],
|
||||
"short_target_hit": [0],
|
||||
"long_stop_hit": [0],
|
||||
"short_stop_hit": [1],
|
||||
"long_time_to_target_ms": [300_000],
|
||||
"short_time_to_target_ms": [-1],
|
||||
"long_time_to_stop_ms": [-1],
|
||||
"short_time_to_stop_ms": [180_000],
|
||||
"long_entry_target": [1],
|
||||
"short_entry_target": [0],
|
||||
}
|
||||
)
|
||||
thresholds = {
|
||||
"long_open_prob": 0.55,
|
||||
"short_open_prob": 0.55,
|
||||
"min_entry_prob": 0.55,
|
||||
"max_market_risk_prob": 0.55,
|
||||
"min_expected_edge_bps": 3.0,
|
||||
"min_direction_margin": 0.02,
|
||||
}
|
||||
|
||||
trades = _simulate_open_trades(
|
||||
frame,
|
||||
thresholds,
|
||||
default_pm_config(),
|
||||
{"stopDistanceBps": 8.0, "costBps": 6.5},
|
||||
)
|
||||
|
||||
self.assertEqual(1, len(trades))
|
||||
self.assertAlmostEqual(11.0, float(trades.iloc[0]["actual_edge_bps"]))
|
||||
self.assertAlmostEqual(30.0, float(trades.iloc[0]["label_max_edge_bps"]))
|
||||
self.assertGreater(float(trades.iloc[0]["planned_ratio"]), 0.05)
|
||||
self.assertLessEqual(float(trades.iloc[0]["planned_ratio"]), 0.20)
|
||||
|
||||
def test_pm_backtest_blocks_overlapping_open_trades_until_exit_and_cooldown(self) -> None:
|
||||
frame = pd.DataFrame(
|
||||
{
|
||||
"sample_id": ["s0", "s1"],
|
||||
"symbol": ["BTC-USDT-PERP", "BTC-USDT-PERP"],
|
||||
"event_time": pd.to_datetime(["2026-01-01T00:00:00Z", "2026-01-01T00:01:00Z"]),
|
||||
"split_id": ["tune_inner", "tune_inner"],
|
||||
"long_prob": [0.70, 0.72],
|
||||
"short_prob": [0.10, 0.10],
|
||||
"neutral_prob": [0.20, 0.18],
|
||||
"long_entry_prob": [0.80, 0.82],
|
||||
"short_entry_prob": [0.20, 0.20],
|
||||
"market_risk_prob": [0.20, 0.20],
|
||||
"long_position_risk_prob": [0.10, 0.10],
|
||||
"short_position_risk_prob": [0.10, 0.10],
|
||||
"pred_long_expected_net_edge_bps": [40.0, 42.0],
|
||||
"pred_short_expected_net_edge_bps": [1.0, 1.0],
|
||||
"actual_long_expected_net_edge_bps": [30.0, 31.0],
|
||||
"actual_short_expected_net_edge_bps": [-10.0, -10.0],
|
||||
"long_trade_net_edge_bps": [11.0, 12.0],
|
||||
"short_trade_net_edge_bps": [-14.5, -14.5],
|
||||
"long_target_hit": [1, 1],
|
||||
"short_target_hit": [0, 0],
|
||||
"long_stop_hit": [0, 0],
|
||||
"short_stop_hit": [1, 1],
|
||||
"long_time_to_target_ms": [300_000, 300_000],
|
||||
"short_time_to_target_ms": [-1, -1],
|
||||
"long_time_to_stop_ms": [-1, -1],
|
||||
"short_time_to_stop_ms": [180_000, 180_000],
|
||||
"long_entry_target": [1, 1],
|
||||
"short_entry_target": [0, 0],
|
||||
}
|
||||
)
|
||||
thresholds = {
|
||||
"long_open_prob": 0.55,
|
||||
"short_open_prob": 0.55,
|
||||
"min_entry_prob": 0.55,
|
||||
"max_market_risk_prob": 0.55,
|
||||
"min_expected_edge_bps": 3.0,
|
||||
"min_direction_margin": 0.02,
|
||||
}
|
||||
|
||||
trades = _simulate_open_trades(
|
||||
frame,
|
||||
thresholds,
|
||||
default_pm_config(),
|
||||
{"stopDistanceBps": 8.0, "costBps": 6.5, "maxHoldMinutes": 45},
|
||||
)
|
||||
|
||||
self.assertEqual(1, len(trades))
|
||||
self.assertEqual("s0", trades.iloc[0]["sample_id"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,207 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
TRAINING_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(TRAINING_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(TRAINING_ROOT))
|
||||
|
||||
from trader_training.onnx_export import LinearHead, export_heads
|
||||
from trader_training.schemas import FEATURE_ORDER
|
||||
from trader_training.state_continue_experiment import STATE_FEATURES, _predict_frozen_linear_model, _state_rows_for_age, _train_side_models, _verdict
|
||||
|
||||
|
||||
class StateContinueExperimentTest(unittest.TestCase):
|
||||
def test_state_rows_include_required_position_path_and_side_market_features(self) -> None:
|
||||
row = {
|
||||
"current_sample_id": "s0",
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"current_event_time": pd.Timestamp("2026-01-01T00:05:00Z"),
|
||||
"current_open_time_ms": 300_000,
|
||||
"side": "LONG",
|
||||
"split_id": "fit_inner",
|
||||
"walk_forward_fold": 0,
|
||||
"time_in_position_minutes": 5,
|
||||
"entry_price": 100.0,
|
||||
"current_price": 100.1,
|
||||
"high_since_entry": 100.2,
|
||||
"low_since_entry": 99.95,
|
||||
"future_return_bps": 12.0,
|
||||
"gross_edge_bps": 12.0,
|
||||
"mae_bps": 20.0,
|
||||
"stop_hit": 0,
|
||||
"entry_predicted_edge_bps": 8.5,
|
||||
"entry_direction_prob": 0.64,
|
||||
"add_count": 0.0,
|
||||
"minutes_since_last_add": 9999.0,
|
||||
}
|
||||
for feature_name in FEATURE_ORDER:
|
||||
row[feature_name] = 0.0
|
||||
row["ret_1m_bps"] = 2.0
|
||||
row["ret_5m_bps"] = 3.0
|
||||
row["taker_imbalance_1m"] = 0.1
|
||||
row["taker_imbalance_5m"] = 0.2
|
||||
row["book_microprice_basis_bps"] = 4.0
|
||||
row["book_pressure_taker_1m"] = 5.0
|
||||
row["book_pressure_taker_5m"] = 6.0
|
||||
frame = pd.DataFrame([row])
|
||||
|
||||
out = _state_rows_for_age(frame, stop_bps=8.0, target_bps=12.0, cost_bps=6.5)
|
||||
|
||||
self.assertEqual(set(STATE_FEATURES), set(STATE_FEATURES).intersection(out.columns))
|
||||
self.assertAlmostEqual(5.5, float(out.iloc[0]["expected_continue_edge_bps"]))
|
||||
self.assertEqual(1, int(out.iloc[0]["continue_target"]))
|
||||
self.assertAlmostEqual(8.5, float(out.iloc[0]["entry_predicted_edge_bps"]))
|
||||
self.assertAlmostEqual(0.64, float(out.iloc[0]["entry_direction_prob"]), places=6)
|
||||
self.assertAlmostEqual(16.5, float(out.iloc[0]["giveback_from_mfe_bps"]), places=4)
|
||||
self.assertAlmostEqual(8.5025, float(out.iloc[0]["recovery_from_mae_bps"]), places=4)
|
||||
self.assertGreater(float(out.iloc[0]["path_efficiency"]), 0.13)
|
||||
self.assertGreater(float(out.iloc[0]["mfe_mae_ratio"]), 3.3)
|
||||
self.assertAlmostEqual(2.0, float(out.iloc[0]["side_ret_1m_bps"]))
|
||||
self.assertAlmostEqual(3.0, float(out.iloc[0]["side_ret_5m_bps"]))
|
||||
self.assertAlmostEqual(0.1, float(out.iloc[0]["side_taker_imbalance_1m"]), places=6)
|
||||
self.assertAlmostEqual(0.2, float(out.iloc[0]["side_taker_imbalance_5m"]), places=6)
|
||||
self.assertAlmostEqual(4.0, float(out.iloc[0]["side_book_microprice_basis_bps"]))
|
||||
self.assertAlmostEqual(5.0, float(out.iloc[0]["side_book_pressure_taker_1m"]))
|
||||
self.assertAlmostEqual(6.0, float(out.iloc[0]["side_book_pressure_taker_5m"]))
|
||||
self.assertAlmostEqual(0.0, float(out.iloc[0]["add_count"]))
|
||||
self.assertAlmostEqual(9999.0, float(out.iloc[0]["minutes_since_last_add"]))
|
||||
|
||||
def test_frozen_linear_onnx_weights_are_read_without_row_by_row_runtime(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
model_path = Path(tmp) / "direction.onnx"
|
||||
export_heads(
|
||||
model_path,
|
||||
[
|
||||
LinearHead(
|
||||
"direction",
|
||||
"softmax",
|
||||
np.zeros((len(FEATURE_ORDER), 3), dtype=np.float32),
|
||||
np.array([0.0, 1.0, 2.0], dtype=np.float32),
|
||||
),
|
||||
LinearHead(
|
||||
"long_expected_net_edge_bps",
|
||||
"identity",
|
||||
np.zeros((len(FEATURE_ORDER), 1), dtype=np.float32),
|
||||
np.array([7.25], dtype=np.float32),
|
||||
),
|
||||
],
|
||||
feature_count=len(FEATURE_ORDER),
|
||||
)
|
||||
frame = pd.DataFrame({"sample_id": ["s0", "s1"]})
|
||||
for feature_name in FEATURE_ORDER:
|
||||
frame[feature_name] = 0.0
|
||||
|
||||
out = _predict_frozen_linear_model(
|
||||
model_path,
|
||||
frame,
|
||||
{
|
||||
"direction": ("softmax", ("long_prob", "short_prob", "neutral_prob")),
|
||||
"long_expected_net_edge_bps": ("identity", ("long_edge",)),
|
||||
},
|
||||
)
|
||||
|
||||
self.assertEqual(["s0", "s1"], out["sample_id"].tolist())
|
||||
self.assertTrue(np.allclose(1.0, out[["long_prob", "short_prob", "neutral_prob"]].sum(axis=1)))
|
||||
self.assertLess(float(out.iloc[0]["long_prob"]), float(out.iloc[0]["neutral_prob"]))
|
||||
self.assertAlmostEqual(7.25, float(out.iloc[0]["long_edge"]), places=6)
|
||||
|
||||
def test_verdict_refuses_state_continue_when_edge_mae_is_not_good_enough(self) -> None:
|
||||
results = {}
|
||||
for side in ("long", "short"):
|
||||
results[f"{side}_market_only"] = {
|
||||
"validation_locked": {"continue_auc": 0.61, "edge_mae_vs_constant_ratio": 0.985},
|
||||
"latest_stress": {"continue_auc": 0.62, "edge_mae_vs_constant_ratio": 0.984},
|
||||
"regressor_converged": True,
|
||||
}
|
||||
results[f"{side}_market_plus_state"] = {
|
||||
"validation_locked": {"continue_auc": 0.63, "edge_mae_vs_constant_ratio": 0.979},
|
||||
"latest_stress": {"continue_auc": 0.64, "edge_mae_vs_constant_ratio": 0.978},
|
||||
"regressor_converged": True,
|
||||
}
|
||||
|
||||
verdict = _verdict(results)
|
||||
|
||||
self.assertEqual("NOT_READY_FOR_FORMAL_CHAIN", verdict["status"])
|
||||
self.assertTrue(any("above 0.97" in reason for reason in verdict["reasons"]))
|
||||
|
||||
def test_verdict_reports_when_state_features_do_not_beat_market_only(self) -> None:
|
||||
results = {}
|
||||
for side in ("long", "short"):
|
||||
results[f"{side}_market_only"] = {
|
||||
"validation_locked": {"continue_auc": 0.64, "edge_mae_vs_constant_ratio": 0.965},
|
||||
"latest_stress": {"continue_auc": 0.65, "edge_mae_vs_constant_ratio": 0.964},
|
||||
"regressor_converged": True,
|
||||
}
|
||||
results[f"{side}_market_plus_state"] = {
|
||||
"validation_locked": {"continue_auc": 0.63, "edge_mae_vs_constant_ratio": 0.975},
|
||||
"latest_stress": {"continue_auc": 0.66, "edge_mae_vs_constant_ratio": 0.963},
|
||||
"regressor_converged": True,
|
||||
}
|
||||
|
||||
verdict = _verdict(results)
|
||||
|
||||
self.assertEqual("NOT_READY_FOR_FORMAL_CHAIN", verdict["status"])
|
||||
self.assertTrue(any("continue_auc not better than market_only" in reason for reason in verdict["reasons"]))
|
||||
self.assertTrue(any("edge_mae_vs_constant_ratio not better than market_only" in reason for reason in verdict["reasons"]))
|
||||
|
||||
def test_train_side_models_supports_ridge_regressor_diagnostic(self) -> None:
|
||||
rows = []
|
||||
for split_id in ("fit_inner", "tune_inner", "validation_locked", "latest_stress"):
|
||||
for index, target in enumerate((0, 1)):
|
||||
row = {
|
||||
"sample_id": f"{split_id}-{index}",
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"event_time": pd.Timestamp("2026-01-01T00:00:00Z") + pd.Timedelta(minutes=len(rows)),
|
||||
"split_id": split_id,
|
||||
"position_side": "LONG",
|
||||
"continue_target": target,
|
||||
"expected_continue_edge_bps": -3.0 if target == 0 else 6.0,
|
||||
}
|
||||
for feature_name in FEATURE_ORDER:
|
||||
row[feature_name] = float(index)
|
||||
for feature_name in STATE_FEATURES:
|
||||
row[feature_name] = float(index)
|
||||
rows.append(row)
|
||||
frame = pd.DataFrame(rows)
|
||||
|
||||
metrics, predictions = _train_side_models(
|
||||
frame,
|
||||
"LONG",
|
||||
[*FEATURE_ORDER, *STATE_FEATURES],
|
||||
regressor_kind="ridge",
|
||||
ridge_alpha=1.0,
|
||||
regression_target_clip_bps=5.0,
|
||||
)
|
||||
|
||||
self.assertEqual("ridge", metrics["regressor_kind"])
|
||||
self.assertEqual(5.0, metrics["regression_target_clip_bps"])
|
||||
self.assertTrue(metrics["regressor_converged"])
|
||||
self.assertEqual(8, len(predictions))
|
||||
self.assertIn("time_in_position_minutes", predictions.columns)
|
||||
|
||||
huber_metrics, _ = _train_side_models(
|
||||
frame,
|
||||
"LONG",
|
||||
[*FEATURE_ORDER, *STATE_FEATURES],
|
||||
regressor_kind="huber",
|
||||
huber_alpha=0.002,
|
||||
huber_epsilon=1.10,
|
||||
huber_max_iter=100,
|
||||
regression_target_clip_bps=4.0,
|
||||
)
|
||||
|
||||
self.assertEqual("huber", huber_metrics["regressor_kind"])
|
||||
self.assertEqual(0.002, huber_metrics["huber_alpha"])
|
||||
self.assertEqual(1.10, huber_metrics["huber_epsilon"])
|
||||
self.assertEqual(4.0, huber_metrics["regression_target_clip_bps"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -14,8 +14,10 @@ if str(TRAINING_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(TRAINING_ROOT))
|
||||
|
||||
from trader_training.onnx_export import LinearHead, export_heads
|
||||
from trader_training.entry_feature_screen import _screen_edge_column
|
||||
from trader_training.io_utils import read_json, write_json
|
||||
from trader_training.labels import ENTRY_LABEL_METHOD, _path_stats_for_group, build_entry_labels
|
||||
from trader_training.ofi_feature_experiment import l1_snapshot_diff_ofi_quote
|
||||
from trader_training.promote import promote_artifact_bundle
|
||||
from trader_training.replay import build_splits
|
||||
from trader_training.schemas import FEATURE_ORDER, LATEST_STRESS_SPLIT, MODEL_OUTPUTS, OUTPUT_MAPPING, TRAINING_SPLITS, VALIDATION_LOCKED_SPLIT
|
||||
@@ -33,6 +35,19 @@ class TrainingContractTest(unittest.TestCase):
|
||||
self.assertEqual(set(fields), set(OUTPUT_MAPPING[model_name]))
|
||||
self.assertEqual([f"prediction[{idx}]" for idx in range(len(fields))], [OUTPUT_MAPPING[model_name][field] for field in fields])
|
||||
|
||||
def test_entry_feature_screen_prefers_actual_plan_edge(self) -> None:
|
||||
dataset = pd.DataFrame(
|
||||
{
|
||||
"long_expected_net_edge_bps": [20.0],
|
||||
"short_expected_net_edge_bps": [15.0],
|
||||
"long_actual_plan_net_edge_bps": [-3.0],
|
||||
"short_actual_plan_net_edge_bps": [4.0],
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual("long_actual_plan_net_edge_bps", _screen_edge_column(dataset, "LONG"))
|
||||
self.assertEqual("short_actual_plan_net_edge_bps", _screen_edge_column(dataset, "SHORT"))
|
||||
|
||||
def test_split_builder_uses_locked_validation_contract(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
data_root = Path(tmp)
|
||||
@@ -90,7 +105,7 @@ class TrainingContractTest(unittest.TestCase):
|
||||
self.assertEqual(120_000, first["time_to_stop_ms"])
|
||||
self.assertAlmostEqual(-8.0, first["gross_edge_bps"])
|
||||
|
||||
def test_entry_label_uses_max_future_edge_not_fixed_target_hit(self) -> None:
|
||||
def test_entry_label_uses_price_plan_outcome_not_max_future_edge(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
data_root = Path(tmp)
|
||||
run_root = data_root / "trader-v4" / "runs" / "unit-entry"
|
||||
@@ -167,11 +182,200 @@ class TrainingContractTest(unittest.TestCase):
|
||||
labels = pd.read_parquet(run_root / "label" / "entry_labels.parquet")
|
||||
row = labels[labels["sample_id"].eq("s0") & labels["side"].eq("LONG")].iloc[0]
|
||||
self.assertEqual(0, row["target_hit"])
|
||||
self.assertEqual(1, row["entry_target"])
|
||||
self.assertEqual(0, row["entry_target"])
|
||||
self.assertEqual(ENTRY_LABEL_METHOD, row["label_method"])
|
||||
self.assertAlmostEqual(13.5, row["expected_net_edge_bps"], places=6)
|
||||
self.assertAlmostEqual(-6.5, row["expected_net_edge_bps"], places=6)
|
||||
self.assertAlmostEqual(row["gross_edge_bps"] - row["cost_bps"], row["expected_net_edge_bps"], places=6)
|
||||
self.assertAlmostEqual(row["mfe_bps"] - row["cost_bps"], row["max_achievable_net_edge_bps"], places=6)
|
||||
|
||||
def test_entry_opportunity_label_keeps_plan_outcome_for_pm(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
data_root = Path(tmp)
|
||||
run_root = data_root / "trader-v4" / "runs" / "unit-entry-opportunity"
|
||||
feature_path = run_root / "feature" / "feature_frame.parquet"
|
||||
replay_path = run_root / "replay" / "replay_1m.parquet"
|
||||
plan_path = run_root / "label" / "price_plan_context.json"
|
||||
config_path = data_root / "label_config.json"
|
||||
feature_path.parent.mkdir(parents=True)
|
||||
replay_path.parent.mkdir(parents=True)
|
||||
|
||||
times = pd.date_range("2026-01-01", periods=5, freq="min", tz="UTC")
|
||||
pd.DataFrame(
|
||||
{
|
||||
"sample_id": ["s0"],
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"event_time": [times[0]],
|
||||
"open_time_ms": [0],
|
||||
"split_id": "fit_inner",
|
||||
"walk_forward_fold": 0,
|
||||
"data_quality_flag": "OK",
|
||||
"spread_bps": 1.0,
|
||||
"spread_rank_24h_pct": 0.1,
|
||||
"realized_vol_15m_bps": 2.0,
|
||||
}
|
||||
).to_parquet(feature_path, index=False)
|
||||
pd.DataFrame(
|
||||
{
|
||||
"event_time": times,
|
||||
"open_time_ms": np.arange(5, dtype=np.int64) * 60_000,
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"open": [100.0] * 5,
|
||||
"high": [100.0, 100.05, 100.19, 100.20, 100.0],
|
||||
"low": [100.0, 99.99, 99.98, 99.97, 100.0],
|
||||
"close": [100.0] * 5,
|
||||
"spread_bps": 1.0,
|
||||
}
|
||||
).to_parquet(replay_path, index=False)
|
||||
write_json(
|
||||
config_path,
|
||||
{
|
||||
"entry": {
|
||||
"max_hold_minutes": 3,
|
||||
"target_bps": 50.0,
|
||||
"stop_bps": 50.0,
|
||||
"min_expected_net_edge_bps": 3.0,
|
||||
"target_method": "OPPORTUNITY_MFE_V1",
|
||||
}
|
||||
},
|
||||
)
|
||||
write_json(
|
||||
plan_path,
|
||||
{
|
||||
"pricePlanId": "unit-plan",
|
||||
"pricePlanConfigHash": "unit-hash",
|
||||
"targetDistanceBps": 50.0,
|
||||
"stopDistanceBps": 50.0,
|
||||
"maxHoldMinutes": 3,
|
||||
"costBps": 6.5,
|
||||
"entryLabelMethod": ENTRY_LABEL_METHOD,
|
||||
"entryTargetMethod": "OPPORTUNITY_MFE_V1",
|
||||
},
|
||||
)
|
||||
|
||||
build_entry_labels(
|
||||
Namespace(
|
||||
data_root=data_root,
|
||||
run_id="unit-entry-opportunity",
|
||||
feature_path=feature_path,
|
||||
replay_path=replay_path,
|
||||
label_config_path=config_path,
|
||||
cost_config_path=None,
|
||||
price_plan_context_path=plan_path,
|
||||
)
|
||||
)
|
||||
|
||||
labels = pd.read_parquet(run_root / "label" / "entry_labels.parquet")
|
||||
row = labels[labels["sample_id"].eq("s0") & labels["side"].eq("LONG")].iloc[0]
|
||||
self.assertEqual(0, row["target_hit"])
|
||||
self.assertEqual(1, row["entry_target"])
|
||||
self.assertEqual("OPPORTUNITY_MFE_V1", row["label_method"])
|
||||
self.assertAlmostEqual(row["mfe_bps"] - row["cost_bps"], row["expected_net_edge_bps"], places=6)
|
||||
self.assertAlmostEqual(-6.5, row["gross_edge_bps"] - row["cost_bps"], places=6)
|
||||
|
||||
def test_entry_quality_label_rejects_untradable_opportunity(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
data_root = Path(tmp)
|
||||
run_root = data_root / "trader-v4" / "runs" / "unit-entry-quality"
|
||||
feature_path = run_root / "feature" / "feature_frame.parquet"
|
||||
replay_path = run_root / "replay" / "replay_1m.parquet"
|
||||
plan_path = run_root / "label" / "price_plan_context.json"
|
||||
config_path = data_root / "label_config.json"
|
||||
feature_path.parent.mkdir(parents=True)
|
||||
replay_path.parent.mkdir(parents=True)
|
||||
|
||||
times = pd.date_range("2026-01-01", periods=5, freq="min", tz="UTC")
|
||||
pd.DataFrame(
|
||||
{
|
||||
"sample_id": ["s0"],
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"event_time": [times[0]],
|
||||
"open_time_ms": [0],
|
||||
"split_id": "fit_inner",
|
||||
"walk_forward_fold": 0,
|
||||
"data_quality_flag": "OK",
|
||||
"spread_bps": 1.0,
|
||||
"spread_rank_24h_pct": 0.1,
|
||||
"realized_vol_15m_bps": 2.0,
|
||||
}
|
||||
).to_parquet(feature_path, index=False)
|
||||
pd.DataFrame(
|
||||
{
|
||||
"event_time": times,
|
||||
"open_time_ms": np.arange(5, dtype=np.int64) * 60_000,
|
||||
"symbol": "BTC-USDT-PERP",
|
||||
"open": [100.0] * 5,
|
||||
"high": [100.0, 100.05, 100.19, 100.20, 100.0],
|
||||
"low": [100.0, 99.99, 99.98, 99.97, 100.0],
|
||||
"close": [100.0] * 5,
|
||||
"spread_bps": 1.0,
|
||||
}
|
||||
).to_parquet(replay_path, index=False)
|
||||
write_json(
|
||||
config_path,
|
||||
{
|
||||
"entry": {
|
||||
"max_hold_minutes": 3,
|
||||
"target_bps": 50.0,
|
||||
"stop_bps": 50.0,
|
||||
"min_expected_net_edge_bps": 3.0,
|
||||
"min_plan_net_edge_bps": 0.0,
|
||||
"max_entry_mae_bps": 12.0,
|
||||
"target_method": "OPPORTUNITY_QUALITY_V1",
|
||||
}
|
||||
},
|
||||
)
|
||||
write_json(
|
||||
plan_path,
|
||||
{
|
||||
"pricePlanId": "unit-plan",
|
||||
"pricePlanConfigHash": "unit-hash",
|
||||
"targetDistanceBps": 50.0,
|
||||
"stopDistanceBps": 50.0,
|
||||
"maxHoldMinutes": 3,
|
||||
"costBps": 6.5,
|
||||
"entryLabelMethod": ENTRY_LABEL_METHOD,
|
||||
"entryTargetMethod": "OPPORTUNITY_QUALITY_V1",
|
||||
},
|
||||
)
|
||||
|
||||
build_entry_labels(
|
||||
Namespace(
|
||||
data_root=data_root,
|
||||
run_id="unit-entry-quality",
|
||||
feature_path=feature_path,
|
||||
replay_path=replay_path,
|
||||
label_config_path=config_path,
|
||||
cost_config_path=None,
|
||||
price_plan_context_path=plan_path,
|
||||
)
|
||||
)
|
||||
|
||||
labels = pd.read_parquet(run_root / "label" / "entry_labels.parquet")
|
||||
row = labels[labels["sample_id"].eq("s0") & labels["side"].eq("LONG")].iloc[0]
|
||||
self.assertEqual("OPPORTUNITY_QUALITY_V1", row["label_method"])
|
||||
self.assertGreater(row["expected_net_edge_bps"], 3.0)
|
||||
self.assertLess(row["actual_plan_net_edge_bps"], 0.0)
|
||||
self.assertEqual(0, row["entry_target"])
|
||||
|
||||
def test_l1_snapshot_diff_ofi_uses_quote_notional_and_signed_ask_side(self) -> None:
|
||||
bid_part, ask_part = l1_snapshot_diff_ofi_quote(
|
||||
pd.Series([101.0, 101.0, 100.5]),
|
||||
pd.Series([2.0, 3.0, 4.0]),
|
||||
pd.Series([102.0, 101.5, 102.5]),
|
||||
pd.Series([5.0, 6.0, 7.0]),
|
||||
pd.Series([100.0, 101.0, 101.0]),
|
||||
pd.Series([1.5, 2.0, 3.0]),
|
||||
pd.Series([102.0, 102.0, 101.5]),
|
||||
pd.Series([4.0, 5.0, 6.0]),
|
||||
)
|
||||
|
||||
self.assertAlmostEqual(202.0, bid_part.iloc[0])
|
||||
self.assertAlmostEqual(-102.0, ask_part.iloc[0])
|
||||
self.assertAlmostEqual(101.0, bid_part.iloc[1])
|
||||
self.assertAlmostEqual(-609.0, ask_part.iloc[1])
|
||||
self.assertAlmostEqual(-303.0, bid_part.iloc[2])
|
||||
self.assertAlmostEqual(609.0, ask_part.iloc[2])
|
||||
|
||||
def test_exported_onnx_accepts_java_feature_shape(self) -> None:
|
||||
import onnxruntime as ort
|
||||
|
||||
|
||||
@@ -83,11 +83,25 @@ def build_train_datasets(args: Any) -> None:
|
||||
|
||||
def _entry_pivot(entry: pd.DataFrame) -> pd.DataFrame:
|
||||
require_columns(entry, ("sample_id", "side", "entry_target", "expected_net_edge_bps"), "entry_labels")
|
||||
long = entry[entry["side"] == "LONG"][["sample_id", "entry_target", "expected_net_edge_bps"]].rename(
|
||||
columns={"entry_target": "long_entry_target", "expected_net_edge_bps": "long_expected_net_edge_bps"}
|
||||
optional_columns = [column for column in ("actual_plan_net_edge_bps", "max_achievable_net_edge_bps", "mae_bps") if column in entry.columns]
|
||||
selected_columns = ["sample_id", "entry_target", "expected_net_edge_bps", *optional_columns]
|
||||
long = entry[entry["side"] == "LONG"][selected_columns].rename(
|
||||
columns={
|
||||
"entry_target": "long_entry_target",
|
||||
"expected_net_edge_bps": "long_expected_net_edge_bps",
|
||||
"actual_plan_net_edge_bps": "long_actual_plan_net_edge_bps",
|
||||
"max_achievable_net_edge_bps": "long_max_achievable_net_edge_bps",
|
||||
"mae_bps": "long_mae_bps",
|
||||
}
|
||||
)
|
||||
short = entry[entry["side"] == "SHORT"][["sample_id", "entry_target", "expected_net_edge_bps"]].rename(
|
||||
columns={"entry_target": "short_entry_target", "expected_net_edge_bps": "short_expected_net_edge_bps"}
|
||||
short = entry[entry["side"] == "SHORT"][selected_columns].rename(
|
||||
columns={
|
||||
"entry_target": "short_entry_target",
|
||||
"expected_net_edge_bps": "short_expected_net_edge_bps",
|
||||
"actual_plan_net_edge_bps": "short_actual_plan_net_edge_bps",
|
||||
"max_achievable_net_edge_bps": "short_max_achievable_net_edge_bps",
|
||||
"mae_bps": "short_mae_bps",
|
||||
}
|
||||
)
|
||||
return long.merge(short, on="sample_id", how="inner")
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ from typing import Any
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from trader_training.io_utils import read_parquet, run_root, write_json, write_text
|
||||
from trader_training.pm import _pm_frame, _simulate_open_trades, _threshold_candidates, _trade_metrics
|
||||
from trader_training.io_utils import read_json, read_parquet, run_root, write_json, write_text
|
||||
from trader_training.pm import _pm_frame, _price_plan_context, _simulate_open_trades, _threshold_candidates, _thresholds_from_config, _trade_metrics, default_pm_config
|
||||
from trader_training.schemas import FIT_SPLIT, LATEST_STRESS_SPLIT, TUNE_SPLIT, VALIDATION_LOCKED_SPLIT
|
||||
|
||||
|
||||
@@ -66,12 +66,19 @@ def _label_summary(root) -> dict[str, Any]:
|
||||
|
||||
def _pm_summary(root) -> dict[str, Any]:
|
||||
summary: dict[str, Any] = {}
|
||||
config_path = root / "pm-search" / "position_manager_config.json"
|
||||
config = read_json(config_path)["config"] if config_path.is_file() else default_pm_config()
|
||||
thresholds = _thresholds_from_config(config)
|
||||
price_plan = _price_plan_context(root)
|
||||
for split_id in PM_EVAL_SPLITS:
|
||||
frame = _pm_frame(root, split_id)
|
||||
selected_trades = _simulate_open_trades(frame, thresholds, config, price_plan)
|
||||
item = {
|
||||
"rows": len(frame),
|
||||
"score_distribution": _score_distribution(frame),
|
||||
"gate_funnel": _gate_funnel(frame),
|
||||
"active_thresholds": thresholds,
|
||||
"gate_funnel": _gate_funnel(frame, thresholds),
|
||||
"selected_trade_metrics": _trade_metrics(selected_trades),
|
||||
"relaxed_variants": _relaxed_variants(frame),
|
||||
"top_bucket_edge": _top_bucket_edge(frame),
|
||||
"grid_search_any_trade": _grid_trade_summary(frame),
|
||||
@@ -89,34 +96,29 @@ def _score_distribution(frame: pd.DataFrame) -> dict[str, dict[str, float]]:
|
||||
"market_risk_prob",
|
||||
"pred_long_expected_net_edge_bps",
|
||||
"pred_short_expected_net_edge_bps",
|
||||
"model_pred_long_expected_net_edge_bps",
|
||||
"model_pred_short_expected_net_edge_bps",
|
||||
"actual_long_expected_net_edge_bps",
|
||||
"actual_short_expected_net_edge_bps",
|
||||
]
|
||||
return {column: _quantiles(frame[column], (0.0, 0.05, 0.5, 0.95, 1.0)) for column in columns}
|
||||
return {column: _quantiles(frame[column], (0.0, 0.05, 0.5, 0.95, 1.0)) for column in columns if column in frame.columns}
|
||||
|
||||
|
||||
def _gate_funnel(frame: pd.DataFrame) -> dict[str, Any]:
|
||||
thresholds = {
|
||||
"long_open_prob": 0.54,
|
||||
"short_open_prob": 0.54,
|
||||
"min_entry_prob": 0.50,
|
||||
"max_market_risk_prob": 0.55,
|
||||
"min_expected_edge_bps": 1.0,
|
||||
"min_direction_margin": 0.02,
|
||||
}
|
||||
def _gate_funnel(frame: pd.DataFrame, thresholds: dict[str, float]) -> dict[str, Any]:
|
||||
direction_margin = (frame["long_prob"] - frame["short_prob"]).abs()
|
||||
long_steps = {
|
||||
"long_prob >= 0.54": frame["long_prob"] >= thresholds["long_open_prob"],
|
||||
"long_prob - short_prob >= 0.02": (frame["long_prob"] - frame["short_prob"]) >= thresholds["min_direction_margin"],
|
||||
"long_entry_prob >= 0.50": frame["long_entry_prob"] >= thresholds["min_entry_prob"],
|
||||
"market_risk_prob <= 0.55": frame["market_risk_prob"] <= thresholds["max_market_risk_prob"],
|
||||
"pred_long_expected_net_edge_bps >= 1.0": frame["pred_long_expected_net_edge_bps"] >= thresholds["min_expected_edge_bps"],
|
||||
f"long_prob > {thresholds['long_open_prob']}": frame["long_prob"] > thresholds["long_open_prob"],
|
||||
f"abs(long_prob - short_prob) > {thresholds['min_direction_margin']}": direction_margin > thresholds["min_direction_margin"],
|
||||
f"long_entry_prob > {thresholds['min_entry_prob']}": frame["long_entry_prob"] > thresholds["min_entry_prob"],
|
||||
f"market_risk_prob < {thresholds['max_market_risk_prob']}": frame["market_risk_prob"] < thresholds["max_market_risk_prob"],
|
||||
f"pred_long_expected_net_edge_bps > {thresholds['min_expected_edge_bps']}": frame["pred_long_expected_net_edge_bps"] > thresholds["min_expected_edge_bps"],
|
||||
}
|
||||
short_steps = {
|
||||
"short_prob >= 0.54": frame["short_prob"] >= thresholds["short_open_prob"],
|
||||
"short_prob - long_prob >= 0.02": (frame["short_prob"] - frame["long_prob"]) >= thresholds["min_direction_margin"],
|
||||
"short_entry_prob >= 0.50": frame["short_entry_prob"] >= thresholds["min_entry_prob"],
|
||||
"market_risk_prob <= 0.55": frame["market_risk_prob"] <= thresholds["max_market_risk_prob"],
|
||||
"pred_short_expected_net_edge_bps >= 1.0": frame["pred_short_expected_net_edge_bps"] >= thresholds["min_expected_edge_bps"],
|
||||
f"short_prob > {thresholds['short_open_prob']}": frame["short_prob"] > thresholds["short_open_prob"],
|
||||
f"abs(long_prob - short_prob) > {thresholds['min_direction_margin']}": direction_margin > thresholds["min_direction_margin"],
|
||||
f"short_entry_prob > {thresholds['min_entry_prob']}": frame["short_entry_prob"] > thresholds["min_entry_prob"],
|
||||
f"market_risk_prob < {thresholds['max_market_risk_prob']}": frame["market_risk_prob"] < thresholds["max_market_risk_prob"],
|
||||
f"pred_short_expected_net_edge_bps > {thresholds['min_expected_edge_bps']}": frame["pred_short_expected_net_edge_bps"] > thresholds["min_expected_edge_bps"],
|
||||
}
|
||||
return {
|
||||
"thresholds": thresholds,
|
||||
@@ -140,6 +142,7 @@ def _cumulative_gate_counts(steps: dict[str, pd.Series], total_rows: int) -> dic
|
||||
def _relaxed_variants(frame: pd.DataFrame) -> dict[str, Any]:
|
||||
variants = {
|
||||
"no_risk_no_edge": {"prob": 0.54, "entry": 0.50, "margin": 0.02, "risk": 1.0, "edge": -99.0},
|
||||
"rare_entry_low_prob": {"prob": 0.50, "entry": 0.03, "margin": 0.02, "risk": 0.98, "edge": 0.0},
|
||||
"entry_only_55": {"prob": 0.0, "entry": 0.55, "margin": -99.0, "risk": 1.0, "edge": -99.0},
|
||||
"direction_only_54": {"prob": 0.54, "entry": 0.0, "margin": 0.02, "risk": 1.0, "edge": -99.0},
|
||||
"very_loose": {"prob": 0.50, "entry": 0.45, "margin": 0.0, "risk": 1.0, "edge": -99.0},
|
||||
@@ -168,10 +171,10 @@ def _variant_trades(frame: pd.DataFrame, thresholds: dict[str, float]) -> pd.Dat
|
||||
)
|
||||
long = frame.loc[long_mask].copy()
|
||||
long["side"] = "LONG"
|
||||
long["actual_edge_bps"] = long["actual_long_expected_net_edge_bps"]
|
||||
long["actual_edge_bps"] = long["long_trade_net_edge_bps"]
|
||||
short = frame.loc[short_mask].copy()
|
||||
short["side"] = "SHORT"
|
||||
short["actual_edge_bps"] = short["actual_short_expected_net_edge_bps"]
|
||||
short["actual_edge_bps"] = short["short_trade_net_edge_bps"]
|
||||
return pd.concat([long, short], ignore_index=True)
|
||||
|
||||
|
||||
@@ -189,7 +192,7 @@ def _plain_trade_metrics(trades: pd.DataFrame) -> dict[str, Any]:
|
||||
def _top_bucket_edge(frame: pd.DataFrame) -> dict[str, Any]:
|
||||
side = np.where(frame["long_prob"] >= frame["short_prob"], "LONG", "SHORT")
|
||||
side_prob = np.where(side == "LONG", frame["long_prob"], frame["short_prob"])
|
||||
side_edge = np.where(side == "LONG", frame["actual_long_expected_net_edge_bps"], frame["actual_short_expected_net_edge_bps"])
|
||||
side_edge = np.where(side == "LONG", frame["long_trade_net_edge_bps"], frame["short_trade_net_edge_bps"])
|
||||
direction_frame = pd.DataFrame({"score": side_prob, "actual_edge_bps": side_edge, "side": side})
|
||||
direction_top = {}
|
||||
for fraction in (0.01, 0.02, 0.05, 0.10):
|
||||
@@ -243,27 +246,19 @@ def _grid_trade_summary(frame: pd.DataFrame) -> dict[str, Any]:
|
||||
|
||||
|
||||
def _diagnostic_conclusion(pm_summary: dict[str, Any]) -> dict[str, Any]:
|
||||
tune = pm_summary.get(TUNE_SPLIT, {})
|
||||
gate = tune.get("gate_funnel", {})
|
||||
long_single = gate.get("long", {}).get("single_gate_pass", {})
|
||||
short_single = gate.get("short", {}).get("single_gate_pass", {})
|
||||
pred_edge_blocked = (
|
||||
long_single.get("pred_long_expected_net_edge_bps >= 1.0", 0) == 0
|
||||
and short_single.get("pred_short_expected_net_edge_bps >= 1.0", 0) == 0
|
||||
)
|
||||
relaxed = tune.get("relaxed_variants", {})
|
||||
any_relaxed_positive = any(item.get("avg_actual_edge_bps", 0.0) > 0 for item in relaxed.values())
|
||||
if pred_edge_blocked and not any_relaxed_positive:
|
||||
validation = pm_summary.get(VALIDATION_LOCKED_SPLIT, {}).get("selected_trade_metrics", {})
|
||||
stress = pm_summary.get(LATEST_STRESS_SPLIT, {}).get("selected_trade_metrics", {})
|
||||
if validation.get("trade_count", 0) == 0:
|
||||
return {
|
||||
"status": "MODEL_SIGNAL_NOT_TRADABLE",
|
||||
"plain_reason": "Entry 预测的净收益基本都是负数;即使放松风险和收益门槛,选出来的样本平均仍亏。",
|
||||
"next_action": "优先重查 Entry 标签和价格计划,再考虑更强模型;不要直接放松 PM 阈值上线。",
|
||||
"status": "NO_VALIDATION_TRADE",
|
||||
"plain_reason": "当前 PM 阈值在验证集没有选出交易,主要要看挡单漏斗。",
|
||||
"next_action": "先看 Direction、Risk、Entry 哪个门槛挡住,再做阈值实验。",
|
||||
}
|
||||
if pred_edge_blocked:
|
||||
if validation.get("avg_weighted_edge_bps", 0.0) <= 0 and stress.get("avg_weighted_edge_bps", 0.0) <= 0:
|
||||
return {
|
||||
"status": "ENTRY_EDGE_GATE_BLOCKED",
|
||||
"plain_reason": "PM 没有交易主要是 Entry 预测净收益过低。",
|
||||
"next_action": "重训 Entry 或调整价格计划后再搜索 PM 阈值。",
|
||||
"status": "PRICE_PLAN_OR_ENTRY_NOT_TRADABLE",
|
||||
"plain_reason": "按固定止盈止损真实收益算,验证集和压力集选出来的交易平均都不赚钱。",
|
||||
"next_action": "优先重新搜索价格计划,再重建 Entry 标签和模型;不要只放松 PM 阈值。",
|
||||
}
|
||||
return {
|
||||
"status": "NEEDS_MANUAL_REVIEW",
|
||||
@@ -312,6 +307,8 @@ def _markdown_report(payload: dict[str, Any]) -> str:
|
||||
lines.append(f"### {split_id}")
|
||||
lines.append("")
|
||||
lines.append(f"- 样本数: {item['rows']}")
|
||||
lines.append(f"- 当前阈值: `{item['active_thresholds']}`")
|
||||
lines.append(f"- 当前阈值选中交易: `{item['selected_trade_metrics']}`")
|
||||
lines.append(f"- 网格里有交易的候选数: {item['grid_search_any_trade']['candidates_with_trade']} / {item['grid_search_any_trade']['candidate_count']}")
|
||||
lines.append("")
|
||||
for side in ("long", "short"):
|
||||
|
||||
@@ -24,9 +24,11 @@ def screen_entry_features(args: Any) -> None:
|
||||
|
||||
min_bucket_rows = int(args.min_bucket_rows or 300)
|
||||
rows: list[dict[str, Any]] = []
|
||||
edge_source_by_side: dict[str, str] = {}
|
||||
for side in ("LONG", "SHORT"):
|
||||
target_col = "long_entry_target" if side == "LONG" else "short_entry_target"
|
||||
edge_col = "long_expected_net_edge_bps" if side == "LONG" else "short_expected_net_edge_bps"
|
||||
edge_col = _screen_edge_column(dataset, side)
|
||||
edge_source_by_side[side] = edge_col
|
||||
baselines = _split_baselines(dataset, target_col, edge_col)
|
||||
for feature in FEATURE_ORDER:
|
||||
rows.extend(_feature_rows(dataset, feature, side, target_col, edge_col, baselines))
|
||||
@@ -43,6 +45,7 @@ def screen_entry_features(args: Any) -> None:
|
||||
"bucket_metric_count": int(len(bucket_metrics)),
|
||||
"candidate_count": int(len(candidates)),
|
||||
"min_bucket_rows": min_bucket_rows,
|
||||
"edge_source_by_side": edge_source_by_side,
|
||||
"selection_rule": "bucket boundaries are learned on fit_inner; candidate is picked by tune_inner and checked on validation_locked/latest_stress",
|
||||
}
|
||||
write_json(root / "diagnostics" / "entry_feature_screen_result.json", result)
|
||||
@@ -59,6 +62,14 @@ def screen_entry_features(args: Any) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _screen_edge_column(dataset: pd.DataFrame, side: str) -> str:
|
||||
prefix = "long" if side == "LONG" else "short"
|
||||
actual_col = f"{prefix}_actual_plan_net_edge_bps"
|
||||
if actual_col in dataset.columns:
|
||||
return actual_col
|
||||
return f"{prefix}_expected_net_edge_bps"
|
||||
|
||||
|
||||
def _split_baselines(dataset: pd.DataFrame, target_col: str, edge_col: str) -> dict[str, dict[str, float]]:
|
||||
baselines: dict[str, dict[str, float]] = {}
|
||||
for split_id in ALL_SPLITS:
|
||||
@@ -225,6 +236,7 @@ def _markdown_report(result: dict[str, Any], candidates: pd.DataFrame) -> str:
|
||||
"",
|
||||
"这份报告只回答一个问题:历史数据里,单个特征的某些区间有没有稳定变好。",
|
||||
"",
|
||||
"- 如果数据里有真实出场净收益,本报告用真实出场净收益;没有时才退回训练收益标签。",
|
||||
"- `tune_inner` 用来挑候选区间。",
|
||||
"- `validation_locked` 和 `latest_stress` 用来检查这个区间是不是出了训练样本也还能站住。",
|
||||
"- `stable_positive_edge=true` 代表这个区间在三个检查集里的平均净收益都大于 0。",
|
||||
@@ -237,6 +249,8 @@ def _markdown_report(result: dict[str, Any], candidates: pd.DataFrame) -> str:
|
||||
f"- 分桶明细数: `{result['bucket_metric_count']}`",
|
||||
f"- 候选数: `{result['candidate_count']}`",
|
||||
f"- 最小分桶行数: `{result['min_bucket_rows']}`",
|
||||
f"- 做多收益来源: `{result['edge_source_by_side'].get('LONG')}`",
|
||||
f"- 做空收益来源: `{result['edge_source_by_side'].get('SHORT')}`",
|
||||
"",
|
||||
]
|
||||
if candidates.empty:
|
||||
|
||||
@@ -24,10 +24,30 @@ from trader_training.schemas import LABEL_VERSION
|
||||
|
||||
DEFAULT_LABEL_CONFIG = {
|
||||
"direction": {"horizon_minutes": 45, "long_threshold_bps": 5.0, "short_threshold_bps": -5.0},
|
||||
"entry": {"max_hold_minutes": 45, "target_bps": 12.0, "stop_bps": 8.0, "min_expected_net_edge_bps": 3.0},
|
||||
"continue": {"horizon_minutes": 30, "min_expected_continue_edge_bps": 2.0},
|
||||
"exit": {"horizon_minutes": 30, "adverse_move_bps": 8.0, "stagnation_abs_return_bps": 2.0},
|
||||
"risk": {"horizon_minutes": 30, "market_drawdown_bps": 12.0, "vol_expansion_ratio": 1.6, "spike_bps": 20.0},
|
||||
"entry": {
|
||||
"max_hold_minutes": 45,
|
||||
"target_bps": 12.0,
|
||||
"stop_bps": 8.0,
|
||||
"min_expected_net_edge_bps": 3.0,
|
||||
"plan_method": "FIXED_TARGET_STOP_V1",
|
||||
"target_method": "PRICE_PLAN_OUTCOME_V1",
|
||||
"min_plan_net_edge_bps": 0.0,
|
||||
"max_entry_mae_bps": 12.0,
|
||||
"partial_take_1_ratio": 0.50,
|
||||
"partial_take_2_ratio": 0.25,
|
||||
"second_target_bps": 24.0,
|
||||
"trailing_stop_bps": 10.0,
|
||||
"breakeven_after_first_target": True,
|
||||
},
|
||||
"continue": {"horizon_minutes": 45, "min_expected_continue_edge_bps": 5.0},
|
||||
"exit": {"horizon_minutes": 45, "adverse_move_bps": 20.0, "stagnation_abs_return_bps": 5.0},
|
||||
"risk": {
|
||||
"horizon_minutes": 45,
|
||||
"market_drawdown_bps": 60.0,
|
||||
"position_path_risk_bps": 35.0,
|
||||
"vol_expansion_ratio": 1.8,
|
||||
"spike_bps": 80.0,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +57,7 @@ DEFAULT_COST_CONFIG = {
|
||||
"funding_cost_bps": 0.5,
|
||||
}
|
||||
|
||||
ENTRY_LABEL_METHOD = "MAX_FUTURE_EDGE_V1"
|
||||
ENTRY_LABEL_METHOD = "PRICE_PLAN_OUTCOME_V1"
|
||||
|
||||
|
||||
def _load_config(path, default):
|
||||
@@ -53,6 +73,13 @@ def _load_config(path, default):
|
||||
return merged
|
||||
|
||||
|
||||
def _config_number(config: dict[str, Any], keys: tuple[str, ...], default: float) -> float:
|
||||
for key in keys:
|
||||
if key in config:
|
||||
return float(config[key])
|
||||
return default
|
||||
|
||||
|
||||
def _base_frames(args: Any) -> tuple[pd.DataFrame, pd.DataFrame]:
|
||||
root = run_root(args)
|
||||
feature_path = args.feature_path or root / "feature" / "feature_frame.parquet"
|
||||
@@ -79,6 +106,7 @@ PATH_STAT_COLUMNS = [
|
||||
"ambiguous_hit",
|
||||
"time_to_target_ms",
|
||||
"time_to_stop_ms",
|
||||
"time_to_exit_ms",
|
||||
"gross_edge_bps",
|
||||
"future_return_bps",
|
||||
"mfe_bps",
|
||||
@@ -99,7 +127,14 @@ def _first_hit_index(hit_window: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
||||
return hit_any, first_idx
|
||||
|
||||
|
||||
def _path_stats_for_group(group: pd.DataFrame, side: str, horizon: int, target_bps: float, stop_bps: float) -> pd.DataFrame:
|
||||
def _path_stats_for_group(
|
||||
group: pd.DataFrame,
|
||||
side: str,
|
||||
horizon: int,
|
||||
target_bps: float,
|
||||
stop_bps: float,
|
||||
plan_config: dict[str, Any] | None = None,
|
||||
) -> pd.DataFrame:
|
||||
if len(group) <= horizon:
|
||||
return _empty_path_stats_frame()
|
||||
|
||||
@@ -138,22 +173,45 @@ def _path_stats_for_group(group: pd.DataFrame, side: str, horizon: int, target_b
|
||||
else:
|
||||
realized_vol_bps = np.full(len(entry), np.nan)
|
||||
|
||||
method = str((plan_config or {}).get("plan_method", "FIXED_TARGET_STOP_V1"))
|
||||
if method == "DYNAMIC_TRAILING_V1":
|
||||
return _dynamic_path_stats_for_group(
|
||||
grouped,
|
||||
side,
|
||||
horizon,
|
||||
target_bps,
|
||||
stop_bps,
|
||||
close,
|
||||
high,
|
||||
low,
|
||||
spread,
|
||||
open_ms,
|
||||
valid,
|
||||
future_high,
|
||||
future_low,
|
||||
future_spread,
|
||||
future_realized_vol_bps=realized_vol_bps,
|
||||
plan_config=plan_config or {},
|
||||
)
|
||||
if method != "FIXED_TARGET_STOP_V1":
|
||||
raise ValueError(f"unsupported entry plan_method: {method}")
|
||||
|
||||
if side == "LONG":
|
||||
target_price = entry * (1.0 + target_bps / 10000.0)
|
||||
stop_price = entry * (1.0 - stop_bps / 10000.0)
|
||||
target_window = future_high >= target_price[:, None]
|
||||
stop_window = future_low <= stop_price[:, None]
|
||||
future_return_bps = (exit_price / entry - 1.0) * 10000.0
|
||||
mfe_bps = (high_max / entry - 1.0) * 10000.0
|
||||
mae_bps = (entry / low_min - 1.0) * 10000.0
|
||||
mfe_bps = np.maximum((high_max / entry - 1.0) * 10000.0, 0.0)
|
||||
mae_bps = np.maximum((entry / low_min - 1.0) * 10000.0, 0.0)
|
||||
else:
|
||||
target_price = entry * (1.0 - target_bps / 10000.0)
|
||||
stop_price = entry * (1.0 + stop_bps / 10000.0)
|
||||
target_window = future_low <= target_price[:, None]
|
||||
stop_window = future_high >= stop_price[:, None]
|
||||
future_return_bps = (entry / exit_price - 1.0) * 10000.0
|
||||
mfe_bps = (entry / low_min - 1.0) * 10000.0
|
||||
mae_bps = (high_max / entry - 1.0) * 10000.0
|
||||
mfe_bps = np.maximum((entry / low_min - 1.0) * 10000.0, 0.0)
|
||||
mae_bps = np.maximum((high_max / entry - 1.0) * 10000.0, 0.0)
|
||||
|
||||
target_any, first_target_idx = _first_hit_index(target_window)
|
||||
stop_any, first_stop_idx = _first_hit_index(stop_window)
|
||||
@@ -162,6 +220,11 @@ def _path_stats_for_group(group: pd.DataFrame, side: str, horizon: int, target_b
|
||||
stop_hit = stop_any & (first_stop_idx <= first_target_idx)
|
||||
timeout_hit = ~(target_hit | stop_hit)
|
||||
gross_edge_bps = np.where(target_hit, target_bps, np.where(stop_hit, -stop_bps, future_return_bps))
|
||||
time_to_exit_ms = np.where(
|
||||
target_hit,
|
||||
(first_target_idx + 1) * 60_000,
|
||||
np.where(stop_hit, (first_stop_idx + 1) * 60_000, horizon * 60_000),
|
||||
)
|
||||
|
||||
out = pd.DataFrame(
|
||||
{
|
||||
@@ -174,6 +237,7 @@ def _path_stats_for_group(group: pd.DataFrame, side: str, horizon: int, target_b
|
||||
"ambiguous_hit": ambiguous_hit.astype("int8"),
|
||||
"time_to_target_ms": np.where(target_hit, (first_target_idx + 1) * 60_000, -1).astype("int64"),
|
||||
"time_to_stop_ms": np.where(stop_hit, (first_stop_idx + 1) * 60_000, -1).astype("int64"),
|
||||
"time_to_exit_ms": time_to_exit_ms.astype("int64"),
|
||||
"gross_edge_bps": gross_edge_bps.astype("float64"),
|
||||
"future_return_bps": future_return_bps.astype("float64"),
|
||||
"mfe_bps": mfe_bps.astype("float64"),
|
||||
@@ -185,27 +249,184 @@ def _path_stats_for_group(group: pd.DataFrame, side: str, horizon: int, target_b
|
||||
return out.loc[valid, PATH_STAT_COLUMNS].reset_index(drop=True)
|
||||
|
||||
|
||||
def _build_path_stats(replay: pd.DataFrame, horizon: int, target_bps: float, stop_bps: float) -> pd.DataFrame:
|
||||
def _dynamic_path_stats_for_group(
|
||||
grouped: pd.DataFrame,
|
||||
side: str,
|
||||
horizon: int,
|
||||
target_bps: float,
|
||||
stop_bps: float,
|
||||
close: np.ndarray,
|
||||
high: np.ndarray,
|
||||
low: np.ndarray,
|
||||
spread: np.ndarray,
|
||||
open_ms: np.ndarray,
|
||||
valid: np.ndarray,
|
||||
future_high: np.ndarray,
|
||||
future_low: np.ndarray,
|
||||
future_spread: np.ndarray,
|
||||
future_realized_vol_bps: np.ndarray,
|
||||
plan_config: dict[str, Any],
|
||||
) -> pd.DataFrame:
|
||||
entry = close[:-horizon]
|
||||
exit_price = close[horizon:]
|
||||
current_open_ms = open_ms[:-horizon]
|
||||
future_close = sliding_window_view(close[1:], horizon)
|
||||
with np.errstate(all="ignore"):
|
||||
high_max = np.nanmax(future_high, axis=1)
|
||||
low_min = np.nanmin(future_low, axis=1)
|
||||
spread_p80 = np.nanquantile(future_spread, 0.8, axis=1)
|
||||
|
||||
take1_ratio = float(plan_config.get("partial_take_1_ratio", 0.50))
|
||||
take2_ratio = float(plan_config.get("partial_take_2_ratio", 0.25))
|
||||
take1_ratio = float(np.clip(take1_ratio, 0.0, 1.0))
|
||||
take2_ratio = float(np.clip(take2_ratio, 0.0, max(0.0, 1.0 - take1_ratio)))
|
||||
target2_bps = float(plan_config.get("second_target_bps", target_bps * 2.0))
|
||||
trailing_stop_bps = float(plan_config.get("trailing_stop_bps", stop_bps))
|
||||
breakeven_after_first = bool(plan_config.get("breakeven_after_first_target", True))
|
||||
|
||||
n = len(entry)
|
||||
active = np.ones(n, dtype=bool)
|
||||
first_target_done = np.zeros(n, dtype=bool)
|
||||
second_target_done = np.zeros(n, dtype=bool)
|
||||
bad_stop_done = np.zeros(n, dtype=bool)
|
||||
trailing_exit_done = np.zeros(n, dtype=bool)
|
||||
remaining = np.ones(n, dtype="float64")
|
||||
gross = np.zeros(n, dtype="float64")
|
||||
first_target_idx = np.full(n, horizon + 1, dtype="int64")
|
||||
stop_idx = np.full(n, horizon + 1, dtype="int64")
|
||||
exit_idx = np.full(n, horizon, dtype="int64")
|
||||
|
||||
if side == "LONG":
|
||||
high_water = entry.copy()
|
||||
for step in range(horizon):
|
||||
h = future_high[:, step]
|
||||
l = future_low[:, step]
|
||||
prior_high_water = high_water.copy()
|
||||
trailing_stop_price = prior_high_water * (1.0 - trailing_stop_bps / 10000.0)
|
||||
if breakeven_after_first:
|
||||
trailing_stop_price = np.maximum(trailing_stop_price, entry)
|
||||
stop_price = np.where(first_target_done, trailing_stop_price, entry * (1.0 - stop_bps / 10000.0))
|
||||
stop_now = active & (l <= stop_price)
|
||||
stop_gross = (stop_price / entry - 1.0) * 10000.0
|
||||
gross = np.where(stop_now, gross + remaining * stop_gross, gross)
|
||||
trailing_exit_done |= stop_now & first_target_done
|
||||
bad_stop_done |= stop_now & (~first_target_done)
|
||||
stop_idx = np.where(stop_now, step, stop_idx)
|
||||
exit_idx = np.where(stop_now, step, exit_idx)
|
||||
remaining = np.where(stop_now, 0.0, remaining)
|
||||
active &= ~stop_now
|
||||
|
||||
first_now = active & (~first_target_done) & (h >= entry * (1.0 + target_bps / 10000.0))
|
||||
gross = np.where(first_now, gross + take1_ratio * target_bps, gross)
|
||||
remaining = np.where(first_now, remaining - take1_ratio, remaining)
|
||||
first_target_done |= first_now
|
||||
first_target_idx = np.where(first_now, step, first_target_idx)
|
||||
|
||||
second_now = active & first_target_done & (~second_target_done) & (h >= entry * (1.0 + target2_bps / 10000.0))
|
||||
gross = np.where(second_now, gross + take2_ratio * target2_bps, gross)
|
||||
remaining = np.where(second_now, remaining - take2_ratio, remaining)
|
||||
second_target_done |= second_now
|
||||
|
||||
high_water = np.maximum(high_water, h)
|
||||
timeout_return = (exit_price / entry - 1.0) * 10000.0
|
||||
future_return_bps = timeout_return
|
||||
mfe_bps = np.maximum((high_max / entry - 1.0) * 10000.0, 0.0)
|
||||
mae_bps = np.maximum((entry / low_min - 1.0) * 10000.0, 0.0)
|
||||
else:
|
||||
low_water = entry.copy()
|
||||
for step in range(horizon):
|
||||
h = future_high[:, step]
|
||||
l = future_low[:, step]
|
||||
prior_low_water = low_water.copy()
|
||||
trailing_stop_price = prior_low_water * (1.0 + trailing_stop_bps / 10000.0)
|
||||
if breakeven_after_first:
|
||||
trailing_stop_price = np.minimum(trailing_stop_price, entry)
|
||||
stop_price = np.where(first_target_done, trailing_stop_price, entry * (1.0 + stop_bps / 10000.0))
|
||||
stop_now = active & (h >= stop_price)
|
||||
stop_gross = (entry / stop_price - 1.0) * 10000.0
|
||||
gross = np.where(stop_now, gross + remaining * stop_gross, gross)
|
||||
trailing_exit_done |= stop_now & first_target_done
|
||||
bad_stop_done |= stop_now & (~first_target_done)
|
||||
stop_idx = np.where(stop_now, step, stop_idx)
|
||||
exit_idx = np.where(stop_now, step, exit_idx)
|
||||
remaining = np.where(stop_now, 0.0, remaining)
|
||||
active &= ~stop_now
|
||||
|
||||
first_now = active & (~first_target_done) & (l <= entry * (1.0 - target_bps / 10000.0))
|
||||
gross = np.where(first_now, gross + take1_ratio * target_bps, gross)
|
||||
remaining = np.where(first_now, remaining - take1_ratio, remaining)
|
||||
first_target_done |= first_now
|
||||
first_target_idx = np.where(first_now, step, first_target_idx)
|
||||
|
||||
second_now = active & first_target_done & (~second_target_done) & (l <= entry * (1.0 - target2_bps / 10000.0))
|
||||
gross = np.where(second_now, gross + take2_ratio * target2_bps, gross)
|
||||
remaining = np.where(second_now, remaining - take2_ratio, remaining)
|
||||
second_target_done |= second_now
|
||||
|
||||
low_water = np.minimum(low_water, l)
|
||||
timeout_return = (entry / exit_price - 1.0) * 10000.0
|
||||
future_return_bps = timeout_return
|
||||
mfe_bps = np.maximum((entry / low_min - 1.0) * 10000.0, 0.0)
|
||||
mae_bps = np.maximum((high_max / entry - 1.0) * 10000.0, 0.0)
|
||||
|
||||
timeout_now = active
|
||||
gross = np.where(timeout_now, gross + remaining * timeout_return, gross)
|
||||
exit_idx = np.where(timeout_now, horizon - 1, exit_idx)
|
||||
target_hit = first_target_done
|
||||
stop_hit = bad_stop_done
|
||||
timeout_hit = timeout_now
|
||||
ambiguous_hit = np.zeros(n, dtype=bool)
|
||||
out = pd.DataFrame(
|
||||
{
|
||||
"symbol": grouped["symbol"].iloc[0],
|
||||
"open_time_ms": current_open_ms,
|
||||
"side": side,
|
||||
"target_hit": target_hit.astype("int8"),
|
||||
"stop_hit": stop_hit.astype("int8"),
|
||||
"timeout_hit": timeout_hit.astype("int8"),
|
||||
"ambiguous_hit": ambiguous_hit.astype("int8"),
|
||||
"time_to_target_ms": np.where(target_hit, (first_target_idx + 1) * 60_000, -1).astype("int64"),
|
||||
"time_to_stop_ms": np.where(stop_hit | trailing_exit_done, (stop_idx + 1) * 60_000, -1).astype("int64"),
|
||||
"time_to_exit_ms": ((exit_idx + 1) * 60_000).astype("int64"),
|
||||
"gross_edge_bps": gross.astype("float64"),
|
||||
"future_return_bps": future_return_bps.astype("float64"),
|
||||
"mfe_bps": mfe_bps.astype("float64"),
|
||||
"mae_bps": mae_bps.astype("float64"),
|
||||
"future_spread_p80": spread_p80.astype("float64"),
|
||||
"future_realized_vol_bps": future_realized_vol_bps.astype("float64"),
|
||||
}
|
||||
)
|
||||
return out.loc[valid, PATH_STAT_COLUMNS].reset_index(drop=True)
|
||||
|
||||
|
||||
def _build_path_stats(replay: pd.DataFrame, horizon: int, target_bps: float, stop_bps: float, plan_config: dict[str, Any] | None = None) -> pd.DataFrame:
|
||||
frames: list[pd.DataFrame] = []
|
||||
for symbol, group in replay.groupby("symbol", sort=False, observed=False):
|
||||
logging.info(
|
||||
"trader.training.path_stats_group_start symbol=%s horizonMinutes=%s rowCount=%s",
|
||||
"trader.training.path_stats_group_start symbol=%s horizonMinutes=%s planMethod=%s rowCount=%s",
|
||||
symbol,
|
||||
horizon,
|
||||
(plan_config or {}).get("plan_method", "FIXED_TARGET_STOP_V1"),
|
||||
len(group),
|
||||
)
|
||||
for side in ("LONG", "SHORT"):
|
||||
stats = _path_stats_for_group(group, side, horizon, target_bps, stop_bps)
|
||||
stats = _path_stats_for_group(group, side, horizon, target_bps, stop_bps, plan_config=plan_config)
|
||||
frames.append(stats)
|
||||
logging.info(
|
||||
"trader.training.path_stats_side_done symbol=%s side=%s horizonMinutes=%s rowCount=%s",
|
||||
"trader.training.path_stats_side_done symbol=%s side=%s horizonMinutes=%s planMethod=%s rowCount=%s",
|
||||
symbol,
|
||||
side,
|
||||
horizon,
|
||||
(plan_config or {}).get("plan_method", "FIXED_TARGET_STOP_V1"),
|
||||
len(stats),
|
||||
)
|
||||
out = pd.concat(frames, ignore_index=True) if frames else _empty_path_stats_frame()
|
||||
logging.info("trader.training.path_stats_built horizonMinutes=%s rowCount=%s", horizon, len(out))
|
||||
logging.info(
|
||||
"trader.training.path_stats_built horizonMinutes=%s planMethod=%s rowCount=%s",
|
||||
horizon,
|
||||
(plan_config or {}).get("plan_method", "FIXED_TARGET_STOP_V1"),
|
||||
len(out),
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
@@ -222,8 +443,17 @@ def write_price_plan_context(args: Any) -> None:
|
||||
"targetDistanceBps": float(entry["target_bps"]),
|
||||
"maxHoldMinutes": int(entry["max_hold_minutes"]),
|
||||
"minExpectedNetEdgeBps": float(entry["min_expected_net_edge_bps"]),
|
||||
"minPlanNetEdgeBps": float(entry.get("min_plan_net_edge_bps", 0.0)),
|
||||
"maxEntryMaeBps": float(entry.get("max_entry_mae_bps", entry["stop_bps"])),
|
||||
"costBps": cost_bps,
|
||||
"entryLabelMethod": ENTRY_LABEL_METHOD,
|
||||
"entryTargetMethod": str(entry.get("target_method", ENTRY_LABEL_METHOD)),
|
||||
"entryPlanMethod": str(entry.get("plan_method", "FIXED_TARGET_STOP_V1")),
|
||||
"partialTake1Ratio": float(entry.get("partial_take_1_ratio", 0.50)),
|
||||
"partialTake2Ratio": float(entry.get("partial_take_2_ratio", 0.25)),
|
||||
"secondTargetBps": float(entry.get("second_target_bps", float(entry["target_bps"]) * 2.0)),
|
||||
"trailingStopBps": float(entry.get("trailing_stop_bps", float(entry["stop_bps"]))),
|
||||
"breakevenAfterFirstTarget": bool(entry.get("breakeven_after_first_target", True)),
|
||||
}
|
||||
path = root / "label" / "price_plan_context.json"
|
||||
write_json(path, context)
|
||||
@@ -234,8 +464,17 @@ def write_price_plan_context(args: Any) -> None:
|
||||
"stop_bps": context["stopDistanceBps"],
|
||||
"max_hold_minutes": context["maxHoldMinutes"],
|
||||
"min_expected_net_edge_bps": context["minExpectedNetEdgeBps"],
|
||||
"min_plan_net_edge_bps": context["minPlanNetEdgeBps"],
|
||||
"max_entry_mae_bps": context["maxEntryMaeBps"],
|
||||
"cost_bps": context["costBps"],
|
||||
"entry_label_method": context["entryLabelMethod"],
|
||||
"entry_target_method": context["entryTargetMethod"],
|
||||
"entry_plan_method": context["entryPlanMethod"],
|
||||
"partial_take_1_ratio": context["partialTake1Ratio"],
|
||||
"partial_take_2_ratio": context["partialTake2Ratio"],
|
||||
"second_target_bps": context["secondTargetBps"],
|
||||
"trailing_stop_bps": context["trailingStopBps"],
|
||||
"breakeven_after_first_target": context["breakevenAfterFirstTarget"],
|
||||
}])
|
||||
write_parquet(root / "label" / "price_plan_context.parquet", frame)
|
||||
logging.info("trader.training.price_plan_written runId=%s path=%s", args.run_id, path)
|
||||
@@ -295,6 +534,7 @@ def build_entry_labels(args: Any) -> None:
|
||||
int(entry_conf["max_hold_minutes"]),
|
||||
float(entry_conf["target_bps"]),
|
||||
float(entry_conf["stop_bps"]),
|
||||
plan_config=entry_conf,
|
||||
)
|
||||
feature_columns = [
|
||||
"sample_id",
|
||||
@@ -308,14 +548,28 @@ def build_entry_labels(args: Any) -> None:
|
||||
"realized_vol_15m_bps",
|
||||
]
|
||||
merged = features[feature_columns].merge(stats, on=["symbol", "open_time_ms"], how="inner")
|
||||
merged["actual_plan_net_edge_bps"] = merged["gross_edge_bps"] - cost_bps
|
||||
merged["max_achievable_gross_edge_bps"] = merged["mfe_bps"]
|
||||
merged["max_achievable_net_edge_bps"] = merged["max_achievable_gross_edge_bps"] - cost_bps
|
||||
merged["expected_net_edge_bps"] = merged["max_achievable_net_edge_bps"]
|
||||
merged["entry_target"] = (merged["max_achievable_net_edge_bps"] >= float(entry_conf["min_expected_net_edge_bps"])).astype("int8")
|
||||
target_method = str(entry_conf.get("target_method", ENTRY_LABEL_METHOD))
|
||||
if target_method == "PRICE_PLAN_OUTCOME_V1":
|
||||
merged["expected_net_edge_bps"] = merged["actual_plan_net_edge_bps"]
|
||||
elif target_method in {"OPPORTUNITY_MFE_V1", "OPPORTUNITY_QUALITY_V1"}:
|
||||
merged["expected_net_edge_bps"] = merged["max_achievable_net_edge_bps"]
|
||||
else:
|
||||
raise ValueError(f"unsupported entry target_method: {target_method}")
|
||||
opportunity = merged["expected_net_edge_bps"] >= float(entry_conf["min_expected_net_edge_bps"])
|
||||
if target_method == "OPPORTUNITY_QUALITY_V1":
|
||||
# MFE 只说明价格曾经给过机会;真实开仓还要确认这笔机会按计划能拿走,
|
||||
# 并且过程中没有先承受过大的反向波动。
|
||||
min_plan_net_edge_bps = float(entry_conf.get("min_plan_net_edge_bps", 0.0))
|
||||
max_entry_mae_bps = float(entry_conf.get("max_entry_mae_bps", entry_conf["stop_bps"]))
|
||||
opportunity = opportunity & (merged["actual_plan_net_edge_bps"] >= min_plan_net_edge_bps) & (merged["mae_bps"] <= max_entry_mae_bps)
|
||||
merged["entry_target"] = opportunity.astype("int8")
|
||||
merged["price_plan_id"] = plan["pricePlanId"]
|
||||
merged["price_plan_hash"] = plan["pricePlanConfigHash"]
|
||||
merged["cost_bps"] = cost_bps
|
||||
merged["label_method"] = ENTRY_LABEL_METHOD
|
||||
merged["label_method"] = target_method
|
||||
merged["label_version"] = LABEL_VERSION
|
||||
out = merged[
|
||||
[
|
||||
@@ -331,10 +585,12 @@ def build_entry_labels(args: Any) -> None:
|
||||
"ambiguous_hit",
|
||||
"time_to_target_ms",
|
||||
"time_to_stop_ms",
|
||||
"time_to_exit_ms",
|
||||
"gross_edge_bps",
|
||||
"future_return_bps",
|
||||
"mfe_bps",
|
||||
"mae_bps",
|
||||
"actual_plan_net_edge_bps",
|
||||
"max_achievable_gross_edge_bps",
|
||||
"max_achievable_net_edge_bps",
|
||||
"cost_bps",
|
||||
@@ -380,7 +636,18 @@ def build_continue_exit_risk_labels(args: Any) -> None:
|
||||
horizon = int(labels["continue"]["horizon_minutes"])
|
||||
target_bps = float(plan["targetDistanceBps"])
|
||||
stop_bps = float(plan["stopDistanceBps"])
|
||||
stats = _build_path_stats(replay, horizon, target_bps, stop_bps)
|
||||
plan_config = {
|
||||
"plan_method": plan.get("entryPlanMethod", labels["entry"].get("plan_method", "FIXED_TARGET_STOP_V1")),
|
||||
"partial_take_1_ratio": plan.get("partialTake1Ratio", labels["entry"].get("partial_take_1_ratio", 0.50)),
|
||||
"partial_take_2_ratio": plan.get("partialTake2Ratio", labels["entry"].get("partial_take_2_ratio", 0.25)),
|
||||
"second_target_bps": plan.get("secondTargetBps", labels["entry"].get("second_target_bps", target_bps * 2.0)),
|
||||
"trailing_stop_bps": plan.get("trailingStopBps", labels["entry"].get("trailing_stop_bps", stop_bps)),
|
||||
"breakeven_after_first_target": plan.get(
|
||||
"breakevenAfterFirstTarget",
|
||||
labels["entry"].get("breakeven_after_first_target", True),
|
||||
),
|
||||
}
|
||||
stats = _build_path_stats(replay, horizon, target_bps, stop_bps, plan_config=plan_config)
|
||||
long_stats = stats[stats["side"] == "LONG"].drop(columns=["side"]).add_prefix("long_")
|
||||
short_stats = stats[stats["side"] == "SHORT"].drop(columns=["side"]).add_prefix("short_")
|
||||
long_stats = long_stats.rename(columns={"long_symbol": "symbol", "long_open_time_ms": "open_time_ms"})
|
||||
@@ -403,9 +670,23 @@ def build_continue_exit_risk_labels(args: Any) -> None:
|
||||
min_continue = float(labels["continue"]["min_expected_continue_edge_bps"])
|
||||
adverse_threshold = float(labels["exit"]["adverse_move_bps"])
|
||||
current_vol = merged["realized_vol_15m_bps"].astype(float).fillna(0.0).clip(lower=1.0)
|
||||
risk_config = labels["risk"]
|
||||
market_risk_threshold = _config_number(
|
||||
risk_config,
|
||||
("market_path_risk_threshold_bps", "market_drawdown_bps"),
|
||||
60.0,
|
||||
)
|
||||
position_risk_threshold = _config_number(
|
||||
risk_config,
|
||||
("position_path_risk_threshold_bps", "position_path_risk_bps"),
|
||||
35.0,
|
||||
)
|
||||
spike_threshold = _config_number(risk_config, ("spike_1m_threshold_bps", "spike_bps"), 80.0)
|
||||
vol_expansion_ratio = _config_number(risk_config, ("vol_expansion_ratio",), 1.8)
|
||||
|
||||
long_edge = merged["long_future_return_bps"] - cost_bps
|
||||
short_edge = merged["short_future_return_bps"] - cost_bps
|
||||
long_edge = merged["long_gross_edge_bps"] - cost_bps
|
||||
short_edge = merged["short_gross_edge_bps"] - cost_bps
|
||||
dynamic_plan = str(plan_config.get("plan_method")) == "DYNAMIC_TRAILING_V1"
|
||||
path_risk = np.maximum(merged["long_mae_bps"], merged["short_mae_bps"])
|
||||
max_path_move = np.maximum.reduce([merged["long_mfe_bps"], merged["short_mfe_bps"], path_risk])
|
||||
if "ret_15m_bps" in merged.columns:
|
||||
@@ -413,7 +694,9 @@ def build_continue_exit_risk_labels(args: Any) -> None:
|
||||
else:
|
||||
reversal = pd.Series(0, index=merged.index, dtype="int8")
|
||||
future_vol = merged["long_future_realized_vol_bps"].fillna(0.0)
|
||||
volatility_expansion = future_vol >= current_vol * float(labels["risk"]["vol_expansion_ratio"])
|
||||
volatility_expansion = future_vol >= current_vol * vol_expansion_ratio
|
||||
spike = max_path_move >= spike_threshold
|
||||
market_risk = (path_risk >= market_risk_threshold) | spike | volatility_expansion
|
||||
liquidity_deterioration = merged["spread_rank_24h_pct"].astype(float).fillna(0.0) >= 0.90
|
||||
|
||||
rows_continue = pd.DataFrame(
|
||||
@@ -421,8 +704,8 @@ def build_continue_exit_risk_labels(args: Any) -> None:
|
||||
"sample_id": merged["sample_id"],
|
||||
"symbol": merged["symbol"],
|
||||
"event_time": merged["event_time"],
|
||||
"long_continue_target": ((long_edge >= min_continue) & (merged["long_mae_bps"] < stop_bps)).astype("int8"),
|
||||
"short_continue_target": ((short_edge >= min_continue) & (merged["short_mae_bps"] < stop_bps)).astype("int8"),
|
||||
"long_continue_target": ((long_edge >= min_continue) & ((merged["long_stop_hit"] == 0) | dynamic_plan)).astype("int8"),
|
||||
"short_continue_target": ((short_edge >= min_continue) & ((merged["short_stop_hit"] == 0) | dynamic_plan)).astype("int8"),
|
||||
"long_expected_continue_edge_bps": long_edge,
|
||||
"short_expected_continue_edge_bps": short_edge,
|
||||
"split_id": merged["split_id"],
|
||||
@@ -453,17 +736,17 @@ def build_continue_exit_risk_labels(args: Any) -> None:
|
||||
"sample_id": merged["sample_id"],
|
||||
"symbol": merged["symbol"],
|
||||
"event_time": merged["event_time"],
|
||||
"market_risk_target": (path_risk >= float(labels["risk"]["market_drawdown_bps"])).astype("int8"),
|
||||
"market_risk_target": market_risk.astype("int8"),
|
||||
"market_path_risk_bps": path_risk,
|
||||
"long_position_path_risk_bps": merged["long_mae_bps"],
|
||||
"short_position_path_risk_bps": merged["short_mae_bps"],
|
||||
"long_position_risk_target": (merged["long_mae_bps"] >= stop_bps).astype("int8"),
|
||||
"short_position_risk_target": (merged["short_mae_bps"] >= stop_bps).astype("int8"),
|
||||
"market_drawdown_prob_label": (path_risk >= float(labels["risk"]["market_drawdown_bps"])).astype("int8"),
|
||||
"long_position_risk_target": ((merged["long_mae_bps"] >= position_risk_threshold) | (merged["long_stop_hit"] == 1)).astype("int8"),
|
||||
"short_position_risk_target": ((merged["short_mae_bps"] >= position_risk_threshold) | (merged["short_stop_hit"] == 1)).astype("int8"),
|
||||
"market_drawdown_prob_label": (path_risk >= market_risk_threshold).astype("int8"),
|
||||
"volatility_expansion_prob_label": volatility_expansion.astype("int8"),
|
||||
"spike_prob_label": (max_path_move >= float(labels["risk"]["spike_bps"])).astype("int8"),
|
||||
"spike_prob_label": spike.astype("int8"),
|
||||
"liquidity_deterioration_prob_label": liquidity_deterioration.astype("int8"),
|
||||
"position_drawdown_prob_label": (path_risk >= stop_bps).astype("int8"),
|
||||
"position_drawdown_prob_label": (path_risk >= position_risk_threshold).astype("int8"),
|
||||
"split_id": merged["split_id"],
|
||||
"walk_forward_fold": merged["walk_forward_fold"],
|
||||
"label_version": LABEL_VERSION,
|
||||
@@ -475,6 +758,21 @@ def build_continue_exit_risk_labels(args: Any) -> None:
|
||||
("risk", pd.DataFrame(rows_risk), "market_risk_target"),
|
||||
]
|
||||
report_parts = ["# Continue Exit Risk Label Report", ""]
|
||||
report_parts.extend(
|
||||
[
|
||||
"## Risk Thresholds",
|
||||
"",
|
||||
str(
|
||||
{
|
||||
"market_risk_threshold_bps": market_risk_threshold,
|
||||
"position_risk_threshold_bps": position_risk_threshold,
|
||||
"spike_threshold_bps": spike_threshold,
|
||||
"vol_expansion_ratio": vol_expansion_ratio,
|
||||
}
|
||||
),
|
||||
"",
|
||||
]
|
||||
)
|
||||
for name, frame, target in outputs:
|
||||
path = root / "label" / f"{name}_labels.parquet"
|
||||
data_hash = write_parquet(path, frame)
|
||||
|
||||
@@ -0,0 +1,890 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.linear_model import HuberRegressor, LogisticRegression
|
||||
from sklearn.metrics import accuracy_score, brier_score_loss, log_loss, mean_absolute_error, roc_auc_score
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
from trader_training.io_utils import (
|
||||
DEFAULT_RAW_ROOT,
|
||||
read_json,
|
||||
read_parquet,
|
||||
run_root,
|
||||
sha256_json,
|
||||
to_utc_series,
|
||||
write_json,
|
||||
write_parquet,
|
||||
write_text,
|
||||
)
|
||||
from trader_training.schemas import FEATURE_ORDER, FIT_SPLIT, LATEST_STRESS_SPLIT, TUNE_SPLIT, VALIDATION_LOCKED_SPLIT
|
||||
|
||||
|
||||
OFI_METHOD = "SNAPSHOT_DIFF_V1"
|
||||
BOOK_OFI_FEATURES = [
|
||||
"ofi_l1_1m",
|
||||
"ofi_l1_3m",
|
||||
"ofi_l1_5m",
|
||||
"ofi_l1_15m",
|
||||
"mlofi_l5_1m",
|
||||
"mlofi_l5_5m",
|
||||
"mlofi_l20_1m",
|
||||
"mlofi_l20_5m",
|
||||
"mlofi_l5_l20_gap_1m",
|
||||
"microprice_basis_change_1m_bps",
|
||||
"microprice_basis_change_5m_bps",
|
||||
"ofi_l1_5m_zscore_240m",
|
||||
"mlofi_l20_5m_zscore_240m",
|
||||
]
|
||||
CROSS_OFI_FEATURES = [
|
||||
"ofi_l1_5m_clipped",
|
||||
"ofi_l1_taker_5m",
|
||||
"ofi_l1_spread_rank_5m",
|
||||
]
|
||||
OFI_FEATURES = [*BOOK_OFI_FEATURES, *CROSS_OFI_FEATURES]
|
||||
|
||||
META_COLUMNS = [
|
||||
"sample_id",
|
||||
"symbol",
|
||||
"event_time",
|
||||
"open_time_ms",
|
||||
"split_id",
|
||||
"walk_forward_fold",
|
||||
"data_quality_flag",
|
||||
]
|
||||
ALL_SPLITS = (FIT_SPLIT, TUNE_SPLIT, VALIDATION_LOCKED_SPLIT, LATEST_STRESS_SPLIT)
|
||||
EVAL_SPLITS = (TUNE_SPLIT, VALIDATION_LOCKED_SPLIT, LATEST_STRESS_SPLIT)
|
||||
|
||||
|
||||
def run_ofi_feature_experiment(args: Any) -> None:
|
||||
root = run_root(args)
|
||||
baseline_root = args.data_root / "trader-v4" / "runs" / args.baseline_run_id
|
||||
out_dir = root / "experiments" / "ofi_b2_mlofi"
|
||||
raw_root = Path(args.raw_root or DEFAULT_RAW_ROOT)
|
||||
logging.info(
|
||||
"trader.training.ofi_experiment_started runId=%s baselineRunId=%s rawRoot=%s",
|
||||
args.run_id,
|
||||
args.baseline_run_id,
|
||||
raw_root,
|
||||
)
|
||||
|
||||
feature = _load_baseline_feature_frame(baseline_root)
|
||||
ofi_delta = build_snapshot_diff_l1_ofi_features(raw_root, feature[["symbol", "event_time", "open_time_ms"]])
|
||||
dataset = _merge_feature_delta(feature, ofi_delta)
|
||||
if args.max_rows_per_split:
|
||||
dataset = _cap_rows_per_split(dataset, int(args.max_rows_per_split))
|
||||
delta_hash = write_parquet(out_dir / "ofi_feature_delta.parquet", ofi_delta)
|
||||
dataset_hash = write_parquet(out_dir / "ofi_experiment_feature_frame.parquet", dataset)
|
||||
write_json(out_dir / "ofi_feature_order.json", OFI_FEATURES)
|
||||
write_json(out_dir / "ofi_feature_schema.json", _ofi_feature_schema())
|
||||
write_json(out_dir / "experiment_manifest.json", _experiment_manifest(args, baseline_root, raw_root, ofi_delta, dataset, delta_hash, dataset_hash))
|
||||
write_text(out_dir / "feature_delta_report.md", _feature_delta_report(ofi_delta, dataset))
|
||||
|
||||
direction = _load_direction_dataset(baseline_root, dataset)
|
||||
entry = _load_entry_dataset(baseline_root, dataset)
|
||||
results: dict[str, Any] = {}
|
||||
prediction_frames: list[pd.DataFrame] = []
|
||||
for feature_set_name, columns in _feature_sets().items():
|
||||
direction_result, direction_predictions = _train_direction(direction, columns)
|
||||
entry_result, entry_predictions = _train_entry(entry, columns)
|
||||
results[feature_set_name] = {"DIRECTION": direction_result, "ENTRY": entry_result}
|
||||
direction_predictions["model"] = "DIRECTION"
|
||||
direction_predictions["feature_set"] = feature_set_name
|
||||
entry_predictions["model"] = "ENTRY"
|
||||
entry_predictions["feature_set"] = feature_set_name
|
||||
prediction_frames.extend([direction_predictions, entry_predictions])
|
||||
logging.info(
|
||||
"trader.training.ofi_feature_set_trained runId=%s featureSet=%s directionTuneShortAuc=%s entryTuneShortAuc=%s",
|
||||
args.run_id,
|
||||
feature_set_name,
|
||||
direction_result.get(TUNE_SPLIT, {}).get("short_auc"),
|
||||
entry_result.get("short_entry_prob", {}).get(TUNE_SPLIT, {}).get("auc"),
|
||||
)
|
||||
|
||||
predictions = pd.concat(prediction_frames, ignore_index=True) if prediction_frames else pd.DataFrame()
|
||||
write_parquet(out_dir / "direction_entry_predictions.parquet", predictions)
|
||||
write_json(out_dir / "ofi_experiment_result.json", results)
|
||||
write_text(out_dir / "model_compare_to_run10.md", _model_compare_report(args, baseline_root, results, dataset))
|
||||
write_text(out_dir / "backtest_compare_to_run10.md", _backtest_placeholder_report(args, baseline_root))
|
||||
write_text(out_dir / "contract_change_report.md", _contract_change_report())
|
||||
write_text(out_dir / "failure_cases_compare.md", _failure_case_placeholder_report(args))
|
||||
logging.info("trader.training.ofi_experiment_finished runId=%s report=%s", args.run_id, out_dir / "model_compare_to_run10.md")
|
||||
|
||||
|
||||
def _load_baseline_feature_frame(baseline_root: Path) -> pd.DataFrame:
|
||||
frame = read_parquet(baseline_root / "feature" / "feature_frame.parquet")
|
||||
required = set(META_COLUMNS + FEATURE_ORDER)
|
||||
missing = sorted(required.difference(frame.columns))
|
||||
if missing:
|
||||
raise ValueError(f"baseline feature frame missing columns: {missing}")
|
||||
frame = frame[frame["data_quality_flag"].isin(["OK", "PARTIAL_OPTIONAL"])].copy()
|
||||
frame = frame[frame["split_id"].isin(ALL_SPLITS)].copy()
|
||||
frame["event_time"] = to_utc_series(frame["event_time"])
|
||||
logging.info("trader.training.ofi_baseline_feature_loaded rowCount=%s splitCounts=%s", len(frame), frame["split_id"].value_counts().to_dict())
|
||||
return frame
|
||||
|
||||
|
||||
def build_snapshot_diff_l1_ofi_features(raw_root: Path, replay_keys: pd.DataFrame) -> pd.DataFrame:
|
||||
if replay_keys.empty:
|
||||
return pd.DataFrame(columns=["symbol", "open_time_ms", *BOOK_OFI_FEATURES])
|
||||
keys = replay_keys.copy()
|
||||
keys["event_time"] = to_utc_series(keys["event_time"])
|
||||
keys["event_date"] = keys["event_time"].dt.strftime("%Y-%m-%d")
|
||||
frames: list[pd.DataFrame] = []
|
||||
for (symbol, event_date), _ in keys.groupby(["symbol", "event_date"], sort=True, observed=False):
|
||||
path = raw_root / "table=book" / "exchange=BINANCE_FUTURES" / f"symbol={symbol}" / f"dt={event_date}" / "data.parquet"
|
||||
if not path.is_file():
|
||||
logging.warning("trader.training.ofi_book_partition_missing symbol=%s eventDate=%s path=%s", symbol, event_date, path)
|
||||
continue
|
||||
day = _read_l1_book_day(path, symbol)
|
||||
if not day.empty:
|
||||
frames.append(day)
|
||||
logging.info("trader.training.ofi_book_partition_loaded symbol=%s eventDate=%s minuteRows=%s path=%s", symbol, event_date, len(day), path)
|
||||
if not frames:
|
||||
raise ValueError(f"no book partitions loaded from {raw_root}")
|
||||
minute_book = pd.concat(frames, ignore_index=True).sort_values(["symbol", "open_time_ms"])
|
||||
minute_book = minute_book.drop_duplicates(["symbol", "open_time_ms"], keep="last").reset_index(drop=True)
|
||||
feature_frames = []
|
||||
for symbol, group in minute_book.groupby("symbol", sort=False, observed=False):
|
||||
feature_frames.append(_compute_l1_ofi_for_symbol(group, str(symbol)))
|
||||
features = pd.concat(feature_frames, ignore_index=True) if feature_frames else pd.DataFrame(columns=["symbol", "open_time_ms", *BOOK_OFI_FEATURES])
|
||||
wanted = keys[["symbol", "open_time_ms"]].drop_duplicates()
|
||||
out = wanted.merge(features, on=["symbol", "open_time_ms"], how="left")
|
||||
logging.info(
|
||||
"trader.training.ofi_features_built wantedRows=%s matchedRows=%s featureRows=%s",
|
||||
len(wanted),
|
||||
int(out[BOOK_OFI_FEATURES].notna().all(axis=1).sum()),
|
||||
len(features),
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
def _read_l1_book_day(path: Path, symbol: str) -> pd.DataFrame:
|
||||
columns = ["origin_time"]
|
||||
for side in ("bid", "ask"):
|
||||
for level in range(20):
|
||||
columns.extend([f"{side}_{level}_price", f"{side}_{level}_size"])
|
||||
book = pd.read_parquet(path, columns=columns)
|
||||
if book.empty:
|
||||
return pd.DataFrame(columns=["symbol", "open_time_ms", *columns[1:]])
|
||||
required = ["origin_time", "bid_0_price", "bid_0_size", "ask_0_price", "ask_0_size"]
|
||||
book = book.dropna(subset=required).copy()
|
||||
book["origin_time"] = to_utc_series(book["origin_time"])
|
||||
book["minute"] = book["origin_time"].dt.floor("min")
|
||||
book = book.sort_values("origin_time").drop_duplicates("minute", keep="last")
|
||||
if book.empty:
|
||||
return pd.DataFrame(columns=["symbol", "open_time_ms", *columns[1:]])
|
||||
out = pd.DataFrame({"symbol": symbol, "open_time_ms": (book["minute"].astype("int64") // 1_000_000).astype("int64")})
|
||||
for column in columns[1:]:
|
||||
out[column] = pd.to_numeric(book[column], errors="coerce").astype("float64")
|
||||
return out
|
||||
|
||||
|
||||
def _compute_l1_ofi_for_symbol(book: pd.DataFrame, symbol: str) -> pd.DataFrame:
|
||||
group = book.sort_values("open_time_ms").reset_index(drop=True).copy()
|
||||
gap = group["open_time_ms"].astype("int64").diff().ne(60_000)
|
||||
segment = gap.cumsum()
|
||||
level_ofi = []
|
||||
for level in range(20):
|
||||
prev_bid_price = group.groupby(segment, sort=False)[f"bid_{level}_price"].shift(1)
|
||||
prev_bid_size = group.groupby(segment, sort=False)[f"bid_{level}_size"].shift(1)
|
||||
prev_ask_price = group.groupby(segment, sort=False)[f"ask_{level}_price"].shift(1)
|
||||
prev_ask_size = group.groupby(segment, sort=False)[f"ask_{level}_size"].shift(1)
|
||||
bid_part, ask_part = l1_snapshot_diff_ofi_quote(
|
||||
group[f"bid_{level}_price"],
|
||||
group[f"bid_{level}_size"],
|
||||
group[f"ask_{level}_price"],
|
||||
group[f"ask_{level}_size"],
|
||||
prev_bid_price,
|
||||
prev_bid_size,
|
||||
prev_ask_price,
|
||||
prev_ask_size,
|
||||
)
|
||||
level_ofi.append((bid_part + ask_part).rename(f"ofi_level_{level}_quote"))
|
||||
level_frame = pd.concat(level_ofi, axis=1)
|
||||
group["ofi_l1_event_quote"] = level_frame["ofi_level_0_quote"]
|
||||
group["mlofi_l5_event_quote"] = level_frame[[f"ofi_level_{level}_quote" for level in range(5)]].sum(axis=1, min_count=5)
|
||||
group["mlofi_l20_event_quote"] = level_frame.sum(axis=1, min_count=20)
|
||||
group["l1_depth_quote"] = _depth_quote(group, 1)
|
||||
group["l5_depth_quote"] = _depth_quote(group, 5)
|
||||
group["l20_depth_quote"] = _depth_quote(group, 20)
|
||||
for window in (1, 3, 5, 15):
|
||||
group[f"ofi_l1_{window}m"] = _rolling_normalized(group, segment, "ofi_l1_event_quote", "l1_depth_quote", window)
|
||||
for window in (1, 5):
|
||||
group[f"mlofi_l5_{window}m"] = _rolling_normalized(group, segment, "mlofi_l5_event_quote", "l5_depth_quote", window)
|
||||
group[f"mlofi_l20_{window}m"] = _rolling_normalized(group, segment, "mlofi_l20_event_quote", "l20_depth_quote", window)
|
||||
group["mlofi_l5_l20_gap_1m"] = group["mlofi_l5_1m"] - group["mlofi_l20_1m"]
|
||||
mid = (group["bid_0_price"] + group["ask_0_price"]) / 2.0
|
||||
microprice = (group["ask_0_price"] * group["bid_0_size"] + group["bid_0_price"] * group["ask_0_size"]) / (group["bid_0_size"] + group["ask_0_size"]).clip(lower=1e-12)
|
||||
group["microprice_basis_bps"] = (microprice / mid - 1.0) * 10000.0
|
||||
group["microprice_basis_change_1m_bps"] = group.groupby(segment, sort=False)["microprice_basis_bps"].diff(1)
|
||||
group["microprice_basis_change_5m_bps"] = group.groupby(segment, sort=False)["microprice_basis_bps"].diff(5)
|
||||
group["ofi_l1_5m_zscore_240m"] = _rolling_zscore(group, segment, "ofi_l1_5m", 240)
|
||||
group["mlofi_l20_5m_zscore_240m"] = _rolling_zscore(group, segment, "mlofi_l20_5m", 240)
|
||||
out = group[["symbol", "open_time_ms", *BOOK_OFI_FEATURES]].replace([np.inf, -np.inf], np.nan)
|
||||
for column in BOOK_OFI_FEATURES:
|
||||
out[column] = pd.to_numeric(out[column], errors="coerce").astype("float32")
|
||||
logging.info(
|
||||
"trader.training.ofi_symbol_features_built symbol=%s minuteRows=%s fullFeatureRows=%s",
|
||||
symbol,
|
||||
len(out),
|
||||
int(out[BOOK_OFI_FEATURES].notna().all(axis=1).sum()),
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
def _depth_quote(group: pd.DataFrame, level_count: int) -> pd.Series:
|
||||
total = pd.Series(0.0, index=group.index, dtype="float64")
|
||||
for level in range(level_count):
|
||||
total = total + group[f"bid_{level}_price"] * group[f"bid_{level}_size"] + group[f"ask_{level}_price"] * group[f"ask_{level}_size"]
|
||||
return total
|
||||
|
||||
|
||||
def _rolling_normalized(group: pd.DataFrame, segment: pd.Series, event_column: str, depth_column: str, window: int) -> pd.Series:
|
||||
summed = group.groupby(segment, sort=False)[event_column].rolling(window, min_periods=window).sum().reset_index(level=0, drop=True)
|
||||
averaged_depth = group.groupby(segment, sort=False)[depth_column].rolling(window, min_periods=window).mean().reset_index(level=0, drop=True)
|
||||
return summed / averaged_depth.clip(lower=1e-12)
|
||||
|
||||
|
||||
def _rolling_zscore(group: pd.DataFrame, segment: pd.Series, column: str, window: int) -> pd.Series:
|
||||
rolling = group.groupby(segment, sort=False)[column].rolling(window, min_periods=window)
|
||||
mean = rolling.mean().reset_index(level=0, drop=True)
|
||||
std = rolling.std().reset_index(level=0, drop=True).replace(0, np.nan)
|
||||
return (group[column] - mean) / std
|
||||
|
||||
|
||||
def l1_snapshot_diff_ofi_quote(
|
||||
bid_price: pd.Series,
|
||||
bid_size: pd.Series,
|
||||
ask_price: pd.Series,
|
||||
ask_size: pd.Series,
|
||||
prev_bid_price: pd.Series,
|
||||
prev_bid_size: pd.Series,
|
||||
prev_ask_price: pd.Series,
|
||||
prev_ask_size: pd.Series,
|
||||
) -> tuple[pd.Series, pd.Series]:
|
||||
bid = pd.to_numeric(bid_price, errors="coerce")
|
||||
bid_sz = pd.to_numeric(bid_size, errors="coerce")
|
||||
ask = pd.to_numeric(ask_price, errors="coerce")
|
||||
ask_sz = pd.to_numeric(ask_size, errors="coerce")
|
||||
prev_bid = pd.to_numeric(prev_bid_price, errors="coerce")
|
||||
prev_bid_sz = pd.to_numeric(prev_bid_size, errors="coerce")
|
||||
prev_ask = pd.to_numeric(prev_ask_price, errors="coerce")
|
||||
prev_ask_sz = pd.to_numeric(prev_ask_size, errors="coerce")
|
||||
valid = prev_bid.notna() & prev_bid_sz.notna() & prev_ask.notna() & prev_ask_sz.notna()
|
||||
|
||||
bid_part = pd.Series(np.nan, index=bid.index, dtype="float64")
|
||||
ask_part = pd.Series(np.nan, index=ask.index, dtype="float64")
|
||||
bid_up = valid & bid.gt(prev_bid)
|
||||
bid_same = valid & bid.eq(prev_bid)
|
||||
bid_down = valid & bid.lt(prev_bid)
|
||||
bid_part.loc[bid_up] = bid_sz.loc[bid_up] * bid.loc[bid_up]
|
||||
bid_part.loc[bid_same] = (bid_sz.loc[bid_same] - prev_bid_sz.loc[bid_same]) * bid.loc[bid_same]
|
||||
bid_part.loc[bid_down] = -prev_bid_sz.loc[bid_down] * prev_bid.loc[bid_down]
|
||||
|
||||
ask_down = valid & ask.lt(prev_ask)
|
||||
ask_same = valid & ask.eq(prev_ask)
|
||||
ask_up = valid & ask.gt(prev_ask)
|
||||
ask_part.loc[ask_down] = -ask_sz.loc[ask_down] * ask.loc[ask_down]
|
||||
ask_part.loc[ask_same] = -(ask_sz.loc[ask_same] - prev_ask_sz.loc[ask_same]) * ask.loc[ask_same]
|
||||
ask_part.loc[ask_up] = prev_ask_sz.loc[ask_up] * prev_ask.loc[ask_up]
|
||||
return bid_part, ask_part
|
||||
|
||||
|
||||
def _merge_feature_delta(feature: pd.DataFrame, delta: pd.DataFrame) -> pd.DataFrame:
|
||||
merged = feature.merge(delta, on=["symbol", "open_time_ms"], how="left")
|
||||
merged["ofi_l1_5m_clipped"] = pd.to_numeric(merged["ofi_l1_5m"], errors="coerce").clip(-5.0, 5.0)
|
||||
merged["ofi_l1_taker_5m"] = merged["ofi_l1_5m_clipped"] * pd.to_numeric(merged["taker_imbalance_5m"], errors="coerce")
|
||||
merged["ofi_l1_spread_rank_5m"] = merged["ofi_l1_5m_clipped"] * pd.to_numeric(merged["spread_rank_24h_pct"], errors="coerce")
|
||||
before = len(merged)
|
||||
merged = merged.dropna(subset=OFI_FEATURES).copy()
|
||||
logging.info(
|
||||
"trader.training.ofi_feature_delta_merged rowBefore=%s rowAfter=%s droppedRows=%s splitCounts=%s",
|
||||
before,
|
||||
len(merged),
|
||||
before - len(merged),
|
||||
merged["split_id"].value_counts().to_dict(),
|
||||
)
|
||||
if merged.empty:
|
||||
raise ValueError("OFI feature experiment has no rows after merging feature delta")
|
||||
return merged
|
||||
|
||||
|
||||
def _cap_rows_per_split(frame: pd.DataFrame, max_rows_per_split: int) -> pd.DataFrame:
|
||||
capped = []
|
||||
for split_id, part in frame.sort_values("event_time").groupby("split_id", sort=False, observed=False):
|
||||
if len(part) > max_rows_per_split:
|
||||
part = part.tail(max_rows_per_split).copy()
|
||||
capped.append(part)
|
||||
logging.info("trader.training.ofi_split_capped splitId=%s rowCount=%s maxRows=%s", split_id, len(part), max_rows_per_split)
|
||||
return pd.concat(capped, ignore_index=True)
|
||||
|
||||
|
||||
def _load_direction_dataset(baseline_root: Path, feature: pd.DataFrame) -> pd.DataFrame:
|
||||
labels = read_parquet(baseline_root / "label" / "direction_labels.parquet")
|
||||
required = {"sample_id", "long_target", "short_target", "neutral_target", "future_return_bps"}
|
||||
missing = sorted(required.difference(labels.columns))
|
||||
if missing:
|
||||
raise ValueError(f"direction labels missing columns: {missing}")
|
||||
dataset = feature.merge(labels[list(required)], on="sample_id", how="inner")
|
||||
logging.info("trader.training.ofi_direction_dataset_loaded rowCount=%s", len(dataset))
|
||||
return dataset
|
||||
|
||||
|
||||
def _load_entry_dataset(baseline_root: Path, feature: pd.DataFrame) -> pd.DataFrame:
|
||||
labels = read_parquet(baseline_root / "label" / "entry_labels.parquet")
|
||||
required = {"sample_id", "side", "entry_target", "expected_net_edge_bps"}
|
||||
missing = sorted(required.difference(labels.columns))
|
||||
if missing:
|
||||
raise ValueError(f"entry labels missing columns: {missing}")
|
||||
long = labels[labels["side"].eq("LONG")][["sample_id", "entry_target", "expected_net_edge_bps"]].rename(
|
||||
columns={"entry_target": "long_entry_target", "expected_net_edge_bps": "long_expected_net_edge_bps"}
|
||||
)
|
||||
short = labels[labels["side"].eq("SHORT")][["sample_id", "entry_target", "expected_net_edge_bps"]].rename(
|
||||
columns={"entry_target": "short_entry_target", "expected_net_edge_bps": "short_expected_net_edge_bps"}
|
||||
)
|
||||
pivot = long.merge(short, on="sample_id", how="inner")
|
||||
dataset = feature.merge(pivot, on="sample_id", how="inner")
|
||||
logging.info("trader.training.ofi_entry_dataset_loaded rowCount=%s", len(dataset))
|
||||
return dataset
|
||||
|
||||
|
||||
def _feature_sets() -> dict[str, list[str]]:
|
||||
return {
|
||||
"market_only": FEATURE_ORDER,
|
||||
"market_plus_ofi": [*FEATURE_ORDER, *OFI_FEATURES],
|
||||
}
|
||||
|
||||
|
||||
def _train_direction(frame: pd.DataFrame, feature_columns: list[str]) -> tuple[dict[str, Any], pd.DataFrame]:
|
||||
train = frame[frame["split_id"].eq(FIT_SPLIT)].copy()
|
||||
if train.empty:
|
||||
raise ValueError("direction experiment has no fit_inner rows")
|
||||
scaler = StandardScaler()
|
||||
x_train = scaler.fit_transform(train[feature_columns].astype("float32"))
|
||||
y_train = train[["long_target", "short_target", "neutral_target"]].to_numpy().argmax(axis=1)
|
||||
model = LogisticRegression(max_iter=500)
|
||||
model.fit(x_train, y_train)
|
||||
train_prior = train[["long_target", "short_target", "neutral_target"]].to_numpy(dtype=float).mean(axis=0)
|
||||
|
||||
metrics: dict[str, Any] = {"feature_count": len(feature_columns), "feature_hash": sha256_json(feature_columns)}
|
||||
prediction_frames = []
|
||||
for split_id in ALL_SPLITS:
|
||||
part = frame[frame["split_id"].eq(split_id)].copy()
|
||||
if part.empty:
|
||||
continue
|
||||
x = scaler.transform(part[feature_columns].astype("float32"))
|
||||
proba = model.predict_proba(x)
|
||||
y = part[["long_target", "short_target", "neutral_target"]].to_numpy().argmax(axis=1)
|
||||
metrics[split_id] = _direction_metrics(y, proba, train_prior)
|
||||
pred = part[["sample_id", "symbol", "event_time", "split_id"]].copy()
|
||||
pred["long_prob"] = proba[:, 0].astype("float32")
|
||||
pred["short_prob"] = proba[:, 1].astype("float32")
|
||||
pred["neutral_prob"] = proba[:, 2].astype("float32")
|
||||
pred["label_long"] = (y == 0).astype("int8")
|
||||
pred["label_short"] = (y == 1).astype("int8")
|
||||
pred["label_neutral"] = (y == 2).astype("int8")
|
||||
prediction_frames.append(pred)
|
||||
return metrics, pd.concat(prediction_frames, ignore_index=True)
|
||||
|
||||
|
||||
def _train_entry(frame: pd.DataFrame, feature_columns: list[str]) -> tuple[dict[str, Any], pd.DataFrame]:
|
||||
train = frame[frame["split_id"].eq(FIT_SPLIT)].copy()
|
||||
if train.empty:
|
||||
raise ValueError("entry experiment has no fit_inner rows")
|
||||
scaler = StandardScaler()
|
||||
x_train = scaler.fit_transform(train[feature_columns].astype("float32"))
|
||||
x_by_split = {
|
||||
split_id: scaler.transform(frame[frame["split_id"].eq(split_id)][feature_columns].astype("float32"))
|
||||
for split_id in ALL_SPLITS
|
||||
if not frame[frame["split_id"].eq(split_id)].empty
|
||||
}
|
||||
specs = [
|
||||
("long_entry_prob", "binary", "long_entry_target"),
|
||||
("short_entry_prob", "binary", "short_entry_target"),
|
||||
("long_expected_net_edge_bps", "regression", "long_expected_net_edge_bps"),
|
||||
("short_expected_net_edge_bps", "regression", "short_expected_net_edge_bps"),
|
||||
]
|
||||
results: dict[str, Any] = {"feature_count": len(feature_columns), "feature_hash": sha256_json(feature_columns)}
|
||||
split_predictions: dict[str, pd.DataFrame] = {
|
||||
split_id: frame[frame["split_id"].eq(split_id)][["sample_id", "symbol", "event_time", "split_id"]].copy().reset_index(drop=True)
|
||||
for split_id in x_by_split
|
||||
}
|
||||
for name, kind, target in specs:
|
||||
y_train = pd.to_numeric(train[target], errors="coerce").fillna(0.0).to_numpy()
|
||||
if kind == "binary":
|
||||
model = LogisticRegression(max_iter=500)
|
||||
model.fit(x_train, y_train.astype(int))
|
||||
else:
|
||||
model = HuberRegressor(alpha=0.001, epsilon=1.35, max_iter=500)
|
||||
model.fit(x_train, y_train.astype(float))
|
||||
results[name] = {}
|
||||
for split_id, x in x_by_split.items():
|
||||
part = frame[frame["split_id"].eq(split_id)].copy()
|
||||
y = pd.to_numeric(part[target], errors="coerce").fillna(0.0).to_numpy()
|
||||
if kind == "binary":
|
||||
pred = model.predict_proba(x)[:, 1]
|
||||
results[name][split_id] = _binary_metrics(y_train.astype(int), y.astype(int), pred)
|
||||
else:
|
||||
pred = model.predict(x)
|
||||
results[name][split_id] = _regression_metrics(y_train.astype(float), y.astype(float), pred)
|
||||
split_predictions[split_id][name] = pred.astype("float32")
|
||||
split_predictions[split_id][f"label_{target}"] = y
|
||||
return results, pd.concat(split_predictions.values(), ignore_index=True)
|
||||
|
||||
|
||||
def _direction_metrics(y: np.ndarray, proba: np.ndarray, train_prior: np.ndarray) -> dict[str, Any]:
|
||||
labels = [0, 1, 2]
|
||||
train_prior = np.asarray(train_prior, dtype=float)
|
||||
train_prior = train_prior / train_prior.sum() if train_prior.sum() > 0 else np.full(3, 1.0 / 3.0)
|
||||
constant = np.tile(train_prior.reshape(1, -1), (len(y), 1))
|
||||
one_hot = np.eye(3, dtype=float)[y]
|
||||
clipped = _clip_normalize(proba)
|
||||
constant_clipped = _clip_normalize(constant)
|
||||
out: dict[str, Any] = {
|
||||
"row_count": int(len(y)),
|
||||
"accuracy": float(accuracy_score(y, proba.argmax(axis=1))),
|
||||
"logloss": float(log_loss(y, clipped, labels=labels)),
|
||||
"constant_logloss": float(log_loss(y, constant_clipped, labels=labels)),
|
||||
"brier_multiclass": float(np.mean(np.sum((one_hot - proba) ** 2, axis=1))),
|
||||
"constant_brier_multiclass": float(np.mean(np.sum((one_hot - constant) ** 2, axis=1))),
|
||||
}
|
||||
for idx, name in enumerate(("long", "short", "neutral")):
|
||||
target = (y == idx).astype(int)
|
||||
if target.sum() >= 200 and (len(target) - target.sum()) >= 200:
|
||||
out[f"{name}_auc"] = float(roc_auc_score(target, proba[:, idx]))
|
||||
top_count = max(1, int(len(y) * 0.10))
|
||||
top_idx = np.argsort(proba.max(axis=1))[-top_count:]
|
||||
out["top10_hit_rate"] = float((proba.argmax(axis=1)[top_idx] == y[top_idx]).mean())
|
||||
out["all_hit_rate"] = float((proba.argmax(axis=1) == y).mean())
|
||||
out["logloss_vs_constant_ratio"] = float(out["logloss"] / out["constant_logloss"]) if out["constant_logloss"] > 0 else None
|
||||
out["brier_vs_constant_ratio"] = float(out["brier_multiclass"] / out["constant_brier_multiclass"]) if out["constant_brier_multiclass"] > 0 else None
|
||||
return out
|
||||
|
||||
|
||||
def _binary_metrics(y_train: np.ndarray, y: np.ndarray, proba: np.ndarray) -> dict[str, Any]:
|
||||
train_rate = float(np.mean(y_train))
|
||||
constant = np.full(len(y), train_rate)
|
||||
out: dict[str, Any] = {
|
||||
"row_count": int(len(y)),
|
||||
"positive_rate": float(np.mean(y)) if len(y) else 0.0,
|
||||
"brier": float(brier_score_loss(y, proba)),
|
||||
"constant_brier": float(brier_score_loss(y, constant)),
|
||||
}
|
||||
if len(np.unique(y)) == 2:
|
||||
out["auc"] = float(roc_auc_score(y, proba))
|
||||
top_count = max(1, int(len(y) * 0.10))
|
||||
top_idx = np.argsort(proba)[-top_count:]
|
||||
out["top10_hit_rate"] = float(np.mean(y[top_idx]))
|
||||
out["all_hit_rate"] = float(np.mean(y))
|
||||
out["brier_vs_constant_ratio"] = float(out["brier"] / out["constant_brier"]) if out["constant_brier"] > 0 else None
|
||||
return out
|
||||
|
||||
|
||||
def _regression_metrics(y_train: np.ndarray, y: np.ndarray, pred: np.ndarray) -> dict[str, Any]:
|
||||
train_median = float(np.median(y_train)) if len(y_train) else 0.0
|
||||
constant = np.full(len(y), train_median)
|
||||
mae = float(mean_absolute_error(y, pred))
|
||||
constant_mae = float(mean_absolute_error(y, constant))
|
||||
return {
|
||||
"row_count": int(len(y)),
|
||||
"mae": mae,
|
||||
"constant_mae": constant_mae,
|
||||
"mae_vs_constant_ratio": float(mae / constant_mae) if constant_mae > 0 else None,
|
||||
"train_target_median": train_median,
|
||||
}
|
||||
|
||||
|
||||
def _clip_normalize(values: np.ndarray) -> np.ndarray:
|
||||
values = np.clip(np.asarray(values, dtype=float), 1e-6, 1.0)
|
||||
return values / values.sum(axis=1, keepdims=True)
|
||||
|
||||
|
||||
def _experiment_manifest(
|
||||
args: Any,
|
||||
baseline_root: Path,
|
||||
raw_root: Path,
|
||||
ofi_delta: pd.DataFrame,
|
||||
dataset: pd.DataFrame,
|
||||
delta_hash: str,
|
||||
dataset_hash: str,
|
||||
) -> dict[str, Any]:
|
||||
return {
|
||||
"experiment": "ofi_l1_microprice_diagnostic_v1",
|
||||
"run_id": args.run_id,
|
||||
"baseline_run_id": args.baseline_run_id,
|
||||
"baseline_root": str(baseline_root),
|
||||
"raw_root": str(raw_root),
|
||||
"ofi_method": OFI_METHOD,
|
||||
"uses_event_stream_ofi": False,
|
||||
"normalization": "quote_notional_over_average_l1_depth_quote",
|
||||
"new_features": OFI_FEATURES,
|
||||
"formal_model_contract_changed": False,
|
||||
"java_contract_changed": False,
|
||||
"label_changed": False,
|
||||
"pm_threshold_changed": False,
|
||||
"delta_row_count": int(len(ofi_delta)),
|
||||
"trainable_row_count": int(len(dataset)),
|
||||
"split_counts": dataset["split_id"].value_counts().to_dict(),
|
||||
"ofi_delta_hash_sha256": delta_hash,
|
||||
"ofi_experiment_feature_frame_hash_sha256": dataset_hash,
|
||||
}
|
||||
|
||||
|
||||
def _ofi_feature_schema() -> list[dict[str, Any]]:
|
||||
rows = [
|
||||
{
|
||||
"name": "ofi_l1_1m",
|
||||
"meaning": "买一卖一盘口变化强度,1分钟窗口",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "1m",
|
||||
"formula": "sum(l1 snapshot-diff quote OFI) / avg(l1 depth quote)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "first minute or gap warmup -> drop in experiment",
|
||||
"order": 1,
|
||||
},
|
||||
{
|
||||
"name": "ofi_l1_3m",
|
||||
"meaning": "买一卖一盘口变化强度,3分钟窗口",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "3m",
|
||||
"formula": "sum(l1 snapshot-diff quote OFI over last 3 closed minutes) / avg(l1 depth quote)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "window warmup or gap warmup -> drop in experiment",
|
||||
"order": 2,
|
||||
},
|
||||
{
|
||||
"name": "ofi_l1_5m",
|
||||
"meaning": "买一卖一盘口变化强度,5分钟窗口",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "5m",
|
||||
"formula": "sum(l1 snapshot-diff quote OFI over last 5 closed minutes) / avg(l1 depth quote)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "window warmup or gap warmup -> drop in experiment",
|
||||
"order": 3,
|
||||
},
|
||||
{
|
||||
"name": "ofi_l1_15m",
|
||||
"meaning": "买一卖一盘口变化强度,15分钟窗口",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "15m",
|
||||
"formula": "sum(l1 snapshot-diff quote OFI over last 15 closed minutes) / avg(l1 depth quote)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "window warmup or gap warmup -> drop in experiment",
|
||||
"order": 4,
|
||||
},
|
||||
{
|
||||
"name": "microprice_basis_change_1m_bps",
|
||||
"meaning": "微价格偏离值的1分钟变化",
|
||||
"unit": "bps",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "1m",
|
||||
"formula": "microprice_basis_bps(t) - microprice_basis_bps(t-1m)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "first minute or gap warmup -> drop in experiment",
|
||||
"order": 5,
|
||||
},
|
||||
{
|
||||
"name": "microprice_basis_change_5m_bps",
|
||||
"meaning": "微价格偏离值的5分钟变化",
|
||||
"unit": "bps",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "5m",
|
||||
"formula": "microprice_basis_bps(t) - microprice_basis_bps(t-5m)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "window warmup or gap warmup -> drop in experiment",
|
||||
"order": 6,
|
||||
},
|
||||
]
|
||||
rows.extend(
|
||||
[
|
||||
{
|
||||
"name": "mlofi_l5_1m",
|
||||
"meaning": "前5档盘口变化强度,1分钟窗口",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "1m",
|
||||
"formula": "sum(level0..4 snapshot-diff quote OFI) / avg(level0..4 depth quote)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "first minute or gap warmup -> drop in experiment",
|
||||
"order": 7,
|
||||
},
|
||||
{
|
||||
"name": "mlofi_l5_5m",
|
||||
"meaning": "前5档盘口变化强度,5分钟窗口",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "5m",
|
||||
"formula": "sum(level0..4 snapshot-diff quote OFI over last 5 closed minutes) / avg(level0..4 depth quote)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "window warmup or gap warmup -> drop in experiment",
|
||||
"order": 8,
|
||||
},
|
||||
{
|
||||
"name": "mlofi_l20_1m",
|
||||
"meaning": "前20档盘口变化强度,1分钟窗口",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "1m",
|
||||
"formula": "sum(level0..19 snapshot-diff quote OFI) / avg(level0..19 depth quote)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "first minute or gap warmup -> drop in experiment",
|
||||
"order": 9,
|
||||
},
|
||||
{
|
||||
"name": "mlofi_l20_5m",
|
||||
"meaning": "前20档盘口变化强度,5分钟窗口",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "5m",
|
||||
"formula": "sum(level0..19 snapshot-diff quote OFI over last 5 closed minutes) / avg(level0..19 depth quote)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "window warmup or gap warmup -> drop in experiment",
|
||||
"order": 10,
|
||||
},
|
||||
{
|
||||
"name": "mlofi_l5_l20_gap_1m",
|
||||
"meaning": "近档盘口变化和深档盘口变化的差",
|
||||
"unit": "ratio",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "1m",
|
||||
"formula": "mlofi_l5_1m - mlofi_l20_1m",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "dependency missing -> drop in experiment",
|
||||
"order": 11,
|
||||
},
|
||||
{
|
||||
"name": "ofi_l1_5m_zscore_240m",
|
||||
"meaning": "L1 OFI 5分钟值相对最近240分钟的异常程度",
|
||||
"unit": "zscore",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "240m",
|
||||
"formula": "(ofi_l1_5m - rolling_mean_240m) / rolling_std_240m",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "window warmup or gap warmup -> drop in experiment",
|
||||
"order": 12,
|
||||
},
|
||||
{
|
||||
"name": "mlofi_l20_5m_zscore_240m",
|
||||
"meaning": "L20 多层 OFI 5分钟值相对最近240分钟的异常程度",
|
||||
"unit": "zscore",
|
||||
"source": "Crypto Lake book snapshot",
|
||||
"window": "240m",
|
||||
"formula": "(mlofi_l20_5m - rolling_mean_240m) / rolling_std_240m",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "window warmup or gap warmup -> drop in experiment",
|
||||
"order": 13,
|
||||
},
|
||||
{
|
||||
"name": "ofi_l1_5m_clipped",
|
||||
"meaning": "截尾后的 L1 OFI 5分钟值",
|
||||
"unit": "ratio",
|
||||
"source": "derived from ofi_l1_5m",
|
||||
"window": "5m",
|
||||
"formula": "clip(ofi_l1_5m, -5, 5)",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "dependency missing -> drop in experiment",
|
||||
"order": 14,
|
||||
},
|
||||
{
|
||||
"name": "ofi_l1_taker_5m",
|
||||
"meaning": "L1 OFI 和5分钟主动成交是否同向",
|
||||
"unit": "ratio",
|
||||
"source": "book + trades",
|
||||
"window": "5m",
|
||||
"formula": "ofi_l1_5m_clipped * taker_imbalance_5m",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "dependency missing -> drop in experiment",
|
||||
"order": 15,
|
||||
},
|
||||
{
|
||||
"name": "ofi_l1_spread_rank_5m",
|
||||
"meaning": "L1 OFI 在高价差环境下的强度",
|
||||
"unit": "ratio",
|
||||
"source": "book + level_1",
|
||||
"window": "5m",
|
||||
"formula": "ofi_l1_5m_clipped * spread_rank_24h_pct",
|
||||
"ofi_method": OFI_METHOD,
|
||||
"null_handling": "dependency missing -> drop in experiment",
|
||||
"order": 16,
|
||||
},
|
||||
]
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def _feature_delta_report(ofi_delta: pd.DataFrame, dataset: pd.DataFrame) -> str:
|
||||
rows = []
|
||||
for feature in OFI_FEATURES:
|
||||
series = pd.to_numeric(dataset[feature], errors="coerce")
|
||||
q = series.quantile([0.01, 0.5, 0.99])
|
||||
rows.append(
|
||||
{
|
||||
"feature": feature,
|
||||
"null_in_delta": int(ofi_delta[feature].isna().sum()) if feature in ofi_delta.columns else "derived_after_merge",
|
||||
"trainable_null": int(series.isna().sum()),
|
||||
"p01": round(float(q.loc[0.01]), 6),
|
||||
"p50": round(float(q.loc[0.5]), 6),
|
||||
"p99": round(float(q.loc[0.99]), 6),
|
||||
}
|
||||
)
|
||||
lines = [
|
||||
"# OFI Feature Delta Report",
|
||||
"",
|
||||
f"- ofi_method: `{OFI_METHOD}`",
|
||||
f"- delta_rows: `{len(ofi_delta)}`",
|
||||
f"- trainable_rows_after_drop: `{len(dataset)}`",
|
||||
f"- split_counts: `{dataset['split_id'].value_counts().to_dict()}`",
|
||||
"",
|
||||
"| feature | null_in_delta | trainable_null | p01 | p50 | p99 |",
|
||||
"| --- | ---: | ---: | ---: | ---: | ---: |",
|
||||
]
|
||||
for row in rows:
|
||||
lines.append(f"| {row['feature']} | {row['null_in_delta']} | {row['trainable_null']} | {row['p01']} | {row['p50']} | {row['p99']} |")
|
||||
lines.extend(
|
||||
[
|
||||
"",
|
||||
"## Leakage Check",
|
||||
"",
|
||||
"- 只用当前分钟及之前的 book 快照。",
|
||||
"- 第一条快照和断档后的窗口不补 0,直接作为 warmup 丢掉。",
|
||||
"- 分子和分母都使用 quote 金额口径。",
|
||||
"",
|
||||
]
|
||||
)
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _model_compare_report(args: Any, baseline_root: Path, results: dict[str, Any], dataset: pd.DataFrame) -> str:
|
||||
baseline = read_json(baseline_root / "model" / "model_train_manifest.json")
|
||||
baseline_direction = baseline["DIRECTION"]["metrics"]["direction"]
|
||||
baseline_entry = baseline["ENTRY"]["metrics"]
|
||||
lines = [
|
||||
"# OFI Model Compare To Run10",
|
||||
"",
|
||||
f"- run_id: `{args.run_id}`",
|
||||
f"- baseline_run_id: `{args.baseline_run_id}`",
|
||||
f"- ofi_method: `{OFI_METHOD}`",
|
||||
f"- rows: `{len(dataset)}`",
|
||||
"",
|
||||
"## Run10 Tune Baseline",
|
||||
"",
|
||||
"| model | metric | value |",
|
||||
"| --- | --- | ---: |",
|
||||
f"| Direction | long_auc | {baseline_direction.get('long_auc')} |",
|
||||
f"| Direction | short_auc | {baseline_direction.get('short_auc')} |",
|
||||
f"| Direction | neutral_auc | {baseline_direction.get('neutral_auc')} |",
|
||||
f"| Entry | long_auc | {baseline_entry['long_entry_prob'].get('auc')} |",
|
||||
f"| Entry | short_auc | {baseline_entry['short_entry_prob'].get('auc')} |",
|
||||
f"| Entry | long_edge_mae_ratio | {baseline_entry['long_expected_net_edge_bps'].get('mae_vs_constant_ratio')} |",
|
||||
f"| Entry | short_edge_mae_ratio | {baseline_entry['short_expected_net_edge_bps'].get('mae_vs_constant_ratio')} |",
|
||||
"",
|
||||
"## Diagnostic Direction Result",
|
||||
"",
|
||||
"| feature_set | split | long_auc | short_auc | neutral_auc | logloss_ratio | top10_hit_rate |",
|
||||
"| --- | --- | ---: | ---: | ---: | ---: | ---: |",
|
||||
]
|
||||
for feature_set_name, payload in results.items():
|
||||
direction = payload["DIRECTION"]
|
||||
for split_id in EVAL_SPLITS:
|
||||
metric = direction.get(split_id, {})
|
||||
lines.append(
|
||||
f"| {feature_set_name} | {split_id} | {metric.get('long_auc')} | {metric.get('short_auc')} | {metric.get('neutral_auc')} | {metric.get('logloss_vs_constant_ratio')} | {metric.get('top10_hit_rate')} |"
|
||||
)
|
||||
lines.extend(
|
||||
[
|
||||
"",
|
||||
"## Diagnostic Entry Result",
|
||||
"",
|
||||
"| head | feature_set | split | auc/mae_ratio | brier_ratio | top10_hit_rate |",
|
||||
"| --- | --- | --- | ---: | ---: | ---: |",
|
||||
]
|
||||
)
|
||||
for feature_set_name, payload in results.items():
|
||||
entry = payload["ENTRY"]
|
||||
for head in ("long_entry_prob", "short_entry_prob"):
|
||||
for split_id in EVAL_SPLITS:
|
||||
metric = entry.get(head, {}).get(split_id, {})
|
||||
lines.append(
|
||||
f"| {head} | {feature_set_name} | {split_id} | {metric.get('auc')} | {metric.get('brier_vs_constant_ratio')} | {metric.get('top10_hit_rate')} |"
|
||||
)
|
||||
for head in ("long_expected_net_edge_bps", "short_expected_net_edge_bps"):
|
||||
for split_id in EVAL_SPLITS:
|
||||
metric = entry.get(head, {}).get(split_id, {})
|
||||
lines.append(f"| {head} | {feature_set_name} | {split_id} | {metric.get('mae_vs_constant_ratio')} | | |")
|
||||
lines.extend(
|
||||
[
|
||||
"",
|
||||
"## Verdict Rule",
|
||||
"",
|
||||
"只有 `market_plus_ofi` 在 validation_locked 和 latest_stress 上同时好过 `market_only`,才进入正式特征链路。",
|
||||
"",
|
||||
]
|
||||
)
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _backtest_placeholder_report(args: Any, baseline_root: Path) -> str:
|
||||
return "\n".join(
|
||||
[
|
||||
"# Backtest Compare To Run10",
|
||||
"",
|
||||
f"- run_id: `{args.run_id}`",
|
||||
f"- baseline_run_id: `{args.baseline_run_id}`",
|
||||
"",
|
||||
"本轮是 Direction / Entry 特征诊断,没有导出正式 ONNX,也没有改 PM 阈值,所以不跑组合回测。",
|
||||
"",
|
||||
"如果诊断指标通过,下一步才把 OFI 特征纳入正式 `feature_schema.json`、导出模型包,再做 validation_locked 和 latest_stress 的完整回测。",
|
||||
"",
|
||||
f"- run10_baseline_root: `{baseline_root}`",
|
||||
"",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def _contract_change_report() -> str:
|
||||
return "\n".join(
|
||||
[
|
||||
"# Contract Change Report",
|
||||
"",
|
||||
"| 项 | 结论 |",
|
||||
"| --- | --- |",
|
||||
"| 正式 ONNX 输入 | 未改变 |",
|
||||
"| Java SHADOW 输入契约 | 未改变 |",
|
||||
"| 模型输出字段 | 未改变 |",
|
||||
"| 标签口径 | 未改变 |",
|
||||
"| PM 阈值 | 未改变 |",
|
||||
"",
|
||||
"原因:本轮只做旁路诊断。只有验证通过后,才会进入正式特征表和 Java 契约同步。",
|
||||
"",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def _failure_case_placeholder_report(args: Any) -> str:
|
||||
return "\n".join(
|
||||
[
|
||||
"# Failure Cases Compare",
|
||||
"",
|
||||
f"- run_id: `{args.run_id}`",
|
||||
"",
|
||||
"本轮没有产生正式交易决策,因此没有最差交易样本可比。",
|
||||
"",
|
||||
"下一步如果 OFI 进入正式模型包,必须用完整回测交易明细比较:",
|
||||
"",
|
||||
"1. validation_locked 最大亏损交易。",
|
||||
"2. latest_stress 最大亏损交易。",
|
||||
"3. 连续亏损段。",
|
||||
"4. 高 OFI 但反向亏损样本。",
|
||||
"",
|
||||
]
|
||||
)
|
||||
+240
-32
@@ -11,6 +11,13 @@ from trader_training.io_utils import read_json, read_parquet, run_root, sha256_j
|
||||
from trader_training.schemas import LATEST_STRESS_SPLIT, PM_CONFIG_VERSION, TUNE_SPLIT, VALIDATION_LOCKED_SPLIT
|
||||
|
||||
|
||||
DEFAULT_BACKTEST_PRICE_PLAN = {
|
||||
"stopDistanceBps": 35.0,
|
||||
"costBps": 4.0,
|
||||
"maxHoldMinutes": 45,
|
||||
}
|
||||
|
||||
|
||||
def default_pm_config() -> dict:
|
||||
return {
|
||||
"pmConfigVersion": PM_CONFIG_VERSION,
|
||||
@@ -70,6 +77,7 @@ def default_pm_config() -> dict:
|
||||
def search_pm_thresholds(args: Any) -> None:
|
||||
root = run_root(args)
|
||||
frame = _pm_tune_frame(root)
|
||||
price_plan = _price_plan_context(root)
|
||||
candidate_rows: list[dict[str, Any]] = []
|
||||
best_score = -float("inf")
|
||||
best_thresholds: dict[str, float] | None = None
|
||||
@@ -77,7 +85,8 @@ def search_pm_thresholds(args: Any) -> None:
|
||||
best_trades = pd.DataFrame()
|
||||
|
||||
for thresholds in _threshold_candidates():
|
||||
trades = _simulate_open_trades(frame, thresholds)
|
||||
config = _pm_config_from_thresholds(thresholds)
|
||||
trades = _simulate_open_trades(frame, thresholds, config, price_plan)
|
||||
metrics = _trade_metrics(trades)
|
||||
score = _score_thresholds(metrics)
|
||||
candidate_rows.append({**thresholds, **metrics, "score": score})
|
||||
@@ -134,11 +143,27 @@ def integrated_backtest(args: Any) -> None:
|
||||
trades_path = root / "pm-search" / "pm_backtest_trades.parquet"
|
||||
# PM search is allowed to use tune_inner, but final acceptance must be
|
||||
# measured on the sealed validation_locked and latest_stress splits.
|
||||
tune_trades = read_parquet(trades_path) if trades_path.is_file() else _simulate_open_trades(_pm_tune_frame(root), _thresholds_from_config(pm_payload["config"]))
|
||||
price_plan = _price_plan_context(root)
|
||||
tune_trades = read_parquet(trades_path) if trades_path.is_file() else _simulate_open_trades(
|
||||
_pm_tune_frame(root),
|
||||
_thresholds_from_config(pm_payload["config"]),
|
||||
pm_payload["config"],
|
||||
price_plan,
|
||||
)
|
||||
tune_trades["eval_split"] = TUNE_SPLIT
|
||||
validation_locked_trades = _simulate_open_trades(_pm_frame(root, VALIDATION_LOCKED_SPLIT), _thresholds_from_config(pm_payload["config"]))
|
||||
validation_locked_trades = _simulate_open_trades(
|
||||
_pm_frame(root, VALIDATION_LOCKED_SPLIT),
|
||||
_thresholds_from_config(pm_payload["config"]),
|
||||
pm_payload["config"],
|
||||
price_plan,
|
||||
)
|
||||
validation_locked_trades["eval_split"] = VALIDATION_LOCKED_SPLIT
|
||||
stress_trades = _simulate_open_trades(_pm_frame(root, LATEST_STRESS_SPLIT), _thresholds_from_config(pm_payload["config"]))
|
||||
stress_trades = _simulate_open_trades(
|
||||
_pm_frame(root, LATEST_STRESS_SPLIT),
|
||||
_thresholds_from_config(pm_payload["config"]),
|
||||
pm_payload["config"],
|
||||
price_plan,
|
||||
)
|
||||
stress_trades["eval_split"] = LATEST_STRESS_SPLIT
|
||||
trades = pd.concat([tune_trades, validation_locked_trades, stress_trades], ignore_index=True)
|
||||
metrics = {
|
||||
@@ -154,6 +179,8 @@ def integrated_backtest(args: Any) -> None:
|
||||
"backtest_manifest_id": f"backtest-{args.run_id}",
|
||||
"mode": "VALIDATION_PM_BACKTEST",
|
||||
"pm_config_hash_sha256": pm_payload["config_hash_sha256"],
|
||||
"price_plan_id": price_plan.get("pricePlanId"),
|
||||
"price_plan_config_hash": price_plan.get("pricePlanConfigHash"),
|
||||
"metrics": metrics,
|
||||
"status_reasons": status_reasons,
|
||||
"status": status,
|
||||
@@ -179,6 +206,14 @@ def _pm_tune_frame(root) -> pd.DataFrame:
|
||||
return _pm_frame(root, TUNE_SPLIT)
|
||||
|
||||
|
||||
def _price_plan_context(root) -> dict[str, Any]:
|
||||
path = root / "label" / "price_plan_context.json"
|
||||
if path.is_file():
|
||||
return read_json(path)
|
||||
logging.warning("trader.training.price_plan_missing_for_pm path=%s usingDefault=%s", path, DEFAULT_BACKTEST_PRICE_PLAN)
|
||||
return DEFAULT_BACKTEST_PRICE_PLAN.copy()
|
||||
|
||||
|
||||
def _pm_frame(root, split_id: str) -> pd.DataFrame:
|
||||
prediction_files = {
|
||||
TUNE_SPLIT: "tune_predictions.parquet",
|
||||
@@ -194,12 +229,14 @@ def _pm_frame(root, split_id: str) -> pd.DataFrame:
|
||||
}
|
||||
)
|
||||
risk = read_parquet(root / "model" / "risk" / prediction_file)
|
||||
price_plan = _price_plan_context(root)
|
||||
entry_dataset = read_parquet(root / "dataset" / "entry_train.parquet").rename(
|
||||
columns={
|
||||
"long_expected_net_edge_bps": "actual_long_expected_net_edge_bps",
|
||||
"short_expected_net_edge_bps": "actual_short_expected_net_edge_bps",
|
||||
}
|
||||
)
|
||||
entry_plan_outcome = _entry_plan_outcome_frame(root)
|
||||
entry_cols = [
|
||||
"sample_id",
|
||||
"long_entry_prob",
|
||||
@@ -214,26 +251,92 @@ def _pm_frame(root, split_id: str) -> pd.DataFrame:
|
||||
.merge(entry[entry_cols], on="sample_id", how="inner")
|
||||
.merge(risk[risk_cols], on="sample_id", how="inner")
|
||||
.merge(entry_dataset[actual_cols], on="sample_id", how="inner")
|
||||
.merge(entry_plan_outcome, on="sample_id", how="inner")
|
||||
)
|
||||
if frame.empty:
|
||||
raise ValueError(f"PM frame is empty for {split_id}; check model predictions and entry dataset")
|
||||
frame["model_pred_long_expected_net_edge_bps"] = frame["pred_long_expected_net_edge_bps"]
|
||||
frame["model_pred_short_expected_net_edge_bps"] = frame["pred_short_expected_net_edge_bps"]
|
||||
edge_mode = "MODEL_EXPECTED_NET_EDGE"
|
||||
if price_plan.get("entryTargetMethod") not in {"OPPORTUNITY_MFE_V1", "OPPORTUNITY_QUALITY_V1"}:
|
||||
frame["pred_long_expected_net_edge_bps"] = _probability_implied_edge(frame["long_entry_prob"], price_plan)
|
||||
frame["pred_short_expected_net_edge_bps"] = _probability_implied_edge(frame["short_entry_prob"], price_plan)
|
||||
edge_mode = "ENTRY_PROBABILITY_PAYOFF"
|
||||
logging.info(
|
||||
"trader.training.pm_frame_loaded splitId=%s rowCount=%s splitCounts=%s",
|
||||
"trader.training.pm_frame_loaded splitId=%s rowCount=%s splitCounts=%s edgeMode=%s",
|
||||
split_id,
|
||||
len(frame),
|
||||
frame["split_id"].value_counts().to_dict(),
|
||||
edge_mode,
|
||||
)
|
||||
return frame
|
||||
|
||||
|
||||
def _probability_implied_edge(entry_prob: pd.Series, price_plan: dict[str, Any]) -> pd.Series:
|
||||
target_net_bps = float(price_plan.get("targetDistanceBps", 0.0)) - float(price_plan.get("costBps", 0.0))
|
||||
stop_net_bps = -float(price_plan.get("stopDistanceBps", DEFAULT_BACKTEST_PRICE_PLAN["stopDistanceBps"])) - float(
|
||||
price_plan.get("costBps", DEFAULT_BACKTEST_PRICE_PLAN["costBps"])
|
||||
)
|
||||
probability = pd.to_numeric(entry_prob, errors="coerce").fillna(0.0).clip(lower=0.0, upper=1.0)
|
||||
# Entry 的概率头比收益回归头稳定。这里用固定止盈止损的盈亏比把概率换成期望收益,
|
||||
# 让低命中、高赔率计划也能被 PM 正常搜索;真实结果仍由标签里的实际路径收益评估。
|
||||
return probability * target_net_bps + (1.0 - probability) * stop_net_bps
|
||||
|
||||
|
||||
def _entry_plan_outcome_frame(root) -> pd.DataFrame:
|
||||
labels = read_parquet(root / "label" / "entry_labels.parquet").copy()
|
||||
required = {
|
||||
"sample_id",
|
||||
"side",
|
||||
"gross_edge_bps",
|
||||
"cost_bps",
|
||||
"target_hit",
|
||||
"stop_hit",
|
||||
"time_to_target_ms",
|
||||
"time_to_stop_ms",
|
||||
"time_to_exit_ms",
|
||||
}
|
||||
missing = sorted(required - set(labels.columns))
|
||||
if missing:
|
||||
raise ValueError(f"entry_labels is missing PM outcome columns: {missing}")
|
||||
labels["trade_net_edge_bps"] = pd.to_numeric(labels["gross_edge_bps"], errors="coerce").fillna(0.0) - pd.to_numeric(
|
||||
labels["cost_bps"], errors="coerce"
|
||||
).fillna(0.0)
|
||||
|
||||
def side_frame(side: str, prefix: str) -> pd.DataFrame:
|
||||
return labels[labels["side"].eq(side)][
|
||||
[
|
||||
"sample_id",
|
||||
"trade_net_edge_bps",
|
||||
"target_hit",
|
||||
"stop_hit",
|
||||
"time_to_target_ms",
|
||||
"time_to_stop_ms",
|
||||
"time_to_exit_ms",
|
||||
]
|
||||
].rename(
|
||||
columns={
|
||||
"trade_net_edge_bps": f"{prefix}_trade_net_edge_bps",
|
||||
"target_hit": f"{prefix}_target_hit",
|
||||
"stop_hit": f"{prefix}_stop_hit",
|
||||
"time_to_target_ms": f"{prefix}_time_to_target_ms",
|
||||
"time_to_stop_ms": f"{prefix}_time_to_stop_ms",
|
||||
"time_to_exit_ms": f"{prefix}_time_to_exit_ms",
|
||||
}
|
||||
)
|
||||
|
||||
return side_frame("LONG", "long").merge(side_frame("SHORT", "short"), on="sample_id", how="inner")
|
||||
|
||||
|
||||
def _threshold_candidates() -> list[dict[str, float]]:
|
||||
# 1.01 表示这一侧不开仓,用来检查“只做多”或“只做空”是否更稳。
|
||||
values = itertools.product(
|
||||
[0.50, 0.52, 0.54, 0.56, 0.58],
|
||||
[0.50, 0.52, 0.54, 0.56, 0.58],
|
||||
[0.10, 0.12, 0.14, 0.16, 0.20, 0.30, 0.50],
|
||||
[0.55, 0.75, 0.90, 1.00],
|
||||
[-8.0, -4.0, 0.0, 1.0, 3.0],
|
||||
[0.00, 0.01, 0.02, 0.05],
|
||||
[0.50, 0.60, 0.70, 1.01],
|
||||
[0.50, 0.60, 0.70, 1.01],
|
||||
[0.03, 0.50, 0.70, 0.85],
|
||||
[0.45, 0.65, 0.85],
|
||||
[0.0, 8.0, 15.0, 25.0],
|
||||
[0.02, 0.06, 0.10],
|
||||
)
|
||||
return [
|
||||
{
|
||||
@@ -248,23 +351,39 @@ def _threshold_candidates() -> list[dict[str, float]]:
|
||||
]
|
||||
|
||||
|
||||
def _simulate_open_trades(frame: pd.DataFrame, thresholds: dict[str, float]) -> pd.DataFrame:
|
||||
def _simulate_open_trades(
|
||||
frame: pd.DataFrame,
|
||||
thresholds: dict[str, float],
|
||||
pm_config: dict[str, Any] | None = None,
|
||||
price_plan: dict[str, Any] | None = None,
|
||||
) -> pd.DataFrame:
|
||||
direction_margin = (frame["long_prob"] - frame["short_prob"]).abs()
|
||||
long_mask = (
|
||||
(frame["long_prob"] >= thresholds["long_open_prob"])
|
||||
& ((frame["long_prob"] - frame["short_prob"]) >= thresholds["min_direction_margin"])
|
||||
& (frame["long_entry_prob"] >= thresholds["min_entry_prob"])
|
||||
& (frame["market_risk_prob"] <= thresholds["max_market_risk_prob"])
|
||||
& (frame["pred_long_expected_net_edge_bps"] >= thresholds["min_expected_edge_bps"])
|
||||
(frame["long_prob"] > thresholds["long_open_prob"])
|
||||
& (direction_margin > thresholds["min_direction_margin"])
|
||||
& (frame["long_entry_prob"] > thresholds["min_entry_prob"])
|
||||
& (frame["market_risk_prob"] < thresholds["max_market_risk_prob"])
|
||||
& (frame["pred_long_expected_net_edge_bps"] > thresholds["min_expected_edge_bps"])
|
||||
)
|
||||
short_mask = (
|
||||
(frame["short_prob"] >= thresholds["short_open_prob"])
|
||||
& ((frame["short_prob"] - frame["long_prob"]) >= thresholds["min_direction_margin"])
|
||||
& (frame["short_entry_prob"] >= thresholds["min_entry_prob"])
|
||||
& (frame["market_risk_prob"] <= thresholds["max_market_risk_prob"])
|
||||
& (frame["pred_short_expected_net_edge_bps"] >= thresholds["min_expected_edge_bps"])
|
||||
(frame["short_prob"] > thresholds["short_open_prob"])
|
||||
& (direction_margin > thresholds["min_direction_margin"])
|
||||
& (frame["short_entry_prob"] > thresholds["min_entry_prob"])
|
||||
& (frame["market_risk_prob"] < thresholds["max_market_risk_prob"])
|
||||
& (frame["pred_short_expected_net_edge_bps"] > thresholds["min_expected_edge_bps"])
|
||||
)
|
||||
long_score = (
|
||||
frame["pred_long_expected_net_edge_bps"].clip(lower=0.0)
|
||||
* frame["long_prob"]
|
||||
* frame["long_entry_prob"]
|
||||
* (1.0 - frame["market_risk_prob"].clip(lower=0.0, upper=1.0))
|
||||
)
|
||||
short_score = (
|
||||
frame["pred_short_expected_net_edge_bps"].clip(lower=0.0)
|
||||
* frame["short_prob"]
|
||||
* frame["short_entry_prob"]
|
||||
* (1.0 - frame["market_risk_prob"].clip(lower=0.0, upper=1.0))
|
||||
)
|
||||
long_score = frame["pred_long_expected_net_edge_bps"] + (frame["long_prob"] - frame["short_prob"]) * 10.0
|
||||
short_score = frame["pred_short_expected_net_edge_bps"] + (frame["short_prob"] - frame["long_prob"]) * 10.0
|
||||
side = np.where(long_mask & (~short_mask | (long_score >= short_score)), "LONG", np.where(short_mask, "SHORT", ""))
|
||||
trades = frame.loc[side != ""].copy().reset_index(drop=True)
|
||||
if trades.empty:
|
||||
@@ -274,11 +393,19 @@ def _simulate_open_trades(frame: pd.DataFrame, thresholds: dict[str, float]) ->
|
||||
trades["direction_prob"] = np.where(is_long, trades["long_prob"], trades["short_prob"])
|
||||
trades["entry_prob"] = np.where(is_long, trades["long_entry_prob"], trades["short_entry_prob"])
|
||||
trades["predicted_edge_bps"] = np.where(is_long, trades["pred_long_expected_net_edge_bps"], trades["pred_short_expected_net_edge_bps"])
|
||||
trades["actual_edge_bps"] = np.where(is_long, trades["actual_long_expected_net_edge_bps"], trades["actual_short_expected_net_edge_bps"])
|
||||
trades["actual_edge_bps"] = np.where(is_long, trades["long_trade_net_edge_bps"], trades["short_trade_net_edge_bps"])
|
||||
trades["label_max_edge_bps"] = np.where(is_long, trades["actual_long_expected_net_edge_bps"], trades["actual_short_expected_net_edge_bps"])
|
||||
trades["entry_target"] = np.where(is_long, trades["long_entry_target"], trades["short_entry_target"])
|
||||
trades["planned_ratio"] = _planned_ratio(trades["predicted_edge_bps"], trades["market_risk_prob"], thresholds["min_expected_edge_bps"])
|
||||
effective_pm_config = pm_config or _pm_config_from_thresholds(thresholds)
|
||||
effective_price_plan = price_plan or DEFAULT_BACKTEST_PRICE_PLAN
|
||||
trades["time_to_exit_ms"] = _time_to_exit_ms(trades, is_long, effective_price_plan)
|
||||
trades["planned_ratio"] = _planned_ratio_like_position_manager(trades, effective_pm_config["sizing"], effective_price_plan)
|
||||
trades = trades[trades["planned_ratio"] > 0].copy()
|
||||
if trades.empty:
|
||||
return _empty_trade_frame()
|
||||
trades["weighted_edge_bps"] = trades["actual_edge_bps"] * trades["planned_ratio"]
|
||||
trades["threshold_hash"] = sha256_json(thresholds)[:16]
|
||||
trades = _enforce_non_overlapping_entries(trades, effective_pm_config, effective_price_plan)
|
||||
return trades[
|
||||
[
|
||||
"sample_id",
|
||||
@@ -290,8 +417,10 @@ def _simulate_open_trades(frame: pd.DataFrame, thresholds: dict[str, float]) ->
|
||||
"entry_prob",
|
||||
"market_risk_prob",
|
||||
"predicted_edge_bps",
|
||||
"label_max_edge_bps",
|
||||
"actual_edge_bps",
|
||||
"entry_target",
|
||||
"time_to_exit_ms",
|
||||
"planned_ratio",
|
||||
"weighted_edge_bps",
|
||||
"threshold_hash",
|
||||
@@ -311,8 +440,10 @@ def _empty_trade_frame() -> pd.DataFrame:
|
||||
"entry_prob",
|
||||
"market_risk_prob",
|
||||
"predicted_edge_bps",
|
||||
"label_max_edge_bps",
|
||||
"actual_edge_bps",
|
||||
"entry_target",
|
||||
"time_to_exit_ms",
|
||||
"planned_ratio",
|
||||
"weighted_edge_bps",
|
||||
"threshold_hash",
|
||||
@@ -320,10 +451,78 @@ def _empty_trade_frame() -> pd.DataFrame:
|
||||
)
|
||||
|
||||
|
||||
def _planned_ratio(predicted_edge: pd.Series, market_risk: pd.Series, min_edge: float) -> np.ndarray:
|
||||
edge_strength = ((predicted_edge.astype(float) - min_edge) / 20.0).clip(lower=0.0, upper=1.5)
|
||||
risk_discount = (1.0 - market_risk.astype(float)).clip(lower=0.0, upper=1.0)
|
||||
return (edge_strength * risk_discount).clip(lower=0.05, upper=1.0).to_numpy()
|
||||
def _time_to_exit_ms(trades: pd.DataFrame, is_long: pd.Series, price_plan: dict[str, Any]) -> np.ndarray:
|
||||
max_hold_ms = int(price_plan.get("maxHoldMinutes", DEFAULT_BACKTEST_PRICE_PLAN["maxHoldMinutes"])) * 60_000
|
||||
long_exit_col = "long_time_to_exit_ms"
|
||||
short_exit_col = "short_time_to_exit_ms"
|
||||
if long_exit_col in trades.columns and short_exit_col in trades.columns:
|
||||
label_exit_ms = np.where(is_long, trades[long_exit_col], trades[short_exit_col]).astype("float64")
|
||||
return np.where(np.isfinite(label_exit_ms) & (label_exit_ms > 0), label_exit_ms, max_hold_ms)
|
||||
target_hit = np.where(is_long, trades["long_target_hit"], trades["short_target_hit"])
|
||||
stop_hit = np.where(is_long, trades["long_stop_hit"], trades["short_stop_hit"])
|
||||
target_ms = np.where(is_long, trades["long_time_to_target_ms"], trades["short_time_to_target_ms"]).astype("float64")
|
||||
stop_ms = np.where(is_long, trades["long_time_to_stop_ms"], trades["short_time_to_stop_ms"]).astype("float64")
|
||||
return np.where((target_hit == 1) & (target_ms >= 0), target_ms, np.where((stop_hit == 1) & (stop_ms >= 0), stop_ms, max_hold_ms))
|
||||
|
||||
|
||||
def _enforce_non_overlapping_entries(trades: pd.DataFrame, pm_config: dict[str, Any], price_plan: dict[str, Any]) -> pd.DataFrame:
|
||||
if trades.empty:
|
||||
return trades
|
||||
cooldown_ms = int(pm_config.get("add", {}).get("cooldownMinutes", 0)) * 60_000
|
||||
max_hold_ms = int(price_plan.get("maxHoldMinutes", DEFAULT_BACKTEST_PRICE_PLAN["maxHoldMinutes"])) * 60_000
|
||||
sort_columns = ["symbol", "event_time", "predicted_edge_bps"]
|
||||
sorted_keys = trades[["symbol", "event_time", "predicted_edge_bps", "time_to_exit_ms"]].sort_values(
|
||||
sort_columns,
|
||||
ascending=[True, True, False],
|
||||
)
|
||||
event_ns = pd.to_datetime(sorted_keys["event_time"], utc=True).astype("int64").to_numpy()
|
||||
symbols = sorted_keys["symbol"].astype(str).to_numpy()
|
||||
exit_delay_values = pd.to_numeric(sorted_keys["time_to_exit_ms"], errors="coerce").fillna(max_hold_ms).to_numpy(dtype="float64")
|
||||
original_indices = sorted_keys.index.to_numpy()
|
||||
|
||||
next_available_ns_by_symbol: dict[str, int] = {}
|
||||
keep: list[int] = []
|
||||
for index, symbol, event_time_ns, exit_delay_ms in zip(original_indices, symbols, event_ns, exit_delay_values):
|
||||
next_available_ns = next_available_ns_by_symbol.get(symbol)
|
||||
if next_available_ns is not None and event_time_ns < next_available_ns:
|
||||
continue
|
||||
keep.append(index)
|
||||
if not np.isfinite(exit_delay_ms) or exit_delay_ms <= 0:
|
||||
exit_delay_ms = max_hold_ms
|
||||
next_available_ns_by_symbol[symbol] = int(event_time_ns + (exit_delay_ms + cooldown_ms) * 1_000_000)
|
||||
return trades.loc[keep].sort_values("event_time").reset_index(drop=True)
|
||||
|
||||
|
||||
def _planned_ratio_like_position_manager(trades: pd.DataFrame, sizing: dict[str, Any], price_plan: dict[str, Any]) -> np.ndarray:
|
||||
expected_edge = trades["predicted_edge_bps"].astype(float).clip(lower=0.0)
|
||||
direction_strength = trades["direction_prob"].astype(float).clip(lower=0.0, upper=1.0)
|
||||
entry_prob = trades["entry_prob"].astype(float).clip(lower=0.0, upper=1.0)
|
||||
market_risk = trades["market_risk_prob"].astype(float).clip(lower=0.0, upper=1.0)
|
||||
|
||||
min_edge = float(sizing["minEdgeBps"])
|
||||
stop_loss_budget = max(
|
||||
float(price_plan.get("stopDistanceBps", DEFAULT_BACKTEST_PRICE_PLAN["stopDistanceBps"]))
|
||||
+ float(price_plan.get("costBps", DEFAULT_BACKTEST_PRICE_PLAN["costBps"])),
|
||||
1.0,
|
||||
)
|
||||
raw = (
|
||||
float(sizing["baseRatio"])
|
||||
* (expected_edge / stop_loss_budget)
|
||||
* direction_strength
|
||||
* entry_prob
|
||||
* (1.0 - market_risk)
|
||||
)
|
||||
hard_cap = min(
|
||||
float(sizing["maxSingleLegRatio"]),
|
||||
float(sizing["maxTotalPositionRatio"]),
|
||||
float(sizing["maxLiquidityUsageRatio"]),
|
||||
float(sizing["maxLossPerTradeBps"]) / stop_loss_budget,
|
||||
)
|
||||
min_ratio = float(sizing["minInitialRatio"])
|
||||
if hard_cap < min_ratio:
|
||||
return np.zeros(len(trades), dtype="float64")
|
||||
ratio = raw.clip(lower=min_ratio, upper=hard_cap)
|
||||
return np.where(expected_edge >= min_edge, ratio, 0.0)
|
||||
|
||||
|
||||
def _trade_metrics(trades: pd.DataFrame) -> dict[str, Any]:
|
||||
@@ -336,6 +535,9 @@ def _trade_metrics(trades: pd.DataFrame) -> dict[str, Any]:
|
||||
"total_weighted_edge_bps": 0.0,
|
||||
"max_drawdown_bps": 0.0,
|
||||
"avg_planned_ratio": 0.0,
|
||||
"min_planned_ratio": 0.0,
|
||||
"p50_planned_ratio": 0.0,
|
||||
"max_planned_ratio": 0.0,
|
||||
"profit_factor": 0.0,
|
||||
"max_consecutive_losses": 0,
|
||||
}
|
||||
@@ -351,6 +553,9 @@ def _trade_metrics(trades: pd.DataFrame) -> dict[str, Any]:
|
||||
"total_weighted_edge_bps": float(equity.iloc[-1]),
|
||||
"max_drawdown_bps": float(drawdown.max()),
|
||||
"avg_planned_ratio": float(trades["planned_ratio"].astype(float).mean()),
|
||||
"min_planned_ratio": float(trades["planned_ratio"].astype(float).min()),
|
||||
"p50_planned_ratio": float(trades["planned_ratio"].astype(float).median()),
|
||||
"max_planned_ratio": float(trades["planned_ratio"].astype(float).max()),
|
||||
"profit_factor": float(gains / losses) if losses > 0 else float("inf"),
|
||||
"max_consecutive_losses": _max_consecutive_losses(trades["weighted_edge_bps"].astype(float).to_numpy()),
|
||||
}
|
||||
@@ -382,6 +587,8 @@ def _backtest_status(metrics: dict[str, dict[str, Any]]) -> tuple[str, list[str]
|
||||
reasons.append("validation_locked_avg_trade_edge_not_positive")
|
||||
if validation_locked["max_consecutive_losses"] > 8:
|
||||
reasons.append("validation_locked_max_consecutive_losses_above_8")
|
||||
if validation_locked["trade_count"] > 0 and validation_locked["max_planned_ratio"] <= 0.050001:
|
||||
reasons.append("validation_locked_sizing_collapsed_to_min_initial")
|
||||
if stress["trade_count"] < 20:
|
||||
reasons.append("latest_stress_trade_count_below_20")
|
||||
if stress["profit_factor"] < 1.0:
|
||||
@@ -427,6 +634,7 @@ def _pm_config_from_thresholds(thresholds: dict[str, float]) -> dict:
|
||||
}
|
||||
)
|
||||
config["add"]["maxMarketRiskProb"] = thresholds["max_market_risk_prob"]
|
||||
config["add"]["minEntryProb"] = thresholds["min_entry_prob"]
|
||||
config["add"]["minExpectedEdgeBps"] = thresholds["min_expected_edge_bps"]
|
||||
config["sizing"]["minEdgeBps"] = thresholds["min_expected_edge_bps"]
|
||||
config["sizing"]["maxSingleLegRatio"] = 1.0
|
||||
@@ -479,7 +687,7 @@ def _write_pm_report(path, candidates: pd.DataFrame, best_thresholds: dict[str,
|
||||
lines = [
|
||||
"# PM Threshold Report",
|
||||
"",
|
||||
"本次不是固定写死阈值,而是在验证集上试一组可复现的阈值,选择净收益、回撤、交易数量综合更好的那组。",
|
||||
"本次不是固定写死阈值,而是在调参集上试一组可复现的阈值。PM 回测使用固定止盈止损后的真实净收益,并且开仓后按持仓结束时间加冷却时间阻止重叠开仓。",
|
||||
"",
|
||||
"## Best Thresholds",
|
||||
"",
|
||||
@@ -505,7 +713,7 @@ def _write_backtest_report(path, result: dict[str, Any]) -> None:
|
||||
lines = [
|
||||
"# Integrated Backtest Report",
|
||||
"",
|
||||
"这里用验证集模型输出和 PM 阈值生成交易明细,统计净收益、胜率、回撤和分段表现。",
|
||||
"这里用验证集模型输出和 PM 阈值生成交易明细,统计净收益、胜率、回撤和分段表现。收益按固定止盈止损计划的真实净收益计算,不使用窗口内最大可拿收益。",
|
||||
"",
|
||||
"```json",
|
||||
str(result).replace("'", '"'),
|
||||
|
||||
@@ -194,7 +194,7 @@ def _plan_side_rows(
|
||||
gross = np.where(target_first, target_bps, np.where(stop_first, -stop_bps, timeout_return))
|
||||
price_plan_net = gross - cost_bps
|
||||
expected_net = max_achievable_gross - cost_bps
|
||||
positive = expected_net >= min_expected_edge_bps
|
||||
positive = price_plan_net >= min_expected_edge_bps
|
||||
ambiguous = target_any & stop_any & (target_index == stop_index)
|
||||
|
||||
rows: list[dict[str, Any]] = []
|
||||
@@ -264,21 +264,25 @@ def _plan_summary(rows: pd.DataFrame) -> pd.DataFrame:
|
||||
split_rows["avg_price_plan_edge_eval"] = split_rows[
|
||||
[f"avg_price_plan_net_edge_bps_{split}" for split in (TUNE_SPLIT, VALIDATION_LOCKED_SPLIT, LATEST_STRESS_SPLIT)]
|
||||
].mean(axis=1)
|
||||
split_rows["min_price_plan_edge_eval"] = split_rows[
|
||||
[f"avg_price_plan_net_edge_bps_{split}" for split in (TUNE_SPLIT, VALIDATION_LOCKED_SPLIT, LATEST_STRESS_SPLIT)]
|
||||
].min(axis=1)
|
||||
split_rows["min_margin_eval"] = split_rows[
|
||||
[f"target_rate_margin_{split}" for split in (TUNE_SPLIT, VALIDATION_LOCKED_SPLIT, LATEST_STRESS_SPLIT)]
|
||||
].min(axis=1)
|
||||
# The search score is not an上线门槛. It only chooses the next experiment:
|
||||
# enough positive samples, less negative average edge, and stable behavior
|
||||
# across tune/validation/stress.
|
||||
# The search score is not an上线门槛. It only chooses the next experiment.
|
||||
# Fixed price-plan net edge is weighted most heavily; max future edge is
|
||||
# retained only as a weak tie-breaker because it can be optimistic.
|
||||
positive_rate_penalty = (
|
||||
(0.08 - split_rows["min_positive_label_rate_eval"]).clip(lower=0.0) * 80.0
|
||||
+ (split_rows["max_positive_label_rate_eval"] - 0.45).clip(lower=0.0) * 30.0
|
||||
(0.03 - split_rows["min_positive_label_rate_eval"]).clip(lower=0.0) * 120.0
|
||||
+ (split_rows["max_positive_label_rate_eval"] - 0.55).clip(lower=0.0) * 20.0
|
||||
)
|
||||
spread_bonus = np.log1p((split_rows["target_bps"] - split_rows["stop_bps"]).clip(lower=0.0))
|
||||
split_rows["score"] = (
|
||||
split_rows["avg_edge_eval"]
|
||||
+ split_rows["avg_price_plan_edge_eval"] * 0.5
|
||||
+ split_rows["min_margin_eval"] * 20.0
|
||||
split_rows["avg_price_plan_edge_eval"] * 8.0
|
||||
+ split_rows["min_price_plan_edge_eval"] * 3.0
|
||||
+ split_rows["min_margin_eval"] * 80.0
|
||||
+ split_rows["avg_edge_eval"] * 0.05
|
||||
- positive_rate_penalty
|
||||
+ spread_bonus
|
||||
)
|
||||
@@ -287,8 +291,10 @@ def _plan_summary(rows: pd.DataFrame) -> pd.DataFrame:
|
||||
|
||||
def _select_best_plan(summary: pd.DataFrame) -> dict[str, Any]:
|
||||
candidates = summary[
|
||||
(summary["min_positive_label_rate_eval"] >= 0.08)
|
||||
& (summary["max_positive_label_rate_eval"] <= 0.45)
|
||||
(summary["min_positive_label_rate_eval"] >= 0.03)
|
||||
& (summary["max_positive_label_rate_eval"] <= 0.55)
|
||||
& (summary["avg_price_plan_edge_eval"] > 0.0)
|
||||
& (summary["min_price_plan_edge_eval"] > -1.0)
|
||||
& (summary["target_bps"] > summary["stop_bps"])
|
||||
]
|
||||
if candidates.empty:
|
||||
@@ -305,6 +311,7 @@ def _select_best_plan(summary: pd.DataFrame) -> dict[str, Any]:
|
||||
"score": float(row["score"]),
|
||||
"avg_edge_eval": float(row["avg_edge_eval"]),
|
||||
"avg_price_plan_edge_eval": float(row["avg_price_plan_edge_eval"]),
|
||||
"min_price_plan_edge_eval": float(row["min_price_plan_edge_eval"]),
|
||||
"min_margin_eval": float(row["min_margin_eval"]),
|
||||
"min_positive_label_rate_eval": float(row["min_positive_label_rate_eval"]),
|
||||
"max_positive_label_rate_eval": float(row["max_positive_label_rate_eval"]),
|
||||
@@ -332,7 +339,7 @@ def _markdown_report(payload: dict[str, Any], summary: pd.DataFrame) -> str:
|
||||
"",
|
||||
_markdown_table(top),
|
||||
"",
|
||||
"说明:positive_label_rate 和 avg_expected_net_edge_bps 按“未来窗口最大可拿净收益”统计;target_hit_rate、stop_hit_rate、avg_price_plan_net_edge_bps 只用来检查固定止盈止损计划是否顺手。这里选的是下一轮实验用的价格计划,不是上线结论。真正能不能上线仍然看模型训练、PM 搜索、validation_locked 和 latest_stress 回测。",
|
||||
"说明:positive_label_rate 和 avg_price_plan_net_edge_bps 按固定止盈止损计划统计;avg_expected_net_edge_bps 只是辅助观察未来最大可拿空间,不能单独决定价格计划。这里选的是下一轮实验用的价格计划,不是上线结论。真正能不能上线仍然看模型训练、PM 搜索、validation_locked 和 latest_stress 回测。",
|
||||
"",
|
||||
]
|
||||
return "\n".join(lines)
|
||||
|
||||
@@ -5,7 +5,7 @@ from typing import Any
|
||||
|
||||
|
||||
FEATURE_VERSION = "feature-v4-p2-book-cross"
|
||||
LABEL_VERSION = "label-v4-p1-max-edge"
|
||||
LABEL_VERSION = "label-v4-p5-entry-quality"
|
||||
SPLIT_VERSION = "split-v4-p0"
|
||||
MODEL_BUNDLE_VERSION = "trader-v4-btc-p0"
|
||||
CALIBRATION_BUNDLE_VERSION = "cal-v4-btc-p0"
|
||||
|
||||
@@ -0,0 +1,744 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.linear_model import HuberRegressor, LogisticRegression, Ridge
|
||||
from sklearn.metrics import brier_score_loss, mean_absolute_error, roc_auc_score
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
from trader_training.io_utils import read_json, read_parquet, run_root, sha256_json, write_json, write_parquet, write_text
|
||||
from trader_training.labels import DEFAULT_LABEL_CONFIG, _build_path_stats
|
||||
from trader_training.schemas import FEATURE_ORDER, FIT_SPLIT, LATEST_STRESS_SPLIT, TUNE_SPLIT, VALIDATION_LOCKED_SPLIT
|
||||
|
||||
|
||||
STATE_FEATURES = [
|
||||
"position_side_sign",
|
||||
"time_in_position_minutes",
|
||||
"unrealized_pnl_bps",
|
||||
"mfe_since_entry_bps",
|
||||
"mae_since_entry_bps",
|
||||
"distance_to_stop_bps",
|
||||
"distance_to_target_bps",
|
||||
"entry_predicted_edge_bps",
|
||||
"entry_direction_prob",
|
||||
"path_efficiency",
|
||||
"giveback_from_mfe_bps",
|
||||
"recovery_from_mae_bps",
|
||||
"mfe_mae_ratio",
|
||||
"side_ret_1m_bps",
|
||||
"side_ret_5m_bps",
|
||||
"side_taker_imbalance_1m",
|
||||
"side_taker_imbalance_5m",
|
||||
"side_book_microprice_basis_bps",
|
||||
"side_book_pressure_taker_1m",
|
||||
"side_book_pressure_taker_5m",
|
||||
"add_count",
|
||||
"minutes_since_last_add",
|
||||
]
|
||||
|
||||
EVAL_SPLITS = (TUNE_SPLIT, VALIDATION_LOCKED_SPLIT, LATEST_STRESS_SPLIT)
|
||||
ALL_SPLITS = (FIT_SPLIT, TUNE_SPLIT, VALIDATION_LOCKED_SPLIT, LATEST_STRESS_SPLIT)
|
||||
|
||||
|
||||
def run_state_continue_experiment(args: Any) -> None:
|
||||
root = run_root(args)
|
||||
baseline_root = args.data_root / "trader-v4" / "runs" / args.baseline_run_id
|
||||
out_dir = root / "experiments" / "state_continue"
|
||||
ages = _parse_ages(args.ages_minutes)
|
||||
regressor_kind = getattr(args, "regressor_kind", "huber")
|
||||
ridge_alpha = float(getattr(args, "ridge_alpha", 10.0))
|
||||
huber_alpha = float(getattr(args, "huber_alpha", 0.001))
|
||||
huber_epsilon = float(getattr(args, "huber_epsilon", 1.35))
|
||||
huber_max_iter = int(getattr(args, "huber_max_iter", 1000))
|
||||
regression_target_clip_bps = float(getattr(args, "regression_target_clip_bps", 0.0))
|
||||
logging.info(
|
||||
"trader.training.state_continue_experiment_started runId=%s baselineRunId=%s ages=%s regressorKind=%s ridgeAlpha=%s huberAlpha=%s huberEpsilon=%s huberMaxIter=%s regressionTargetClipBps=%s",
|
||||
args.run_id,
|
||||
args.baseline_run_id,
|
||||
ages,
|
||||
regressor_kind,
|
||||
ridge_alpha,
|
||||
huber_alpha,
|
||||
huber_epsilon,
|
||||
huber_max_iter,
|
||||
regression_target_clip_bps,
|
||||
)
|
||||
|
||||
feature = _load_feature_frame(baseline_root)
|
||||
frozen_scores = _frozen_entry_scores_by_sample(baseline_root, feature)
|
||||
entry = _load_entry_labels(baseline_root, feature, frozen_scores)
|
||||
replay = _load_replay(baseline_root)
|
||||
plan = read_json(baseline_root / "label" / "price_plan_context.json")
|
||||
stop_bps = float(plan["stopDistanceBps"])
|
||||
target_bps = float(plan["targetDistanceBps"])
|
||||
cost_bps = float(plan["costBps"])
|
||||
continue_horizon = int(DEFAULT_LABEL_CONFIG["continue"]["horizon_minutes"])
|
||||
min_continue_edge_bps = float(DEFAULT_LABEL_CONFIG["continue"]["min_expected_continue_edge_bps"])
|
||||
|
||||
state_frame = _build_state_frame(feature, entry, replay, ages, stop_bps, target_bps, cost_bps, continue_horizon, min_continue_edge_bps)
|
||||
if args.max_rows_per_split:
|
||||
state_frame = _cap_rows_per_split(state_frame, int(args.max_rows_per_split))
|
||||
dataset_hash = write_parquet(out_dir / "state_continue_train.parquet", state_frame)
|
||||
logging.info(
|
||||
"trader.training.state_continue_dataset_written runId=%s rowCount=%s splitCounts=%s path=%s",
|
||||
args.run_id,
|
||||
len(state_frame),
|
||||
state_frame["split_id"].value_counts().to_dict(),
|
||||
out_dir / "state_continue_train.parquet",
|
||||
)
|
||||
|
||||
source_manifest = _source_manifest(
|
||||
args,
|
||||
baseline_root,
|
||||
ages,
|
||||
stop_bps,
|
||||
target_bps,
|
||||
cost_bps,
|
||||
continue_horizon,
|
||||
min_continue_edge_bps,
|
||||
state_frame,
|
||||
dataset_hash,
|
||||
regressor_kind,
|
||||
ridge_alpha,
|
||||
huber_alpha,
|
||||
huber_epsilon,
|
||||
huber_max_iter,
|
||||
regression_target_clip_bps,
|
||||
)
|
||||
write_json(out_dir / "experiment_manifest.json", source_manifest)
|
||||
write_json(out_dir / "position_state_feature_schema.json", _state_feature_schema())
|
||||
order_hash = write_json(out_dir / "position_state_feature_order.json", STATE_FEATURES)
|
||||
write_json(
|
||||
out_dir / "position_state_source_manifest.json",
|
||||
{
|
||||
"entry_predicted_edge_bps": "run-10 frozen ENTRY ONNX output selected by entry side",
|
||||
"entry_direction_prob": "run-10 frozen DIRECTION ONNX output selected by entry side",
|
||||
"path_features": "position path shape and side-adjusted market pressure features computed at current state time",
|
||||
"out_of_fold_used": False,
|
||||
"frozen_model_output_used": True,
|
||||
"frozen_model_output_policy": "baseline model is fixed and is not retrained inside this experiment",
|
||||
"replay_decision_trace_used": False,
|
||||
"state_feature_order_hash": order_hash,
|
||||
"row_count": len(state_frame),
|
||||
"split_counts": state_frame["split_id"].value_counts().to_dict(),
|
||||
},
|
||||
)
|
||||
|
||||
feature_sets = {
|
||||
"market_only": FEATURE_ORDER,
|
||||
"market_plus_state": [*FEATURE_ORDER, *STATE_FEATURES],
|
||||
}
|
||||
results: dict[str, Any] = {}
|
||||
prediction_frames: list[pd.DataFrame] = []
|
||||
for side in ("LONG", "SHORT"):
|
||||
side_frame = state_frame[state_frame["position_side"].eq(side)].copy()
|
||||
for feature_set_name, feature_columns in feature_sets.items():
|
||||
key = f"{side.lower()}_{feature_set_name}"
|
||||
result, predictions = _train_side_models(
|
||||
side_frame,
|
||||
side,
|
||||
feature_columns,
|
||||
regressor_kind,
|
||||
ridge_alpha,
|
||||
huber_alpha,
|
||||
huber_epsilon,
|
||||
huber_max_iter,
|
||||
regression_target_clip_bps,
|
||||
)
|
||||
results[key] = result
|
||||
predictions["side"] = side
|
||||
predictions["feature_set"] = feature_set_name
|
||||
prediction_frames.append(predictions)
|
||||
logging.info(
|
||||
"trader.training.state_continue_model_trained runId=%s side=%s featureSet=%s tuneAuc=%s tuneMaeRatio=%s",
|
||||
args.run_id,
|
||||
side,
|
||||
feature_set_name,
|
||||
result.get(TUNE_SPLIT, {}).get("continue_auc"),
|
||||
result.get(TUNE_SPLIT, {}).get("edge_mae_vs_constant_ratio"),
|
||||
)
|
||||
|
||||
predictions = pd.concat(prediction_frames, ignore_index=True) if prediction_frames else pd.DataFrame()
|
||||
verdict = _verdict(results)
|
||||
write_parquet(out_dir / "state_continue_predictions.parquet", predictions)
|
||||
write_json(out_dir / "state_continue_result.json", results)
|
||||
write_json(out_dir / "state_continue_verdict.json", verdict)
|
||||
write_text(out_dir / "state_continue_experiment_report.md", _report(args, baseline_root, source_manifest, results, verdict))
|
||||
logging.info("trader.training.state_continue_experiment_finished runId=%s report=%s", args.run_id, out_dir / "state_continue_experiment_report.md")
|
||||
|
||||
|
||||
def _parse_ages(raw: str) -> list[int]:
|
||||
ages = [int(item.strip()) for item in raw.split(",") if item.strip()]
|
||||
if not ages or any(age <= 0 for age in ages):
|
||||
raise ValueError(f"invalid ages-minutes: {raw}")
|
||||
return sorted(set(ages))
|
||||
|
||||
|
||||
def _load_feature_frame(baseline_root: Path) -> pd.DataFrame:
|
||||
feature = read_parquet(baseline_root / "feature" / "feature_frame.parquet")
|
||||
required = {"sample_id", "symbol", "event_time", "open_time_ms", "split_id", "walk_forward_fold", "data_quality_flag", *FEATURE_ORDER}
|
||||
missing = sorted(required.difference(feature.columns))
|
||||
if missing:
|
||||
raise ValueError(f"baseline feature frame missing columns: {missing}")
|
||||
feature = feature[feature["data_quality_flag"].isin(["OK", "PARTIAL_OPTIONAL"])].copy()
|
||||
feature = feature[feature["split_id"].isin(ALL_SPLITS)].copy()
|
||||
return feature
|
||||
|
||||
|
||||
def _load_entry_labels(baseline_root: Path, feature: pd.DataFrame, frozen_scores: pd.DataFrame) -> pd.DataFrame:
|
||||
entry = read_parquet(baseline_root / "label" / "entry_labels.parquet")
|
||||
required = {"sample_id", "symbol", "event_time", "side", "entry_target", "split_id", "walk_forward_fold"}
|
||||
missing = sorted(required.difference(entry.columns))
|
||||
if missing:
|
||||
raise ValueError(f"baseline entry labels missing columns: {missing}")
|
||||
entry = entry[(entry["entry_target"] == 1) & (entry["side"].isin(["LONG", "SHORT"]))].copy()
|
||||
entry["entry_open_time_ms"] = pd.to_datetime(entry["event_time"], utc=True).astype("int64") // 1_000_000
|
||||
entry = entry.merge(frozen_scores, on="sample_id", how="inner")
|
||||
if entry.empty:
|
||||
raise ValueError("state continue entry set is empty after merging frozen baseline model outputs")
|
||||
long_mask = entry["side"].eq("LONG")
|
||||
entry["entry_predicted_edge_bps"] = np.where(
|
||||
long_mask,
|
||||
entry["frozen_long_expected_net_edge_bps"],
|
||||
entry["frozen_short_expected_net_edge_bps"],
|
||||
)
|
||||
entry["entry_direction_prob"] = np.where(long_mask, entry["frozen_long_prob"], entry["frozen_short_prob"])
|
||||
return entry[
|
||||
[
|
||||
"sample_id",
|
||||
"symbol",
|
||||
"event_time",
|
||||
"side",
|
||||
"entry_open_time_ms",
|
||||
"entry_predicted_edge_bps",
|
||||
"entry_direction_prob",
|
||||
]
|
||||
].copy()
|
||||
|
||||
|
||||
def _frozen_entry_scores_by_sample(baseline_root: Path, feature: pd.DataFrame) -> pd.DataFrame:
|
||||
source = feature[["sample_id", *FEATURE_ORDER]].drop_duplicates("sample_id").copy()
|
||||
direction = _predict_frozen_linear_model(
|
||||
baseline_root / "model" / "direction" / "direction.onnx",
|
||||
source,
|
||||
{
|
||||
"direction": ("softmax", ("frozen_long_prob", "frozen_short_prob", "frozen_neutral_prob")),
|
||||
},
|
||||
)
|
||||
entry = _predict_frozen_linear_model(
|
||||
baseline_root / "model" / "entry" / "entry.onnx",
|
||||
source,
|
||||
{
|
||||
"long_entry_prob": ("sigmoid", ("frozen_long_entry_prob",)),
|
||||
"short_entry_prob": ("sigmoid", ("frozen_short_entry_prob",)),
|
||||
"long_expected_net_edge_bps": ("identity", ("frozen_long_expected_net_edge_bps",)),
|
||||
"short_expected_net_edge_bps": ("identity", ("frozen_short_expected_net_edge_bps",)),
|
||||
},
|
||||
)
|
||||
return direction.merge(entry, on="sample_id", how="inner")
|
||||
|
||||
|
||||
def _predict_frozen_linear_model(model_path: Path, frame: pd.DataFrame, heads: dict[str, tuple[str, tuple[str, ...]]]) -> pd.DataFrame:
|
||||
try:
|
||||
import onnx
|
||||
from onnx import numpy_helper
|
||||
except ModuleNotFoundError as exc:
|
||||
raise SystemExit("Python package 'onnx' is required to read frozen baseline ONNX weights.") from exc
|
||||
if not model_path.is_file():
|
||||
raise FileNotFoundError(f"frozen model is missing: {model_path}")
|
||||
model = onnx.load(model_path)
|
||||
initializers = {item.name: numpy_helper.to_array(item) for item in model.graph.initializer}
|
||||
x = frame[FEATURE_ORDER].apply(pd.to_numeric, errors="coerce").replace([np.inf, -np.inf], np.nan).fillna(0.0).astype("float32").to_numpy()
|
||||
out = pd.DataFrame({"sample_id": frame["sample_id"].to_numpy()})
|
||||
for head_name, (kind, output_columns) in heads.items():
|
||||
weight_name = f"{head_name}_W"
|
||||
bias_name = f"{head_name}_B"
|
||||
if weight_name not in initializers or bias_name not in initializers:
|
||||
raise ValueError(f"frozen model {model_path} is missing head initializers: {head_name}")
|
||||
values = x @ np.asarray(initializers[weight_name], dtype=np.float32) + np.asarray(initializers[bias_name], dtype=np.float32).reshape(1, -1)
|
||||
if kind == "softmax":
|
||||
values = _softmax(values)
|
||||
elif kind == "sigmoid":
|
||||
values = _sigmoid(values)
|
||||
elif kind != "identity":
|
||||
raise ValueError(f"unsupported frozen head kind: {kind}")
|
||||
if values.shape[1] != len(output_columns):
|
||||
raise ValueError(f"head {head_name} output width mismatch: {values.shape[1]} != {len(output_columns)}")
|
||||
for index, column in enumerate(output_columns):
|
||||
out[column] = values[:, index].astype("float32")
|
||||
return out
|
||||
|
||||
|
||||
def _softmax(values: np.ndarray) -> np.ndarray:
|
||||
shifted = values - np.max(values, axis=1, keepdims=True)
|
||||
exp = np.exp(shifted)
|
||||
return exp / exp.sum(axis=1, keepdims=True)
|
||||
|
||||
|
||||
def _sigmoid(values: np.ndarray) -> np.ndarray:
|
||||
clipped = np.clip(values, -50.0, 50.0)
|
||||
return 1.0 / (1.0 + np.exp(-clipped))
|
||||
|
||||
|
||||
def _load_replay(baseline_root: Path) -> pd.DataFrame:
|
||||
split_manifest = read_json(baseline_root / "split" / "split_manifest.json")
|
||||
replay_path = Path(split_manifest["source_replay_path"])
|
||||
replay = read_parquet(replay_path)
|
||||
required = {"symbol", "event_time", "open_time_ms", "high", "low", "close", "spread_bps"}
|
||||
missing = sorted(required.difference(replay.columns))
|
||||
if missing:
|
||||
raise ValueError(f"source replay missing columns: {missing}")
|
||||
return replay.sort_values(["symbol", "open_time_ms"]).reset_index(drop=True)
|
||||
|
||||
|
||||
def _build_state_frame(
|
||||
feature: pd.DataFrame,
|
||||
entry: pd.DataFrame,
|
||||
replay: pd.DataFrame,
|
||||
ages: list[int],
|
||||
stop_bps: float,
|
||||
target_bps: float,
|
||||
cost_bps: float,
|
||||
continue_horizon: int,
|
||||
min_continue_edge_bps: float,
|
||||
) -> pd.DataFrame:
|
||||
future_stats = _build_path_stats(replay, horizon=continue_horizon, target_bps=target_bps, stop_bps=stop_bps)
|
||||
future_stats = future_stats.rename(columns={"open_time_ms": "current_open_time_ms"})
|
||||
current_feature = feature.rename(columns={"sample_id": "current_sample_id", "event_time": "current_event_time", "open_time_ms": "current_open_time_ms"})
|
||||
replay_state_source = _state_source_by_age(replay, ages)
|
||||
frames: list[pd.DataFrame] = []
|
||||
for age in ages:
|
||||
candidates = entry.copy()
|
||||
candidates["time_in_position_minutes"] = age
|
||||
candidates["add_count"] = 0.0
|
||||
candidates["minutes_since_last_add"] = 9999.0
|
||||
candidates["current_open_time_ms"] = candidates["entry_open_time_ms"] + age * 60_000
|
||||
candidates = candidates.merge(
|
||||
replay_state_source[replay_state_source["time_in_position_minutes"].eq(age)],
|
||||
on=["symbol", "current_open_time_ms", "time_in_position_minutes"],
|
||||
how="inner",
|
||||
)
|
||||
candidates = candidates.merge(current_feature, on=["symbol", "current_open_time_ms"], how="inner")
|
||||
candidates = candidates.merge(
|
||||
future_stats,
|
||||
left_on=["symbol", "current_open_time_ms", "side"],
|
||||
right_on=["symbol", "current_open_time_ms", "side"],
|
||||
how="inner",
|
||||
)
|
||||
if candidates.empty:
|
||||
continue
|
||||
frames.append(_state_rows_for_age(candidates, stop_bps, target_bps, cost_bps, min_continue_edge_bps))
|
||||
logging.info("trader.training.state_continue_age_built ageMinutes=%s rowCount=%s", age, len(candidates))
|
||||
if not frames:
|
||||
raise ValueError("state continue experiment produced no rows")
|
||||
out = pd.concat(frames, ignore_index=True)
|
||||
out = out.replace([np.inf, -np.inf], np.nan)
|
||||
required = [*FEATURE_ORDER, *STATE_FEATURES, "continue_target", "expected_continue_edge_bps"]
|
||||
out = out.dropna(subset=required).copy()
|
||||
return out
|
||||
|
||||
|
||||
def _state_source_by_age(replay: pd.DataFrame, ages: list[int]) -> pd.DataFrame:
|
||||
frames: list[pd.DataFrame] = []
|
||||
for _, group in replay.groupby("symbol", sort=False, observed=False):
|
||||
group = group.sort_values("open_time_ms").copy()
|
||||
for age in ages:
|
||||
rolling_high = group["high"].rolling(age + 1, min_periods=age + 1).max()
|
||||
rolling_low = group["low"].rolling(age + 1, min_periods=age + 1).min()
|
||||
frame = pd.DataFrame(
|
||||
{
|
||||
"symbol": group["symbol"],
|
||||
"current_open_time_ms": group["open_time_ms"],
|
||||
"time_in_position_minutes": age,
|
||||
"entry_price": group["close"].shift(age),
|
||||
"current_price": group["close"],
|
||||
"high_since_entry": rolling_high,
|
||||
"low_since_entry": rolling_low,
|
||||
}
|
||||
)
|
||||
frames.append(frame.dropna())
|
||||
return pd.concat(frames, ignore_index=True) if frames else pd.DataFrame()
|
||||
|
||||
|
||||
def _state_rows_for_age(frame: pd.DataFrame, stop_bps: float, target_bps: float, cost_bps: float, min_continue_edge_bps: float = 5.0) -> pd.DataFrame:
|
||||
side_sign = np.where(frame["side"].eq("LONG"), 1.0, -1.0)
|
||||
entry_price = frame["entry_price"].astype(float)
|
||||
current_price = frame["current_price"].astype(float)
|
||||
high_since = frame["high_since_entry"].astype(float)
|
||||
low_since = frame["low_since_entry"].astype(float)
|
||||
|
||||
long_mask = frame["side"].eq("LONG")
|
||||
unrealized = np.where(long_mask, (current_price / entry_price - 1.0) * 10000.0, (entry_price / current_price - 1.0) * 10000.0) - cost_bps
|
||||
mfe = np.where(long_mask, (high_since / entry_price - 1.0) * 10000.0, (entry_price / low_since - 1.0) * 10000.0)
|
||||
mae = np.where(long_mask, (entry_price / low_since - 1.0) * 10000.0, (high_since / entry_price - 1.0) * 10000.0)
|
||||
stop_price = np.where(long_mask, entry_price * (1.0 - stop_bps / 10000.0), entry_price * (1.0 + stop_bps / 10000.0))
|
||||
target_price = np.where(long_mask, entry_price * (1.0 + target_bps / 10000.0), entry_price * (1.0 - target_bps / 10000.0))
|
||||
distance_to_stop = np.where(long_mask, (current_price / stop_price - 1.0) * 10000.0, (stop_price / current_price - 1.0) * 10000.0)
|
||||
distance_to_target = np.where(long_mask, (target_price / current_price - 1.0) * 10000.0, (current_price / target_price - 1.0) * 10000.0)
|
||||
# Continue must score the first price-plan outcome from the current state, not the raw horizon close.
|
||||
expected_edge = frame["gross_edge_bps"].astype(float) - cost_bps
|
||||
continue_target = ((expected_edge >= min_continue_edge_bps) & (frame["stop_hit"].astype(int) == 0)).astype("int8")
|
||||
|
||||
out = frame[
|
||||
[
|
||||
"current_sample_id",
|
||||
"symbol",
|
||||
"current_event_time",
|
||||
"current_open_time_ms",
|
||||
"side",
|
||||
"split_id",
|
||||
"walk_forward_fold",
|
||||
*FEATURE_ORDER,
|
||||
]
|
||||
].copy()
|
||||
out = out.rename(
|
||||
columns={
|
||||
"current_sample_id": "sample_id",
|
||||
"current_event_time": "event_time",
|
||||
"current_open_time_ms": "open_time_ms",
|
||||
"side": "position_side",
|
||||
}
|
||||
)
|
||||
out["position_side_sign"] = side_sign.astype("float32")
|
||||
out["time_in_position_minutes"] = frame["time_in_position_minutes"].astype("float32")
|
||||
out["unrealized_pnl_bps"] = unrealized.astype("float32")
|
||||
out["mfe_since_entry_bps"] = np.maximum(mfe, 0.0).astype("float32")
|
||||
out["mae_since_entry_bps"] = np.maximum(mae, 0.0).astype("float32")
|
||||
out["distance_to_stop_bps"] = distance_to_stop.astype("float32")
|
||||
out["distance_to_target_bps"] = distance_to_target.astype("float32")
|
||||
out["entry_predicted_edge_bps"] = frame["entry_predicted_edge_bps"].astype("float32")
|
||||
out["entry_direction_prob"] = frame["entry_direction_prob"].astype("float32")
|
||||
safe_mfe = np.maximum(mfe, 0.0)
|
||||
safe_mae = np.maximum(mae, 0.0)
|
||||
out["path_efficiency"] = (unrealized / (safe_mfe + safe_mae + 1.0)).astype("float32")
|
||||
out["giveback_from_mfe_bps"] = (safe_mfe - np.maximum(unrealized, 0.0)).astype("float32")
|
||||
out["recovery_from_mae_bps"] = (unrealized + safe_mae).astype("float32")
|
||||
out["mfe_mae_ratio"] = (safe_mfe / (safe_mae + 1.0)).astype("float32")
|
||||
# Convert market pressure into "helps the current position" direction so LONG and SHORT share one meaning.
|
||||
out["side_ret_1m_bps"] = (side_sign * frame["ret_1m_bps"].astype(float)).astype("float32")
|
||||
out["side_ret_5m_bps"] = (side_sign * frame["ret_5m_bps"].astype(float)).astype("float32")
|
||||
out["side_taker_imbalance_1m"] = (side_sign * frame["taker_imbalance_1m"].astype(float)).astype("float32")
|
||||
out["side_taker_imbalance_5m"] = (side_sign * frame["taker_imbalance_5m"].astype(float)).astype("float32")
|
||||
out["side_book_microprice_basis_bps"] = (side_sign * frame["book_microprice_basis_bps"].astype(float)).astype("float32")
|
||||
out["side_book_pressure_taker_1m"] = (side_sign * frame["book_pressure_taker_1m"].astype(float)).astype("float32")
|
||||
out["side_book_pressure_taker_5m"] = (side_sign * frame["book_pressure_taker_5m"].astype(float)).astype("float32")
|
||||
out["add_count"] = frame["add_count"].astype("float32")
|
||||
out["minutes_since_last_add"] = frame["minutes_since_last_add"].astype("float32")
|
||||
out["continue_target"] = continue_target
|
||||
out["expected_continue_edge_bps"] = expected_edge.astype("float32")
|
||||
return out
|
||||
|
||||
|
||||
def _cap_rows_per_split(frame: pd.DataFrame, max_rows_per_split: int) -> pd.DataFrame:
|
||||
capped = []
|
||||
for split_id, part in frame.sort_values("event_time").groupby("split_id", sort=False, observed=False):
|
||||
if len(part) > max_rows_per_split:
|
||||
part = part.tail(max_rows_per_split).copy()
|
||||
capped.append(part)
|
||||
logging.info("trader.training.state_continue_split_capped splitId=%s rowCount=%s maxRows=%s", split_id, len(part), max_rows_per_split)
|
||||
return pd.concat(capped, ignore_index=True)
|
||||
|
||||
|
||||
def _train_side_models(
|
||||
frame: pd.DataFrame,
|
||||
side: str,
|
||||
feature_columns: list[str],
|
||||
regressor_kind: str = "huber",
|
||||
ridge_alpha: float = 10.0,
|
||||
huber_alpha: float = 0.001,
|
||||
huber_epsilon: float = 1.35,
|
||||
huber_max_iter: int = 1000,
|
||||
regression_target_clip_bps: float = 0.0,
|
||||
) -> tuple[dict[str, Any], pd.DataFrame]:
|
||||
train = frame[frame["split_id"].eq(FIT_SPLIT)].copy()
|
||||
if train.empty:
|
||||
raise ValueError(f"state continue {side} has no fit_inner rows")
|
||||
scaler = StandardScaler()
|
||||
x_train = scaler.fit_transform(train[feature_columns].astype("float32"))
|
||||
y_train_cls = train["continue_target"].astype(int).to_numpy()
|
||||
y_train_reg = train["expected_continue_edge_bps"].astype(float).to_numpy()
|
||||
y_train_fit = y_train_reg
|
||||
if regression_target_clip_bps > 0:
|
||||
y_train_fit = np.clip(y_train_reg, -regression_target_clip_bps, regression_target_clip_bps)
|
||||
|
||||
clf = LogisticRegression(max_iter=500)
|
||||
clf.fit(x_train, y_train_cls)
|
||||
reg_max_iter = huber_max_iter
|
||||
if regressor_kind == "huber":
|
||||
reg = HuberRegressor(alpha=huber_alpha, epsilon=huber_epsilon, max_iter=reg_max_iter)
|
||||
elif regressor_kind == "ridge":
|
||||
reg = Ridge(alpha=ridge_alpha)
|
||||
else:
|
||||
raise ValueError(f"unsupported state continue regressor kind: {regressor_kind}")
|
||||
reg.fit(x_train, y_train_fit)
|
||||
|
||||
metrics: dict[str, Any] = {}
|
||||
prediction_frames: list[pd.DataFrame] = []
|
||||
for split_id in ALL_SPLITS:
|
||||
part = frame[frame["split_id"].eq(split_id)].copy()
|
||||
if part.empty:
|
||||
continue
|
||||
x = scaler.transform(part[feature_columns].astype("float32"))
|
||||
y_cls = part["continue_target"].astype(int).to_numpy()
|
||||
y_reg = part["expected_continue_edge_bps"].astype(float).to_numpy()
|
||||
proba = clf.predict_proba(x)[:, 1]
|
||||
pred_edge = reg.predict(x)
|
||||
if regression_target_clip_bps > 0:
|
||||
pred_edge = np.clip(pred_edge, -regression_target_clip_bps, regression_target_clip_bps)
|
||||
metrics[split_id] = _split_metrics(y_train_cls, y_train_reg, y_cls, y_reg, proba, pred_edge)
|
||||
pred_frame = part[
|
||||
[
|
||||
"sample_id",
|
||||
"symbol",
|
||||
"event_time",
|
||||
"split_id",
|
||||
"position_side",
|
||||
"time_in_position_minutes",
|
||||
"unrealized_pnl_bps",
|
||||
"mfe_since_entry_bps",
|
||||
"mae_since_entry_bps",
|
||||
"entry_predicted_edge_bps",
|
||||
"entry_direction_prob",
|
||||
"continue_target",
|
||||
"expected_continue_edge_bps",
|
||||
]
|
||||
].copy()
|
||||
pred_frame["continue_prob"] = proba.astype("float32")
|
||||
pred_frame["predicted_continue_edge_bps"] = pred_edge.astype("float32")
|
||||
prediction_frames.append(pred_frame)
|
||||
metrics["row_count"] = int(len(frame))
|
||||
metrics["feature_count"] = len(feature_columns)
|
||||
metrics["feature_hash"] = sha256_json(feature_columns)
|
||||
n_iter = getattr(reg, "n_iter_", None)
|
||||
metrics["regressor_kind"] = regressor_kind
|
||||
metrics["ridge_alpha"] = ridge_alpha if regressor_kind == "ridge" else None
|
||||
metrics["huber_alpha"] = huber_alpha if regressor_kind == "huber" else None
|
||||
metrics["huber_epsilon"] = huber_epsilon if regressor_kind == "huber" else None
|
||||
metrics["regressor_iterations"] = int(n_iter) if n_iter is not None else 0
|
||||
metrics["regressor_max_iter"] = reg_max_iter
|
||||
metrics["regressor_converged"] = True if n_iter is None else 0 <= metrics["regressor_iterations"] < reg_max_iter
|
||||
metrics["regression_target_clip_bps"] = regression_target_clip_bps if regression_target_clip_bps > 0 else None
|
||||
return metrics, pd.concat(prediction_frames, ignore_index=True)
|
||||
|
||||
|
||||
def _split_metrics(
|
||||
y_train_cls: np.ndarray,
|
||||
y_train_reg: np.ndarray,
|
||||
y_cls: np.ndarray,
|
||||
y_reg: np.ndarray,
|
||||
proba: np.ndarray,
|
||||
pred_edge: np.ndarray,
|
||||
) -> dict[str, Any]:
|
||||
train_rate = float(np.mean(y_train_cls))
|
||||
constant_proba = np.full(len(y_cls), train_rate)
|
||||
train_median = float(np.median(y_train_reg))
|
||||
constant_edge = np.full(len(y_reg), train_median)
|
||||
out: dict[str, Any] = {
|
||||
"row_count": int(len(y_cls)),
|
||||
"positive_rate": float(np.mean(y_cls)),
|
||||
"brier": float(brier_score_loss(y_cls, proba)),
|
||||
"constant_brier": float(brier_score_loss(y_cls, constant_proba)),
|
||||
"edge_mae": float(mean_absolute_error(y_reg, pred_edge)),
|
||||
"edge_constant_mae": float(mean_absolute_error(y_reg, constant_edge)),
|
||||
}
|
||||
if len(np.unique(y_cls)) == 2:
|
||||
out["continue_auc"] = float(roc_auc_score(y_cls, proba))
|
||||
out["brier_vs_constant_ratio"] = float(out["brier"] / out["constant_brier"]) if out["constant_brier"] > 0 else None
|
||||
out["edge_mae_vs_constant_ratio"] = float(out["edge_mae"] / out["edge_constant_mae"]) if out["edge_constant_mae"] > 0 else None
|
||||
return out
|
||||
|
||||
|
||||
def _source_manifest(
|
||||
args: Any,
|
||||
baseline_root: Path,
|
||||
ages: list[int],
|
||||
stop_bps: float,
|
||||
target_bps: float,
|
||||
cost_bps: float,
|
||||
continue_horizon: int,
|
||||
min_continue_edge_bps: float,
|
||||
state_frame: pd.DataFrame,
|
||||
dataset_hash: str,
|
||||
regressor_kind: str,
|
||||
ridge_alpha: float,
|
||||
huber_alpha: float,
|
||||
huber_epsilon: float,
|
||||
huber_max_iter: int,
|
||||
regression_target_clip_bps: float,
|
||||
) -> dict[str, Any]:
|
||||
return {
|
||||
"experiment": "state_continue_diagnostic_v1",
|
||||
"run_id": args.run_id,
|
||||
"baseline_run_id": args.baseline_run_id,
|
||||
"baseline_root": str(baseline_root),
|
||||
"ages_minutes": ages,
|
||||
"target_bps": target_bps,
|
||||
"stop_bps": stop_bps,
|
||||
"cost_bps": cost_bps,
|
||||
"continue_horizon_minutes": continue_horizon,
|
||||
"min_continue_edge_bps": min_continue_edge_bps,
|
||||
"regressor_kind": regressor_kind,
|
||||
"ridge_alpha": ridge_alpha if regressor_kind == "ridge" else None,
|
||||
"huber_alpha": huber_alpha if regressor_kind == "huber" else None,
|
||||
"huber_epsilon": huber_epsilon if regressor_kind == "huber" else None,
|
||||
"huber_max_iter": huber_max_iter if regressor_kind == "huber" else None,
|
||||
"regression_target_clip_bps": regression_target_clip_bps if regression_target_clip_bps > 0 else None,
|
||||
"dataset_hash_sha256": dataset_hash,
|
||||
"row_count": int(len(state_frame)),
|
||||
"split_counts": state_frame["split_id"].value_counts().to_dict(),
|
||||
"side_counts": state_frame["position_side"].value_counts().to_dict(),
|
||||
"feature_inputs": {
|
||||
"market_feature_count": len(FEATURE_ORDER),
|
||||
"state_features": STATE_FEATURES,
|
||||
"state_feature_count": len(STATE_FEATURES),
|
||||
},
|
||||
"leakage_policy": {
|
||||
"uses_future_entry_label_as_feature": False,
|
||||
"uses_same_round_model_prediction_as_feature": False,
|
||||
"entry_predicted_edge_bps": "baseline frozen ENTRY ONNX output selected by side",
|
||||
"entry_direction_prob": "baseline frozen DIRECTION ONNX output selected by side",
|
||||
"expected_continue_edge_bps": "price-plan gross edge minus cost; target/stop/timeout outcome is respected",
|
||||
"continue_target": "expected_continue_edge_bps >= threshold and stop is not the first path barrier",
|
||||
"path_features": "position path shape and side-adjusted market pressure at current state time",
|
||||
"add_count": "synthetic first-position diagnostic, fixed to 0",
|
||||
"minutes_since_last_add": "synthetic first-position diagnostic, fixed to 9999",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _state_feature_schema() -> list[dict[str, Any]]:
|
||||
return [
|
||||
{"name": "position_side_sign", "unit": "-1/1", "source": "synthetic position state", "leakage_check": "known at current position time"},
|
||||
{"name": "time_in_position_minutes", "unit": "minute", "source": "entry time to current time", "leakage_check": "known at current position time"},
|
||||
{"name": "unrealized_pnl_bps", "unit": "bps", "source": "entry price and current close", "leakage_check": "uses <= current time price"},
|
||||
{"name": "mfe_since_entry_bps", "unit": "bps", "source": "high since entry", "leakage_check": "uses only entry..current high"},
|
||||
{"name": "mae_since_entry_bps", "unit": "bps", "source": "low/high since entry", "leakage_check": "uses only entry..current low/high"},
|
||||
{"name": "distance_to_stop_bps", "unit": "bps", "source": "price plan and current close", "leakage_check": "uses fixed plan and current price"},
|
||||
{"name": "distance_to_target_bps", "unit": "bps", "source": "price plan and current close", "leakage_check": "uses fixed plan and current price"},
|
||||
{"name": "entry_predicted_edge_bps", "unit": "bps", "source": "baseline frozen ENTRY ONNX", "leakage_check": "baseline model is fixed before this experiment"},
|
||||
{"name": "entry_direction_prob", "unit": "probability", "source": "baseline frozen DIRECTION ONNX", "leakage_check": "baseline model is fixed before this experiment"},
|
||||
{"name": "path_efficiency", "unit": "ratio", "source": "unrealized_pnl_bps / (mfe + mae + 1)", "leakage_check": "uses entry..current path only"},
|
||||
{"name": "giveback_from_mfe_bps", "unit": "bps", "source": "mfe_since_entry_bps - max(unrealized_pnl_bps, 0)", "leakage_check": "uses entry..current path only"},
|
||||
{"name": "recovery_from_mae_bps", "unit": "bps", "source": "unrealized_pnl_bps + mae_since_entry_bps", "leakage_check": "uses entry..current path only"},
|
||||
{"name": "mfe_mae_ratio", "unit": "ratio", "source": "mfe_since_entry_bps / (mae_since_entry_bps + 1)", "leakage_check": "uses entry..current path only"},
|
||||
{"name": "side_ret_1m_bps", "unit": "bps", "source": "position_side_sign * ret_1m_bps", "leakage_check": "uses <= current time feature only"},
|
||||
{"name": "side_ret_5m_bps", "unit": "bps", "source": "position_side_sign * ret_5m_bps", "leakage_check": "uses <= current time feature only"},
|
||||
{"name": "side_taker_imbalance_1m", "unit": "ratio", "source": "position_side_sign * taker_imbalance_1m", "leakage_check": "uses <= current time feature only"},
|
||||
{"name": "side_taker_imbalance_5m", "unit": "ratio", "source": "position_side_sign * taker_imbalance_5m", "leakage_check": "uses <= current time feature only"},
|
||||
{"name": "side_book_microprice_basis_bps", "unit": "bps", "source": "position_side_sign * book_microprice_basis_bps", "leakage_check": "uses <= current time feature only"},
|
||||
{"name": "side_book_pressure_taker_1m", "unit": "bps", "source": "position_side_sign * book_pressure_taker_1m", "leakage_check": "uses <= current time feature only"},
|
||||
{"name": "side_book_pressure_taker_5m", "unit": "bps", "source": "position_side_sign * book_pressure_taker_5m", "leakage_check": "uses <= current time feature only"},
|
||||
{"name": "add_count", "unit": "count", "source": "synthetic position state", "leakage_check": "known at current position time"},
|
||||
{"name": "minutes_since_last_add", "unit": "minute", "source": "synthetic position state", "leakage_check": "known at current position time"},
|
||||
]
|
||||
|
||||
|
||||
def _verdict(results: dict[str, Any]) -> dict[str, Any]:
|
||||
reasons: list[str] = []
|
||||
passed_checks: list[str] = []
|
||||
for side in ("long", "short"):
|
||||
plus = results[f"{side}_market_plus_state"]
|
||||
base = results[f"{side}_market_only"]
|
||||
if not plus.get("regressor_converged"):
|
||||
reasons.append(f"{side} market_plus_state regressor did not converge")
|
||||
for split_id in (VALIDATION_LOCKED_SPLIT, LATEST_STRESS_SPLIT):
|
||||
plus_metric = plus.get(split_id, {})
|
||||
base_metric = base.get(split_id, {})
|
||||
plus_auc = plus_metric.get("continue_auc")
|
||||
base_auc = base_metric.get("continue_auc")
|
||||
plus_mae = plus_metric.get("edge_mae_vs_constant_ratio")
|
||||
base_mae = base_metric.get("edge_mae_vs_constant_ratio")
|
||||
auc_ok = plus_auc is not None and plus_auc >= 0.60
|
||||
auc_beats_market_only = base_auc is None or (plus_auc is not None and plus_auc > base_auc)
|
||||
if not auc_ok:
|
||||
reasons.append(f"{side} {split_id} continue_auc below 0.60: {plus_auc}")
|
||||
if not auc_beats_market_only:
|
||||
reasons.append(f"{side} {split_id} continue_auc not better than market_only: {plus_auc} <= {base_auc}")
|
||||
if auc_ok and auc_beats_market_only:
|
||||
passed_checks.append(f"{side} {split_id} continue_auc")
|
||||
mae_ok = plus_mae is not None and plus_mae <= 0.97
|
||||
mae_beats_market_only = base_mae is None or (plus_mae is not None and plus_mae < base_mae)
|
||||
if not mae_ok:
|
||||
reasons.append(f"{side} {split_id} edge_mae_vs_constant_ratio above 0.97: {plus_mae}")
|
||||
if not mae_beats_market_only:
|
||||
reasons.append(f"{side} {split_id} edge_mae_vs_constant_ratio not better than market_only: {plus_mae} >= {base_mae}")
|
||||
if mae_ok and mae_beats_market_only:
|
||||
passed_checks.append(f"{side} {split_id} edge_mae_vs_constant_ratio")
|
||||
return {
|
||||
"status": "PASS_TO_FORMAL_CHAIN" if not reasons else "NOT_READY_FOR_FORMAL_CHAIN",
|
||||
"acceptance_rule": {
|
||||
"validation_and_latest_auc_min": 0.60,
|
||||
"validation_and_latest_edge_mae_vs_constant_max": 0.97,
|
||||
"must_beat_market_only": True,
|
||||
"regressor_must_converge": True,
|
||||
},
|
||||
"passed_checks": passed_checks,
|
||||
"reasons": reasons,
|
||||
}
|
||||
|
||||
|
||||
def _report(args: Any, baseline_root: Path, manifest: dict[str, Any], results: dict[str, Any], verdict: dict[str, Any]) -> str:
|
||||
baseline = read_json(baseline_root / "model" / "model_train_manifest.json")
|
||||
continue_metrics = baseline["CONTINUE"]["metrics"]
|
||||
lines = [
|
||||
"# State Continue Experiment Report",
|
||||
"",
|
||||
f"- run_id: `{args.run_id}`",
|
||||
f"- baseline_run_id: `{args.baseline_run_id}`",
|
||||
f"- row_count: `{manifest['row_count']}`",
|
||||
f"- ages_minutes: `{manifest['ages_minutes']}`",
|
||||
f"- regressor_kind: `{manifest['regressor_kind']}`",
|
||||
f"- huber_alpha: `{manifest['huber_alpha']}`",
|
||||
f"- huber_epsilon: `{manifest['huber_epsilon']}`",
|
||||
f"- huber_max_iter: `{manifest['huber_max_iter']}`",
|
||||
f"- regression_target_clip_bps: `{manifest['regression_target_clip_bps']}`",
|
||||
f"- continue_horizon_minutes: `{manifest['continue_horizon_minutes']}`",
|
||||
f"- min_continue_edge_bps: `{manifest['min_continue_edge_bps']}`",
|
||||
"",
|
||||
"## Baseline run-10 Continue",
|
||||
"",
|
||||
"| head | auc | mae_vs_constant |",
|
||||
"| --- | ---: | ---: |",
|
||||
f"| long_continue_prob | {continue_metrics['long_continue_prob'].get('auc')} | |",
|
||||
f"| short_continue_prob | {continue_metrics['short_continue_prob'].get('auc')} | |",
|
||||
f"| long_expected_continue_edge_bps | | {continue_metrics['long_expected_continue_edge_bps'].get('mae_vs_constant_ratio')} |",
|
||||
f"| short_expected_continue_edge_bps | | {continue_metrics['short_expected_continue_edge_bps'].get('mae_vs_constant_ratio')} |",
|
||||
"",
|
||||
"## Diagnostic Result",
|
||||
"",
|
||||
"| side | feature_set | split | rows | auc | brier_ratio | mae_ratio |",
|
||||
"| --- | --- | --- | ---: | ---: | ---: | ---: |",
|
||||
]
|
||||
for key, item in results.items():
|
||||
side, feature_set = key.split("_", 1)
|
||||
for split_id in EVAL_SPLITS:
|
||||
metric = item.get(split_id, {})
|
||||
lines.append(
|
||||
f"| {side.upper()} | {feature_set} | {split_id} | {metric.get('row_count')} | {metric.get('continue_auc')} | {metric.get('brier_vs_constant_ratio')} | {metric.get('edge_mae_vs_constant_ratio')} |"
|
||||
)
|
||||
lines.extend(
|
||||
[
|
||||
"",
|
||||
"## Verdict Rule",
|
||||
"",
|
||||
"状态特征只有在 `market_plus_state` 同时好过 `market_only`,并且 validation_locked / latest_stress 没有反向变差时,才进入正式链路。",
|
||||
"",
|
||||
"## Verdict",
|
||||
"",
|
||||
f"- status: `{verdict['status']}`",
|
||||
f"- reasons: `{len(verdict['reasons'])}`",
|
||||
"",
|
||||
]
|
||||
)
|
||||
for reason in verdict["reasons"]:
|
||||
lines.append(f"- {reason}")
|
||||
if verdict["passed_checks"]:
|
||||
lines.extend(["", "## Passed Checks", ""])
|
||||
for item in verdict["passed_checks"]:
|
||||
lines.append(f"- {item}")
|
||||
return "\n".join(lines)
|
||||
Reference in New Issue
Block a user