|
| 1 | +"""Numba-accelerated backtest operations.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +try: |
| 6 | + from numba import jit, prange |
| 7 | + |
| 8 | + NUMBA_AVAILABLE = True |
| 9 | +except ImportError: |
| 10 | + NUMBA_AVAILABLE = False |
| 11 | + |
| 12 | + def jit(*args, **kwargs): |
| 13 | + def decorator(func): |
| 14 | + return func |
| 15 | + |
| 16 | + return decorator |
| 17 | + |
| 18 | + prange = range |
| 19 | + |
| 20 | + |
| 21 | +@jit(nopython=True, cache=True) |
| 22 | +def compute_strategy_returns( |
| 23 | + weights_prev: np.ndarray, |
| 24 | + returns: np.ndarray, |
| 25 | + turnover: np.ndarray, |
| 26 | + transaction_cost: float, |
| 27 | +) -> np.ndarray: |
| 28 | + """Compute strategy returns with transaction costs.""" |
| 29 | + n_days, n_assets = returns.shape |
| 30 | + strat_ret = np.zeros(n_days) |
| 31 | + |
| 32 | + for i in prange(n_days): |
| 33 | + ret_sum = 0.0 |
| 34 | + for j in prange(n_assets): |
| 35 | + ret_sum += weights_prev[i, j] * returns[i, j] |
| 36 | + strat_ret[i] = ret_sum - (turnover[i] * transaction_cost) |
| 37 | + |
| 38 | + return strat_ret |
| 39 | + |
| 40 | + |
| 41 | +@jit(nopython=True, cache=True) |
| 42 | +def compute_turnover(weights: np.ndarray, weights_prev: np.ndarray) -> np.ndarray: |
| 43 | + """Compute turnover (L1 change / 2).""" |
| 44 | + n_days, n_assets = weights.shape |
| 45 | + turnover = np.zeros(n_days) |
| 46 | + |
| 47 | + for i in prange(n_days): |
| 48 | + total_change = 0.0 |
| 49 | + for j in prange(n_assets): |
| 50 | + total_change += abs(weights[i, j] - weights_prev[i, j]) |
| 51 | + turnover[i] = total_change * 0.5 |
| 52 | + |
| 53 | + return turnover |
| 54 | + |
| 55 | + |
| 56 | +@jit(nopython=True, cache=True) |
| 57 | +def compute_portfolio_value( |
| 58 | + strategy_returns: np.ndarray, initial_capital: float |
| 59 | +) -> np.ndarray: |
| 60 | + """Compute cumulative portfolio value.""" |
| 61 | + n_days = len(strategy_returns) |
| 62 | + portfolio_value = np.zeros(n_days + 1) |
| 63 | + portfolio_value[0] = initial_capital |
| 64 | + |
| 65 | + for i in prange(n_days): |
| 66 | + portfolio_value[i + 1] = portfolio_value[i] * (1.0 + strategy_returns[i]) |
| 67 | + |
| 68 | + return portfolio_value[1:] |
| 69 | + |
| 70 | + |
| 71 | +@jit(nopython=True, cache=True) |
| 72 | +def compute_returns_from_prices(prices: np.ndarray) -> np.ndarray: |
| 73 | + """Compute percentage returns from prices.""" |
| 74 | + n_days, n_assets = prices.shape |
| 75 | + returns = np.zeros((n_days - 1, n_assets)) |
| 76 | + |
| 77 | + for i in prange(n_days - 1): |
| 78 | + for j in prange(n_assets): |
| 79 | + if prices[i, j] > 0: |
| 80 | + returns[i, j] = (prices[i + 1, j] - prices[i, j]) / prices[i, j] |
| 81 | + |
| 82 | + return returns |
| 83 | + |
| 84 | + |
| 85 | +@jit(nopython=True, cache=True) |
| 86 | +def rank_based_weights( |
| 87 | + signals: np.ndarray, max_leverage: float, long_pct: float, short_pct: float |
| 88 | +) -> np.ndarray: |
| 89 | + """Compute rank-based portfolio weights.""" |
| 90 | + n_assets = len(signals) |
| 91 | + weights = np.zeros(n_assets) |
| 92 | + |
| 93 | + valid_mask = np.zeros(n_assets, dtype=np.bool_) |
| 94 | + n_valid = 0 |
| 95 | + for i in range(n_assets): |
| 96 | + if not np.isnan(signals[i]): |
| 97 | + valid_mask[i] = True |
| 98 | + n_valid += 1 |
| 99 | + |
| 100 | + if n_valid == 0: |
| 101 | + return weights |
| 102 | + |
| 103 | + valid_values = np.zeros(n_valid) |
| 104 | + valid_indices = np.zeros(n_valid, dtype=np.int64) |
| 105 | + idx = 0 |
| 106 | + for i in range(n_assets): |
| 107 | + if valid_mask[i]: |
| 108 | + valid_values[idx] = signals[i] |
| 109 | + valid_indices[idx] = i |
| 110 | + idx += 1 |
| 111 | + |
| 112 | + sorted_idx = np.argsort(valid_values) |
| 113 | + ranks = np.zeros(n_valid) |
| 114 | + for i in range(n_valid): |
| 115 | + ranks[sorted_idx[i]] = i + 1.0 |
| 116 | + |
| 117 | + sorted_ranks = np.sort(ranks) |
| 118 | + long_idx = int(n_valid * long_pct) |
| 119 | + short_idx = int(n_valid * short_pct) |
| 120 | + long_threshold = sorted_ranks[long_idx] if long_idx < n_valid else sorted_ranks[-1] |
| 121 | + short_threshold = sorted_ranks[short_idx] if short_idx >= 0 else sorted_ranks[0] |
| 122 | + |
| 123 | + long_count = 0 |
| 124 | + short_count = 0 |
| 125 | + |
| 126 | + for idx in range(n_valid): |
| 127 | + i = valid_indices[idx] |
| 128 | + rank_val = ranks[idx] |
| 129 | + if rank_val >= long_threshold: |
| 130 | + weights[i] = 1.0 |
| 131 | + long_count += 1 |
| 132 | + elif rank_val <= short_threshold: |
| 133 | + weights[i] = -1.0 |
| 134 | + short_count += 1 |
| 135 | + |
| 136 | + if long_count > 0: |
| 137 | + long_weight = 1.0 / long_count |
| 138 | + for i in range(n_assets): |
| 139 | + if weights[i] > 0: |
| 140 | + weights[i] = long_weight |
| 141 | + if short_count > 0: |
| 142 | + short_weight = -1.0 / short_count |
| 143 | + for i in range(n_assets): |
| 144 | + if weights[i] < 0: |
| 145 | + weights[i] = short_weight |
| 146 | + |
| 147 | + total_leverage = 0.0 |
| 148 | + for i in range(n_assets): |
| 149 | + total_leverage += abs(weights[i]) |
| 150 | + |
| 151 | + if total_leverage > max_leverage and total_leverage > 0: |
| 152 | + scale = max_leverage / total_leverage |
| 153 | + for i in range(n_assets): |
| 154 | + weights[i] *= scale |
| 155 | + |
| 156 | + return weights |
0 commit comments