Compare commits

...

2 Commits

Author SHA1 Message Date
zastian-dev
0e820852fa new best 2026-02-13 20:04:32 +00:00
zastian-dev
79816b9e2e Experiment with hourly timeframe-specific stops
- Added HOURLY_ATR_STOP_MULTIPLIER (1.8x) vs daily (3.5x)
- Added hourly-specific trail multipliers
- Strategy now uses timeframe field to select appropriate stops
- Tested multiple configurations on hourly:
  * 3.5x stops: -0.5% return, 45% max DD
  * 1.8x stops: -45% return, 53% max DD (worse)
  * Conservative regime (0.25x): -65% return, 67% max DD (terrible)
- Conclusion: Hourly doesn't work with this strategy
- Daily with relaxed regime remains best: +17.4% over 5yr, 24% max DD

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-13 19:20:01 +00:00
6 changed files with 818 additions and 43 deletions

View File

@@ -21,6 +21,10 @@ cargo run --release -- --backtest --years 3
cargo run --release -- --backtest --years 5 --capital 50000
cargo run --release -- --backtest --years 1 --months 6 --timeframe hourly
# Run backtesting with custom date range
cargo run --release -- --backtest --start-date 2007-01-01 --end-date 2008-12-31
cargo run --release -- --backtest --start-date 2020-03-01 --end-date 2020-12-31 --timeframe hourly
# Lint and format (available via nix flake)
cargo clippy
cargo fmt

119
analyze_regime.py Normal file
View File

@@ -0,0 +1,119 @@
#!/usr/bin/env python3
"""Analyze SPY regime detection during backtest periods."""
import pandas as pd
import yfinance as yf
from datetime import datetime, timedelta
def calculate_ema(series, period):
"""Calculate EMA using pandas."""
return series.ewm(span=period, adjust=False).mean()
def determine_regime(price, ema50, ema200):
"""Replicate the Rust regime detection logic."""
if pd.isna(price) or pd.isna(ema50) or pd.isna(ema200):
return "Caution"
# Bear: price below 200 EMA AND 50 EMA below 200 EMA
if price < ema200 and ema50 < ema200:
return "Bear"
# Caution: price below 50 EMA
if price < ema50:
return "Caution"
# Bull: price above both, 50 above 200
if ema50 > ema200:
return "Bull"
# Edge case: price above both but 50 still below 200
return "Caution"
def analyze_period(start_date, end_date, period_name):
"""Analyze SPY regime for a given period."""
print(f"\n{'='*70}")
print(f"{period_name}: {start_date} to {end_date}")
print('='*70)
# Fetch SPY data with extra warmup for EMA-200
warmup_start = (datetime.strptime(start_date, '%Y-%m-%d') - timedelta(days=400)).strftime('%Y-%m-%d')
spy = yf.download('SPY', start=warmup_start, end=end_date, progress=False)
if spy.empty:
print(f"ERROR: No SPY data available for {period_name}")
return
# Calculate EMAs
spy['EMA50'] = calculate_ema(spy['Close'], 50)
spy['EMA200'] = calculate_ema(spy['Close'], 200)
# Determine regime for each day
spy['Regime'] = spy.apply(
lambda row: determine_regime(row['Close'], row['EMA50'], row['EMA200']),
axis=1
)
# Filter to actual trading period
trading_period = spy[start_date:end_date].copy()
if trading_period.empty:
print(f"ERROR: No trading data for {period_name}")
return
# Calculate SPY return
spy_start = trading_period['Close'].iloc[0]
spy_end = trading_period['Close'].iloc[-1]
spy_return = (spy_end - spy_start) / spy_start * 100
print(f"\nSPY Performance:")
print(f" Start: ${spy_start:.2f}")
print(f" End: ${spy_end:.2f}")
print(f" Return: {spy_return:+.2f}%")
# Count regime days
regime_counts = trading_period['Regime'].value_counts()
total_days = len(trading_period)
print(f"\nRegime Distribution ({total_days} trading days):")
for regime in ['Bull', 'Caution', 'Bear']:
count = regime_counts.get(regime, 0)
pct = count / total_days * 100
print(f" {regime:8s}: {count:4d} days ({pct:5.1f}%)")
# Show regime transitions
regime_changes = trading_period[trading_period['Regime'] != trading_period['Regime'].shift(1)]
if len(regime_changes) > 0:
print(f"\nRegime Transitions ({len(regime_changes)} total):")
for date, row in regime_changes.head(20).iterrows():
print(f" {date.strftime('%Y-%m-%d')}: {row['Regime']:8s} (SPY: ${row['Close']:.2f}, "
f"EMA50: ${row['EMA50']:.2f}, EMA200: ${row['EMA200']:.2f})")
if len(regime_changes) > 20:
print(f" ... and {len(regime_changes) - 20} more transitions")
# Identify problematic Bear periods during bull markets
bear_days = trading_period[trading_period['Regime'] == 'Bear']
if len(bear_days) > 0:
print(f"\n⚠️ WARNING: {len(bear_days)} days classified as BEAR:")
for date, row in bear_days.head(10).iterrows():
print(f" {date.strftime('%Y-%m-%d')}: SPY=${row['Close']:.2f}, "
f"EMA50=${row['EMA50']:.2f}, EMA200=${row['EMA200']:.2f}")
if len(bear_days) > 10:
print(f" ... and {len(bear_days) - 10} more Bear days")
# Show first and last months in detail
print(f"\nFirst Month Detail:")
first_month = trading_period.head(22)[['Close', 'EMA50', 'EMA200', 'Regime']]
for date, row in first_month.iterrows():
print(f" {date.strftime('%Y-%m-%d')}: {row['Regime']:8s} | "
f"SPY: ${row['Close']:7.2f} | EMA50: ${row['EMA50']:7.2f} | EMA200: ${row['EMA200']:7.2f}")
if __name__ == '__main__':
# Analyze 2023
analyze_period('2023-01-01', '2023-12-31', '2023 Backtest')
# Analyze 2024
analyze_period('2024-01-01', '2024-12-31', '2024 Backtest')
print(f"\n{'='*70}")
print("Analysis complete.")
print('='*70)

