Initial commit: quant backtesting framework with daily trading simulator
Backtesting engine supporting 11 strategies across US (S&P 500) and CN (CSI 300) markets with open-to-close execution, proportional + fixed per-trade fees. Daily trader (trader.py) with auto/morning/evening/simulate/status commands and cron-friendly `auto` mode for unattended daily runs on a server. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
32
strategies/inverse_vol.py
Normal file
32
strategies/inverse_vol.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from strategies.base import Strategy
|
||||
|
||||
|
||||
class InverseVolatilityStrategy(Strategy):
|
||||
"""
|
||||
Risk parity via inverse-volatility weighting.
|
||||
|
||||
Allocates capital inversely proportional to each asset's realized volatility
|
||||
over a rolling window. Assets with lower recent volatility receive larger
|
||||
allocations, equalizing the risk contribution of each position.
|
||||
|
||||
This is a simple but robust baseline for risk-based portfolio construction.
|
||||
"""
|
||||
|
||||
def __init__(self, vol_window: int = 20):
|
||||
self.vol_window = vol_window
|
||||
|
||||
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
|
||||
returns = data.pct_change()
|
||||
vol = returns.rolling(self.vol_window).std()
|
||||
|
||||
# Replace zero vol with NaN to avoid division by zero
|
||||
vol = vol.replace(0, np.nan)
|
||||
inv_vol = 1.0 / vol
|
||||
|
||||
row_sums = inv_vol.sum(axis=1).replace(0, np.nan)
|
||||
signals = inv_vol.div(row_sums, axis=0).fillna(0.0)
|
||||
signals.iloc[:self.vol_window] = 0.0
|
||||
|
||||
return signals.shift(1).fillna(0.0)
|
||||
Reference in New Issue
Block a user