feat: dump alpha values + daily PnL as parquet (pyarrow)
This commit is contained in:
@@ -122,6 +122,51 @@ def plot_ic(signal_eval: dict, output_path: str = "reports/ic.png") -> str:
|
||||
return output_path
|
||||
|
||||
|
||||
def dump_signals(signals_df: pd.DataFrame, output_dir: str = "results/") -> str:
|
||||
"""Save the signal matrix (date x stock) as a parquet file.
|
||||
|
||||
Args:
|
||||
signals_df: Date-indexed DataFrame of per-stock signal values.
|
||||
output_dir: Directory to write the parquet file into.
|
||||
|
||||
Returns:
|
||||
The path the parquet file was written to.
|
||||
"""
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
path = os.path.join(output_dir, "signals.parquet")
|
||||
signals_df.to_parquet(path)
|
||||
return path
|
||||
|
||||
|
||||
def dump_daily_pnl(
|
||||
results: list, output_dir: str = "results/", initial_cash: float = 1_000_000.0
|
||||
) -> str:
|
||||
"""Extract the daily portfolio value from a backtest run and save as parquet.
|
||||
|
||||
Compounds the per-day TimeReturn analyzer into an equity curve.
|
||||
|
||||
Args:
|
||||
results: The list returned by ``cerebro.run()``.
|
||||
output_dir: Directory to write the parquet file into.
|
||||
initial_cash: Starting portfolio value for scaling the curve.
|
||||
|
||||
Returns:
|
||||
The path the parquet file was written to.
|
||||
"""
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
series = pd.Series(dtype=float)
|
||||
if results:
|
||||
tr = results[0].analyzers.timereturn.get_analysis()
|
||||
series = pd.Series(tr).sort_index()
|
||||
|
||||
equity = (1.0 + series).cumprod() * initial_cash
|
||||
pnl_df = pd.DataFrame({"date": equity.index, "value": equity.values})
|
||||
|
||||
path = os.path.join(output_dir, "daily_pnl.parquet")
|
||||
pnl_df.to_parquet(path)
|
||||
return path
|
||||
|
||||
|
||||
def generate_report(
|
||||
results: list,
|
||||
signal_eval: dict,
|
||||
|
||||
Reference in New Issue
Block a user