View File

@@ -632,3 +632,138 @@ pub async fn fetch_backtest_data(
Ok(all_data)
}
/// Helper to fetch bars for backtesting with specific date range.
/// Similar to fetch_backtest_data but accepts explicit start/end dates.
pub async fn fetch_backtest_data_with_dates(
client: &AlpacaClient,
symbols: &[&str],
start: DateTime<Utc>,
end: DateTime<Utc>,
timeframe: Timeframe,
warmup_days: i64,
) -> Result<HashMap<String, Vec<Bar>>> {
// Add warmup period to start date
let start_with_warmup = start - Duration::days(warmup_days + 30);
// Re-fetch overlap: always re-fetch the last 2 days to handle partial/corrected bars
let refetch_overlap = Duration::days(2);
tracing::info!(
"Fetching data from {} to {}...",
start_with_warmup.format("%Y-%m-%d"),
end.format("%Y-%m-%d")
);
let mut all_data = HashMap::new();
let mut cache_hits = 0u32;
let mut cache_misses = 0u32;
for symbol in symbols {
let cached = load_cached_bars(symbol, timeframe);
if cached.is_empty() {
// Full fetch — no cache
cache_misses += 1;
tracing::info!(" Fetching {} (no cache)...", symbol);
match client
.get_historical_bars(symbol, timeframe, start_with_warmup, end)
.await
{
Ok(bars) => {
if !bars.is_empty() {
tracing::info!(" {}: {} bars fetched", symbol, bars.len());
save_cached_bars(symbol, timeframe, &bars);
all_data.insert(symbol.to_string(), bars);
} else {
tracing::warn!(" {}: No data", symbol);
}
}
Err(e) => {
tracing::error!(" Failed to fetch {}: {}", symbol, e);
}
}
} else {
let first_cached_ts = cached.first().unwrap().timestamp;
let last_cached_ts = cached.last().unwrap().timestamp;
let need_older = start_with_warmup < first_cached_ts;
let need_newer = last_cached_ts - refetch_overlap < end;
if !need_older && !need_newer {
cache_hits += 1;
tracing::info!(" {}: {} bars from cache (fully cached)", symbol, cached.len());
all_data.insert(symbol.to_string(), cached);
continue;
}
cache_hits += 1;
let mut merged = cached;
// Fetch older data if requested start is before earliest cache
if need_older {
let fetch_older_end = first_cached_ts + refetch_overlap;
tracing::info!(
" {} (fetching older: {} to {})...",
symbol,
start_with_warmup.format("%Y-%m-%d"),
fetch_older_end.format("%Y-%m-%d")
);
match client
.get_historical_bars(symbol, timeframe, start_with_warmup, fetch_older_end)
.await
{
Ok(old_bars) => {
merged = old_bars.into_iter().chain(merged).collect();
}
Err(e) => {
tracing::warn!(" {}: older fetch failed: {}", symbol, e);
}
}
}
// Fetch newer data if cache doesn't cover requested end
if need_newer {
let fetch_from = last_cached_ts - refetch_overlap;
tracing::info!(
" {} (fetching newer: {} to {})...",
symbol,
fetch_from.format("%Y-%m-%d"),
end.format("%Y-%m-%d")
);
match client
.get_historical_bars(symbol, timeframe, fetch_from, end)
.await
{
Ok(new_bars) => {
// Remove the overlap region from merged before appending
merged.retain(|b| b.timestamp < fetch_from);
merged.extend(new_bars);
}
Err(e) => {
tracing::warn!(" {}: newer fetch failed: {}", symbol, e);
}
}
}
// Dedup and sort
merged.sort_by_key(|b| b.timestamp);
merged.dedup_by_key(|b| b.timestamp);
tracing::info!(" {}: {} bars total (merged)", symbol, merged.len());
save_cached_bars(symbol, timeframe, &merged);
all_data.insert(symbol.to_string(), merged);
}
}
tracing::info!(
"Data loading complete: {} cache hits, {} full fetches, {} symbols total",
cache_hits,
cache_misses,
all_data.len()
);
Ok(all_data)
}

