Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fc0c756203 | |||
| 3a9f7564ad | |||
| fb3a2dda25 | |||
| 3420edee2b | |||
| fa6959bb99 | |||
| 514c235f1c | |||
| 7e91293e1a | |||
| c9451069d8 | |||
| 997e234a85 | |||
| e049a2f968 | |||
| f7592d5d79 | |||
| 87545683eb | |||
| eb5335e809 | |||
| 0ff67e7fe2 | |||
| ab568cc9fb | |||
| 4d4ea2e5e9 | |||
| a82ddc598a | |||
| bb6ce05c57 |
Generated
+1
-1
@@ -502,7 +502,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "raptorbt"
|
||||
version = "0.2.1"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"approx",
|
||||
"criterion",
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
[package]
|
||||
name = "raptorbt"
|
||||
version = "0.2.2"
|
||||
version = "0.4.0"
|
||||
edition = "2021"
|
||||
description = "High-performance Rust backtesting engine with Python bindings. Drop-in VectorBT replacement with up insanely faster performance at fractional memory footprint."
|
||||
description = "High-performance Rust backtesting engine with Python bindings. Bar-level and tick-level simulation with sub-millisecond execution and a minimal footprint."
|
||||
authors = ["Alphabench <contact@alphabench.in>"]
|
||||
license = "MIT"
|
||||
repository = "https://github.com/alphabench/raptorbt"
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
**Blazing-fast backtesting for the modern quant.**
|
||||
|
||||
RaptorBT is a high-performance backtesting engine written in Rust with Python bindings via PyO3. It serves as a drop-in replacement for VectorBT — delivering **HFT-grade compute efficiency** with full metric parity.
|
||||
RaptorBT is a high-performance backtesting engine written in Rust with Python bindings via PyO3. Built for production quantitative trading — delivering **HFT-grade compute efficiency** with full tick-to-bar coverage.
|
||||
|
||||
<p align="center">
|
||||
<strong>5,800x faster</strong> · <strong>45x smaller</strong> · <strong>100% deterministic</strong>
|
||||
@@ -58,7 +58,6 @@ Developed and maintained by the [Alphabench](https://alphabench.in) team.
|
||||
- [Metrics](#metrics)
|
||||
- [Indicators](#indicators)
|
||||
- [Stop-Loss & Take-Profit](#stop-loss--take-profit)
|
||||
- [VectorBT Comparison](#vectorbt-comparison)
|
||||
- [API Reference](#api-reference)
|
||||
- [Building from Source](#building-from-source)
|
||||
- [Testing](#testing)
|
||||
@@ -67,21 +66,24 @@ Developed and maintained by the [Alphabench](https://alphabench.in) team.
|
||||
|
||||
## Overview
|
||||
|
||||
RaptorBT was built to address the performance limitations of VectorBT. Benchmarked by the Alphabench team:
|
||||
RaptorBT is benchmarked by the Alphabench team on Apple Silicon M-series:
|
||||
|
||||
| Metric | VectorBT | RaptorBT | Improvement |
|
||||
| ----------------------------- | ------------------- | ------------ | ------------------------- |
|
||||
| **Disk Footprint** | ~450MB | <10MB | **45x smaller** |
|
||||
| **Startup Latency** | 200-600ms | <10ms | **20-60x faster** |
|
||||
| **Backtest Speed (1K bars)** | 1460ms | 0.25ms | **5,800x faster** |
|
||||
| **Backtest Speed (50K bars)** | 43ms | 1.7ms | **25x faster** |
|
||||
| **Memory Usage** | High (JIT + pandas) | Low (native) | **Significant reduction** |
|
||||
| Metric | RaptorBT |
|
||||
| ----------------------------- | ------------ |
|
||||
| **Disk Footprint** | <10MB |
|
||||
| **Startup Latency** | <10ms |
|
||||
| **Backtest Speed (1K bars)** | 0.25ms |
|
||||
| **Backtest Speed (50K bars)** | 1.7ms |
|
||||
| **Memory Usage** | Low (native) |
|
||||
|
||||
### Key Features
|
||||
|
||||
- **5 Strategy Types**: Single instrument, basket/collective, pairs trading, options, and multi-strategy
|
||||
- **30+ Metrics**: Full parity with VectorBT including Sharpe, Sortino, Calmar, Omega, SQN, and more
|
||||
- **10 Technical Indicators**: SMA, EMA, RSI, MACD, Stochastic, ATR, Bollinger Bands, ADX, VWAP, Supertrend
|
||||
- **7 Strategy Types**: Single instrument, basket/collective, pairs trading, options, spreads, multi-strategy, and tick-level
|
||||
- **Tick-Level Simulation**: Full tick resolution for intraday options momentum, scalping, and microstructure strategies
|
||||
- **Batch Spread Backtesting**: Run multiple spread backtests in parallel via Rayon with GIL released
|
||||
- **Monte Carlo Simulation**: Correlated multi-asset forward projection via GBM + Cholesky decomposition
|
||||
- **33 Metrics**: Sharpe, Sortino, Calmar, Omega, SQN, Payoff Ratio, Recovery Factor, and more
|
||||
- **Technical Indicators**: SMA, EMA, RSI, MACD, Stochastic, ATR, Bollinger Bands, ADX, VWAP, Supertrend, Rolling Min/Max, and tick feature functions
|
||||
- **Stop/Target Management**: Fixed, ATR-based, and trailing stops with risk-reward targets
|
||||
- **100% Deterministic**: No JIT compilation variance between runs
|
||||
- **Native Parallelism**: Rayon-based parallel processing with explicit SIMD optimizations
|
||||
@@ -95,26 +97,23 @@ RaptorBT was built to address the performance limitations of VectorBT. Benchmark
|
||||
Tested on Apple Silicon M-series with random walk price data and SMA crossover strategy:
|
||||
|
||||
```
|
||||
┌─────────────┬────────────┬───────────┬──────────┐
|
||||
│ Data Size │ VectorBT │ RaptorBT │ Speedup │
|
||||
├─────────────┼────────────┼───────────┼──────────┤
|
||||
│ 1,000 bars │ 1,460 ms │ 0.25 ms │ 5,827x │
|
||||
│ 5,000 bars │ 36 ms │ 0.24 ms │ 153x │
|
||||
│ 10,000 bars │ 37 ms │ 0.46 ms │ 80x │
|
||||
│ 50,000 bars │ 43 ms │ 1.68 ms │ 26x │
|
||||
└─────────────┴────────────┴───────────┴──────────┘
|
||||
┌─────────────┬───────────┐
|
||||
│ Data Size │ RaptorBT │
|
||||
├─────────────┼───────────┤
|
||||
│ 1,000 bars │ 0.25 ms │
|
||||
│ 5,000 bars │ 0.24 ms │
|
||||
│ 10,000 bars │ 0.46 ms │
|
||||
│ 50,000 bars │ 1.68 ms │
|
||||
└─────────────┴───────────┘
|
||||
```
|
||||
|
||||
> **Note**: First VectorBT run includes Numba JIT compilation overhead. Subsequent runs are faster but still significantly slower than RaptorBT.
|
||||
|
||||
### Metric Accuracy
|
||||
|
||||
RaptorBT produces **identical results** to VectorBT:
|
||||
RaptorBT produces deterministic, reproducible results across runs:
|
||||
|
||||
```
|
||||
VectorBT Total Return: 7.2764%
|
||||
RaptorBT Total Return: 7.2764%
|
||||
Difference: 0.0000% ✓
|
||||
RaptorBT Total Return: 7.2764% (seed=42, 500 bars, SMA crossover)
|
||||
Difference between runs: 0.0000% ✓
|
||||
```
|
||||
|
||||
---
|
||||
@@ -127,6 +126,7 @@ raptorbt/
|
||||
│ ├── core/ # Core types and error handling
|
||||
│ │ ├── types.rs # BacktestConfig, BacktestResult, Trade, Metrics
|
||||
│ │ ├── error.rs # RaptorError enum
|
||||
│ │ ├── session.rs # SessionTracker, SessionConfig (intraday sessions)
|
||||
│ │ └── timeseries.rs # Time series utilities
|
||||
│ │
|
||||
│ ├── strategies/ # Strategy implementations
|
||||
@@ -134,6 +134,7 @@ raptorbt/
|
||||
│ │ ├── basket.rs # Basket/collective strategies
|
||||
│ │ ├── pairs.rs # Pairs trading
|
||||
│ │ ├── options.rs # Options strategies
|
||||
│ │ ├── spreads.rs # Multi-leg spread strategies
|
||||
│ │ └── multi.rs # Multi-strategy combining
|
||||
│ │
|
||||
│ ├── indicators/ # Technical indicators
|
||||
@@ -141,7 +142,8 @@ raptorbt/
|
||||
│ │ ├── momentum.rs # RSI, MACD, Stochastic
|
||||
│ │ ├── volatility.rs # ATR, Bollinger Bands
|
||||
│ │ ├── strength.rs # ADX
|
||||
│ │ └── volume.rs # VWAP
|
||||
│ │ ├── volume.rs # VWAP
|
||||
│ │ └── rolling.rs # Rolling Min/Max (LLV/HHV)
|
||||
│ │
|
||||
│ ├── metrics/ # Performance metrics
|
||||
│ │ ├── streaming.rs # Streaming metric calculations
|
||||
@@ -158,6 +160,12 @@ raptorbt/
|
||||
│ │ ├── atr.rs # ATR-based stops
|
||||
│ │ └── trailing.rs # Trailing stops
|
||||
│ │
|
||||
│ ├── portfolio/ # Portfolio-level analysis
|
||||
│ │ ├── monte_carlo.rs # Monte Carlo forward simulation (GBM + Cholesky)
|
||||
│ │ ├── allocation.rs # Capital allocation
|
||||
│ │ ├── engine.rs # Portfolio engine
|
||||
│ │ └── position.rs # Position management
|
||||
│ │
|
||||
│ ├── python/ # PyO3 bindings
|
||||
│ │ ├── bindings.rs # Python function exports
|
||||
│ │ └── numpy_bridge.rs # NumPy array conversion
|
||||
@@ -265,6 +273,9 @@ trades = result.trades() # Returns list of PyTrade objects
|
||||
Basic long or short strategy on a single instrument.
|
||||
|
||||
```python
|
||||
# Optional: Instrument-specific configuration
|
||||
inst_config = raptorbt.PyInstrumentConfig(lot_size=1.0)
|
||||
|
||||
result = raptorbt.run_single_backtest(
|
||||
timestamps=timestamps,
|
||||
open=open_prices, high=high_prices, low=low_prices,
|
||||
@@ -274,6 +285,7 @@ result = raptorbt.run_single_backtest(
|
||||
weight=1.0,
|
||||
symbol="SYMBOL",
|
||||
config=config,
|
||||
instrument_config=inst_config, # Optional: lot_size rounding, capital caps
|
||||
)
|
||||
```
|
||||
|
||||
@@ -288,10 +300,18 @@ instruments = [
|
||||
(timestamps, open3, high3, low3, close3, volume3, entries3, exits3, 1, 0.34, "MSFT"),
|
||||
]
|
||||
|
||||
# Optional: Per-instrument configs for lot_size and capital allocation
|
||||
instrument_configs = {
|
||||
"AAPL": raptorbt.PyInstrumentConfig(lot_size=1.0, alloted_capital=33000),
|
||||
"GOOGL": raptorbt.PyInstrumentConfig(lot_size=1.0, alloted_capital=33000),
|
||||
"MSFT": raptorbt.PyInstrumentConfig(lot_size=1.0, alloted_capital=34000),
|
||||
}
|
||||
|
||||
result = raptorbt.run_basket_backtest(
|
||||
instruments=instruments,
|
||||
config=config,
|
||||
sync_mode="all", # "all", "any", "majority", "master"
|
||||
instrument_configs=instrument_configs, # Optional
|
||||
)
|
||||
```
|
||||
|
||||
@@ -382,6 +402,118 @@ result = raptorbt.run_multi_backtest(
|
||||
- `weighted`: Weight signals by strategy weight
|
||||
- `independent`: Run strategies independently (aggregate PnL)
|
||||
|
||||
### 6. Batch Spread Backtest
|
||||
|
||||
Run multiple spread backtests in parallel. Shared data (timestamps, underlying close) is converted once, then each item is backtested on its own Rayon thread with the GIL released for maximum throughput.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import raptorbt
|
||||
|
||||
config = raptorbt.PyBacktestConfig(initial_capital=100000, fees=0.001)
|
||||
|
||||
# Create batch items — one per strategy variation
|
||||
items = [
|
||||
raptorbt.PyBatchSpreadItem(
|
||||
strategy_id="straddle_24000",
|
||||
legs_premiums=[call_24000_premiums, put_24000_premiums],
|
||||
leg_configs=[("CE", 24000.0, -1, 50), ("PE", 24000.0, -1, 50)],
|
||||
entries=entries,
|
||||
exits=exits,
|
||||
spread_type="straddle",
|
||||
max_loss=5000.0,
|
||||
target_profit=3000.0,
|
||||
),
|
||||
raptorbt.PyBatchSpreadItem(
|
||||
strategy_id="strangle_23500_24500",
|
||||
legs_premiums=[call_24500_premiums, put_23500_premiums],
|
||||
leg_configs=[("CE", 24500.0, -1, 50), ("PE", 23500.0, -1, 50)],
|
||||
entries=entries,
|
||||
exits=exits,
|
||||
spread_type="strangle",
|
||||
),
|
||||
]
|
||||
|
||||
# Run all in parallel — returns list of (strategy_id, result) tuples
|
||||
results = raptorbt.batch_spread_backtest(
|
||||
timestamps=timestamps,
|
||||
underlying_close=underlying_close,
|
||||
items=items,
|
||||
config=config,
|
||||
)
|
||||
|
||||
for strategy_id, result in results:
|
||||
print(f"{strategy_id}: {result.metrics.total_return_pct:.2f}%")
|
||||
```
|
||||
|
||||
### 7. Tick-Level Backtest
|
||||
|
||||
Simulate intraday strategies at full tick resolution — no bar resampling, no intra-bar path approximation. Designed for options momentum, scalping, and any setup where the exact fill tick matters.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import raptorbt
|
||||
|
||||
# Raw tick arrays (one element per tick, same length N)
|
||||
# buy_qty_delta / sell_qty_delta must be per-tick deltas, NOT Zerodha cumulative sums
|
||||
result = raptorbt.run_tick_backtest(
|
||||
timestamps=timestamps_ns, # int64 nanoseconds-since-epoch
|
||||
ltp=ltp_arr, # last traded price
|
||||
bid=bid_arr,
|
||||
ask=ask_arr,
|
||||
buy_qty_delta=buy_delta, # pre-converted from cumulative: np.diff(buy_cum).clip(0)
|
||||
sell_qty_delta=sell_delta,
|
||||
oi=oi_arr,
|
||||
entries=entry_signals, # bool array — True where entry is allowed
|
||||
exits=exit_signals, # bool array — True where position should exit
|
||||
symbol="NIFTY26APR24600PE",
|
||||
initial_capital=100_000.0,
|
||||
fees=0.001,
|
||||
slippage=0.0005,
|
||||
stop_loss_pct=5.0,
|
||||
take_profit_pct=10.0,
|
||||
max_hold_seconds=1800, # 30-minute maximum hold
|
||||
entry_cooldown_ticks=10, # minimum ticks between entries
|
||||
max_trades=50,
|
||||
)
|
||||
|
||||
print(f"trades: {result.metrics.total_trades}")
|
||||
print(f"profit_factor: {result.metrics.profit_factor:.2f}")
|
||||
print(f"win_rate: {result.metrics.win_rate_pct:.1f}%")
|
||||
```
|
||||
|
||||
#### Tick Signal & Feature Helpers
|
||||
|
||||
Precompute entry/exit signal arrays and tick microstructure features before calling `run_tick_backtest`:
|
||||
|
||||
```python
|
||||
# Signal arrays
|
||||
entries = raptorbt.compute_tick_entry_signals(
|
||||
spread_pct=raptorbt.tick_spread_pct(bid, ask),
|
||||
bsi_delta=raptorbt.buy_sell_imbalance_delta(buy_cum, sell_cum), # pass raw cumulative
|
||||
return_1m=raptorbt.return_window(timestamps_ns, ltp, window_seconds=60.0),
|
||||
spread_pct_max=3.0,
|
||||
bsi_min=0.55, # minimum buy-side delta fraction
|
||||
return_1m_min_abs=0.3, # minimum 1-min return % (abs)
|
||||
return_direction=1, # +1 long, -1 short
|
||||
cooldown_ticks=10,
|
||||
)
|
||||
exits = raptorbt.compute_tick_exit_signals(
|
||||
timestamps_ns=timestamps_ns,
|
||||
eod_exit_time_ns=eod_ns, # force exit at/after this timestamp; 0 = disabled
|
||||
)
|
||||
|
||||
# Feature arrays (all return Vec<f64> of same length as input)
|
||||
spread = raptorbt.tick_spread_pct(bid, ask) # (ask-bid)/mid * 100
|
||||
bsi = raptorbt.buy_sell_imbalance_delta(buy_cum, sell_cum) # delta BSI per tick
|
||||
ret_1m = raptorbt.return_window(ts_ns, ltp, 60.0) # 1-min lookback return %
|
||||
vol = raptorbt.realized_vol_rolling(ts_ns, ltp, 300.0) # 5-min realized vol %
|
||||
oi_pos = raptorbt.oi_position_pct(oi, oi_day_high, oi_day_low) # [0, 100]
|
||||
velocity = raptorbt.tick_velocity(ts_ns, 60.0) # ticks/min over last 60s
|
||||
```
|
||||
|
||||
**Important for Zerodha data:** `total_buy_qty` and `total_sell_qty` from KiteTicker are cumulative session running sums, not per-tick values. Pass them as-is to `buy_sell_imbalance_delta` (it computes deltas internally). For `run_tick_backtest`, convert first: `buy_delta = np.diff(buy_cum, prepend=0).clip(min=0)`.
|
||||
|
||||
---
|
||||
|
||||
## Metrics
|
||||
@@ -517,75 +649,65 @@ config.set_risk_reward_target(ratio=2.0) # 2:1 risk-reward ratio
|
||||
|
||||
---
|
||||
|
||||
## VectorBT Comparison
|
||||
## Monte Carlo Portfolio Simulation
|
||||
|
||||
RaptorBT is designed as a drop-in replacement for VectorBT. Here's a side-by-side comparison:
|
||||
|
||||
### VectorBT (before)
|
||||
RaptorBT includes a high-performance Monte Carlo forward simulation engine for portfolio risk analysis. It uses Geometric Brownian Motion (GBM) with Cholesky decomposition for correlated multi-asset simulation, parallelized via Rayon.
|
||||
|
||||
```python
|
||||
import vectorbt as vbt
|
||||
import pandas as pd
|
||||
|
||||
# Run backtest
|
||||
pf = vbt.Portfolio.from_signals(
|
||||
close=close_series,
|
||||
entries=entries,
|
||||
exits=exits,
|
||||
init_cash=100000,
|
||||
fees=0.001,
|
||||
)
|
||||
|
||||
# Get metrics
|
||||
print(pf.stats()["Total Return [%]"])
|
||||
print(pf.stats()["Sharpe Ratio"])
|
||||
print(pf.stats()["Max Drawdown [%]"])
|
||||
```
|
||||
|
||||
### RaptorBT (after)
|
||||
|
||||
```python
|
||||
import raptorbt
|
||||
import numpy as np
|
||||
import raptorbt
|
||||
|
||||
# Configure backtest
|
||||
config = raptorbt.PyBacktestConfig(
|
||||
initial_capital=100000,
|
||||
fees=0.001,
|
||||
# Historical daily returns per strategy/asset (numpy arrays)
|
||||
returns = [
|
||||
np.array([0.001, -0.002, 0.003, ...]), # Strategy 1 returns
|
||||
np.array([0.002, 0.001, -0.001, ...]), # Strategy 2 returns
|
||||
]
|
||||
|
||||
# Portfolio weights (must sum to 1.0)
|
||||
weights = np.array([0.6, 0.4])
|
||||
|
||||
# Correlation matrix (N x N)
|
||||
correlation_matrix = [
|
||||
np.array([1.0, 0.3]),
|
||||
np.array([0.3, 1.0]),
|
||||
]
|
||||
|
||||
# Run simulation
|
||||
result = raptorbt.simulate_portfolio_mc(
|
||||
returns=returns,
|
||||
weights=weights,
|
||||
correlation_matrix=correlation_matrix,
|
||||
initial_value=100000.0,
|
||||
n_simulations=10000, # Number of Monte Carlo paths (default: 10,000)
|
||||
horizon_days=252, # Forward projection horizon (default: 252)
|
||||
seed=42, # Random seed for reproducibility (default: 42)
|
||||
)
|
||||
|
||||
# Run backtest
|
||||
result = raptorbt.run_single_backtest(
|
||||
timestamps=timestamps,
|
||||
open=open_prices, high=high_prices,
|
||||
low=low_prices, close=close_prices,
|
||||
volume=volume,
|
||||
entries=entries, exits=exits,
|
||||
direction=1, weight=1.0,
|
||||
symbol="SYMBOL",
|
||||
config=config,
|
||||
)
|
||||
# Results
|
||||
print(f"Expected Return: {result['expected_return']:.2f}%")
|
||||
print(f"Probability of Loss: {result['probability_of_loss']:.2%}")
|
||||
print(f"VaR (95%): {result['var_95']:.2f}%")
|
||||
print(f"CVaR (95%): {result['cvar_95']:.2f}%")
|
||||
|
||||
# Get metrics
|
||||
print(f"Total Return: {result.metrics.total_return_pct}%")
|
||||
print(f"Sharpe Ratio: {result.metrics.sharpe_ratio}")
|
||||
print(f"Max Drawdown: {result.metrics.max_drawdown_pct}%")
|
||||
# Percentile paths: list of (percentile, path_values)
|
||||
# Percentiles: 5th, 25th, 50th, 75th, 95th
|
||||
for pct, path in result['percentile_paths']:
|
||||
print(f" P{pct:.0f} final value: {path[-1]:.2f}")
|
||||
|
||||
# Final values: numpy array of terminal values for all simulations
|
||||
final_values = result['final_values'] # numpy array, length = n_simulations
|
||||
```
|
||||
|
||||
### Metric Mapping
|
||||
### Result Fields
|
||||
|
||||
| VectorBT Key | RaptorBT Attribute |
|
||||
| ------------------ | -------------------------- |
|
||||
| `Total Return [%]` | `metrics.total_return_pct` |
|
||||
| `Sharpe Ratio` | `metrics.sharpe_ratio` |
|
||||
| `Sortino Ratio` | `metrics.sortino_ratio` |
|
||||
| `Max Drawdown [%]` | `metrics.max_drawdown_pct` |
|
||||
| `Win Rate [%]` | `metrics.win_rate_pct` |
|
||||
| `Profit Factor` | `metrics.profit_factor` |
|
||||
| `SQN` | `metrics.sqn` |
|
||||
| `Omega Ratio` | `metrics.omega_ratio` |
|
||||
| `Total Trades` | `metrics.total_trades` |
|
||||
| `Expectancy` | `metrics.expectancy` |
|
||||
| Field | Type | Description |
|
||||
| --------------------- | -------------------------- | ---------------------------------------------------------- |
|
||||
| `expected_return` | `float` | Expected return as percentage over the horizon |
|
||||
| `probability_of_loss` | `float` | Probability that final value < initial value (0.0 to 1.0) |
|
||||
| `var_95` | `float` | Value at Risk at 95% confidence (percentage) |
|
||||
| `cvar_95` | `float` | Conditional VaR at 95% confidence (percentage) |
|
||||
| `percentile_paths` | `List[Tuple[float, List]]` | Portfolio paths at 5th, 25th, 50th, 75th, 95th percentiles |
|
||||
| `final_values` | `numpy.ndarray` | Terminal portfolio values for all simulations |
|
||||
|
||||
---
|
||||
|
||||
@@ -612,6 +734,74 @@ config.set_atr_target(multiplier: float, period: int)
|
||||
config.set_risk_reward_target(ratio: float)
|
||||
```
|
||||
|
||||
### PyInstrumentConfig
|
||||
|
||||
Per-instrument configuration for position sizing and risk management.
|
||||
|
||||
```python
|
||||
inst_config = raptorbt.PyInstrumentConfig(
|
||||
lot_size=1.0, # Min tradeable quantity (1 for equity, 50 for NIFTY F&O)
|
||||
alloted_capital=50000.0, # Capital allocated to this instrument (optional)
|
||||
existing_qty=None, # Existing position quantity (future use)
|
||||
avg_price=None, # Existing position avg price (future use)
|
||||
)
|
||||
|
||||
# Optional: per-instrument stop/target overrides
|
||||
inst_config.set_fixed_stop(0.02)
|
||||
inst_config.set_trailing_stop(0.03)
|
||||
inst_config.set_fixed_target(0.05)
|
||||
```
|
||||
|
||||
**Fields:**
|
||||
|
||||
- `lot_size` - Minimum tradeable quantity. Position sizes are rounded down to nearest lot_size multiple. Use `1.0` for equities, `50.0` for NIFTY F&O, `0.01` for forex.
|
||||
- `alloted_capital` - Per-instrument capital cap (capped at available cash).
|
||||
- `existing_qty` / `avg_price` - Reserved for future live-to-backtest transitions.
|
||||
|
||||
### PyBatchSpreadItem
|
||||
|
||||
```python
|
||||
item = raptorbt.PyBatchSpreadItem(
|
||||
strategy_id: str, # Unique identifier for this backtest
|
||||
legs_premiums: List[np.ndarray], # Premium series per leg
|
||||
leg_configs: List[Tuple[str, float, int, int]], # (option_type, strike, quantity, lot_size)
|
||||
entries: np.ndarray, # bool entry signals
|
||||
exits: np.ndarray, # bool exit signals
|
||||
spread_type: str = "custom", # Spread type string
|
||||
max_loss: float = None, # Optional max loss exit
|
||||
target_profit: float = None, # Optional target profit exit
|
||||
)
|
||||
```
|
||||
|
||||
### batch_spread_backtest
|
||||
|
||||
```python
|
||||
results = raptorbt.batch_spread_backtest(
|
||||
timestamps: np.ndarray, # int64 nanosecond timestamps (shared)
|
||||
underlying_close: np.ndarray, # Underlying close prices (shared)
|
||||
items: List[PyBatchSpreadItem], # List of spread backtest items
|
||||
config: PyBacktestConfig = None, # Optional shared config
|
||||
) -> List[Tuple[str, PyBacktestResult]] # (strategy_id, result) pairs
|
||||
```
|
||||
|
||||
Runs all spread backtests in parallel via Rayon. Timestamps and underlying close are shared across all items and converted once. The GIL is released during execution for maximum Python concurrency.
|
||||
|
||||
### simulate_portfolio_mc
|
||||
|
||||
```python
|
||||
result = raptorbt.simulate_portfolio_mc(
|
||||
returns: List[np.ndarray], # Per-asset daily returns (N arrays)
|
||||
weights: np.ndarray, # Portfolio weights (length N, sum to 1)
|
||||
correlation_matrix: List[np.ndarray], # N x N correlation matrix
|
||||
initial_value: float, # Starting portfolio value
|
||||
n_simulations: int = 10000, # Number of Monte Carlo paths
|
||||
horizon_days: int = 252, # Forward projection horizon in days
|
||||
seed: int = 42, # Random seed for reproducibility
|
||||
) -> dict
|
||||
```
|
||||
|
||||
Returns a dictionary with keys: `expected_return`, `probability_of_loss`, `var_95`, `cvar_95`, `percentile_paths`, `final_values`.
|
||||
|
||||
### PyBacktestResult
|
||||
|
||||
```python
|
||||
@@ -664,8 +854,10 @@ metrics.max_consecutive_wins
|
||||
metrics.max_consecutive_losses
|
||||
metrics.exposure_pct
|
||||
metrics.open_trade_pnl
|
||||
metrics.payoff_ratio # avg win / avg loss (risk/reward per trade)
|
||||
metrics.recovery_factor # net profit / max drawdown (resilience)
|
||||
|
||||
# Convert to dictionary (VectorBT format)
|
||||
# Convert to dictionary
|
||||
stats_dict = metrics.to_dict()
|
||||
```
|
||||
|
||||
@@ -684,7 +876,7 @@ for trade in result.trades():
|
||||
print(trade.pnl) # Profit/Loss
|
||||
print(trade.return_pct) # Return percentage
|
||||
print(trade.fees) # Fees paid
|
||||
print(trade.exit_reason) # "Signal", "StopLoss", "TakeProfit"
|
||||
print(trade.exit_reason) # "Signal", "StopLoss", "TakeProfit", "TrailingStop", "EndOfData", "Settlement"
|
||||
```
|
||||
|
||||
---
|
||||
@@ -748,44 +940,32 @@ print(f'Total Return: {result.metrics.total_return_pct:.2f}%')
|
||||
print('RaptorBT is working correctly!')
|
||||
```
|
||||
|
||||
### Comparison Test (VectorBT vs RaptorBT)
|
||||
### Verification Test
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import vectorbt as vbt
|
||||
import raptorbt
|
||||
|
||||
# Create test data
|
||||
np.random.seed(42)
|
||||
n = 500
|
||||
dates = pd.date_range('2023-01-01', periods=n, freq='D')
|
||||
close = np.cumprod(1 + np.random.randn(n) * 0.02) * 100
|
||||
entries = np.zeros(n, dtype=bool)
|
||||
exits = np.zeros(n, dtype=bool)
|
||||
entries[::20] = True
|
||||
exits[10::20] = True
|
||||
|
||||
# VectorBT
|
||||
pf = vbt.Portfolio.from_signals(
|
||||
close=pd.Series(close, index=dates),
|
||||
entries=pd.Series(entries, index=dates),
|
||||
exits=pd.Series(exits, index=dates),
|
||||
init_cash=100000, fees=0.001
|
||||
)
|
||||
|
||||
# RaptorBT
|
||||
config = raptorbt.PyBacktestConfig(initial_capital=100000, fees=0.001)
|
||||
result = raptorbt.run_single_backtest(
|
||||
timestamps=dates.astype('int64').values,
|
||||
timestamps=np.arange(n, dtype=np.int64),
|
||||
open=close, high=close, low=close, close=close,
|
||||
volume=np.ones(n), entries=entries, exits=exits,
|
||||
direction=1, weight=1.0, symbol="TEST", config=config
|
||||
)
|
||||
|
||||
print(f"VectorBT: {pf.stats()['Total Return [%]']:.4f}%")
|
||||
print(f"RaptorBT: {result.metrics.total_return_pct:.4f}%")
|
||||
# Results should match within 0.01%
|
||||
print(f"Total Return: {result.metrics.total_return_pct:.4f}%")
|
||||
print(f"Sharpe Ratio: {result.metrics.sharpe_ratio:.4f}")
|
||||
print(f"Max Drawdown: {result.metrics.max_drawdown_pct:.4f}%")
|
||||
print("RaptorBT is working correctly!")
|
||||
```
|
||||
|
||||
---
|
||||
@@ -798,11 +978,87 @@ MIT License - see [LICENSE](LICENSE) for details.
|
||||
|
||||
## Changelog
|
||||
|
||||
### v0.4.0
|
||||
|
||||
**Tick-level backtesting — full tick resolution, no bar resampling.**
|
||||
|
||||
- Add `TickData` struct — parallel arrays of `timestamps`, `ltp`, `bid`, `ask`, `buy_qty_delta`, `sell_qty_delta`, `oi` (one element per tick). Callers must pre-convert Zerodha cumulative session totals to per-tick deltas before passing.
|
||||
- Add `ExitReason::TimeExit` — max hold-time exceeded exit for tick strategies.
|
||||
- Add `run_tick_backtest` — tick-native simulation engine. Entry fills at ask+slippage; stop/target checked against ltp on every tick (not OHLC approximation); max-hold-seconds time exit; configurable cooldown between entries. Returns the same `PyBacktestResult` / 27-metric `PyBacktestMetrics` as all other strategy types.
|
||||
- Add `compute_tick_entry_signals` — compute momentum entry bool array from precomputed feature arrays (spread gate, delta BSI gate, 1-min return gate, cooldown enforcement). O(N) single pass.
|
||||
- Add `compute_tick_exit_signals` — time-based (EOD) exit bool array from tick timestamps.
|
||||
- Add `tick_spread_pct` — per-tick bid/ask spread as percentage of mid price.
|
||||
- Add `buy_sell_imbalance_delta` — per-tick delta BSI from Zerodha cumulative running sums. Fixes the raw-cumulative BSI artefact (~0.95 all day regardless of order flow).
|
||||
- Add `return_window` — per-tick lookback return over a configurable time window using binary search (O(N log N)). Returns NaN where history is insufficient — correctly gates the entry filter rather than silently passing.
|
||||
- Add `realized_vol_rolling` — rolling realized volatility proxy (stddev of log-returns) over a time window.
|
||||
- Add `oi_position_pct` — OI position within the day's high/low range, per tick: [0, 100].
|
||||
- Add `tick_velocity` — rolling tick count per minute over a configurable time window.
|
||||
- Expose `compute_backtest_metrics` as a public free function in `portfolio::engine` — non-OHLCV strategy types can produce identical metrics without duplicating the calculation logic.
|
||||
|
||||
### v0.3.4
|
||||
|
||||
- Add single-leg option spread types: `LongCall`, `LongPut`, `NakedCall`, `NakedPut` to `SpreadType` enum
|
||||
- Add `ExitReason::Settlement` for option expiry settlement exits
|
||||
- Add `leg_expiry_timestamps` parameter to `run_spread_backtest` for per-leg expiry tracking
|
||||
- Positions are force-closed at settlement when any leg expires, with premiums replaced by intrinsic value
|
||||
- Prevent re-entry after all legs have expired
|
||||
|
||||
### v0.3.3
|
||||
|
||||
- Add `batch_spread_backtest` function for running multiple spread backtests in parallel via Rayon
|
||||
- Add `PyBatchSpreadItem` class for defining individual items in a batch spread backtest
|
||||
- Shared data (timestamps, underlying close) is converted once and reused across all items
|
||||
- GIL released during parallel execution for maximum Python concurrency
|
||||
- Each item carries its own `strategy_id`, leg configs, signals, spread type, and optional max loss / target profit
|
||||
- Returns a list of `(strategy_id, PyBacktestResult)` tuples preserving result-to-input mapping
|
||||
|
||||
### v0.3.2
|
||||
|
||||
- Add `payoff_ratio` metric to `BacktestMetrics` — average winning trade return divided by average losing trade return (absolute), measures risk/reward per trade
|
||||
- Add `recovery_factor` metric to `BacktestMetrics` — net profit divided by maximum drawdown in absolute terms, measures how many times over the strategy recovered from its worst drawdown
|
||||
- Both metrics computed in `StreamingMetrics::finalize()` (single-instrument backtest) and `PortfolioEngine` (multi-strategy aggregation)
|
||||
- Both metrics exposed via PyO3 as `#[pyo3(get)]` attributes on `PyBacktestMetrics`
|
||||
- Handles edge cases: returns `f64::INFINITY` when denominator is zero with positive numerator, `0.0` otherwise
|
||||
|
||||
### v0.3.1
|
||||
|
||||
- Add Monte Carlo portfolio simulation (`simulate_portfolio_mc`) for forward risk projection
|
||||
- Geometric Brownian Motion (GBM) with Cholesky decomposition for correlated multi-asset simulation
|
||||
- Rayon-parallelized simulation paths with deterministic seeding (xoshiro256\*\*)
|
||||
- Returns percentile paths (P5/P25/P50/P75/P95), VaR, CVaR, expected return, and probability of loss
|
||||
- GIL released during simulation for maximum Python concurrency
|
||||
|
||||
### v0.3.0
|
||||
|
||||
- Per-instrument configuration via `PyInstrumentConfig` (lot_size, alloted_capital, stop/target overrides)
|
||||
- Position sizes now correctly rounded to lot_size multiples
|
||||
- Support for per-instrument capital allocation in basket backtests
|
||||
- Future-ready fields: existing_qty, avg_price for live-to-backtest transitions
|
||||
|
||||
### v0.2.2
|
||||
|
||||
- Export `run_spread_backtest` Python binding for multi-leg options spread strategies
|
||||
- Export `rolling_min` and `rolling_max` indicator functions to Python
|
||||
|
||||
### v0.2.1
|
||||
|
||||
- Add `rolling_min` and `rolling_max` indicators for LLV (Lowest Low Value) and HHV (Highest High Value) support
|
||||
- NaN handling for warmup period
|
||||
|
||||
### v0.2.0
|
||||
|
||||
- Add multi-leg spread backtesting (`run_spread_backtest`) supporting straddles, strangles, vertical spreads, iron condors, iron butterflies, butterfly spreads, calendar spreads, and diagonal spreads
|
||||
- Coordinated entry/exit across all legs with net premium P&L calculation
|
||||
- Max loss and target profit exit thresholds for spreads
|
||||
- Add `SessionTracker` for intraday session management: market hours detection, squareoff time enforcement, session high/low/open tracking
|
||||
- Pre-built session configs for NSE equity (9:15-15:30), MCX commodity (9:00-23:30), and CDS currency (9:00-17:00)
|
||||
- Extend `StreamingMetrics` with equity/drawdown tracking, trade recording, and `finalize()` method
|
||||
|
||||
### v0.1.0
|
||||
|
||||
- Initial release
|
||||
- 5 strategy types: single, basket, pairs, options, multi
|
||||
- 30+ performance metrics with full VectorBT parity
|
||||
- 30+ performance metrics: Sharpe, Sortino, Calmar, Omega, SQN, profit factor, drawdown duration, and more
|
||||
- 10 technical indicators (SMA, EMA, RSI, MACD, Stochastic, ATR, Bollinger Bands, ADX, VWAP, Supertrend)
|
||||
- Stop-loss management: fixed, ATR-based, and trailing stops
|
||||
- Take-profit management: fixed, ATR-based, and risk-reward targets
|
||||
|
||||
+2
-2
@@ -4,8 +4,8 @@ build-backend = "maturin"
|
||||
|
||||
[project]
|
||||
name = "raptorbt"
|
||||
version = "0.2.2"
|
||||
description = "High-performance Rust backtesting engine with Python bindings. Drop-in VectorBT replacement with up insanely faster performance at fractional memory footprint."
|
||||
version = "0.4.0"
|
||||
description = "High-performance Rust backtesting engine with Python bindings. Bar-level and tick-level simulation with sub-millisecond execution and a minimal footprint."
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
license = {file = "LICENSE"}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
"""
|
||||
RaptorBT - High-performance Rust backtesting engine.
|
||||
|
||||
This module provides Python bindings for the Rust-based backtesting engine,
|
||||
offering significant performance improvements over vectorbt:
|
||||
- Disk footprint: <10MB (vs vectorbt's ~450MB)
|
||||
- Startup latency: <10ms (vs 200-600ms)
|
||||
Provides Python bindings for a Rust-based backtesting engine built for
|
||||
production quantitative trading:
|
||||
- Sub-millisecond execution on thousands of bars
|
||||
- Disk footprint: <10MB, startup latency: <10ms
|
||||
- 100% deterministic execution (no JIT cache)
|
||||
- Native parallelism via Rayon + explicit SIMD
|
||||
- Full tick-level simulation (no bar resampling required)
|
||||
"""
|
||||
|
||||
from raptorbt._raptorbt import (
|
||||
# Config classes
|
||||
PyBacktestConfig,
|
||||
PyInstrumentConfig,
|
||||
PyStopConfig,
|
||||
PyTargetConfig,
|
||||
# Result classes
|
||||
@@ -25,6 +27,22 @@ from raptorbt._raptorbt import (
|
||||
run_pairs_backtest,
|
||||
run_multi_backtest,
|
||||
run_spread_backtest,
|
||||
run_tick_backtest,
|
||||
# Batch backtest
|
||||
PyBatchSpreadItem,
|
||||
batch_spread_backtest,
|
||||
# Monte Carlo simulation
|
||||
simulate_portfolio_mc,
|
||||
# Tick signal functions
|
||||
compute_tick_entry_signals,
|
||||
compute_tick_exit_signals,
|
||||
# Tick feature functions
|
||||
tick_spread_pct,
|
||||
buy_sell_imbalance_delta,
|
||||
return_window,
|
||||
realized_vol_rolling,
|
||||
oi_position_pct,
|
||||
tick_velocity,
|
||||
# Indicator functions
|
||||
sma,
|
||||
ema,
|
||||
@@ -40,11 +58,12 @@ from raptorbt._raptorbt import (
|
||||
rolling_max,
|
||||
)
|
||||
|
||||
__version__ = "0.2.2"
|
||||
__version__ = "0.4.0"
|
||||
|
||||
__all__ = [
|
||||
# Config classes
|
||||
"PyBacktestConfig",
|
||||
"PyInstrumentConfig",
|
||||
"PyStopConfig",
|
||||
"PyTargetConfig",
|
||||
# Result classes
|
||||
@@ -58,6 +77,22 @@ __all__ = [
|
||||
"run_pairs_backtest",
|
||||
"run_multi_backtest",
|
||||
"run_spread_backtest",
|
||||
"run_tick_backtest",
|
||||
# Batch backtest
|
||||
"PyBatchSpreadItem",
|
||||
"batch_spread_backtest",
|
||||
# Monte Carlo simulation
|
||||
"simulate_portfolio_mc",
|
||||
# Tick signal functions
|
||||
"compute_tick_entry_signals",
|
||||
"compute_tick_exit_signals",
|
||||
# Tick feature functions
|
||||
"tick_spread_pct",
|
||||
"buy_sell_imbalance_delta",
|
||||
"return_window",
|
||||
"realized_vol_rolling",
|
||||
"oi_position_pct",
|
||||
"tick_velocity",
|
||||
# Indicator functions
|
||||
"sma",
|
||||
"ema",
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+134
-1
@@ -104,6 +104,44 @@ impl OhlcvData {
|
||||
}
|
||||
}
|
||||
|
||||
/// Raw tick data series for tick-level backtesting.
|
||||
///
|
||||
/// All fields are parallel arrays of length N (one entry per tick).
|
||||
/// `buy_qty_delta` and `sell_qty_delta` must be per-tick deltas, not
|
||||
/// cumulative session totals — callers are responsible for converting
|
||||
/// Zerodha-style running sums before passing them here.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TickData {
|
||||
/// Nanoseconds-since-epoch timestamp for each tick.
|
||||
pub timestamps: Vec<Timestamp>,
|
||||
/// Last traded price at each tick.
|
||||
pub ltp: Vec<Price>,
|
||||
/// Best bid price at each tick (0.0 if unavailable).
|
||||
pub bid: Vec<Price>,
|
||||
/// Best ask price at each tick (0.0 if unavailable).
|
||||
pub ask: Vec<Price>,
|
||||
/// Per-tick buy quantity delta (not cumulative).
|
||||
pub buy_qty_delta: Vec<f64>,
|
||||
/// Per-tick sell quantity delta (not cumulative).
|
||||
pub sell_qty_delta: Vec<f64>,
|
||||
/// Open interest at each tick (0 if unavailable).
|
||||
pub oi: Vec<f64>,
|
||||
}
|
||||
|
||||
impl TickData {
|
||||
/// Number of ticks.
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize {
|
||||
self.ltp.len()
|
||||
}
|
||||
|
||||
/// Whether the series is empty.
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.ltp.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Compiled trading signals from strategy.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CompiledSignals {
|
||||
@@ -212,6 +250,10 @@ pub enum ExitReason {
|
||||
TrailingStop,
|
||||
/// End of data.
|
||||
EndOfData,
|
||||
/// Option expiry settlement.
|
||||
Settlement,
|
||||
/// Max hold time exceeded (tick backtest).
|
||||
TimeExit,
|
||||
}
|
||||
|
||||
/// Backtest configuration.
|
||||
@@ -244,6 +286,47 @@ impl Default for BacktestConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-instrument configuration for position sizing and risk management.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct InstrumentConfig {
|
||||
/// Minimum tradeable quantity (1.0 for NSE EQ, 50.0 for NIFTY F&O, 0.01 for forex).
|
||||
pub lot_size: Option<f64>,
|
||||
/// Per-instrument capital cap.
|
||||
pub alloted_capital: Option<f64>,
|
||||
/// Per-instrument stop override.
|
||||
pub stop: Option<StopConfig>,
|
||||
/// Per-instrument target override.
|
||||
pub target: Option<TargetConfig>,
|
||||
/// Existing position quantity (future use).
|
||||
pub existing_qty: Option<f64>,
|
||||
/// Existing position average price (future use).
|
||||
pub avg_price: Option<f64>,
|
||||
}
|
||||
|
||||
impl InstrumentConfig {
|
||||
/// Round a raw position size down to the nearest lot_size multiple.
|
||||
/// Returns raw_size unchanged if lot_size is None or <= 0.
|
||||
pub fn round_to_lot(&self, raw_size: f64) -> f64 {
|
||||
match self.lot_size {
|
||||
Some(lot) if lot > 0.0 => (raw_size / lot).floor() * lot,
|
||||
_ => raw_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InstrumentConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
lot_size: None,
|
||||
alloted_capital: None,
|
||||
stop: None,
|
||||
target: None,
|
||||
existing_qty: None,
|
||||
avg_price: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Stop-loss configuration.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum StopConfig {
|
||||
@@ -335,6 +418,10 @@ pub struct BacktestMetrics {
|
||||
pub avg_holding_period: f64,
|
||||
/// Exposure time percentage (time in market).
|
||||
pub exposure_pct: f64,
|
||||
/// Payoff ratio (avg win / avg loss).
|
||||
pub payoff_ratio: f64,
|
||||
/// Recovery factor (net profit / max drawdown).
|
||||
pub recovery_factor: f64,
|
||||
}
|
||||
|
||||
/// Complete backtest result.
|
||||
@@ -386,7 +473,7 @@ pub struct Position {
|
||||
pub highest_since_entry: Price,
|
||||
/// Lowest price since entry (for trailing stops).
|
||||
pub lowest_since_entry: Price,
|
||||
/// Entry fees (to include in trade PnL like VectorBT).
|
||||
/// Entry fees included in trade PnL.
|
||||
pub entry_fees: f64,
|
||||
}
|
||||
|
||||
@@ -460,3 +547,49 @@ impl Default for Position {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_round_to_lot_whole_shares() {
|
||||
let config = InstrumentConfig { lot_size: Some(1.0), ..Default::default() };
|
||||
assert_eq!(config.round_to_lot(242.47), 242.0);
|
||||
assert_eq!(config.round_to_lot(1.0), 1.0);
|
||||
assert_eq!(config.round_to_lot(0.5), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_round_to_lot_nifty_fo() {
|
||||
let config = InstrumentConfig { lot_size: Some(50.0), ..Default::default() };
|
||||
assert_eq!(config.round_to_lot(242.0), 200.0);
|
||||
assert_eq!(config.round_to_lot(50.0), 50.0);
|
||||
assert_eq!(config.round_to_lot(49.0), 0.0);
|
||||
assert_eq!(config.round_to_lot(150.0), 150.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_round_to_lot_fractional() {
|
||||
let config = InstrumentConfig { lot_size: Some(0.01), ..Default::default() };
|
||||
assert!((config.round_to_lot(1.234) - 1.23).abs() < 1e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_round_to_lot_none() {
|
||||
let config = InstrumentConfig::default();
|
||||
assert_eq!(config.round_to_lot(242.47), 242.47);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_round_to_lot_zero() {
|
||||
let config = InstrumentConfig { lot_size: Some(0.0), ..Default::default() };
|
||||
assert_eq!(config.round_to_lot(242.47), 242.47);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_round_to_lot_negative() {
|
||||
let config = InstrumentConfig { lot_size: Some(-1.0), ..Default::default() };
|
||||
assert_eq!(config.round_to_lot(242.47), 242.47);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
pub mod momentum;
|
||||
pub mod rolling;
|
||||
pub mod strength;
|
||||
pub mod tick_features;
|
||||
pub mod trend;
|
||||
pub mod volatility;
|
||||
pub mod volume;
|
||||
@@ -13,6 +14,10 @@ pub mod volume;
|
||||
pub use momentum::{macd, rsi, stochastic, MacdResult, StochasticResult};
|
||||
pub use rolling::{rolling_max, rolling_min};
|
||||
pub use strength::adx;
|
||||
pub use tick_features::{
|
||||
buy_sell_imbalance_delta, oi_position_pct, realized_vol_rolling, return_window, spread_pct,
|
||||
tick_velocity,
|
||||
};
|
||||
pub use trend::{ema, sma, supertrend, SupertrendResult};
|
||||
pub use volatility::{atr, bollinger_bands, BollingerBandsResult};
|
||||
pub use volume::{obv, vwap};
|
||||
|
||||
@@ -276,9 +276,9 @@ mod tests {
|
||||
assert!(result.macd_line[24].is_nan());
|
||||
assert!(!result.macd_line[25].is_nan());
|
||||
|
||||
// Signal line should be valid later
|
||||
assert!(result.signal_line[33].is_nan());
|
||||
assert!(!result.signal_line[34].is_nan());
|
||||
// Signal line starts at index slow_period-1 + signal_period-1 = 25+8 = 33
|
||||
assert!(result.signal_line[32].is_nan());
|
||||
assert!(!result.signal_line[33].is_nan());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
//! Tick-level feature extraction functions.
|
||||
//!
|
||||
//! All functions accept parallel arrays (one element per tick) and return a
|
||||
//! Vec<f64> of the same length. NaN is used where the feature is undefined
|
||||
//! (e.g. insufficient history for a lookback window).
|
||||
//!
|
||||
//! These are building blocks for the signal generation layer — compute features
|
||||
//! once on the full tick window, then pass the resulting arrays to
|
||||
//! `tick_signals::tick_momentum_entry`.
|
||||
|
||||
/// Per-tick bid/ask spread as a percentage of the mid price.
|
||||
///
|
||||
/// Returns 0.0 where both bid and ask are zero.
|
||||
pub fn spread_pct(bid: &[f64], ask: &[f64]) -> Vec<f64> {
|
||||
bid.iter()
|
||||
.zip(ask.iter())
|
||||
.map(|(&b, &a)| {
|
||||
let mid = (b + a) / 2.0;
|
||||
if mid > 0.0 {
|
||||
(a - b) / mid * 100.0
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Per-tick delta BSI from Zerodha cumulative session totals.
|
||||
///
|
||||
/// Zerodha's `total_buy_qty` / `total_sell_qty` are running sums that grow
|
||||
/// monotonically from market open. Computing BSI from raw cumulative values
|
||||
/// yields ~0.95 for the whole day (artefact of early-session buy-side dominance).
|
||||
///
|
||||
/// This function computes the imbalance of the most recent tick's activity only:
|
||||
/// `bsi[i] = Δbuy[i] / (Δbuy[i] + Δsell[i])` where `Δbuy[i] = max(0, buy[i] - buy[i-1])`
|
||||
///
|
||||
/// Returns 0.5 (neutral) where the total delta is zero (no activity).
|
||||
pub fn buy_sell_imbalance_delta(
|
||||
buy_qty_cumulative: &[f64],
|
||||
sell_qty_cumulative: &[f64],
|
||||
) -> Vec<f64> {
|
||||
let n = buy_qty_cumulative.len();
|
||||
let mut out = vec![0.5_f64; n];
|
||||
for i in 1..n {
|
||||
let db = (buy_qty_cumulative[i] - buy_qty_cumulative[i - 1]).max(0.0);
|
||||
let ds = (sell_qty_cumulative[i] - sell_qty_cumulative[i - 1]).max(0.0);
|
||||
let total = db + ds;
|
||||
if total > 0.0 {
|
||||
out[i] = db / total;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Per-tick lookback return over a fixed time window.
|
||||
///
|
||||
/// For each tick i, finds the latest tick whose timestamp is at most
|
||||
/// `timestamps_ns[i] - window_seconds * 1e9` and computes:
|
||||
/// `(ltp[i] - ltp_ref) / ltp_ref * 100`
|
||||
///
|
||||
/// Returns `f64::NAN` for ticks where no reference tick exists (start of series
|
||||
/// or insufficient history).
|
||||
///
|
||||
/// Uses binary search → O(N log N) total.
|
||||
pub fn return_window(timestamps_ns: &[i64], ltp: &[f64], window_seconds: f64) -> Vec<f64> {
|
||||
let n = timestamps_ns.len();
|
||||
let window_ns = (window_seconds * 1_000_000_000.0) as i64;
|
||||
let mut out = vec![f64::NAN; n];
|
||||
|
||||
for i in 0..n {
|
||||
let cutoff = timestamps_ns[i] - window_ns;
|
||||
// Binary search for the last index with ts <= cutoff
|
||||
let pos = timestamps_ns[..i].partition_point(|&ts| ts <= cutoff);
|
||||
// pos is the first index > cutoff; we want pos.saturating_sub(1)
|
||||
if pos > 0 {
|
||||
let ref_idx = pos - 1;
|
||||
let ltp_ref = ltp[ref_idx];
|
||||
if ltp_ref > 0.0 {
|
||||
out[i] = (ltp[i] - ltp_ref) / ltp_ref * 100.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Rolling realized volatility proxy: annualized stddev of log returns.
|
||||
///
|
||||
/// For each tick i, computes stddev of log-returns over all ticks within
|
||||
/// the preceding `window_seconds`. Returns `f64::NAN` if fewer than 2 ticks
|
||||
/// in the window.
|
||||
///
|
||||
/// O(N²) worst case but typical windows are short (60–300 s at ~80 ticks/min
|
||||
/// = 80–400 ticks), making the inner loop fast in practice.
|
||||
pub fn realized_vol_rolling(timestamps_ns: &[i64], ltp: &[f64], window_seconds: f64) -> Vec<f64> {
|
||||
let n = timestamps_ns.len();
|
||||
let window_ns = (window_seconds * 1_000_000_000.0) as i64;
|
||||
let mut out = vec![f64::NAN; n];
|
||||
|
||||
for i in 1..n {
|
||||
let cutoff = timestamps_ns[i] - window_ns;
|
||||
// Find the first tick inside the window
|
||||
let start = timestamps_ns[..i].partition_point(|&ts| ts < cutoff);
|
||||
// We need log returns from start..=i
|
||||
let count = i - start;
|
||||
if count < 1 {
|
||||
continue;
|
||||
}
|
||||
let mut log_rets = Vec::with_capacity(count);
|
||||
for j in (start + 1)..=i {
|
||||
if ltp[j - 1] > 0.0 {
|
||||
log_rets.push((ltp[j] / ltp[j - 1]).ln());
|
||||
}
|
||||
}
|
||||
if log_rets.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
let mean = log_rets.iter().sum::<f64>() / log_rets.len() as f64;
|
||||
let variance = log_rets.iter().map(|r| (r - mean).powi(2)).sum::<f64>()
|
||||
/ (log_rets.len() - 1) as f64;
|
||||
out[i] = variance.sqrt() * 100.0; // as percentage of price
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Per-tick OI position within the day's high/low range.
|
||||
///
|
||||
/// Returns `(oi[i] - oi_day_low) / (oi_day_high - oi_day_low) * 100` ∈ [0, 100].
|
||||
/// Returns `f64::NAN` where `oi_day_high <= oi_day_low`.
|
||||
pub fn oi_position_pct(oi: &[f64], oi_day_high: f64, oi_day_low: f64) -> Vec<f64> {
|
||||
let range = oi_day_high - oi_day_low;
|
||||
if range <= 0.0 {
|
||||
return vec![f64::NAN; oi.len()];
|
||||
}
|
||||
oi.iter()
|
||||
.map(|&o| (o - oi_day_low) / range * 100.0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Rolling tick velocity: number of ticks per minute in the preceding window.
|
||||
///
|
||||
/// For each tick i, counts ticks in (timestamps_ns[i] - window_seconds*1e9, timestamps_ns[i]].
|
||||
/// Returns 0.0 for the first tick.
|
||||
pub fn tick_velocity(timestamps_ns: &[i64], window_seconds: f64) -> Vec<f64> {
|
||||
let n = timestamps_ns.len();
|
||||
let window_ns = (window_seconds * 1_000_000_000.0) as i64;
|
||||
let mut out = vec![0.0_f64; n];
|
||||
|
||||
for i in 1..n {
|
||||
let cutoff = timestamps_ns[i] - window_ns;
|
||||
let start = timestamps_ns[..i].partition_point(|&ts| ts <= cutoff);
|
||||
let count = (i - start + 1) as f64; // include current tick
|
||||
let minutes = window_seconds / 60.0;
|
||||
out[i] = if minutes > 0.0 { count / minutes } else { 0.0 };
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_spread_pct_basic() {
|
||||
let bid = vec![100.0, 200.0];
|
||||
let ask = vec![101.0, 202.0];
|
||||
let s = spread_pct(&bid, &ask);
|
||||
// (101-100)/100.5 * 100 ≈ 0.995
|
||||
assert!((s[0] - 0.9950248756218905).abs() < 1e-9);
|
||||
// (202-200)/201 * 100 ≈ 0.995
|
||||
assert!((s[1] - 0.9950248756218905).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spread_pct_zero_bid_ask() {
|
||||
let bid = vec![0.0];
|
||||
let ask = vec![0.0];
|
||||
let s = spread_pct(&bid, &ask);
|
||||
assert_eq!(s[0], 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bsi_delta_basic() {
|
||||
// Cumulative: buy grows by 100, sell by 0 → bsi = 1.0
|
||||
let buy = vec![1000.0, 1100.0, 1100.0, 1150.0];
|
||||
let sell = vec![800.0, 800.0, 850.0, 850.0];
|
||||
let bsi = buy_sell_imbalance_delta(&buy, &sell);
|
||||
assert_eq!(bsi[0], 0.5); // first tick always neutral
|
||||
assert_eq!(bsi[1], 1.0); // all buy
|
||||
assert_eq!(bsi[2], 0.0); // all sell
|
||||
assert_eq!(bsi[3], 1.0); // all buy
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bsi_delta_no_activity() {
|
||||
// No change → neutral 0.5
|
||||
let buy = vec![1000.0, 1000.0];
|
||||
let sell = vec![800.0, 800.0];
|
||||
let bsi = buy_sell_imbalance_delta(&buy, &sell);
|
||||
assert_eq!(bsi[1], 0.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_return_window_basic() {
|
||||
// Ticks at 0s, 30s, 61s, 90s (nanoseconds)
|
||||
let sec = 1_000_000_000_i64;
|
||||
let ts = vec![0, 30 * sec, 61 * sec, 90 * sec];
|
||||
let ltp = vec![100.0, 102.0, 101.0, 105.0];
|
||||
let ret = return_window(&ts, <p, 60.0);
|
||||
// ts[0]: no history → NAN
|
||||
assert!(ret[0].is_nan());
|
||||
// ts[1] at 30s: no tick <= -30s → NAN
|
||||
assert!(ret[1].is_nan());
|
||||
// ts[2] at 61s: cutoff = 1s, ts[0]=0 ≤ 1s → ref = ltp[0]=100.0
|
||||
// (101 - 100) / 100 * 100 = 1.0
|
||||
assert!((ret[2] - 1.0).abs() < 1e-9);
|
||||
// ts[3] at 90s: cutoff = 30s, ts[1]=30s ≤ 30s → ref = ltp[1]=102.0
|
||||
// (105 - 102) / 102 * 100 ≈ 2.941
|
||||
assert!((ret[3] - (3.0 / 102.0 * 100.0)).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oi_position_pct() {
|
||||
let oi = vec![50.0, 100.0, 150.0];
|
||||
let result = oi_position_pct(&oi, 200.0, 0.0);
|
||||
assert_eq!(result, vec![25.0, 50.0, 75.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oi_position_pct_no_range() {
|
||||
let oi = vec![100.0, 100.0];
|
||||
let result = oi_position_pct(&oi, 100.0, 100.0);
|
||||
assert!(result[0].is_nan());
|
||||
assert!(result[1].is_nan());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_velocity_basic() {
|
||||
// 4 ticks at 0s, 10s, 20s, 30s; window=60s
|
||||
let sec = 1_000_000_000_i64;
|
||||
let ts = vec![0, 10 * sec, 20 * sec, 30 * sec];
|
||||
let vel = tick_velocity(&ts, 60.0);
|
||||
// At i=3 (30s): ticks in (−30s, 30s] = all 4 → 4 ticks / 1 min = 4.0
|
||||
assert_eq!(vel[0], 0.0);
|
||||
assert!((vel[3] - 4.0).abs() < 1e-9);
|
||||
}
|
||||
}
|
||||
+21
@@ -27,6 +27,7 @@ pub mod strategies;
|
||||
fn _raptorbt(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
// Register config classes
|
||||
m.add_class::<python::bindings::PyBacktestConfig>()?;
|
||||
m.add_class::<python::bindings::PyInstrumentConfig>()?;
|
||||
m.add_class::<python::bindings::PyStopConfig>()?;
|
||||
m.add_class::<python::bindings::PyTargetConfig>()?;
|
||||
|
||||
@@ -42,6 +43,26 @@ fn _raptorbt(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(python::bindings::run_pairs_backtest, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::run_multi_backtest, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::run_spread_backtest, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::run_tick_backtest, m)?)?;
|
||||
|
||||
// Register batch spread backtest
|
||||
m.add_class::<python::bindings::PyBatchSpreadItem>()?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::batch_spread_backtest, m)?)?;
|
||||
|
||||
// Register Monte Carlo simulation
|
||||
m.add_function(wrap_pyfunction!(python::bindings::simulate_portfolio_mc, m)?)?;
|
||||
|
||||
// Register tick signal functions
|
||||
m.add_function(wrap_pyfunction!(python::bindings::compute_tick_entry_signals, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::compute_tick_exit_signals, m)?)?;
|
||||
|
||||
// Register tick feature functions
|
||||
m.add_function(wrap_pyfunction!(python::bindings::tick_spread_pct, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::buy_sell_imbalance_delta, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::return_window, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::realized_vol_rolling, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::oi_position_pct, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(python::bindings::tick_velocity, m)?)?;
|
||||
|
||||
// Register indicator functions
|
||||
m.add_function(wrap_pyfunction!(python::bindings::sma, m)?)?;
|
||||
|
||||
@@ -513,6 +513,30 @@ impl StreamingMetrics {
|
||||
let worst_trade_pct =
|
||||
if self.worst_trade_pct == f64::INFINITY { 0.0 } else { self.worst_trade_pct };
|
||||
|
||||
// Payoff ratio: average win / average loss (absolute value)
|
||||
let payoff_ratio = if avg_loss_pct.abs() > 0.0 {
|
||||
avg_win_pct / avg_loss_pct.abs()
|
||||
} else if avg_win_pct > 0.0 {
|
||||
f64::INFINITY
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
// Recovery factor: net profit / max drawdown (absolute value)
|
||||
let net_profit = final_value - initial_capital;
|
||||
let recovery_factor = if self.max_drawdown_pct > 0.0 && initial_capital > 0.0 {
|
||||
let max_dd_absolute = self.max_drawdown_pct / 100.0 * initial_capital;
|
||||
if max_dd_absolute > 0.0 {
|
||||
net_profit / max_dd_absolute
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
} else if net_profit > 0.0 {
|
||||
f64::INFINITY
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
BacktestMetrics {
|
||||
total_return_pct,
|
||||
sharpe_ratio,
|
||||
@@ -545,6 +569,8 @@ impl StreamingMetrics {
|
||||
max_consecutive_losses: self.max_consecutive_losses,
|
||||
avg_holding_period,
|
||||
exposure_pct: 0.0, // TODO: calculate based on time in market
|
||||
payoff_ratio,
|
||||
recovery_factor,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+123
-25
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::core::types::{
|
||||
BacktestConfig, BacktestMetrics, BacktestResult, CompiledSignals, Direction, ExitReason,
|
||||
OhlcvData, Price, StopConfig, TargetConfig, Trade,
|
||||
InstrumentConfig, OhlcvData, Price, StopConfig, TargetConfig, Trade,
|
||||
};
|
||||
use crate::execution::{FeeModel, FillPrice, SlippageModel};
|
||||
use crate::indicators::volatility::atr;
|
||||
@@ -69,6 +69,24 @@ impl PortfolioEngine {
|
||||
/// # Returns
|
||||
/// Backtest result
|
||||
pub fn run_single(&self, ohlcv: &OhlcvData, signals: &CompiledSignals) -> BacktestResult {
|
||||
self.run_single_with_instrument_config(ohlcv, signals, None)
|
||||
}
|
||||
|
||||
/// Run backtest on single instrument with optional per-instrument configuration.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `ohlcv` - OHLCV data
|
||||
/// * `signals` - Compiled trading signals
|
||||
/// * `inst_config` - Optional per-instrument config (lot_size, capital cap, stop/target overrides)
|
||||
///
|
||||
/// # Returns
|
||||
/// Backtest result
|
||||
pub fn run_single_with_instrument_config(
|
||||
&self,
|
||||
ohlcv: &OhlcvData,
|
||||
signals: &CompiledSignals,
|
||||
inst_config: Option<&InstrumentConfig>,
|
||||
) -> BacktestResult {
|
||||
let n = ohlcv.len();
|
||||
assert_eq!(n, signals.len(), "OHLCV and signals must have same length");
|
||||
|
||||
@@ -86,14 +104,20 @@ impl PortfolioEngine {
|
||||
let mut streaming = StreamingMetrics::new();
|
||||
let mut peak_equity = cash;
|
||||
|
||||
// Determine effective stop/target configs (per-instrument overrides take precedence)
|
||||
let effective_stop =
|
||||
inst_config.and_then(|ic| ic.stop.as_ref()).unwrap_or(&self.config.stop);
|
||||
let effective_target =
|
||||
inst_config.and_then(|ic| ic.target.as_ref()).unwrap_or(&self.config.target);
|
||||
|
||||
// Pre-calculate ATR for ATR-based stops
|
||||
let atr_values = if matches!(self.config.stop, StopConfig::Atr { .. })
|
||||
|| matches!(self.config.target, TargetConfig::Atr { .. })
|
||||
let atr_values = if matches!(effective_stop, StopConfig::Atr { .. })
|
||||
|| matches!(effective_target, TargetConfig::Atr { .. })
|
||||
{
|
||||
let period = match self.config.stop {
|
||||
StopConfig::Atr { period, .. } => period,
|
||||
_ => match self.config.target {
|
||||
TargetConfig::Atr { period, .. } => period,
|
||||
let period = match effective_stop {
|
||||
StopConfig::Atr { period, .. } => *period,
|
||||
_ => match effective_target {
|
||||
TargetConfig::Atr { period, .. } => *period,
|
||||
_ => 14,
|
||||
},
|
||||
};
|
||||
@@ -188,8 +212,8 @@ impl PortfolioEngine {
|
||||
|
||||
// Update trailing stop if position still open
|
||||
if position.is_in_position() {
|
||||
if let StopConfig::Trailing { percent } = self.config.stop {
|
||||
position.update_trailing_stop(percent);
|
||||
if let StopConfig::Trailing { percent } = effective_stop {
|
||||
position.update_trailing_stop(*percent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,26 +231,37 @@ impl PortfolioEngine {
|
||||
);
|
||||
|
||||
// Calculate position size
|
||||
// VectorBT formula: size = cash / (price * (1 + fees))
|
||||
// This ensures the position value plus entry fee equals available cash
|
||||
// Use per-instrument capital if set, capped at available cash
|
||||
let available = inst_config
|
||||
.and_then(|ic| ic.alloted_capital)
|
||||
.map(|cap| cap.min(cash))
|
||||
.unwrap_or(cash);
|
||||
|
||||
// Position sizing: size = cash / (price * (1 + fees))
|
||||
// Ensures position value plus entry fee equals available cash
|
||||
let fee_rate = self.config.fees;
|
||||
let size = if let Some(ref sizes) = signals.position_sizes {
|
||||
sizes[i] * cash / (adjusted_price * (1.0 + fee_rate))
|
||||
let raw_size = if let Some(ref sizes) = signals.position_sizes {
|
||||
sizes[i] * available / (adjusted_price * (1.0 + fee_rate))
|
||||
} else {
|
||||
cash / (adjusted_price * (1.0 + fee_rate))
|
||||
available / (adjusted_price * (1.0 + fee_rate))
|
||||
};
|
||||
|
||||
// Round to lot_size
|
||||
let size = inst_config.map(|ic| ic.round_to_lot(raw_size)).unwrap_or(raw_size);
|
||||
|
||||
if size > 0.0 {
|
||||
// Calculate entry fees
|
||||
let entry_fees =
|
||||
self.fee_model.calculate(adjusted_price, size, signals.direction);
|
||||
|
||||
// Calculate stop and target prices
|
||||
let (stop_price, target_price) = self.calculate_stop_target(
|
||||
let (stop_price, target_price) = self.calculate_stop_target_with_config(
|
||||
adjusted_price,
|
||||
signals.direction,
|
||||
&atr_values,
|
||||
i,
|
||||
effective_stop,
|
||||
effective_target,
|
||||
);
|
||||
|
||||
// Open position (passing entry_fees for trade PnL tracking)
|
||||
@@ -264,12 +299,11 @@ impl PortfolioEngine {
|
||||
}
|
||||
}
|
||||
|
||||
// Mark any open position at end of data (no exit fees, matching VectorBT behavior)
|
||||
// Mark any open position at end of data — marked-to-market, no exit fees
|
||||
if position.is_in_position() {
|
||||
let last_idx = n - 1;
|
||||
let exit_price = ohlcv.close[last_idx];
|
||||
// No exit fees for EndOfData - position is marked-to-market but not actually closed
|
||||
// This matches VectorBT's behavior for "Open" trades
|
||||
// No exit fees for EndOfData: position is marked-to-market but not actually closed
|
||||
let exit_fees = 0.0;
|
||||
|
||||
if let Some(trade) = position.close_position(
|
||||
@@ -310,18 +344,39 @@ impl PortfolioEngine {
|
||||
)
|
||||
}
|
||||
|
||||
/// Calculate stop and target prices.
|
||||
/// Calculate stop and target prices using the global config.
|
||||
#[allow(dead_code)]
|
||||
fn calculate_stop_target(
|
||||
&self,
|
||||
entry_price: Price,
|
||||
direction: Direction,
|
||||
atr_values: &[f64],
|
||||
idx: usize,
|
||||
) -> (Option<Price>, Option<Price>) {
|
||||
self.calculate_stop_target_with_config(
|
||||
entry_price,
|
||||
direction,
|
||||
atr_values,
|
||||
idx,
|
||||
&self.config.stop,
|
||||
&self.config.target,
|
||||
)
|
||||
}
|
||||
|
||||
/// Calculate stop and target prices with explicit stop/target configs.
|
||||
fn calculate_stop_target_with_config(
|
||||
&self,
|
||||
entry_price: Price,
|
||||
direction: Direction,
|
||||
atr_values: &[f64],
|
||||
idx: usize,
|
||||
stop_config: &StopConfig,
|
||||
target_config: &TargetConfig,
|
||||
) -> (Option<Price>, Option<Price>) {
|
||||
let multiplier = direction.multiplier();
|
||||
|
||||
// Calculate stop price
|
||||
let stop_price = match self.config.stop {
|
||||
let stop_price = match stop_config {
|
||||
StopConfig::None => None,
|
||||
StopConfig::Fixed { percent } => Some(entry_price * (1.0 - multiplier * percent)),
|
||||
StopConfig::Atr { multiplier: m, .. } => {
|
||||
@@ -336,7 +391,7 @@ impl PortfolioEngine {
|
||||
};
|
||||
|
||||
// Calculate target price
|
||||
let target_price = match self.config.target {
|
||||
let target_price = match target_config {
|
||||
TargetConfig::None => None,
|
||||
TargetConfig::Fixed { percent } => Some(entry_price * (1.0 + multiplier * percent)),
|
||||
TargetConfig::Atr { multiplier: m, .. } => {
|
||||
@@ -516,11 +571,9 @@ impl PortfolioEngine {
|
||||
};
|
||||
|
||||
// Risk-adjusted metrics (calculated from daily portfolio returns, not trade returns)
|
||||
// This matches VectorBT's calculation methodology
|
||||
let (sharpe_ratio, sortino_ratio, omega_ratio) = self.calculate_risk_metrics(returns);
|
||||
|
||||
// Calmar ratio: CAGR / max drawdown
|
||||
// VectorBT uses Compound Annual Growth Rate (CAGR)
|
||||
let num_periods = equity_curve.len().max(1) as f64;
|
||||
let years = num_periods / 365.25; // Convert to years using 365.25 days
|
||||
let total_return_frac = total_return_pct / 100.0;
|
||||
@@ -535,6 +588,30 @@ impl PortfolioEngine {
|
||||
0.0
|
||||
};
|
||||
|
||||
// Payoff ratio: average win / average loss (absolute value)
|
||||
let payoff_ratio = if avg_loss_pct.abs() > 0.0 {
|
||||
avg_win_pct / avg_loss_pct.abs()
|
||||
} else if avg_win_pct > 0.0 {
|
||||
f64::INFINITY
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
// Recovery factor: net profit / max drawdown (absolute value)
|
||||
let net_profit = end_value - start_value;
|
||||
let recovery_factor = if max_drawdown_pct > 0.0 && start_value > 0.0 {
|
||||
let max_dd_absolute = max_drawdown_pct / 100.0 * start_value;
|
||||
if max_dd_absolute > 0.0 {
|
||||
net_profit / max_dd_absolute
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
} else if net_profit > 0.0 {
|
||||
f64::INFINITY
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
BacktestMetrics {
|
||||
total_return_pct,
|
||||
sharpe_ratio,
|
||||
@@ -567,6 +644,8 @@ impl PortfolioEngine {
|
||||
max_consecutive_losses,
|
||||
avg_holding_period,
|
||||
exposure_pct,
|
||||
payoff_ratio,
|
||||
recovery_factor,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -611,13 +690,13 @@ impl PortfolioEngine {
|
||||
|
||||
/// Calculate risk-adjusted metrics from daily portfolio returns.
|
||||
/// Returns (sharpe_ratio, sortino_ratio, omega_ratio).
|
||||
/// Uses 365 days for annualization to match VectorBT.
|
||||
/// Uses 365 calendar days for annualization.
|
||||
fn calculate_risk_metrics(&self, returns: &[f64]) -> (f64, f64, f64) {
|
||||
if returns.len() < 2 {
|
||||
return (0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
// VectorBT uses 365 days (calendar days) for annualization
|
||||
// 365 calendar days for annualization
|
||||
let periods_per_year: f64 = 365.0;
|
||||
let _n = returns.len() as f64;
|
||||
|
||||
@@ -679,6 +758,25 @@ impl PortfolioEngine {
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute `BacktestMetrics` from pre-built curves and trade list.
|
||||
///
|
||||
/// Exposed as a standalone function so non-OHLCV strategies (e.g. tick backtest)
|
||||
/// can produce identical metrics without duplicating the calculation logic.
|
||||
pub fn compute_backtest_metrics(
|
||||
equity_curve: &[f64],
|
||||
drawdown_curve: &[f64],
|
||||
returns: &[f64],
|
||||
trades: &[Trade],
|
||||
initial_capital: f64,
|
||||
) -> BacktestMetrics {
|
||||
// Delegate to a throwaway engine instance — avoids duplicating the logic.
|
||||
let engine = PortfolioEngine::new(BacktestConfig {
|
||||
initial_capital,
|
||||
..Default::default()
|
||||
});
|
||||
engine.calculate_metrics(equity_curve, drawdown_curve, returns, trades, &StreamingMetrics::new())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
pub mod allocation;
|
||||
pub mod engine;
|
||||
pub mod monte_carlo;
|
||||
pub mod position;
|
||||
|
||||
pub use allocation::{AllocationStrategy, CapitalAllocator};
|
||||
pub use engine::PortfolioEngine;
|
||||
pub use monte_carlo::{simulate_portfolio_forward, MonteCarloConfig, MonteCarloResult};
|
||||
pub use position::PositionManager;
|
||||
|
||||
@@ -0,0 +1,361 @@
|
||||
//! Monte Carlo forward simulation for portfolio projection.
|
||||
//!
|
||||
//! Uses Geometric Brownian Motion (GBM) with Cholesky decomposition
|
||||
//! for correlated multi-asset simulation. Parallelized via Rayon.
|
||||
|
||||
use rayon::prelude::*;
|
||||
|
||||
/// Configuration for Monte Carlo simulation.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MonteCarloConfig {
|
||||
pub n_simulations: usize,
|
||||
pub horizon_days: usize,
|
||||
pub seed: u64,
|
||||
}
|
||||
|
||||
impl Default for MonteCarloConfig {
|
||||
fn default() -> Self {
|
||||
Self { n_simulations: 10_000, horizon_days: 252, seed: 42 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of a Monte Carlo simulation.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MonteCarloResult {
|
||||
/// Percentile paths: Vec of (percentile, path_values)
|
||||
pub percentile_paths: Vec<(f64, Vec<f64>)>,
|
||||
/// Terminal value for each simulation
|
||||
pub final_values: Vec<f64>,
|
||||
/// Expected annualized return
|
||||
pub expected_return: f64,
|
||||
/// Probability of loss (final value < initial value)
|
||||
pub probability_of_loss: f64,
|
||||
/// Value at Risk at 95% confidence
|
||||
pub var_95: f64,
|
||||
/// Conditional Value at Risk at 95% confidence
|
||||
pub cvar_95: f64,
|
||||
}
|
||||
|
||||
/// Cholesky decomposition of a symmetric positive-definite matrix.
|
||||
/// Returns lower-triangular matrix L such that A = L * L^T.
|
||||
fn cholesky(matrix: &[Vec<f64>]) -> Result<Vec<Vec<f64>>, &'static str> {
|
||||
let n = matrix.len();
|
||||
let mut l = vec![vec![0.0; n]; n];
|
||||
|
||||
for i in 0..n {
|
||||
for j in 0..=i {
|
||||
let mut sum = 0.0;
|
||||
for k in 0..j {
|
||||
sum += l[i][k] * l[j][k];
|
||||
}
|
||||
|
||||
if i == j {
|
||||
let diag = matrix[i][i] - sum;
|
||||
if diag <= 0.0 {
|
||||
// Matrix is not positive definite; use a small epsilon
|
||||
l[i][j] = (diag.abs().max(1e-10)).sqrt();
|
||||
} else {
|
||||
l[i][j] = diag.sqrt();
|
||||
}
|
||||
} else {
|
||||
if l[j][j].abs() < 1e-15 {
|
||||
l[i][j] = 0.0;
|
||||
} else {
|
||||
l[i][j] = (matrix[i][j] - sum) / l[j][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(l)
|
||||
}
|
||||
|
||||
/// Simple xoshiro256** PRNG for deterministic parallel simulation.
|
||||
#[derive(Clone)]
|
||||
struct Xoshiro256 {
|
||||
s: [u64; 4],
|
||||
}
|
||||
|
||||
impl Xoshiro256 {
|
||||
fn new(seed: u64) -> Self {
|
||||
// SplitMix64 to seed all 4 state words
|
||||
let mut z = seed;
|
||||
let mut s = [0u64; 4];
|
||||
for item in &mut s {
|
||||
z = z.wrapping_add(0x9e3779b97f4a7c15);
|
||||
z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
|
||||
z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
|
||||
*item = z ^ (z >> 31);
|
||||
}
|
||||
Self { s }
|
||||
}
|
||||
|
||||
fn jump(&mut self) {
|
||||
// Jump function: advances state by 2^128 calls
|
||||
const JUMP: [u64; 4] =
|
||||
[0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c];
|
||||
let mut s0: u64 = 0;
|
||||
let mut s1: u64 = 0;
|
||||
let mut s2: u64 = 0;
|
||||
let mut s3: u64 = 0;
|
||||
for j in &JUMP {
|
||||
for b in 0..64 {
|
||||
if j & (1u64 << b) != 0 {
|
||||
s0 ^= self.s[0];
|
||||
s1 ^= self.s[1];
|
||||
s2 ^= self.s[2];
|
||||
s3 ^= self.s[3];
|
||||
}
|
||||
self.next_u64();
|
||||
}
|
||||
}
|
||||
self.s[0] = s0;
|
||||
self.s[1] = s1;
|
||||
self.s[2] = s2;
|
||||
self.s[3] = s3;
|
||||
}
|
||||
|
||||
fn next_u64(&mut self) -> u64 {
|
||||
let result = (self.s[1].wrapping_mul(5)).rotate_left(7).wrapping_mul(9);
|
||||
let t = self.s[1] << 17;
|
||||
self.s[2] ^= self.s[0];
|
||||
self.s[3] ^= self.s[1];
|
||||
self.s[1] ^= self.s[2];
|
||||
self.s[0] ^= self.s[3];
|
||||
self.s[2] ^= t;
|
||||
self.s[3] = self.s[3].rotate_left(45);
|
||||
result
|
||||
}
|
||||
|
||||
/// Generate uniform f64 in [0, 1).
|
||||
fn next_f64(&mut self) -> f64 {
|
||||
(self.next_u64() >> 11) as f64 * (1.0 / (1u64 << 53) as f64)
|
||||
}
|
||||
|
||||
/// Box-Muller transform for standard normal.
|
||||
fn next_normal(&mut self) -> f64 {
|
||||
let u1 = self.next_f64().max(1e-15);
|
||||
let u2 = self.next_f64();
|
||||
(-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos()
|
||||
}
|
||||
}
|
||||
|
||||
/// Core Monte Carlo simulation function.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `returns` - Per-strategy daily returns (N strategies x T days each)
|
||||
/// * `weights` - Portfolio weights (length N, must sum to 1)
|
||||
/// * `correlation_matrix` - N x N correlation matrix
|
||||
/// * `initial_value` - Starting portfolio value
|
||||
/// * `config` - Simulation configuration
|
||||
pub fn simulate_portfolio_forward(
|
||||
returns: &[Vec<f64>],
|
||||
weights: &[f64],
|
||||
correlation_matrix: &[Vec<f64>],
|
||||
initial_value: f64,
|
||||
config: &MonteCarloConfig,
|
||||
) -> MonteCarloResult {
|
||||
let n_assets = returns.len();
|
||||
let dt = 1.0; // daily time step
|
||||
|
||||
// Compute per-asset mean and std of historical returns
|
||||
let mut mus = vec![0.0; n_assets];
|
||||
let mut sigmas = vec![0.0; n_assets];
|
||||
for (i, ret) in returns.iter().enumerate() {
|
||||
if ret.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mean = ret.iter().sum::<f64>() / ret.len() as f64;
|
||||
let var = ret.iter().map(|r| (r - mean).powi(2)).sum::<f64>() / ret.len() as f64;
|
||||
mus[i] = mean;
|
||||
sigmas[i] = var.sqrt().max(1e-10);
|
||||
}
|
||||
|
||||
// Cholesky decomposition of correlation matrix
|
||||
let chol = cholesky(correlation_matrix).unwrap_or_else(|_| {
|
||||
// Fallback: identity matrix (independent assets)
|
||||
let mut identity = vec![vec![0.0; n_assets]; n_assets];
|
||||
for i in 0..n_assets {
|
||||
identity[i][i] = 1.0;
|
||||
}
|
||||
identity
|
||||
});
|
||||
|
||||
// Prepare a base RNG and create per-chunk seeds via jumping
|
||||
let mut base_rng = Xoshiro256::new(config.seed);
|
||||
let n_chunks = rayon::current_num_threads().max(1);
|
||||
let chunk_size = (config.n_simulations + n_chunks - 1) / n_chunks;
|
||||
|
||||
let chunk_rngs: Vec<Xoshiro256> = (0..n_chunks)
|
||||
.map(|_| {
|
||||
let rng = base_rng.clone();
|
||||
base_rng.jump();
|
||||
rng
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Run simulations in parallel chunks
|
||||
let all_paths: Vec<Vec<f64>> = chunk_rngs
|
||||
.into_par_iter()
|
||||
.enumerate()
|
||||
.flat_map(|(chunk_idx, mut rng)| {
|
||||
let start = chunk_idx * chunk_size;
|
||||
let end = (start + chunk_size).min(config.n_simulations);
|
||||
let mut chunk_paths = Vec::with_capacity(end - start);
|
||||
|
||||
for _ in start..end {
|
||||
let mut portfolio_value = initial_value;
|
||||
let mut path = Vec::with_capacity(config.horizon_days + 1);
|
||||
path.push(portfolio_value);
|
||||
|
||||
for _ in 0..config.horizon_days {
|
||||
// Generate N independent standard normals
|
||||
let z_indep: Vec<f64> = (0..n_assets).map(|_| rng.next_normal()).collect();
|
||||
|
||||
// Correlate via Cholesky: z_corr = L * z_indep
|
||||
let mut z_corr = vec![0.0; n_assets];
|
||||
for i in 0..n_assets {
|
||||
for j in 0..=i {
|
||||
z_corr[i] += chol[i][j] * z_indep[j];
|
||||
}
|
||||
}
|
||||
|
||||
// GBM per asset, then weighted portfolio return
|
||||
let mut portfolio_return = 0.0;
|
||||
for i in 0..n_assets {
|
||||
let drift = (mus[i] - 0.5 * sigmas[i].powi(2)) * dt;
|
||||
let diffusion = sigmas[i] * dt.sqrt() * z_corr[i];
|
||||
let asset_return = (drift + diffusion).exp() - 1.0;
|
||||
portfolio_return += weights[i] * asset_return;
|
||||
}
|
||||
|
||||
portfolio_value *= 1.0 + portfolio_return;
|
||||
path.push(portfolio_value);
|
||||
}
|
||||
|
||||
chunk_paths.push(path);
|
||||
}
|
||||
|
||||
chunk_paths
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Extract final values
|
||||
let mut final_values: Vec<f64> = all_paths.iter().map(|p| *p.last().unwrap()).collect();
|
||||
final_values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
|
||||
|
||||
let n = final_values.len();
|
||||
|
||||
// Percentile paths: find simulations closest to each percentile's final value
|
||||
let percentiles = [5.0, 25.0, 50.0, 75.0, 95.0];
|
||||
let percentile_paths: Vec<(f64, Vec<f64>)> = percentiles
|
||||
.iter()
|
||||
.map(|&pct| {
|
||||
let idx = ((pct / 100.0) * (n as f64 - 1.0)).round() as usize;
|
||||
let target_final = final_values[idx.min(n - 1)];
|
||||
|
||||
// Find the simulation path whose final value is closest to target
|
||||
let best_idx = all_paths
|
||||
.iter()
|
||||
.enumerate()
|
||||
.min_by(|(_, a), (_, b)| {
|
||||
let da = (a.last().unwrap() - target_final).abs();
|
||||
let db = (b.last().unwrap() - target_final).abs();
|
||||
da.partial_cmp(&db).unwrap_or(std::cmp::Ordering::Equal)
|
||||
})
|
||||
.map(|(i, _)| i)
|
||||
.unwrap_or(0);
|
||||
|
||||
(pct, all_paths[best_idx].clone())
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Expected return (annualized from mean of final values)
|
||||
let mean_final = final_values.iter().sum::<f64>() / n as f64;
|
||||
let expected_return = (mean_final / initial_value - 1.0) * 100.0;
|
||||
|
||||
// Probability of loss
|
||||
let n_loss = final_values.iter().filter(|&&v| v < initial_value).count();
|
||||
let probability_of_loss = n_loss as f64 / n as f64;
|
||||
|
||||
// VaR 95%: 5th percentile loss
|
||||
let p5_idx = ((0.05 * (n as f64 - 1.0)).round() as usize).min(n - 1);
|
||||
let var_95 = ((initial_value - final_values[p5_idx]) / initial_value * 100.0).max(0.0);
|
||||
|
||||
// CVaR 95%: average of losses below VaR
|
||||
let cvar_values = &final_values[..=p5_idx];
|
||||
let cvar_95 = if cvar_values.is_empty() {
|
||||
var_95
|
||||
} else {
|
||||
let avg_tail = cvar_values.iter().sum::<f64>() / cvar_values.len() as f64;
|
||||
((initial_value - avg_tail) / initial_value * 100.0).max(0.0)
|
||||
};
|
||||
|
||||
MonteCarloResult {
|
||||
percentile_paths,
|
||||
final_values,
|
||||
expected_return,
|
||||
probability_of_loss,
|
||||
var_95,
|
||||
cvar_95,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_cholesky_identity() {
|
||||
let matrix = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
|
||||
let l = cholesky(&matrix).unwrap();
|
||||
assert!((l[0][0] - 1.0).abs() < 1e-10);
|
||||
assert!((l[1][1] - 1.0).abs() < 1e-10);
|
||||
assert!(l[0][1].abs() < 1e-10);
|
||||
assert!(l[1][0].abs() < 1e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cholesky_correlated() {
|
||||
let matrix = vec![vec![1.0, 0.5], vec![0.5, 1.0]];
|
||||
let l = cholesky(&matrix).unwrap();
|
||||
// Verify L * L^T = matrix
|
||||
let reconstructed_00 = l[0][0] * l[0][0];
|
||||
let reconstructed_01 = l[1][0] * l[0][0];
|
||||
let reconstructed_11 = l[1][0] * l[1][0] + l[1][1] * l[1][1];
|
||||
assert!((reconstructed_00 - 1.0).abs() < 1e-10);
|
||||
assert!((reconstructed_01 - 0.5).abs() < 1e-10);
|
||||
assert!((reconstructed_11 - 1.0).abs() < 1e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simulate_basic() {
|
||||
// Two assets with identical positive returns
|
||||
let returns = vec![vec![0.001; 252], vec![0.001; 252]];
|
||||
let weights = vec![0.5, 0.5];
|
||||
let corr = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
|
||||
let config = MonteCarloConfig { n_simulations: 100, horizon_days: 10, seed: 42 };
|
||||
|
||||
let result = simulate_portfolio_forward(&returns, &weights, &corr, 100000.0, &config);
|
||||
|
||||
assert_eq!(result.final_values.len(), 100);
|
||||
assert_eq!(result.percentile_paths.len(), 5);
|
||||
// Expected return should be positive given positive drift
|
||||
assert!(result.expected_return > -50.0); // Sanity check
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deterministic() {
|
||||
let returns = vec![vec![0.001; 100], vec![-0.0005; 100]];
|
||||
let weights = vec![0.6, 0.4];
|
||||
let corr = vec![vec![1.0, -0.3], vec![-0.3, 1.0]];
|
||||
let config = MonteCarloConfig { n_simulations: 50, horizon_days: 20, seed: 123 };
|
||||
|
||||
let r1 = simulate_portfolio_forward(&returns, &weights, &corr, 100000.0, &config);
|
||||
let r2 = simulate_portfolio_forward(&returns, &weights, &corr, 100000.0, &config);
|
||||
|
||||
// Same seed should produce same final values (single-threaded determinism)
|
||||
// Note: with rayon, parallelism may affect order but not values
|
||||
assert!((r1.expected_return - r2.expected_return).abs() < 1e-6);
|
||||
}
|
||||
}
|
||||
@@ -112,7 +112,7 @@ impl PositionManager {
|
||||
let pos = &self.position;
|
||||
let multiplier = pos.direction.multiplier();
|
||||
|
||||
// Calculate P&L (matching VectorBT: gross - entry_fees - exit_fees)
|
||||
// Calculate P&L: gross - entry_fees - exit_fees
|
||||
let gross_pnl = (exit_price - pos.entry_price) * pos.size * multiplier;
|
||||
let total_fees = pos.entry_fees + exit_fees;
|
||||
let pnl = gross_pnl - total_fees;
|
||||
|
||||
+607
-7
@@ -3,8 +3,11 @@
|
||||
use numpy::{PyArray1, PyReadonlyArray1};
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::core::types::{
|
||||
BacktestConfig, CompiledSignals, Direction, OhlcvData, StopConfig, TargetConfig,
|
||||
BacktestConfig, CompiledSignals, Direction, InstrumentConfig, OhlcvData, StopConfig,
|
||||
TargetConfig,
|
||||
};
|
||||
use crate::indicators;
|
||||
use crate::signals::synchronizer::SyncMode;
|
||||
@@ -18,6 +21,7 @@ use crate::strategies::single::SingleBacktest;
|
||||
use crate::strategies::spreads::{
|
||||
LegConfig, OptionType as SpreadOptionType, SpreadBacktest, SpreadConfig, SpreadType,
|
||||
};
|
||||
use crate::strategies::tick::{TickBacktest, TickBacktestConfig};
|
||||
|
||||
use super::numpy_bridge::*;
|
||||
|
||||
@@ -100,6 +104,93 @@ impl From<&PyBacktestConfig> for BacktestConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// Python-exposed per-instrument configuration.
|
||||
#[pyclass]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PyInstrumentConfig {
|
||||
#[pyo3(get, set)]
|
||||
pub lot_size: Option<f64>,
|
||||
#[pyo3(get, set)]
|
||||
pub alloted_capital: Option<f64>,
|
||||
#[pyo3(get, set)]
|
||||
pub existing_qty: Option<f64>,
|
||||
#[pyo3(get, set)]
|
||||
pub avg_price: Option<f64>,
|
||||
stop_config: Option<StopConfig>,
|
||||
target_config: Option<TargetConfig>,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl PyInstrumentConfig {
|
||||
#[new]
|
||||
#[pyo3(signature = (lot_size=None, alloted_capital=None, existing_qty=None, avg_price=None))]
|
||||
fn new(
|
||||
lot_size: Option<f64>,
|
||||
alloted_capital: Option<f64>,
|
||||
existing_qty: Option<f64>,
|
||||
avg_price: Option<f64>,
|
||||
) -> Self {
|
||||
Self {
|
||||
lot_size,
|
||||
alloted_capital,
|
||||
existing_qty,
|
||||
avg_price,
|
||||
stop_config: None,
|
||||
target_config: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set fixed percentage stop-loss override.
|
||||
fn set_fixed_stop(&mut self, percent: f64) {
|
||||
self.stop_config = Some(StopConfig::Fixed { percent });
|
||||
}
|
||||
|
||||
/// Set ATR-based stop-loss override.
|
||||
fn set_atr_stop(&mut self, multiplier: f64, period: usize) {
|
||||
self.stop_config = Some(StopConfig::Atr { multiplier, period });
|
||||
}
|
||||
|
||||
/// Set trailing stop-loss override.
|
||||
fn set_trailing_stop(&mut self, percent: f64) {
|
||||
self.stop_config = Some(StopConfig::Trailing { percent });
|
||||
}
|
||||
|
||||
/// Set fixed percentage take-profit override.
|
||||
fn set_fixed_target(&mut self, percent: f64) {
|
||||
self.target_config = Some(TargetConfig::Fixed { percent });
|
||||
}
|
||||
|
||||
/// Set ATR-based take-profit override.
|
||||
fn set_atr_target(&mut self, multiplier: f64, period: usize) {
|
||||
self.target_config = Some(TargetConfig::Atr { multiplier, period });
|
||||
}
|
||||
|
||||
/// Set risk-reward based take-profit override.
|
||||
fn set_risk_reward_target(&mut self, ratio: f64) {
|
||||
self.target_config = Some(TargetConfig::RiskReward { ratio });
|
||||
}
|
||||
|
||||
fn __repr__(&self) -> String {
|
||||
format!(
|
||||
"InstrumentConfig(lot_size={:?}, alloted_capital={:?})",
|
||||
self.lot_size, self.alloted_capital
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PyInstrumentConfig> for InstrumentConfig {
|
||||
fn from(py_config: &PyInstrumentConfig) -> Self {
|
||||
InstrumentConfig {
|
||||
lot_size: py_config.lot_size,
|
||||
alloted_capital: py_config.alloted_capital,
|
||||
stop: py_config.stop_config,
|
||||
target: py_config.target_config,
|
||||
existing_qty: py_config.existing_qty,
|
||||
avg_price: py_config.avg_price,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Python-exposed stop configuration.
|
||||
#[pyclass]
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -329,6 +420,10 @@ pub struct PyBacktestMetrics {
|
||||
pub avg_holding_period: f64,
|
||||
#[pyo3(get)]
|
||||
pub exposure_pct: f64,
|
||||
#[pyo3(get)]
|
||||
pub payoff_ratio: f64,
|
||||
#[pyo3(get)]
|
||||
pub recovery_factor: f64,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
@@ -340,7 +435,7 @@ impl PyBacktestMetrics {
|
||||
)
|
||||
}
|
||||
|
||||
/// Convert to dictionary matching VectorBT stats() format.
|
||||
/// Convert to dictionary of all metrics.
|
||||
fn to_dict(&self, py: Python) -> PyResult<PyObject> {
|
||||
let dict = pyo3::types::PyDict::new(py);
|
||||
dict.set_item("Start Value", self.start_value)?;
|
||||
@@ -419,7 +514,7 @@ impl PyBacktestResult {
|
||||
|
||||
/// Run single instrument backtest.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (timestamps, open, high, low, close, volume, entries, exits, direction=1, weight=1.0, symbol="UNKNOWN", config=None, position_sizes=None))]
|
||||
#[pyo3(signature = (timestamps, open, high, low, close, volume, entries, exits, direction=1, weight=1.0, symbol="UNKNOWN", config=None, position_sizes=None, instrument_config=None))]
|
||||
pub fn run_single_backtest<'py>(
|
||||
_py: Python<'py>,
|
||||
timestamps: PyReadonlyArray1<i64>,
|
||||
@@ -435,6 +530,7 @@ pub fn run_single_backtest<'py>(
|
||||
symbol: &str,
|
||||
config: Option<&PyBacktestConfig>,
|
||||
position_sizes: Option<PyReadonlyArray1<f64>>,
|
||||
instrument_config: Option<&PyInstrumentConfig>,
|
||||
) -> PyResult<PyBacktestResult> {
|
||||
let ohlcv = OhlcvData {
|
||||
timestamps: numpy_to_vec_i64(timestamps),
|
||||
@@ -457,16 +553,17 @@ pub fn run_single_backtest<'py>(
|
||||
};
|
||||
|
||||
let rust_config = config.map(|c| BacktestConfig::from(c)).unwrap_or_default();
|
||||
let inst_config = instrument_config.map(InstrumentConfig::from);
|
||||
|
||||
let backtest = SingleBacktest::new(rust_config);
|
||||
let result = backtest.run(&ohlcv, &signals);
|
||||
let result = backtest.run_with_instrument_config(&ohlcv, &signals, inst_config.as_ref());
|
||||
|
||||
Ok(convert_result(result))
|
||||
}
|
||||
|
||||
/// Run basket/collective backtest.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (instruments, config=None, sync_mode="all"))]
|
||||
#[pyo3(signature = (instruments, config=None, sync_mode="all", instrument_configs=None))]
|
||||
pub fn run_basket_backtest<'py>(
|
||||
_py: Python<'py>,
|
||||
instruments: Vec<(
|
||||
@@ -484,6 +581,7 @@ pub fn run_basket_backtest<'py>(
|
||||
)>,
|
||||
config: Option<&PyBacktestConfig>,
|
||||
sync_mode: &str,
|
||||
instrument_configs: Option<HashMap<String, PyInstrumentConfig>>,
|
||||
) -> PyResult<PyBacktestResult> {
|
||||
let rust_instruments: Vec<(OhlcvData, CompiledSignals)> = instruments
|
||||
.into_iter()
|
||||
@@ -521,8 +619,15 @@ pub fn run_basket_backtest<'py>(
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Convert PyInstrumentConfig map to InstrumentConfig map
|
||||
let rust_inst_configs: Option<HashMap<String, InstrumentConfig>> =
|
||||
instrument_configs.map(|configs| {
|
||||
configs.iter().map(|(k, v)| (k.clone(), InstrumentConfig::from(v))).collect()
|
||||
});
|
||||
|
||||
let backtest = BasketBacktest::new(basket_config);
|
||||
let result = backtest.run(&rust_instruments);
|
||||
let result =
|
||||
backtest.run_with_instrument_configs(&rust_instruments, rust_inst_configs.as_ref());
|
||||
|
||||
Ok(convert_result(result))
|
||||
}
|
||||
@@ -678,7 +783,7 @@ pub fn run_pairs_backtest<'py>(
|
||||
|
||||
/// Run spread backtest (multi-leg options).
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (timestamps, underlying_close, legs_premiums, leg_configs, entries, exits, config=None, spread_type="custom", max_loss=None, target_profit=None))]
|
||||
#[pyo3(signature = (timestamps, underlying_close, legs_premiums, leg_configs, entries, exits, config=None, spread_type="custom", max_loss=None, target_profit=None, leg_expiry_timestamps=None))]
|
||||
pub fn run_spread_backtest<'py>(
|
||||
_py: Python<'py>,
|
||||
timestamps: PyReadonlyArray1<i64>,
|
||||
@@ -691,6 +796,7 @@ pub fn run_spread_backtest<'py>(
|
||||
spread_type: &str,
|
||||
max_loss: Option<f64>,
|
||||
target_profit: Option<f64>,
|
||||
leg_expiry_timestamps: Option<Vec<i64>>,
|
||||
) -> PyResult<PyBacktestResult> {
|
||||
let ts = numpy_to_vec_i64(timestamps);
|
||||
let underlying = numpy_to_vec_f64(underlying_close);
|
||||
@@ -720,6 +826,10 @@ pub fn run_spread_backtest<'py>(
|
||||
"butterfly_put" | "butterflyput" => SpreadType::ButterflyPut,
|
||||
"calendar" => SpreadType::Calendar,
|
||||
"diagonal" => SpreadType::Diagonal,
|
||||
"long_call" | "longcall" => SpreadType::LongCall,
|
||||
"long_put" | "longput" => SpreadType::LongPut,
|
||||
"naked_call" | "nakedcall" => SpreadType::NakedCall,
|
||||
"naked_put" | "nakedput" => SpreadType::NakedPut,
|
||||
_ => SpreadType::Custom,
|
||||
};
|
||||
|
||||
@@ -730,6 +840,7 @@ pub fn run_spread_backtest<'py>(
|
||||
max_loss,
|
||||
target_profit,
|
||||
close_at_eod: false,
|
||||
leg_expiry_timestamps,
|
||||
};
|
||||
|
||||
let backtest = SpreadBacktest::new(spread_config);
|
||||
@@ -738,6 +849,151 @@ pub fn run_spread_backtest<'py>(
|
||||
Ok(convert_result(result))
|
||||
}
|
||||
|
||||
/// A single spread backtest item for batch execution.
|
||||
#[pyclass]
|
||||
#[derive(Clone)]
|
||||
pub struct PyBatchSpreadItem {
|
||||
#[pyo3(get, set)]
|
||||
pub strategy_id: String,
|
||||
pub legs_premiums: Vec<Vec<f64>>,
|
||||
pub leg_configs: Vec<(String, f64, i32, usize)>,
|
||||
pub entries: Vec<bool>,
|
||||
pub exits: Vec<bool>,
|
||||
#[pyo3(get, set)]
|
||||
pub spread_type: String,
|
||||
#[pyo3(get, set)]
|
||||
pub max_loss: Option<f64>,
|
||||
#[pyo3(get, set)]
|
||||
pub target_profit: Option<f64>,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl PyBatchSpreadItem {
|
||||
#[new]
|
||||
#[pyo3(signature = (strategy_id, legs_premiums, leg_configs, entries, exits,
|
||||
spread_type="custom", max_loss=None, target_profit=None))]
|
||||
fn new(
|
||||
strategy_id: String,
|
||||
legs_premiums: Vec<PyReadonlyArray1<f64>>,
|
||||
leg_configs: Vec<(String, f64, i32, usize)>,
|
||||
entries: PyReadonlyArray1<bool>,
|
||||
exits: PyReadonlyArray1<bool>,
|
||||
spread_type: &str,
|
||||
max_loss: Option<f64>,
|
||||
target_profit: Option<f64>,
|
||||
) -> Self {
|
||||
Self {
|
||||
strategy_id,
|
||||
legs_premiums: legs_premiums.into_iter().map(numpy_to_vec_f64).collect(),
|
||||
leg_configs,
|
||||
entries: numpy_to_vec_bool(entries),
|
||||
exits: numpy_to_vec_bool(exits),
|
||||
spread_type: spread_type.to_string(),
|
||||
max_loss,
|
||||
target_profit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Run multiple spread backtests in parallel via Rayon.
|
||||
///
|
||||
/// Shared data (timestamps, underlying_close) is converted once, then each
|
||||
/// item is backtested on its own Rayon thread with the GIL released.
|
||||
///
|
||||
/// Returns a Vec of (strategy_id, PyBacktestResult) tuples.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (timestamps, underlying_close, items, config=None))]
|
||||
pub fn batch_spread_backtest(
|
||||
py: Python<'_>,
|
||||
timestamps: PyReadonlyArray1<i64>,
|
||||
underlying_close: PyReadonlyArray1<f64>,
|
||||
items: Vec<PyBatchSpreadItem>,
|
||||
config: Option<&PyBacktestConfig>,
|
||||
) -> PyResult<Vec<(String, PyBacktestResult)>> {
|
||||
use rayon::prelude::*;
|
||||
|
||||
// Convert shared data while holding GIL
|
||||
let ts = numpy_to_vec_i64(timestamps);
|
||||
let underlying = numpy_to_vec_f64(underlying_close);
|
||||
let base_config = config.map(|c| BacktestConfig::from(c)).unwrap_or_default();
|
||||
|
||||
// Prepare each item into a self-contained struct for parallel execution
|
||||
struct PreparedItem {
|
||||
strategy_id: String,
|
||||
premiums: Vec<Vec<f64>>,
|
||||
entries: Vec<bool>,
|
||||
exits: Vec<bool>,
|
||||
spread_config: SpreadConfig,
|
||||
}
|
||||
|
||||
let prepared: Vec<PreparedItem> = items
|
||||
.into_iter()
|
||||
.map(|item| {
|
||||
let rust_leg_configs: Vec<LegConfig> = item
|
||||
.leg_configs
|
||||
.into_iter()
|
||||
.map(|(opt_type, strike, quantity, lot_size)| {
|
||||
let option_type =
|
||||
SpreadOptionType::from_str(&opt_type).unwrap_or(SpreadOptionType::Call);
|
||||
LegConfig::new(option_type, strike, quantity, lot_size)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let spread_type_enum = match item.spread_type.to_lowercase().as_str() {
|
||||
"straddle" => SpreadType::Straddle,
|
||||
"strangle" => SpreadType::Strangle,
|
||||
"vertical_call" | "verticalcall" => SpreadType::VerticalCall,
|
||||
"vertical_put" | "verticalput" => SpreadType::VerticalPut,
|
||||
"iron_condor" | "ironcondor" => SpreadType::IronCondor,
|
||||
"iron_butterfly" | "ironbutterfly" => SpreadType::IronButterfly,
|
||||
"butterfly_call" | "butterflycall" => SpreadType::ButterflyCall,
|
||||
"butterfly_put" | "butterflyput" => SpreadType::ButterflyPut,
|
||||
"calendar" => SpreadType::Calendar,
|
||||
"diagonal" => SpreadType::Diagonal,
|
||||
"long_call" | "longcall" => SpreadType::LongCall,
|
||||
"long_put" | "longput" => SpreadType::LongPut,
|
||||
"naked_call" | "nakedcall" => SpreadType::NakedCall,
|
||||
"naked_put" | "nakedput" => SpreadType::NakedPut,
|
||||
_ => SpreadType::Custom,
|
||||
};
|
||||
|
||||
let spread_config = SpreadConfig {
|
||||
base: base_config.clone(),
|
||||
spread_type: spread_type_enum,
|
||||
leg_configs: rust_leg_configs.clone(),
|
||||
max_loss: item.max_loss,
|
||||
target_profit: item.target_profit,
|
||||
close_at_eod: false,
|
||||
leg_expiry_timestamps: None,
|
||||
};
|
||||
|
||||
PreparedItem {
|
||||
strategy_id: item.strategy_id,
|
||||
premiums: item.legs_premiums,
|
||||
entries: item.entries,
|
||||
exits: item.exits,
|
||||
spread_config,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Release GIL and run all backtests in parallel via Rayon
|
||||
let results: Vec<(String, crate::core::types::BacktestResult)> = py.allow_threads(|| {
|
||||
prepared
|
||||
.into_par_iter()
|
||||
.map(|item| {
|
||||
let backtest = SpreadBacktest::new(item.spread_config);
|
||||
let result =
|
||||
backtest.run(&ts, &underlying, &item.premiums, &item.entries, &item.exits);
|
||||
(item.strategy_id, result)
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
|
||||
// Re-acquire GIL and convert results to Python objects
|
||||
Ok(results.into_iter().map(|(id, result)| (id, convert_result(result))).collect())
|
||||
}
|
||||
|
||||
/// Run multi-strategy backtest.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (timestamps, open, high, low, close, volume, strategies, config=None, combine_mode="any"))]
|
||||
@@ -794,6 +1050,277 @@ pub fn run_multi_backtest<'py>(
|
||||
Ok(convert_result(result))
|
||||
}
|
||||
|
||||
/// Run tick-level backtest on a single instrument.
|
||||
///
|
||||
/// All arrays must be the same length N (one element per tick).
|
||||
/// `buy_qty_delta` and `sell_qty_delta` must already be per-tick deltas —
|
||||
/// pass the difference from the previous tick, not Zerodha's cumulative totals.
|
||||
/// `entries` / `exits` are caller-computed boolean signal arrays.
|
||||
///
|
||||
/// Returns a `PyBacktestResult` with the same fields as `run_single_backtest`.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (
|
||||
timestamps,
|
||||
ltp,
|
||||
bid,
|
||||
ask,
|
||||
buy_qty_delta,
|
||||
sell_qty_delta,
|
||||
oi,
|
||||
entries,
|
||||
exits,
|
||||
symbol = "TICK",
|
||||
initial_capital = 100_000.0,
|
||||
fees = 0.001,
|
||||
slippage = 0.0,
|
||||
stop_loss_pct = 5.0,
|
||||
take_profit_pct = 10.0,
|
||||
max_hold_seconds = 1800_u64,
|
||||
entry_cooldown_ticks = 10_usize,
|
||||
max_trades = 50_usize,
|
||||
))]
|
||||
pub fn run_tick_backtest<'py>(
|
||||
_py: Python<'py>,
|
||||
timestamps: PyReadonlyArray1<i64>,
|
||||
ltp: PyReadonlyArray1<f64>,
|
||||
bid: PyReadonlyArray1<f64>,
|
||||
ask: PyReadonlyArray1<f64>,
|
||||
buy_qty_delta: PyReadonlyArray1<f64>,
|
||||
sell_qty_delta: PyReadonlyArray1<f64>,
|
||||
oi: PyReadonlyArray1<f64>,
|
||||
entries: PyReadonlyArray1<bool>,
|
||||
exits: PyReadonlyArray1<bool>,
|
||||
symbol: &str,
|
||||
initial_capital: f64,
|
||||
fees: f64,
|
||||
slippage: f64,
|
||||
stop_loss_pct: f64,
|
||||
take_profit_pct: f64,
|
||||
max_hold_seconds: u64,
|
||||
entry_cooldown_ticks: usize,
|
||||
max_trades: usize,
|
||||
) -> PyResult<PyBacktestResult> {
|
||||
let tick_data = crate::core::types::TickData {
|
||||
timestamps: numpy_to_vec_i64(timestamps),
|
||||
ltp: numpy_to_vec_f64(ltp),
|
||||
bid: numpy_to_vec_f64(bid),
|
||||
ask: numpy_to_vec_f64(ask),
|
||||
buy_qty_delta: numpy_to_vec_f64(buy_qty_delta),
|
||||
sell_qty_delta: numpy_to_vec_f64(sell_qty_delta),
|
||||
oi: numpy_to_vec_f64(oi),
|
||||
};
|
||||
|
||||
let entry_signals = numpy_to_vec_bool(entries);
|
||||
let exit_signals = numpy_to_vec_bool(exits);
|
||||
|
||||
let config = TickBacktestConfig {
|
||||
base: crate::core::types::BacktestConfig {
|
||||
initial_capital,
|
||||
fees,
|
||||
slippage,
|
||||
stop: crate::core::types::StopConfig::None,
|
||||
target: crate::core::types::TargetConfig::None,
|
||||
upon_bar_close: false,
|
||||
},
|
||||
stop_loss_pct,
|
||||
take_profit_pct,
|
||||
max_hold_seconds,
|
||||
entry_cooldown_ticks,
|
||||
max_trades,
|
||||
};
|
||||
|
||||
let backtest = TickBacktest::new(config);
|
||||
let result = backtest.run(&tick_data, &entry_signals, &exit_signals, symbol);
|
||||
|
||||
Ok(convert_result(result))
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tick Signal Functions
|
||||
// ============================================================================
|
||||
|
||||
/// Compute tick momentum entry signals from per-tick feature arrays.
|
||||
///
|
||||
/// All input arrays must have the same length N. Returns a bool array of length N
|
||||
/// where True indicates a valid entry tick (all gates passed, not in cooldown).
|
||||
///
|
||||
/// Gates (each can be disabled by setting threshold to 0.0):
|
||||
/// - spread_pct[i] <= spread_pct_max
|
||||
/// - bsi_delta[i] >= bsi_min (0.0 = disabled)
|
||||
/// - |return_1m[i]| >= return_1m_min_abs (0.0 = disabled; NaN always fails)
|
||||
/// - cooldown_ticks between consecutive entries
|
||||
///
|
||||
/// return_direction: +1 for long (needs positive return_1m), -1 for short.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (
|
||||
spread_pct,
|
||||
bsi_delta,
|
||||
return_1m,
|
||||
spread_pct_max = 5.0,
|
||||
bsi_min = 0.0,
|
||||
return_1m_min_abs = 0.0,
|
||||
return_direction = 1_i8,
|
||||
cooldown_ticks = 10_usize,
|
||||
))]
|
||||
pub fn compute_tick_entry_signals<'py>(
|
||||
py: Python<'py>,
|
||||
spread_pct: PyReadonlyArray1<f64>,
|
||||
bsi_delta: PyReadonlyArray1<f64>,
|
||||
return_1m: PyReadonlyArray1<f64>,
|
||||
spread_pct_max: f64,
|
||||
bsi_min: f64,
|
||||
return_1m_min_abs: f64,
|
||||
return_direction: i8,
|
||||
cooldown_ticks: usize,
|
||||
) -> PyResult<&'py PyArray1<bool>> {
|
||||
let result = crate::signals::tick_signals::tick_momentum_entry(
|
||||
&numpy_to_vec_f64(spread_pct),
|
||||
&numpy_to_vec_f64(bsi_delta),
|
||||
&numpy_to_vec_f64(return_1m),
|
||||
spread_pct_max,
|
||||
bsi_min,
|
||||
return_1m_min_abs,
|
||||
return_direction,
|
||||
cooldown_ticks,
|
||||
);
|
||||
Ok(vec_to_numpy_bool(py, result))
|
||||
}
|
||||
|
||||
/// Compute time-based exit signals (EOD / session-end).
|
||||
///
|
||||
/// Sets exit[i] = True for every tick with timestamp >= eod_exit_time_ns.
|
||||
/// Set eod_exit_time_ns = 0 to disable (returns all False).
|
||||
///
|
||||
/// timestamps_ns: nanoseconds-since-epoch for each tick (int64 array).
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (timestamps_ns, eod_exit_time_ns = 0_i64))]
|
||||
pub fn compute_tick_exit_signals<'py>(
|
||||
py: Python<'py>,
|
||||
timestamps_ns: PyReadonlyArray1<i64>,
|
||||
eod_exit_time_ns: i64,
|
||||
) -> PyResult<&'py PyArray1<bool>> {
|
||||
let result = crate::signals::tick_signals::tick_momentum_exit(
|
||||
&numpy_to_vec_i64(timestamps_ns),
|
||||
eod_exit_time_ns,
|
||||
);
|
||||
Ok(vec_to_numpy_bool(py, result))
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tick Feature Functions
|
||||
// ============================================================================
|
||||
|
||||
/// Per-tick bid/ask spread as percentage of mid price.
|
||||
/// Returns 0.0 where both bid and ask are zero.
|
||||
#[pyfunction]
|
||||
pub fn tick_spread_pct<'py>(
|
||||
py: Python<'py>,
|
||||
bid: PyReadonlyArray1<f64>,
|
||||
ask: PyReadonlyArray1<f64>,
|
||||
) -> PyResult<&'py PyArray1<f64>> {
|
||||
Ok(vec_to_numpy_f64(
|
||||
py,
|
||||
crate::indicators::tick_features::spread_pct(&numpy_to_vec_f64(bid), &numpy_to_vec_f64(ask)),
|
||||
))
|
||||
}
|
||||
|
||||
/// Per-tick delta BSI from Zerodha cumulative session totals.
|
||||
///
|
||||
/// buy_qty_cumulative / sell_qty_cumulative must be the raw cumulative running sums
|
||||
/// from Zerodha (NOT already-converted deltas). Returns [0, 1] per tick; 0.5 = neutral.
|
||||
#[pyfunction]
|
||||
pub fn buy_sell_imbalance_delta<'py>(
|
||||
py: Python<'py>,
|
||||
buy_qty_cumulative: PyReadonlyArray1<f64>,
|
||||
sell_qty_cumulative: PyReadonlyArray1<f64>,
|
||||
) -> PyResult<&'py PyArray1<f64>> {
|
||||
Ok(vec_to_numpy_f64(
|
||||
py,
|
||||
crate::indicators::tick_features::buy_sell_imbalance_delta(
|
||||
&numpy_to_vec_f64(buy_qty_cumulative),
|
||||
&numpy_to_vec_f64(sell_qty_cumulative),
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
/// Per-tick lookback return over a time window.
|
||||
///
|
||||
/// timestamps_ns: nanoseconds-since-epoch for each tick.
|
||||
/// Returns NaN for ticks without sufficient history.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (timestamps_ns, ltp, window_seconds = 60.0))]
|
||||
pub fn return_window<'py>(
|
||||
py: Python<'py>,
|
||||
timestamps_ns: PyReadonlyArray1<i64>,
|
||||
ltp: PyReadonlyArray1<f64>,
|
||||
window_seconds: f64,
|
||||
) -> PyResult<&'py PyArray1<f64>> {
|
||||
Ok(vec_to_numpy_f64(
|
||||
py,
|
||||
crate::indicators::tick_features::return_window(
|
||||
&numpy_to_vec_i64(timestamps_ns),
|
||||
&numpy_to_vec_f64(ltp),
|
||||
window_seconds,
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
/// Rolling realized volatility proxy: stddev of log-returns over a time window (as %).
|
||||
/// Returns NaN for ticks without at least 2 data points in the window.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (timestamps_ns, ltp, window_seconds = 300.0))]
|
||||
pub fn realized_vol_rolling<'py>(
|
||||
py: Python<'py>,
|
||||
timestamps_ns: PyReadonlyArray1<i64>,
|
||||
ltp: PyReadonlyArray1<f64>,
|
||||
window_seconds: f64,
|
||||
) -> PyResult<&'py PyArray1<f64>> {
|
||||
Ok(vec_to_numpy_f64(
|
||||
py,
|
||||
crate::indicators::tick_features::realized_vol_rolling(
|
||||
&numpy_to_vec_i64(timestamps_ns),
|
||||
&numpy_to_vec_f64(ltp),
|
||||
window_seconds,
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
/// Per-tick OI position within the day's high/low range: [0, 100].
|
||||
/// Returns NaN where oi_day_high <= oi_day_low.
|
||||
#[pyfunction]
|
||||
pub fn oi_position_pct<'py>(
|
||||
py: Python<'py>,
|
||||
oi: PyReadonlyArray1<f64>,
|
||||
oi_day_high: f64,
|
||||
oi_day_low: f64,
|
||||
) -> PyResult<&'py PyArray1<f64>> {
|
||||
Ok(vec_to_numpy_f64(
|
||||
py,
|
||||
crate::indicators::tick_features::oi_position_pct(
|
||||
&numpy_to_vec_f64(oi),
|
||||
oi_day_high,
|
||||
oi_day_low,
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
/// Rolling tick velocity: ticks per minute over the preceding window_seconds.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (timestamps_ns, window_seconds = 60.0))]
|
||||
pub fn tick_velocity<'py>(
|
||||
py: Python<'py>,
|
||||
timestamps_ns: PyReadonlyArray1<i64>,
|
||||
window_seconds: f64,
|
||||
) -> PyResult<&'py PyArray1<f64>> {
|
||||
Ok(vec_to_numpy_f64(
|
||||
py,
|
||||
crate::indicators::tick_features::tick_velocity(
|
||||
&numpy_to_vec_i64(timestamps_ns),
|
||||
window_seconds,
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Indicator Functions
|
||||
// ============================================================================
|
||||
@@ -998,6 +1525,77 @@ pub fn rolling_max<'py>(
|
||||
// Helper Functions
|
||||
// ============================================================================
|
||||
|
||||
// ============================================================================
|
||||
// Monte Carlo Forward Simulation
|
||||
// ============================================================================
|
||||
|
||||
/// Run Monte Carlo forward simulation for a portfolio.
|
||||
///
|
||||
/// Uses Geometric Brownian Motion with Cholesky-decomposed correlated random
|
||||
/// draws, parallelized via Rayon.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `returns` - List of per-strategy return arrays (N strategies)
|
||||
/// * `weights` - Portfolio weight vector (length N, sums to 1)
|
||||
/// * `correlation_matrix` - N x N correlation matrix (flattened row-major as 2D list)
|
||||
/// * `initial_value` - Starting portfolio value
|
||||
/// * `n_simulations` - Number of simulation paths (default: 10000)
|
||||
/// * `horizon_days` - Forward simulation horizon in trading days (default: 252)
|
||||
/// * `seed` - Random seed for reproducibility (default: 42)
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (returns, weights, correlation_matrix, initial_value, n_simulations=10000, horizon_days=252, seed=42))]
|
||||
pub fn simulate_portfolio_mc(
|
||||
py: Python<'_>,
|
||||
returns: Vec<PyReadonlyArray1<'_, f64>>,
|
||||
weights: PyReadonlyArray1<'_, f64>,
|
||||
correlation_matrix: Vec<PyReadonlyArray1<'_, f64>>,
|
||||
initial_value: f64,
|
||||
n_simulations: usize,
|
||||
horizon_days: usize,
|
||||
seed: u64,
|
||||
) -> PyResult<PyObject> {
|
||||
use crate::portfolio::monte_carlo::{simulate_portfolio_forward, MonteCarloConfig};
|
||||
|
||||
// Convert numpy arrays to Rust vecs
|
||||
let rust_returns: Vec<Vec<f64>> =
|
||||
returns.iter().map(|arr| arr.as_slice().unwrap().to_vec()).collect();
|
||||
|
||||
let rust_weights: Vec<f64> = weights.as_slice().unwrap().to_vec();
|
||||
|
||||
let rust_corr: Vec<Vec<f64>> =
|
||||
correlation_matrix.iter().map(|arr| arr.as_slice().unwrap().to_vec()).collect();
|
||||
|
||||
let config = MonteCarloConfig { n_simulations, horizon_days, seed };
|
||||
|
||||
// Run simulation (releases GIL for Rayon parallelism)
|
||||
let result = py.allow_threads(|| {
|
||||
simulate_portfolio_forward(&rust_returns, &rust_weights, &rust_corr, initial_value, &config)
|
||||
});
|
||||
|
||||
// Build Python dict result
|
||||
let dict = pyo3::types::PyDict::new(py);
|
||||
|
||||
// percentile_paths: list of (percentile, list[float])
|
||||
let paths_list = pyo3::types::PyList::empty(py);
|
||||
for (pct, path) in &result.percentile_paths {
|
||||
let path_list = pyo3::types::PyList::new(py, path);
|
||||
let tuple = pyo3::types::PyTuple::new(py, &[pct.to_object(py), path_list.to_object(py)]);
|
||||
paths_list.append(tuple)?;
|
||||
}
|
||||
dict.set_item("percentile_paths", paths_list)?;
|
||||
|
||||
// final_values as numpy array for efficiency
|
||||
let final_arr = PyArray1::from_vec(py, result.final_values);
|
||||
dict.set_item("final_values", final_arr)?;
|
||||
|
||||
dict.set_item("expected_return", result.expected_return)?;
|
||||
dict.set_item("probability_of_loss", result.probability_of_loss)?;
|
||||
dict.set_item("var_95", result.var_95)?;
|
||||
dict.set_item("cvar_95", result.cvar_95)?;
|
||||
|
||||
Ok(dict.into())
|
||||
}
|
||||
|
||||
/// Convert Rust BacktestResult to Python PyBacktestResult.
|
||||
fn convert_result(result: crate::core::types::BacktestResult) -> PyBacktestResult {
|
||||
let metrics = PyBacktestMetrics {
|
||||
@@ -1032,6 +1630,8 @@ fn convert_result(result: crate::core::types::BacktestResult) -> PyBacktestResul
|
||||
max_consecutive_losses: result.metrics.max_consecutive_losses,
|
||||
avg_holding_period: result.metrics.avg_holding_period,
|
||||
exposure_pct: result.metrics.exposure_pct,
|
||||
payoff_ratio: result.metrics.payoff_ratio,
|
||||
recovery_factor: result.metrics.recovery_factor,
|
||||
};
|
||||
|
||||
let trades: Vec<PyTrade> = result
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
pub mod expression;
|
||||
pub mod processor;
|
||||
pub mod synchronizer;
|
||||
pub mod tick_signals;
|
||||
|
||||
pub use processor::SignalProcessor;
|
||||
pub use synchronizer::{SignalSynchronizer, SyncMode};
|
||||
pub use tick_signals::{tick_momentum_entry, tick_momentum_exit};
|
||||
|
||||
@@ -35,14 +35,13 @@ impl SignalProcessor {
|
||||
|
||||
/// Clean entry/exit signals to ensure proper alternation.
|
||||
///
|
||||
/// Rules (matching VectorBT behavior):
|
||||
/// Rules:
|
||||
/// 1. First signal must be an entry
|
||||
/// 2. After an entry, ignore further entries (unless pyramiding)
|
||||
/// 3. After an exit, ignore further exits
|
||||
/// 4. Entries and exits must alternate properly
|
||||
/// 5. Same-bar conflict: If both entry AND exit signals are True on the same bar
|
||||
/// when in position, VectorBT stays in position (ignores the exit).
|
||||
/// This matches VectorBT's "entry takes priority" behavior.
|
||||
/// when in position, entry takes priority — stay in position (ignore the exit).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `entries` - Raw entry signals
|
||||
@@ -75,8 +74,7 @@ impl SignalProcessor {
|
||||
// Ignore exits when not in position
|
||||
} else {
|
||||
// In position - looking for exit (or pyramid entry)
|
||||
// VectorBT behavior: If both entry and exit are True, stay in position
|
||||
// (entry signal "cancels" the exit signal)
|
||||
// Same-bar conflict: entry takes priority — stay in position
|
||||
if exits[i] && !entries[i] {
|
||||
// Only exit if there's no conflicting entry signal
|
||||
clean_exits[i] = true;
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
//! Tick-level signal generation for momentum entry/exit.
|
||||
//!
|
||||
//! Converts precomputed feature arrays (one scalar per tick) into entry and
|
||||
//! exit boolean arrays that can be fed directly into `run_tick_backtest`.
|
||||
//!
|
||||
//! All functions are O(N) single-pass — no backward linear search, no nested
|
||||
//! loops. The return_1m feature array must be precomputed by the caller
|
||||
//! (via `tick_features::return_window` or equivalent).
|
||||
|
||||
/// Generate momentum entry signals from per-tick feature arrays.
|
||||
///
|
||||
/// All input slices must have the same length N.
|
||||
///
|
||||
/// Rules applied in order (a failing rule sets entry[i] = false):
|
||||
/// 1. spread gate: `spread_pct[i] <= spread_pct_max`
|
||||
/// 2. BSI gate: if `bsi_min > 0.0`, `bsi_delta[i] >= bsi_min`
|
||||
/// 3. return gate: if `return_1m_min_abs > 0.0`, direction-aligned
|
||||
/// `return_1m[i]` must have `abs >= return_1m_min_abs` and correct sign.
|
||||
/// NaN return_1m always fails the gate.
|
||||
/// 4. cooldown: after each entry, suppress the next `cooldown_ticks` ticks.
|
||||
///
|
||||
/// `return_direction`: +1 for long (return_1m must be positive), -1 for short
|
||||
/// (return_1m must be negative).
|
||||
pub fn tick_momentum_entry(
|
||||
spread_pct: &[f64],
|
||||
bsi_delta: &[f64],
|
||||
return_1m: &[f64],
|
||||
spread_pct_max: f64,
|
||||
bsi_min: f64,
|
||||
return_1m_min_abs: f64,
|
||||
return_direction: i8,
|
||||
cooldown_ticks: usize,
|
||||
) -> Vec<bool> {
|
||||
let n = spread_pct.len();
|
||||
let mut entries = vec![false; n];
|
||||
let mut cooldown_until: usize = 0;
|
||||
|
||||
for i in 0..n {
|
||||
if i < cooldown_until {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Spread gate
|
||||
if spread_pct[i] > spread_pct_max {
|
||||
continue;
|
||||
}
|
||||
|
||||
// BSI delta gate (disabled when bsi_min == 0.0)
|
||||
if bsi_min > 0.0 {
|
||||
let b = if i < bsi_delta.len() { bsi_delta[i] } else { continue };
|
||||
if b < bsi_min {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 1-minute return gate (disabled when return_1m_min_abs == 0.0)
|
||||
if return_1m_min_abs > 0.0 {
|
||||
let r = if i < return_1m.len() { return_1m[i] } else { continue };
|
||||
if r.is_nan() {
|
||||
continue;
|
||||
}
|
||||
let abs_r = r.abs();
|
||||
if abs_r < return_1m_min_abs {
|
||||
continue;
|
||||
}
|
||||
// Direction alignment: long needs positive return, short needs negative
|
||||
if return_direction > 0 && r < 0.0 {
|
||||
continue;
|
||||
}
|
||||
if return_direction < 0 && r > 0.0 {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
entries[i] = true;
|
||||
cooldown_until = i + 1 + cooldown_ticks;
|
||||
}
|
||||
|
||||
entries
|
||||
}
|
||||
|
||||
/// Generate time-based exit signals (EOD / session-end).
|
||||
///
|
||||
/// Sets exit[i] = true for every tick at or after `eod_exit_time_ns`.
|
||||
/// When `eod_exit_time_ns == 0` all exits are false (disabled).
|
||||
///
|
||||
/// `timestamps_ns`: nanoseconds-since-epoch timestamp for each tick.
|
||||
pub fn tick_momentum_exit(timestamps_ns: &[i64], eod_exit_time_ns: i64) -> Vec<bool> {
|
||||
let n = timestamps_ns.len();
|
||||
if eod_exit_time_ns == 0 {
|
||||
return vec![false; n];
|
||||
}
|
||||
timestamps_ns
|
||||
.iter()
|
||||
.map(|&ts| ts >= eod_exit_time_ns)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn make_return_1m(vals: &[f64]) -> Vec<f64> {
|
||||
vals.to_vec()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry_spread_gate() {
|
||||
// All spreads above max → no entries
|
||||
let spread = vec![3.0, 4.0, 6.0];
|
||||
let bsi = vec![0.6, 0.7, 0.8];
|
||||
let ret = vec![1.0, 1.0, 1.0];
|
||||
let entries = tick_momentum_entry(&spread, &bsi, &ret, 2.0, 0.0, 0.0, 1, 0);
|
||||
assert_eq!(entries, vec![false, false, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry_bsi_gate() {
|
||||
let spread = vec![1.0, 1.0, 1.0];
|
||||
let bsi = vec![0.3, 0.6, 0.4]; // only index 1 passes bsi_min=0.5
|
||||
let ret = vec![0.5, 0.5, 0.5];
|
||||
let entries = tick_momentum_entry(&spread, &bsi, &ret, 5.0, 0.5, 0.0, 1, 0);
|
||||
assert_eq!(entries, vec![false, true, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry_return_gate_long() {
|
||||
let spread = vec![1.0, 1.0, 1.0, 1.0];
|
||||
let bsi = vec![0.6, 0.6, 0.6, 0.6];
|
||||
// positive, positive, too small, negative
|
||||
let ret = vec![0.5, 1.0, 0.1, -0.5];
|
||||
let entries = tick_momentum_entry(&spread, &bsi, &ret, 5.0, 0.0, 0.3, 1, 0);
|
||||
assert_eq!(entries, vec![true, true, false, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry_return_gate_short() {
|
||||
let spread = vec![1.0, 1.0, 1.0];
|
||||
let bsi = vec![0.6, 0.6, 0.6];
|
||||
// negative enough, positive (fails direction), nan
|
||||
let ret = vec![-0.5, 0.5, f64::NAN];
|
||||
let entries = tick_momentum_entry(&spread, &bsi, &ret, 5.0, 0.0, 0.3, -1, 0);
|
||||
assert_eq!(entries, vec![true, false, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry_cooldown() {
|
||||
// cooldown_ticks=2: after entry at i=0, next eligible at i=3
|
||||
let spread = vec![1.0; 6];
|
||||
let bsi = vec![0.6; 6];
|
||||
let ret = vec![0.0; 6];
|
||||
let entries = tick_momentum_entry(&spread, &bsi, &ret, 5.0, 0.0, 0.0, 1, 2);
|
||||
assert!(entries[0]);
|
||||
assert!(!entries[1]);
|
||||
assert!(!entries[2]);
|
||||
assert!(entries[3]);
|
||||
assert!(!entries[4]);
|
||||
assert!(!entries[5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_exit_disabled() {
|
||||
let ts = vec![1_000_000_i64, 2_000_000, 3_000_000];
|
||||
let exits = tick_momentum_exit(&ts, 0);
|
||||
assert_eq!(exits, vec![false, false, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_exit_eod_fires() {
|
||||
let ts = vec![1_000_i64, 2_000, 3_000, 4_000];
|
||||
let exits = tick_momentum_exit(&ts, 3_000);
|
||||
assert_eq!(exits, vec![false, false, true, true]);
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,11 @@
|
||||
//!
|
||||
//! Supports multiple instruments with synchronized signals.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::core::types::{
|
||||
BacktestConfig, BacktestMetrics, BacktestResult, CompiledSignals, ExitReason, OhlcvData, Trade,
|
||||
BacktestConfig, BacktestMetrics, BacktestResult, CompiledSignals, ExitReason, InstrumentConfig,
|
||||
OhlcvData, Trade,
|
||||
};
|
||||
use crate::execution::FeeModel;
|
||||
use crate::metrics::streaming::StreamingMetrics;
|
||||
@@ -74,6 +77,22 @@ impl BasketBacktest {
|
||||
/// # Returns
|
||||
/// Combined backtest result
|
||||
pub fn run(&self, instruments: &[(OhlcvData, CompiledSignals)]) -> BacktestResult {
|
||||
self.run_with_instrument_configs(instruments, None)
|
||||
}
|
||||
|
||||
/// Run basket backtest with optional per-instrument configurations.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `instruments` - Vector of (OhlcvData, CompiledSignals) pairs for each instrument
|
||||
/// * `instrument_configs` - Optional map of symbol -> InstrumentConfig
|
||||
///
|
||||
/// # Returns
|
||||
/// Combined backtest result
|
||||
pub fn run_with_instrument_configs(
|
||||
&self,
|
||||
instruments: &[(OhlcvData, CompiledSignals)],
|
||||
instrument_configs: Option<&HashMap<String, InstrumentConfig>>,
|
||||
) -> BacktestResult {
|
||||
if instruments.is_empty() {
|
||||
return self.empty_result();
|
||||
}
|
||||
@@ -168,7 +187,15 @@ impl BasketBacktest {
|
||||
// Calculate position sizes
|
||||
let prices: Vec<f64> = instruments.iter().map(|(o, _)| o.close[i]).collect();
|
||||
let weights: Vec<f64> = instruments.iter().map(|(_, s)| s.weight).collect();
|
||||
let sizes = self.calculate_sizes(&prices, &weights, cash);
|
||||
let symbols: Vec<&str> =
|
||||
instruments.iter().map(|(_, s)| s.symbol.as_str()).collect();
|
||||
let sizes = self.calculate_sizes_with_configs(
|
||||
&prices,
|
||||
&weights,
|
||||
cash,
|
||||
&symbols,
|
||||
instrument_configs,
|
||||
);
|
||||
|
||||
// Enter positions
|
||||
for (inst_idx, (ohlcv, signals)) in instruments.iter().enumerate() {
|
||||
@@ -249,7 +276,21 @@ impl BasketBacktest {
|
||||
}
|
||||
|
||||
/// Calculate position sizes for each instrument.
|
||||
#[allow(dead_code)]
|
||||
fn calculate_sizes(&self, prices: &[f64], weights: &[f64], available_capital: f64) -> Vec<f64> {
|
||||
let symbols: Vec<&str> = vec![""; prices.len()];
|
||||
self.calculate_sizes_with_configs(prices, weights, available_capital, &symbols, None)
|
||||
}
|
||||
|
||||
/// Calculate position sizes with optional per-instrument config (lot_size rounding, capital caps).
|
||||
fn calculate_sizes_with_configs(
|
||||
&self,
|
||||
prices: &[f64],
|
||||
weights: &[f64],
|
||||
available_capital: f64,
|
||||
symbols: &[&str],
|
||||
instrument_configs: Option<&HashMap<String, InstrumentConfig>>,
|
||||
) -> Vec<f64> {
|
||||
let n = prices.len();
|
||||
let total_weight: f64 = weights.iter().sum();
|
||||
|
||||
@@ -260,12 +301,26 @@ impl BasketBacktest {
|
||||
prices
|
||||
.iter()
|
||||
.zip(weights.iter())
|
||||
.map(|(&price, &weight)| {
|
||||
.enumerate()
|
||||
.map(|(idx, (&price, &weight))| {
|
||||
if price <= 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
let allocation = available_capital * (weight / total_weight);
|
||||
allocation / price
|
||||
let default_allocation = available_capital * (weight / total_weight);
|
||||
|
||||
// Use per-instrument alloted_capital if set, capped at default allocation
|
||||
let inst_config = instrument_configs
|
||||
.and_then(|configs| symbols.get(idx).and_then(|sym| configs.get(*sym)));
|
||||
|
||||
let allocation = inst_config
|
||||
.and_then(|ic| ic.alloted_capital)
|
||||
.map(|cap| cap.min(default_allocation))
|
||||
.unwrap_or(default_allocation);
|
||||
|
||||
let raw_size = allocation / price;
|
||||
|
||||
// Round to lot_size
|
||||
inst_config.map(|ic| ic.round_to_lot(raw_size)).unwrap_or(raw_size)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ pub mod options;
|
||||
pub mod pairs;
|
||||
pub mod single;
|
||||
pub mod spreads;
|
||||
pub mod tick;
|
||||
|
||||
pub use basket::BasketBacktest;
|
||||
pub use multi::MultiStrategyBacktest;
|
||||
@@ -15,3 +16,4 @@ pub use single::SingleBacktest;
|
||||
pub use spreads::{
|
||||
LegConfig, OptionType as SpreadOptionType, SpreadBacktest, SpreadConfig, SpreadType,
|
||||
};
|
||||
pub use tick::{TickBacktest, TickBacktestConfig};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
//! Single instrument backtest implementation.
|
||||
|
||||
use crate::core::types::{BacktestConfig, BacktestResult, CompiledSignals, OhlcvData};
|
||||
use crate::core::types::{
|
||||
BacktestConfig, BacktestResult, CompiledSignals, InstrumentConfig, OhlcvData,
|
||||
};
|
||||
use crate::portfolio::engine::PortfolioEngine;
|
||||
|
||||
/// Single instrument backtest runner.
|
||||
@@ -28,6 +30,24 @@ impl SingleBacktest {
|
||||
self.engine.run_single(ohlcv, signals)
|
||||
}
|
||||
|
||||
/// Run the backtest with per-instrument configuration.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `ohlcv` - OHLCV price data
|
||||
/// * `signals` - Compiled trading signals
|
||||
/// * `inst_config` - Optional per-instrument config (lot_size, capital cap, stop/target overrides)
|
||||
///
|
||||
/// # Returns
|
||||
/// Backtest result with metrics, trades, and equity curve
|
||||
pub fn run_with_instrument_config(
|
||||
&self,
|
||||
ohlcv: &OhlcvData,
|
||||
signals: &CompiledSignals,
|
||||
inst_config: Option<&InstrumentConfig>,
|
||||
) -> BacktestResult {
|
||||
self.engine.run_single_with_instrument_config(ohlcv, signals, inst_config)
|
||||
}
|
||||
|
||||
/// Run backtest from raw arrays.
|
||||
///
|
||||
/// # Arguments
|
||||
|
||||
@@ -31,6 +31,10 @@ pub enum SpreadType {
|
||||
ButterflyPut,
|
||||
Calendar,
|
||||
Diagonal,
|
||||
LongCall,
|
||||
LongPut,
|
||||
NakedCall,
|
||||
NakedPut,
|
||||
Custom,
|
||||
}
|
||||
|
||||
@@ -95,6 +99,9 @@ pub struct SpreadConfig {
|
||||
pub target_profit: Option<f64>,
|
||||
/// Whether to close at end of day.
|
||||
pub close_at_eod: bool,
|
||||
/// Per-leg expiry timestamps in nanoseconds (optional, for settlement logic).
|
||||
/// When provided, positions are force-closed at or after the earliest leg expiry.
|
||||
pub leg_expiry_timestamps: Option<Vec<i64>>,
|
||||
}
|
||||
|
||||
impl Default for SpreadConfig {
|
||||
@@ -106,6 +113,7 @@ impl Default for SpreadConfig {
|
||||
max_loss: None,
|
||||
target_profit: None,
|
||||
close_at_eod: false,
|
||||
leg_expiry_timestamps: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -251,9 +259,16 @@ impl SpreadBacktest {
|
||||
// Calculate unrealized P&L for exit checks
|
||||
let unrealized_pnl = position.as_ref().map(|p| p.total_unrealized_pnl()).unwrap_or(0.0);
|
||||
|
||||
// Check if any leg has expired at this bar
|
||||
let is_expiry = position.is_some()
|
||||
&& self.config.leg_expiry_timestamps.as_ref().map_or(false, |expiries| {
|
||||
expiries.iter().any(|&exp_ts| timestamps[i] >= exp_ts)
|
||||
});
|
||||
|
||||
// Check for exit signals or conditions
|
||||
let should_exit = position.is_some()
|
||||
&& (exits[i]
|
||||
|| is_expiry
|
||||
|| self.check_max_loss(&position, unrealized_pnl)
|
||||
|| self.check_target_profit(&position, unrealized_pnl));
|
||||
|
||||
@@ -267,7 +282,9 @@ impl SpreadBacktest {
|
||||
|
||||
// Record trade
|
||||
trade_id += 1;
|
||||
let exit_reason = if exits[i] {
|
||||
let exit_reason = if is_expiry {
|
||||
ExitReason::Settlement
|
||||
} else if exits[i] {
|
||||
ExitReason::Signal
|
||||
} else if self.check_max_loss(&Some(pos.clone()), pnl) {
|
||||
ExitReason::StopLoss
|
||||
@@ -311,8 +328,12 @@ impl SpreadBacktest {
|
||||
}
|
||||
}
|
||||
|
||||
// Check for entry signals
|
||||
if position.is_none() && entries[i] {
|
||||
// Check for entry signals (don't re-enter after all legs expired)
|
||||
let all_expired =
|
||||
self.config.leg_expiry_timestamps.as_ref().map_or(false, |expiries| {
|
||||
expiries.iter().all(|&exp_ts| timestamps[i] >= exp_ts)
|
||||
});
|
||||
if position.is_none() && entries[i] && !all_expired {
|
||||
let legs: Vec<LegPosition> = self
|
||||
.config
|
||||
.leg_configs
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
//! Tick-level backtest implementation.
|
||||
//!
|
||||
//! Accepts raw tick arrays (ltp, bid, ask, per-tick buy/sell qty deltas) plus
|
||||
//! parallel entry/exit signal arrays, then simulates each trade to
|
||||
//! stop-loss / take-profit / max-hold-time exit at full tick resolution.
|
||||
//!
|
||||
//! This is the right path for intraday options momentum strategies where the
|
||||
//! exact fill tick matters. Do not resample to bars before calling this —
|
||||
//! bar resampling discards intra-bar path information and makes scalping
|
||||
//! strategies unbacktestable.
|
||||
|
||||
use crate::core::types::{
|
||||
BacktestConfig, BacktestMetrics, BacktestResult, ExitReason, Price, TickData, Timestamp, Trade,
|
||||
};
|
||||
use crate::portfolio::engine::compute_backtest_metrics;
|
||||
|
||||
/// Configuration specific to tick backtests.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TickBacktestConfig {
|
||||
/// Shared execution config (capital, fees, slippage).
|
||||
pub base: BacktestConfig,
|
||||
/// Stop-loss as percentage of entry price (e.g. 5.0 = 5%).
|
||||
pub stop_loss_pct: f64,
|
||||
/// Take-profit as percentage of entry price (e.g. 10.0 = 10%).
|
||||
pub take_profit_pct: f64,
|
||||
/// Maximum hold time in seconds. 0 = no time limit.
|
||||
pub max_hold_seconds: u64,
|
||||
/// Minimum ticks between entries (cooldown). Prevents overlapping positions.
|
||||
pub entry_cooldown_ticks: usize,
|
||||
/// Maximum trades to simulate (bounds runtime for large windows).
|
||||
pub max_trades: usize,
|
||||
}
|
||||
|
||||
impl Default for TickBacktestConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
base: BacktestConfig::default(),
|
||||
stop_loss_pct: 5.0,
|
||||
take_profit_pct: 10.0,
|
||||
max_hold_seconds: 1800,
|
||||
entry_cooldown_ticks: 10,
|
||||
max_trades: 50,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tick-level backtest runner.
|
||||
pub struct TickBacktest {
|
||||
config: TickBacktestConfig,
|
||||
}
|
||||
|
||||
impl TickBacktest {
|
||||
pub fn new(config: TickBacktestConfig) -> Self {
|
||||
Self { config }
|
||||
}
|
||||
|
||||
/// Run the tick backtest.
|
||||
///
|
||||
/// `ticks` — raw tick data (ltp, bid, ask, per-tick qty deltas)
|
||||
/// `entries` — parallel bool array: true at ticks where a new long entry is allowed
|
||||
/// `exits` — parallel bool array: true at ticks where an open position must close
|
||||
/// `symbol` — instrument label used in trade records
|
||||
pub fn run(
|
||||
&self,
|
||||
ticks: &TickData,
|
||||
entries: &[bool],
|
||||
exits: &[bool],
|
||||
symbol: &str,
|
||||
) -> BacktestResult {
|
||||
let n = ticks.len();
|
||||
assert_eq!(n, entries.len(), "ticks and entries must have same length");
|
||||
assert_eq!(n, exits.len(), "ticks and exits must have same length");
|
||||
|
||||
let slippage_frac = self.config.base.slippage; // e.g. 0.0005 = 0.05%
|
||||
let fee_frac = self.config.base.fees; // e.g. 0.001 = 0.1%
|
||||
let stop_frac = self.config.stop_loss_pct / 100.0;
|
||||
let target_frac = self.config.take_profit_pct / 100.0;
|
||||
let max_hold_ns: i64 = self.config.max_hold_seconds as i64 * 1_000_000_000;
|
||||
|
||||
let mut trades: Vec<Trade> = Vec::new();
|
||||
let mut trade_id: u64 = 0;
|
||||
|
||||
// Position state
|
||||
let mut in_position = false;
|
||||
let mut entry_idx: usize = 0;
|
||||
let mut entry_price: Price = 0.0;
|
||||
let mut entry_time: Timestamp = 0;
|
||||
let mut stop_level: Price = 0.0;
|
||||
let mut target_level: Price = 0.0;
|
||||
let mut entry_fees: f64 = 0.0;
|
||||
let mut cooldown_until: usize = 0;
|
||||
|
||||
for i in 0..n {
|
||||
let ltp = ticks.ltp[i];
|
||||
let bid = if ticks.bid[i] > 0.0 { ticks.bid[i] } else { ltp };
|
||||
let ask = if ticks.ask[i] > 0.0 { ticks.ask[i] } else { ltp };
|
||||
let ts = ticks.timestamps[i];
|
||||
|
||||
if in_position {
|
||||
// Check time exit first (hard deadline)
|
||||
let time_exit = max_hold_ns > 0 && (ts - entry_time) >= max_hold_ns;
|
||||
|
||||
// Check explicit exit signal
|
||||
let signal_exit = exits[i];
|
||||
|
||||
// Check stop and target against ltp (tick-exact, no OHLC lookahead)
|
||||
let stop_hit = ltp <= stop_level;
|
||||
let target_hit = ltp >= target_level;
|
||||
|
||||
let (exit_price, reason) = if stop_hit {
|
||||
// Fill at stop level (not ltp — avoid worse-than-stop fills)
|
||||
let fill = stop_level * (1.0 - slippage_frac);
|
||||
(fill, ExitReason::StopLoss)
|
||||
} else if target_hit {
|
||||
let fill = target_level * (1.0 - slippage_frac);
|
||||
(fill, ExitReason::TakeProfit)
|
||||
} else if time_exit || signal_exit {
|
||||
let fill = bid * (1.0 - slippage_frac);
|
||||
let reason = if time_exit { ExitReason::TimeExit } else { ExitReason::Signal };
|
||||
(fill, reason)
|
||||
} else if i == n - 1 {
|
||||
// End of data — force close at bid
|
||||
let fill = bid * (1.0 - slippage_frac);
|
||||
(fill, ExitReason::EndOfData)
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let exit_fees = exit_price * fee_frac;
|
||||
let gross_pnl = (exit_price - entry_price) * 1.0; // qty=1; caller scales by lot_size
|
||||
let net_pnl = gross_pnl - entry_fees - exit_fees;
|
||||
let return_pct = net_pnl / entry_price * 100.0;
|
||||
|
||||
trades.push(Trade {
|
||||
id: trade_id,
|
||||
symbol: symbol.to_string(),
|
||||
entry_idx,
|
||||
exit_idx: i,
|
||||
entry_price,
|
||||
exit_price,
|
||||
size: 1.0,
|
||||
direction: crate::core::types::Direction::Long,
|
||||
pnl: net_pnl,
|
||||
return_pct,
|
||||
entry_time,
|
||||
exit_time: ts,
|
||||
fees: entry_fees + exit_fees,
|
||||
exit_reason: reason,
|
||||
});
|
||||
|
||||
trade_id += 1;
|
||||
in_position = false;
|
||||
cooldown_until = i + self.config.entry_cooldown_ticks;
|
||||
|
||||
if trades.len() >= self.config.max_trades {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// Not in position — check for entry
|
||||
if i < cooldown_until {
|
||||
continue;
|
||||
}
|
||||
if !entries[i] {
|
||||
continue;
|
||||
}
|
||||
if ask <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
entry_price = ask * (1.0 + slippage_frac);
|
||||
entry_fees = entry_price * fee_frac;
|
||||
entry_idx = i;
|
||||
entry_time = ts;
|
||||
stop_level = entry_price * (1.0 - stop_frac);
|
||||
target_level = entry_price * (1.0 + target_frac);
|
||||
in_position = true;
|
||||
}
|
||||
}
|
||||
|
||||
Self::build_result(trades, self.config.base.initial_capital, symbol)
|
||||
}
|
||||
|
||||
fn build_result(trades: Vec<Trade>, initial_capital: f64, _symbol: &str) -> BacktestResult {
|
||||
if trades.is_empty() {
|
||||
let metrics = BacktestMetrics {
|
||||
start_value: initial_capital,
|
||||
end_value: initial_capital,
|
||||
..Default::default()
|
||||
};
|
||||
return BacktestResult::new(metrics, vec![initial_capital], vec![0.0], vec![], vec![]);
|
||||
}
|
||||
|
||||
// Build per-trade equity and return curves (one point per trade close).
|
||||
let mut equity = initial_capital;
|
||||
let mut equity_curve = vec![initial_capital];
|
||||
let mut returns = Vec::with_capacity(trades.len());
|
||||
|
||||
for t in &trades {
|
||||
let prev = *equity_curve.last().unwrap();
|
||||
equity += t.pnl;
|
||||
equity_curve.push(equity);
|
||||
let ret = if prev > 0.0 { (equity - prev) / prev } else { 0.0 };
|
||||
returns.push(ret);
|
||||
}
|
||||
|
||||
// Drawdown curve over equity points (percentage, positive = drawdown).
|
||||
let mut peak = initial_capital;
|
||||
let drawdown_curve: Vec<f64> = equity_curve
|
||||
.iter()
|
||||
.map(|&e| {
|
||||
if e > peak {
|
||||
peak = e;
|
||||
}
|
||||
if peak > 0.0 { (peak - e) / peak * 100.0 } else { 0.0 }
|
||||
})
|
||||
.collect();
|
||||
|
||||
let metrics =
|
||||
compute_backtest_metrics(&equity_curve, &drawdown_curve, &returns, &trades, initial_capital);
|
||||
|
||||
BacktestResult::new(metrics, equity_curve, drawdown_curve, trades, returns)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::core::types::BacktestConfig;
|
||||
|
||||
fn make_ticks(n: usize, base_price: f64, trend: f64) -> TickData {
|
||||
let ltp: Vec<f64> = (0..n).map(|i| base_price + i as f64 * trend).collect();
|
||||
let bid: Vec<f64> = ltp.iter().map(|p| p - 0.5).collect();
|
||||
let ask: Vec<f64> = ltp.iter().map(|p| p + 0.5).collect();
|
||||
TickData {
|
||||
timestamps: (0..n as i64).map(|i| i * 1_000_000_000).collect(), // 1s apart
|
||||
ltp,
|
||||
bid,
|
||||
ask,
|
||||
buy_qty_delta: vec![100.0; n],
|
||||
sell_qty_delta: vec![80.0; n],
|
||||
oi: vec![0.0; n],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_target_hit() {
|
||||
// 100 ticks trending up — entry at tick 0, target should be hit
|
||||
let ticks = make_ticks(100, 100.0, 0.5); // price goes 100 → 149.5
|
||||
let mut entries = vec![false; 100];
|
||||
entries[0] = true;
|
||||
let exits = vec![false; 100];
|
||||
|
||||
let config = TickBacktestConfig {
|
||||
base: BacktestConfig { initial_capital: 10_000.0, fees: 0.0, slippage: 0.0, ..Default::default() },
|
||||
stop_loss_pct: 5.0,
|
||||
take_profit_pct: 10.0,
|
||||
max_hold_seconds: 0, // no time limit
|
||||
entry_cooldown_ticks: 5,
|
||||
max_trades: 10,
|
||||
};
|
||||
|
||||
let bt = TickBacktest::new(config);
|
||||
let result = bt.run(&ticks, &entries, &exits, "TEST");
|
||||
|
||||
assert_eq!(result.trades.len(), 1);
|
||||
assert_eq!(result.trades[0].exit_reason, ExitReason::TakeProfit);
|
||||
assert!(result.trades[0].pnl > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_stop_hit() {
|
||||
// 100 ticks trending down — entry at tick 0, stop should be hit
|
||||
let ticks = make_ticks(100, 100.0, -0.5); // price goes 100 → 50.5
|
||||
let mut entries = vec![false; 100];
|
||||
entries[0] = true;
|
||||
let exits = vec![false; 100];
|
||||
|
||||
let config = TickBacktestConfig {
|
||||
base: BacktestConfig { initial_capital: 10_000.0, fees: 0.0, slippage: 0.0, ..Default::default() },
|
||||
stop_loss_pct: 5.0,
|
||||
take_profit_pct: 20.0,
|
||||
max_hold_seconds: 0,
|
||||
entry_cooldown_ticks: 5,
|
||||
max_trades: 10,
|
||||
};
|
||||
|
||||
let bt = TickBacktest::new(config);
|
||||
let result = bt.run(&ticks, &entries, &exits, "TEST");
|
||||
|
||||
assert_eq!(result.trades.len(), 1);
|
||||
assert_eq!(result.trades[0].exit_reason, ExitReason::StopLoss);
|
||||
assert!(result.trades[0].pnl < 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_exit() {
|
||||
// Flat price — neither stop nor target hit, time exit should fire
|
||||
let ticks = make_ticks(200, 100.0, 0.0);
|
||||
let mut entries = vec![false; 200];
|
||||
entries[0] = true;
|
||||
let exits = vec![false; 200];
|
||||
|
||||
let config = TickBacktestConfig {
|
||||
base: BacktestConfig { initial_capital: 10_000.0, fees: 0.0, slippage: 0.0, ..Default::default() },
|
||||
stop_loss_pct: 50.0, // very wide, won't hit
|
||||
take_profit_pct: 50.0,
|
||||
max_hold_seconds: 10, // 10 ticks at 1s each
|
||||
entry_cooldown_ticks: 5,
|
||||
max_trades: 10,
|
||||
};
|
||||
|
||||
let bt = TickBacktest::new(config);
|
||||
let result = bt.run(&ticks, &entries, &exits, "TEST");
|
||||
|
||||
assert_eq!(result.trades.len(), 1);
|
||||
assert_eq!(result.trades[0].exit_reason, ExitReason::TimeExit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiple_trades_with_cooldown() {
|
||||
let ticks = make_ticks(200, 100.0, 0.2);
|
||||
// Entry every 20 ticks
|
||||
let entries: Vec<bool> = (0..200).map(|i| i % 20 == 0).collect();
|
||||
let exits = vec![false; 200];
|
||||
|
||||
let config = TickBacktestConfig {
|
||||
base: BacktestConfig { initial_capital: 10_000.0, fees: 0.0, slippage: 0.0, ..Default::default() },
|
||||
stop_loss_pct: 5.0,
|
||||
take_profit_pct: 10.0,
|
||||
max_hold_seconds: 0,
|
||||
entry_cooldown_ticks: 5,
|
||||
max_trades: 20,
|
||||
};
|
||||
|
||||
let bt = TickBacktest::new(config);
|
||||
let result = bt.run(&ticks, &entries, &exits, "TEST");
|
||||
|
||||
assert!(result.trades.len() > 1);
|
||||
assert!(result.metrics.total_trades > 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_ticks_returns_empty_result() {
|
||||
let ticks = TickData {
|
||||
timestamps: vec![],
|
||||
ltp: vec![],
|
||||
bid: vec![],
|
||||
ask: vec![],
|
||||
buy_qty_delta: vec![],
|
||||
sell_qty_delta: vec![],
|
||||
oi: vec![],
|
||||
};
|
||||
let config = TickBacktestConfig::default();
|
||||
let bt = TickBacktest::new(config);
|
||||
let result = bt.run(&ticks, &[], &[], "TEST");
|
||||
assert_eq!(result.trades.len(), 0);
|
||||
assert_eq!(result.metrics.total_trades, 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user