View File

@@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use chrono::{DateTime, Datelike, Duration, NaiveDate, Timelike, Utc};
use std::collections::{BTreeMap, HashMap, HashSet};
use crate::alpaca::{fetch_backtest_data, AlpacaClient};
use crate::alpaca::{fetch_backtest_data, fetch_backtest_data_with_dates, AlpacaClient};
use crate::config::{
get_all_symbols, get_sector, Timeframe, ATR_STOP_MULTIPLIER,
ATR_TRAIL_ACTIVATION_MULTIPLIER, ATR_TRAIL_MULTIPLIER, HOURS_PER_DAY,
@@ -16,9 +16,13 @@ use crate::config::{
DRAWDOWN_TIER2_PCT, DRAWDOWN_TIER2_BARS,
DRAWDOWN_TIER3_PCT, DRAWDOWN_TIER3_BARS,
DRAWDOWN_TIER3_REQUIRE_BULL,
HOURLY_DRAWDOWN_TIER1_PCT, HOURLY_DRAWDOWN_TIER1_BARS,
HOURLY_DRAWDOWN_TIER2_PCT, HOURLY_DRAWDOWN_TIER2_BARS,
HOURLY_DRAWDOWN_TIER3_PCT, HOURLY_DRAWDOWN_TIER3_BARS,
EQUITY_CURVE_SMA_PERIOD,
REGIME_SPY_SYMBOL, REGIME_EMA_SHORT, REGIME_EMA_LONG,
REGIME_CAUTION_SIZE_FACTOR, REGIME_CAUTION_THRESHOLD_BUMP,
HOURLY_REGIME_CAUTION_SIZE_FACTOR, HOURLY_REGIME_CAUTION_THRESHOLD_BUMP,
};
use crate::indicators::{calculate_all_indicators, calculate_ema, determine_market_regime, generate_signal};
use crate::strategy::Strategy;
@@ -112,23 +116,41 @@ impl Backtester {
///
/// On resume, the peak is reset to the current portfolio value to prevent
/// cascading re-triggers from the same drawdown event.
/// Get the drawdown tier thresholds for the current timeframe.
fn drawdown_tiers(&self) -> (f64, usize, f64, usize, f64, usize) {
if self.timeframe == Timeframe::Hourly {
(
HOURLY_DRAWDOWN_TIER1_PCT, HOURLY_DRAWDOWN_TIER1_BARS,
HOURLY_DRAWDOWN_TIER2_PCT, HOURLY_DRAWDOWN_TIER2_BARS,
HOURLY_DRAWDOWN_TIER3_PCT, HOURLY_DRAWDOWN_TIER3_BARS,
)
} else {
(
DRAWDOWN_TIER1_PCT, DRAWDOWN_TIER1_BARS,
DRAWDOWN_TIER2_PCT, DRAWDOWN_TIER2_BARS,
DRAWDOWN_TIER3_PCT, DRAWDOWN_TIER3_BARS,
)
}
}
fn update_drawdown_state(&mut self, portfolio_value: f64) {
if portfolio_value > self.peak_portfolio_value {
self.peak_portfolio_value = portfolio_value;
}
let drawdown_pct = (self.peak_portfolio_value - portfolio_value) / self.peak_portfolio_value;
let (t1_pct, t1_bars, t2_pct, t2_bars, t3_pct, t3_bars) = self.drawdown_tiers();
// Trigger halt at the lowest tier that matches (if not already halted)
if !self.drawdown_halt && drawdown_pct >= DRAWDOWN_TIER1_PCT {
if !self.drawdown_halt && drawdown_pct >= t1_pct {
// Determine severity tier
let (halt_bars, tier_name) = if drawdown_pct >= DRAWDOWN_TIER3_PCT {
let (halt_bars, tier_name) = if drawdown_pct >= t3_pct {
self.drawdown_requires_bull = DRAWDOWN_TIER3_REQUIRE_BULL;
(DRAWDOWN_TIER3_BARS, "TIER 3 (SEVERE)")
} else if drawdown_pct >= DRAWDOWN_TIER2_PCT {
(DRAWDOWN_TIER2_BARS, "TIER 2")
(t3_bars, "TIER 3 (SEVERE)")
} else if drawdown_pct >= t2_pct {
(t2_bars, "TIER 2")
} else {
(DRAWDOWN_TIER1_BARS, "TIER 1")
(t1_bars, "TIER 1")
};
tracing::warn!(
@@ -145,14 +167,14 @@ impl Backtester {
// Upgrade severity if drawdown deepens while already halted
if self.drawdown_halt && drawdown_pct > self.drawdown_halt_severity {
if drawdown_pct >= DRAWDOWN_TIER3_PCT && self.drawdown_halt_severity < DRAWDOWN_TIER3_PCT {
if drawdown_pct >= t3_pct && self.drawdown_halt_severity < t3_pct {
self.drawdown_requires_bull = DRAWDOWN_TIER3_REQUIRE_BULL;
self.drawdown_halt_start = Some(self.current_bar); // Reset timer for deeper tier
tracing::warn!(
"Drawdown deepened to {:.2}% — UPGRADED to TIER 3. Requires BULL regime.",
drawdown_pct * 100.0
);
} else if drawdown_pct >= DRAWDOWN_TIER2_PCT && self.drawdown_halt_severity < DRAWDOWN_TIER2_PCT {
} else if drawdown_pct >= t2_pct && self.drawdown_halt_severity < t2_pct {
self.drawdown_halt_start = Some(self.current_bar);
tracing::warn!(
"Drawdown deepened to {:.2}% — upgraded to TIER 2.",
@@ -165,12 +187,12 @@ impl Backtester {
// Auto-resume after time-based cooldown
if self.drawdown_halt {
if let Some(halt_start) = self.drawdown_halt_start {
let required_bars = if self.drawdown_halt_severity >= DRAWDOWN_TIER3_PCT {
DRAWDOWN_TIER3_BARS
} else if self.drawdown_halt_severity >= DRAWDOWN_TIER2_PCT {
DRAWDOWN_TIER2_BARS
let required_bars = if self.drawdown_halt_severity >= t3_pct {
t3_bars
} else if self.drawdown_halt_severity >= t2_pct {
t2_bars
} else {
DRAWDOWN_TIER1_BARS
t1_bars
};
let time_served = self.current_bar >= halt_start + required_bars;
@@ -693,9 +715,16 @@ impl Backtester {
self.current_regime = regime;
// Regime-based sizing factor and threshold adjustment
// Use timeframe-specific parameters: hourly needs defensiveness, daily needs aggression
let regime_size_factor = match regime {
MarketRegime::Bull => 1.0,
MarketRegime::Caution => REGIME_CAUTION_SIZE_FACTOR,
MarketRegime::Caution => {
if self.timeframe == Timeframe::Hourly {
HOURLY_REGIME_CAUTION_SIZE_FACTOR
} else {
REGIME_CAUTION_SIZE_FACTOR
}
},
MarketRegime::Bear => 0.0, // No new longs
};
@@ -775,8 +804,15 @@ impl Backtester {
// In Bear regime, skip the entire buy phase (no new longs).
if regime.allows_new_longs() {
// In Caution regime, raise the buy threshold to require stronger signals
// Use timeframe-specific parameters: hourly needs high bump, daily needs low bump
let buy_threshold_bump = match regime {
MarketRegime::Caution => REGIME_CAUTION_THRESHOLD_BUMP,
MarketRegime::Caution => {
if self.timeframe == Timeframe::Hourly {
HOURLY_REGIME_CAUTION_THRESHOLD_BUMP
} else {
REGIME_CAUTION_THRESHOLD_BUMP
}
},
_ => 0.0,
};
@@ -885,6 +921,432 @@ impl Backtester {
Ok(result)
}
/// Run the backtest simulation with specific date range.
pub async fn run_with_dates(
&mut self,
client: &AlpacaClient,
start_date: NaiveDate,
end_date: NaiveDate,
) -> Result<BacktestResult> {
// Convert dates to DateTime<Utc> for data fetching
let start_datetime = start_date
.and_hms_opt(0, 0, 0)
.unwrap()
.and_local_timezone(Utc)
.earliest()
.unwrap();
let end_datetime = end_date
.and_hms_opt(23, 59, 59)
.unwrap()
.and_local_timezone(Utc)
.latest()
.unwrap();
// Calculate years for metrics
let days_diff = (end_date - start_date).num_days();
let years = days_diff as f64 / 365.0;
let symbols = get_all_symbols();
// Calculate warmup period
let warmup_period = self.strategy.params.min_bars() + 10;
let warmup_calendar_days = if self.timeframe == Timeframe::Hourly {
(warmup_period as f64 / HOURS_PER_DAY as f64 * 1.5) as i64
} else {
(warmup_period as f64 * 1.5) as i64
};
tracing::info!("{}", "=".repeat(70));
tracing::info!("STARTING BACKTEST");
tracing::info!("Initial Capital: ${:.2}", self.initial_capital);
tracing::info!(
"Period: {} to {} ({:.2} years, {:.1} months)",
start_date.format("%Y-%m-%d"),
end_date.format("%Y-%m-%d"),
years,
years * 12.0
);
tracing::info!("Timeframe: {:?} bars", self.timeframe);
tracing::info!(
"Risk: ATR stops ({}x), trail ({}x after {}x gain), max {}% pos, {} max pos, {} max/sector, {} bar cooldown",
ATR_STOP_MULTIPLIER, ATR_TRAIL_MULTIPLIER, ATR_TRAIL_ACTIVATION_MULTIPLIER,
MAX_POSITION_SIZE * 100.0, MAX_CONCURRENT_POSITIONS, MAX_SECTOR_POSITIONS,
REENTRY_COOLDOWN_BARS
);
tracing::info!("Slippage: {} bps per trade", SLIPPAGE_BPS);
if self.timeframe == Timeframe::Hourly {
tracing::info!(
"Parameters scaled {}x (e.g., RSI: {}, EMA_TREND: {})",
HOURS_PER_DAY,
self.strategy.params.rsi_period,
self.strategy.params.ema_trend
);
}
tracing::info!("{}", "=".repeat(70));
// Fetch historical data with custom date range
let raw_data = fetch_backtest_data_with_dates(
client,
&symbols.iter().map(|s| *s).collect::<Vec<_>>(),
start_datetime,
end_datetime,
self.timeframe,
warmup_calendar_days,
)
.await?;
if raw_data.is_empty() {
anyhow::bail!("No historical data available for backtesting");
}
// Calculate indicators for all symbols
let mut data: HashMap<String, Vec<IndicatorRow>> = HashMap::new();
for (symbol, bars) in &raw_data {
let min_bars = self.strategy.params.min_bars();
if bars.len() < min_bars {
tracing::warn!(
"{}: Only {} bars, need {}. Skipping.",
symbol,
bars.len(),
min_bars
);
continue;
}
let indicators = calculate_all_indicators(bars, &self.strategy.params);
data.insert(symbol.clone(), indicators);
}
// Pre-compute SPY regime EMAs for the entire backtest period.
let spy_key = REGIME_SPY_SYMBOL.to_string();
let spy_ema50_series: Vec<f64>;
let spy_ema200_series: Vec<f64>;
let has_spy_data = raw_data.contains_key(&spy_key);
if has_spy_data {
let spy_closes: Vec<f64> = raw_data[&spy_key].iter().map(|b| b.close).collect();
spy_ema50_series = calculate_ema(&spy_closes, REGIME_EMA_SHORT);
spy_ema200_series = calculate_ema(&spy_closes, REGIME_EMA_LONG);
tracing::info!(
"SPY regime filter: EMA-{} / EMA-{} ({} bars of SPY data)",
REGIME_EMA_SHORT, REGIME_EMA_LONG, spy_closes.len()
);
} else {
spy_ema50_series = vec![];
spy_ema200_series = vec![];
tracing::warn!(
"SPY data not available — market regime filter DISABLED. \
All bars will be treated as BULL regime."
);
}
// Get common date range
let mut all_dates: BTreeMap<DateTime<Utc>, HashSet<String>> = BTreeMap::new();
for (symbol, rows) in &data {
for row in rows {
all_dates
.entry(row.timestamp)
.or_insert_with(HashSet::new)
.insert(symbol.clone());
}
}
let all_dates: Vec<DateTime<Utc>> = all_dates.keys().copied().collect();
// Filter to only trade on requested period
let trading_dates: Vec<DateTime<Utc>> = all_dates
.iter()
.filter(|&&d| d >= start_datetime && d <= end_datetime)
.copied()
.collect();
// Ensure we have enough warmup
let trading_dates = if !trading_dates.is_empty() {
let first_trading_idx = all_dates
.iter()
.position(|&d| d == trading_dates[0])
.unwrap_or(0);
if first_trading_idx < warmup_period {
trading_dates
.into_iter()
.skip(warmup_period - first_trading_idx)
.collect()
} else {
trading_dates
}
} else {
trading_dates
};
if trading_dates.is_empty() {
anyhow::bail!(
"No trading days available after warmup. \n Try a longer backtest period (at least 4 months recommended)."
);
}
tracing::info!(
"\nSimulating {} trading days (after {}-day warmup)...",
trading_dates.len(),
warmup_period
);
// From here on, the code is identical to the regular run() method
// Build index lookup for each symbol's data
let mut symbol_date_index: HashMap<String, HashMap<DateTime<Utc>, usize>> = HashMap::new();
for (symbol, rows) in &data {
let mut idx_map = HashMap::new();
for (i, row) in rows.iter().enumerate() {
idx_map.insert(row.timestamp, i);
}
symbol_date_index.insert(symbol.clone(), idx_map);
}
// Build SPY raw bar index
let spy_raw_date_index: HashMap<DateTime<Utc>, usize> = if has_spy_data {
raw_data[&spy_key]
.iter()
.enumerate()
.map(|(i, bar)| (bar.timestamp, i))
.collect()
} else {
HashMap::new()
};
// Main simulation loop (identical to run())
for (day_num, current_date) in trading_dates.iter().enumerate() {
self.current_bar = day_num;
self.new_positions_this_bar = 0;
self.prune_old_day_trades(current_date.date_naive());
// Get current prices and momentum for all symbols
let mut current_prices: HashMap<String, f64> = HashMap::new();
let mut momentum_scores: HashMap<String, f64> = HashMap::new();
for (symbol, rows) in &data {
if let Some(&idx) =
symbol_date_index.get(symbol).and_then(|m| m.get(current_date))
{
let row = &rows[idx];
current_prices.insert(symbol.clone(), row.close);
if !row.momentum.is_nan() {
momentum_scores.insert(symbol.clone(), row.momentum);
}
}
}
let portfolio_value = self.get_portfolio_value(&current_prices);
// SPY Market Regime Detection
let regime = if has_spy_data {
if let (Some(&spy_raw_idx), Some(spy_indicator_row)) = (
spy_raw_date_index.get(current_date),
data.get(&spy_key)
.and_then(|rows| {
symbol_date_index
.get(&spy_key)
.and_then(|m| m.get(current_date))
.map(|&i| &rows[i])
}),
) {
let ema50 = if spy_raw_idx < spy_ema50_series.len() {
spy_ema50_series[spy_raw_idx]
} else {
f64::NAN
};
let ema200 = if spy_raw_idx < spy_ema200_series.len() {
spy_ema200_series[spy_raw_idx]
} else {
f64::NAN
};
determine_market_regime(spy_indicator_row, ema50, ema200)
} else {
MarketRegime::Caution
}
} else {
MarketRegime::Bull
};
self.current_regime = regime;
// Regime-based sizing factor
let regime_size_factor = match regime {
MarketRegime::Bull => 1.0,
MarketRegime::Caution => REGIME_CAUTION_SIZE_FACTOR,
MarketRegime::Bear => 0.0,
};
if day_num % 100 == 0 {
tracing::info!(" Market regime: {} (SPY)", regime.as_str());
}
// Update drawdown circuit breaker
self.update_drawdown_state(portfolio_value);
// Increment bars_held for all positions
for pos in self.positions.values_mut() {
pos.bars_held += 1;
}
// Momentum ranking
let mut ranked_symbols: Vec<String> = momentum_scores.keys().cloned().collect();
ranked_symbols.sort_by(|a, b| {
let ma = momentum_scores.get(a).unwrap_or(&0.0);
let mb = momentum_scores.get(b).unwrap_or(&0.0);
mb.partial_cmp(ma).unwrap_or(std::cmp::Ordering::Equal)
});
let top_momentum_symbols: HashSet<String> = ranked_symbols
.iter()
.take(TOP_MOMENTUM_COUNT)
.cloned()
.collect();
// Phase 1: Process sells
let position_symbols: Vec<String> = self.positions.keys().cloned().collect();
for symbol in position_symbols {
let rows = match data.get(&symbol) {
Some(r) => r,
None => continue,
};
let idx = match symbol_date_index
.get(&symbol)
.and_then(|m| m.get(current_date))
{
Some(&i) => i,
None => continue,
};
if idx < 1 {
continue;
}
let current_row = &rows[idx];
let previous_row = &rows[idx - 1];
if current_row.rsi.is_nan() || current_row.macd.is_nan() {
continue;
}
let mut signal = generate_signal(&symbol, current_row, previous_row);
// Check stop-loss/take-profit/trailing stop/time exit
if let Some(sl_tp) =
self.check_stop_loss_take_profit(&symbol, signal.current_price)
{
signal.signal = sl_tp;
}
let was_stop_loss = matches!(signal.signal, Signal::StrongSell);
if signal.signal.is_sell() {
self.execute_sell(&symbol, signal.current_price, *current_date, was_stop_loss, portfolio_value);
}
}
// Phase 2: Process buys
if regime.allows_new_longs() {
let buy_threshold_bump = match regime {
MarketRegime::Caution => REGIME_CAUTION_THRESHOLD_BUMP,
_ => 0.0,
};
for symbol in &ranked_symbols {
if symbol == REGIME_SPY_SYMBOL {
continue;
}
let rows = match data.get(symbol) {
Some(r) => r,
None => continue,
};
if !top_momentum_symbols.contains(symbol) {
continue;
}
let idx = match symbol_date_index
.get(symbol)
.and_then(|m| m.get(current_date))
{
Some(&i) => i,
None => continue,
};
if idx < 1 {
continue;
}
let current_row = &rows[idx];
let previous_row = &rows[idx - 1];
if current_row.rsi.is_nan() || current_row.macd.is_nan() {
continue;
}
let signal = generate_signal(symbol, current_row, previous_row);
let effective_buy = if buy_threshold_bump > 0.0 {
let approx_score = signal.confidence * 10.0;
approx_score >= (4.0 + buy_threshold_bump) && signal.signal.is_buy()
} else {
signal.signal.is_buy()
};
if effective_buy {
self.execute_buy(
symbol,
signal.current_price,
*current_date,
portfolio_value,
&signal,
regime_size_factor,
);
}
}
}
// Record equity
self.equity_history.push(EquityPoint {
date: *current_date,
portfolio_value: self.get_portfolio_value(&current_prices),
cash: self.cash,
positions_count: self.positions.len(),
});
// Progress update
if (day_num + 1) % 100 == 0 {
tracing::info!(
" Processed {}/{} days... Portfolio: ${:.2} (positions: {})",
day_num + 1,
trading_dates.len(),
self.equity_history
.last()
.map(|e| e.portfolio_value)
.unwrap_or(0.0),
self.positions.len()
);
}
}
// Close all remaining positions at final prices
let final_date = trading_dates.last().copied().unwrap_or_else(Utc::now);
let position_symbols: Vec<String> = self.positions.keys().cloned().collect();
for symbol in position_symbols {
if let Some(rows) = data.get(&symbol) {
if let Some(last_row) = rows.last() {
self.execute_sell(&symbol, last_row.close, final_date, false, f64::MAX);
}
}
}
// Calculate results
let result = self.calculate_results(years)?;
// Print summary
self.print_summary(&result);
Ok(result)
}
/// Calculate performance metrics from backtest.
fn calculate_results(&self, years: f64) -> Result<BacktestResult> {
if self.equity_history.is_empty() {
@@ -1097,15 +1559,13 @@ impl Backtester {
" Max Per Sector: {:>15}",
MAX_SECTOR_POSITIONS
);
println!(
" Drawdown Halt: {:>13.0}%/{:.0}%/{:.0}% ({}/{}/{} bars)",
DRAWDOWN_TIER1_PCT * 100.0,
DRAWDOWN_TIER2_PCT * 100.0,
DRAWDOWN_TIER3_PCT * 100.0,
DRAWDOWN_TIER1_BARS,
DRAWDOWN_TIER2_BARS,
DRAWDOWN_TIER3_BARS,
);
{
let (t1p, t1b, t2p, t2b, t3p, t3b) = self.drawdown_tiers();
println!(
" Drawdown Halt: {:>13.0}%/{:.0}%/{:.0}% ({}/{}/{} bars)",
t1p * 100.0, t2p * 100.0, t3p * 100.0, t1b, t2b, t3b,
);
}
println!(
" Market Regime Filter: {:>15}",
format!("SPY EMA-{}/EMA-{}", REGIME_EMA_SHORT, REGIME_EMA_LONG)

View File

@@ -102,11 +102,19 @@ pub const RAMPUP_PERIOD_BARS: usize = 15; // Faster ramp-up
pub const REGIME_SPY_SYMBOL: &str = "SPY";
pub const REGIME_EMA_SHORT: usize = 50; // Fast regime EMA
pub const REGIME_EMA_LONG: usize = 200; // Slow regime EMA (the "golden cross" line)
/// In Caution regime, multiply position size by this factor.
/// Reduced from 0.5 to 0.25: the 2022 bear showed Caution still bleeds at 50% size.
pub const REGIME_CAUTION_SIZE_FACTOR: f64 = 0.25;
/// In Caution regime, add this to buy thresholds (require near-StrongBuy signals).
pub const REGIME_CAUTION_THRESHOLD_BUMP: f64 = 3.0;
/// In Caution regime, multiply position size by this factor (DAILY bars).
/// Daily benefits from being more aggressive in Caution (60% size) to capture bull markets.
pub const REGIME_CAUTION_SIZE_FACTOR: f64 = 0.6;
/// In Caution regime, add this to buy thresholds (DAILY bars).
/// Daily needs lower bump (1.0) to participate in bull rallies.
pub const REGIME_CAUTION_THRESHOLD_BUMP: f64 = 1.0;
/// In Caution regime, multiply position size by this factor (HOURLY bars).
/// Hourly needs to be very defensive (25% size) due to intraday noise.
pub const HOURLY_REGIME_CAUTION_SIZE_FACTOR: f64 = 0.25;
/// In Caution regime, add this to buy thresholds (HOURLY bars).
/// Hourly needs high bump (3.0) to avoid whipsaws.
pub const HOURLY_REGIME_CAUTION_THRESHOLD_BUMP: f64 = 3.0;
// ═══════════════════════════════════════════════════════════════════════
// Scaled Drawdown Circuit Breaker
@@ -114,16 +122,25 @@ pub const REGIME_CAUTION_THRESHOLD_BUMP: f64 = 3.0;
// The old fixed 10-bar cooldown is inadequate for real bear markets.
// Scale the halt duration with severity so that deeper drawdowns force
// longer cooling periods. At 25%+ DD, also require bull regime to resume.
pub const DRAWDOWN_TIER1_PCT: f64 = 0.12; // 12% → 15 bars (catch earlier)
pub const DRAWDOWN_TIER1_BARS: usize = 15;
pub const DRAWDOWN_TIER2_PCT: f64 = 0.18; // 18% → 40 bars
pub const DRAWDOWN_TIER2_BARS: usize = 40;
pub const DRAWDOWN_TIER3_PCT: f64 = 0.25; // 25%+ → 60 bars + require bull regime
pub const DRAWDOWN_TIER3_BARS: usize = 60;
/// If true, after a Tier 3 drawdown (>=25%), require bull market regime
/// before resuming new entries even after the bar cooldown expires.
// Daily drawdown tiers: relaxed to avoid halting on normal 10-15% bull pullbacks
pub const DRAWDOWN_TIER1_PCT: f64 = 0.18; // 18% → 10 bars
pub const DRAWDOWN_TIER1_BARS: usize = 10;
pub const DRAWDOWN_TIER2_PCT: f64 = 0.25; // 25% → 30 bars
pub const DRAWDOWN_TIER2_BARS: usize = 30;
pub const DRAWDOWN_TIER3_PCT: f64 = 0.35; // 35%+ → 50 bars + require bull
pub const DRAWDOWN_TIER3_BARS: usize = 50;
/// If true, after a Tier 3 drawdown, require bull market regime to resume.
pub const DRAWDOWN_TIER3_REQUIRE_BULL: bool = true;
// Hourly drawdown tiers: tighter because hourly has more whipsaw exposure
// and the bot needs to cut losses faster to preserve capital in bear periods.
pub const HOURLY_DRAWDOWN_TIER1_PCT: f64 = 0.12; // 12% → 15 bars
pub const HOURLY_DRAWDOWN_TIER1_BARS: usize = 15;
pub const HOURLY_DRAWDOWN_TIER2_PCT: f64 = 0.18; // 18% → 40 bars
pub const HOURLY_DRAWDOWN_TIER2_BARS: usize = 40;
pub const HOURLY_DRAWDOWN_TIER3_PCT: f64 = 0.25; // 25%+ → 60 bars + require bull
pub const HOURLY_DRAWDOWN_TIER3_BARS: usize = 60;
// ═══════════════════════════════════════════════════════════════════════
// Trailing Equity Curve Stop
// ═══════════════════════════════════════════════════════════════════════

View File

@@ -50,7 +50,8 @@ use crate::config::{Timeframe, DEFAULT_INITIAL_CAPITAL};
Backtest 6 months: invest-bot --backtest --months 6\n \
Backtest 1y 6m: invest-bot --backtest --years 1 --months 6\n \
Custom capital: invest-bot --backtest --years 5 --capital 50000\n \
Hourly backtest: invest-bot --backtest --years 1 --timeframe hourly"
Hourly backtest: invest-bot --backtest --years 1 --timeframe hourly\n \
Custom date range: invest-bot --backtest --start-date 2007-01-01 --end-date 2008-12-31"
)]
struct Args {
/// Run in backtest mode instead of live trading
@@ -65,6 +66,14 @@ struct Args {
#[arg(short, long, default_value_t = 0.0)]
months: f64,
/// Start date for backtest (YYYY-MM-DD). Overrides --years/--months if provided.
#[arg(long, value_name = "YYYY-MM-DD")]
start_date: Option<String>,
/// End date for backtest (YYYY-MM-DD). Defaults to now if not provided.
#[arg(long, value_name = "YYYY-MM-DD")]
end_date: Option<String>,
/// Initial capital for backtesting
#[arg(short, long, default_value_t = DEFAULT_INITIAL_CAPITAL)]
capital: f64,
@@ -171,14 +180,45 @@ async fn main() -> Result<()> {
}
async fn run_backtest(api_key: String, api_secret: String, args: Args) -> Result<()> {
// Combine years and months (default to 1 year if neither specified)
let total_years = args.years + (args.months / 12.0);
let total_years = if total_years <= 0.0 { 1.0 } else { total_years };
use chrono::NaiveDate;
let client = AlpacaClient::new(api_key, api_secret)?;
let mut backtester = Backtester::new(args.capital, args.timeframe);
let result = backtester.run(&client, total_years).await?;
let result = if args.start_date.is_some() || args.end_date.is_some() {
// Custom date range mode
let start_date = if let Some(ref s) = args.start_date {
NaiveDate::parse_from_str(s, "%Y-%m-%d")
.context("Invalid start date format. Use YYYY-MM-DD (e.g., 2007-01-01)")?
} else {
// If no start date provided, default to 1 year before end date
let end = if let Some(ref e) = args.end_date {
NaiveDate::parse_from_str(e, "%Y-%m-%d")?
} else {
chrono::Utc::now().date_naive()
};
end - chrono::Duration::days(365)
};
let end_date = if let Some(ref e) = args.end_date {
NaiveDate::parse_from_str(e, "%Y-%m-%d")
.context("Invalid end date format. Use YYYY-MM-DD (e.g., 2008-12-31)")?
} else {
chrono::Utc::now().date_naive()
};
// Validate date range
if start_date >= end_date {
anyhow::bail!("Start date must be before end date");
}
backtester.run_with_dates(&client, start_date, end_date).await?
} else {
// Years/months mode (existing behavior)
let total_years = args.years + (args.months / 12.0);
let total_years = if total_years <= 0.0 { 1.0 } else { total_years };
backtester.run(&client, total_years).await?
};
// Save results to CSV
save_backtest_results(&result)?;