Commit Graph

78 Commits

Author SHA1 Message Date
Bu5hm4nn
b2bc4db41a Improve backtest lazy loading and test automation 2026-04-07 12:18:50 +02:00
Bu5hm4nn
faa06a106e feat: add LTV unhedged column to daily results table
- Add LTV unhedged column before LTV hedged column
- Update both render_result and render_job_result tables
- Show both hedged and unhedged LTV for comparison
2026-04-06 22:28:03 +02:00
Bu5hm4nn
b546b59b33 feat: split portfolio chart into stacked bar chart above candle chart
- Create separate portfolio stacked bar chart (underlying + option value)
- Place portfolio chart above candle chart with same X axis alignment
- Make candle chart double height (h-[48rem] vs h-48 for portfolio)
- Portfolio chart shows underlying (gray) + option value (blue) as stacked bars
- Charts now render above the daily results table
2026-04-06 18:50:41 +02:00
Bu5hm4nn
f00b1b7755 feat: add candlestick chart with portfolio value line (BT-004)
- Add spot_open field to BacktestDailyPoint for complete OHLC data
- Replace line chart with candlestick chart showing price OHLC
- Add portfolio value line on secondary Y-axis
- Add _chart_options_from_dict for rendering job results
- Update both render_result and render_job_result to use new chart
2026-04-06 11:22:10 +02:00
Bu5hm4nn
aff4df325d feat: defer entry spot derivation to backtest run (BT-005)
- Remove async refresh_workspace_seeded_units from date change handlers
- Date changes now only call on_form_change() (updates cost estimates, marks results stale)
- Entry spot is derived only when user clicks Run button
- Form remains responsive during configuration
- No more API errors when changing dates while configuring other fields
2026-04-06 11:14:51 +02:00
Bu5hm4nn
4af7a09c6e feat: add option contracts to overview, fix default dates, add roadmap items
- Move option contracts from daily results table to overview cards (constant throughout backtest)
- Fix default dates to March 2026 (2026-03-02 to 2026-03-25)
- Add BT-004 backlog item: candlestick chart with portfolio value line on secondary axis
- Add BT-005 backlog item: defer entry spot derivation to backtest run (not on every date change)
2026-04-05 09:24:25 +02:00
Bu5hm4nn
6b8336ab7e feat: add Portfolio Value, Option Value, and Contracts columns to daily results
- Add option_contracts field to BacktestDailyPoint (number of contracts held)
- Update engine to calculate total option contracts from positions
- Update job serialization to include underlying_value, option_market_value, net_portfolio_value, option_contracts
- Update both render_result and render_job_result tables to show:
  - Low, High, Close (from previous commit)
  - Portfolio value (net_portfolio_value)
  - Option value (option_market_value)
  - Contracts (option_contracts)
  - LTV hedged
  - Margin call status
2026-04-05 08:54:38 +02:00
Bu5hm4nn
063ccb6781 feat: default to March 2026 dates and show Low/High/Close in results
- Change default backtest date range to 2026-03-02 through 2026-03-25
- Add spot_low and spot_high to BacktestDailyPoint for intraday range
- Update engine to populate low/high from DailyClosePoint
- Update daily results table to show Low, High, Close columns instead of just Spot
- Update job serialization to include spot_low and spot_high
2026-04-04 23:18:01 +02:00
Bu5hm4nn
1e567775f9 fix: also catch RuntimeError in derive_entry_spot exception handler
Databento can raise RuntimeError for API key issues, but derive_entry_spot
only caught ValueError and KeyError. This ensures Databento errors are
properly caught and displayed to the user.
2026-04-04 22:53:06 +02:00
Bu5hm4nn
4e9a610452 fix: update render_job_result to use correct result field names
The job serialization was fixed to use new field names, but the UI render
function was still using old field names (total_pnl, hedging_cost, etc.)
which don't exist anymore. Now uses:
- start_value, end_value_hedged_net, total_hedge_cost from summary_metrics
- template_results[0].daily_path for daily results table
- Added margin call metrics display
2026-04-04 22:40:39 +02:00
Bu5hm4nn
dbd6e103c0 fix: pin black to 26.3.1 across all environments
- Pin black version in requirements-dev.txt (was >=24.0.0)
- Update pre-commit to use black 26.3.1 with Python 3.12
- Add language_version: python3.12 to pre-commit black hook
- Reformat files with new black version for consistency
2026-04-01 13:58:49 +02:00
Bu5hm4nn
6bcf78e5df style: format UI files and remove lint excludes
- Remove app/components/ and app/pages/ from ruff/black excludes
- Pre-commit reformatted multi-line strings for consistency
- All files now follow the same code style
2026-04-01 13:55:55 +02:00
Bu5hm4nn
27ade507cd feat(backtest): async job queue for non-blocking backtest execution
BREAKING CHANGE: Complete redesign of backtest execution

- Add BacktestJob system with progress stages (validating, fetching_prices, calculating)
- Run backtests in background threads, UI polls for status
- Show progress label with current stage during execution
- Remove synchronous Databento API calls from page load
- Use static default entry spot for initial render (defers API call)
- Make refresh_workspace_seeded_units async with run.io_bound

This fixes:
- 'Connection lost' WebSocket timeout errors
- Slow page load (30s initial load)
- Backtest never completing

The job system provides:
- Non-blocking execution
- Progress tracking with stages
- Error handling with user-friendly messages
- Result caching for retrieval after completion
2026-04-01 09:31:53 +02:00
Bu5hm4nn
2b500dfcb3 fix(backtest): run backtest asynchronously to prevent WebSocket timeout
- Use run.io_bound() from NiceGUI to run Databento API calls in background thread
- Add loading state to Run Backtest button
- Show notification when backtest starts and completes
- Remove loading state on completion/error

This prevents 'Connection lost' errors when the backtest takes longer than the WebSocket timeout.
2026-03-31 23:31:07 +02:00
Bu5hm4nn
c650cec159 perf(backtest): reduce Databento API calls on input changes
- on_form_change: Only update cost estimate, skip expensive derive_entry_spot
- Only call derive_entry_spot on date changes (start/end inputs)
- Other inputs (template, units, loan, LTV) just mark results stale
- This reduces lag from constant API polling
2026-03-30 20:58:36 +02:00
Bu5hm4nn
69109c9e36 fix(backtest): pass data_source to validate_preview_inputs in validate_current_scenario 2026-03-30 17:50:47 +02:00
Bu5hm4nn
b161c51109 fix(backtest): handle Databento errors gracefully during page load
- Set default dates to 2024-07-01 to 2024-12-31 (valid for XNAS.BASIC)
- Catch all exceptions during entry spot derivation, not just ValueError
- Don't auto-run backtest on page load - let user configure first
- Use recent GLD price (~30) as fallback
2026-03-30 14:48:08 +02:00
Bu5hm4nn
79980c33ec feat(backtest): add dataset-specific date validation and better error handling
- Add DATABENTO_DATASET_MIN_DATES for XNAS.BASIC (2024-07-01) and GLBX.MDP3 (2010-01-01)
- Validate start date against dataset minimum before running backtest
- Parse Databento API errors and show user-friendly messages
- Update date range hint to show dataset-specific availability
- Catch BentoClientError and show appropriate warning tone
2026-03-30 14:37:04 +02:00
Bu5hm4nn
2d1ecc2fcf fix(backtest): ensure data_source is passed through all validation calls
- Pass data_source to derive_entry_spot in backtests.py
- Remove default 'synthetic' value for data_source in derive_entry_spot and validate_preview_inputs
- Update all tests to explicitly pass data_source parameter
- Improve error message with helpful suggestion for Databento/Yahoo Finance
2026-03-30 09:21:49 +02:00
Bu5hm4nn
70b09cbf0b fix(backtest): remove BT-001A exact window restriction now that full data access is available
- Change WindowPolicy from EXACT to BOUNDED for backtest fixture
- Pass data_source to run_read_only_scenario so real data can be used
- Fix injected provider identity preservation in BacktestPageService
- Add type: ignore for BacktestHistoricalProvider protocol assignment
- Revert TypedDict change to avoid cascading type issues in pages/
- Update tests to reflect new BOUNDED policy behavior
2026-03-30 08:57:15 +02:00
Bu5hm4nn
269745cd3e fix: address PR review feedback for validation functions
1. Fix Friday logic edge case comment
   - Clarified get_default_backtest_dates() docstring
   - Removed confusing 'at least a week old' comment
   - Explicitly documented Friday behavior

2. Reorder validation checks in validate_date_range_for_symbol()
   - Now checks start > end first (most fundamental)
   - Then checks end > today (future dates)
   - Finally checks symbol-specific bounds
   - Users get most actionable error first

3. Add server-side numeric bounds validation
   - New validate_numeric_inputs() function
   - Validates units > 0, loan >= 0, 0 < LTV < 1
   - Called in run_backtest() before service call

4. Add boundary tests
   - Test start_date exactly at SYMBOL_MIN_DATES boundary
   - Test same-day date range (start == end)
   - Test end_date exactly today
   - Test end_date tomorrow (future)
   - Test validation order returns most actionable error
   - Test near-zero and large values for units calculation
   - Test LTV at boundaries (0, 1, 0.01, 0.99)

5. Add tests for validate_numeric_inputs
   - Valid inputs, zero/negative values
   - LTV boundary conditions
2026-03-29 19:29:46 +02:00
Bu5hm4nn
f9ea7f0b67 fix: address PR review issues for event comparison and backtests
Critical fixes:
- Add validate_and_calculate_units() helper with proper error handling
- Handle division by zero for entry_spot in refresh_preview() and render_report()
- Add server-side validation for initial_value > 0
- Add try/except for derive_entry_spot() to handle fixture source limitations

Important improvements:
- Add dynamic default dates with get_default_backtest_dates()
- Add validate_date_range_for_symbol() for symbol-specific date bounds
- Add SYMBOL_MIN_DATES validation for backtests
- Update date_range_hint based on selected symbol

Tests:
- Add test_page_validation.py with 21 tests for:
  - validate_and_calculate_units edge cases
  - validate_date_range_for_symbol bounds checking
  - get_default_backtest_dates dynamic generation
  - SYMBOL_MIN_DATES constant verification
2026-03-29 18:45:29 +02:00
Bu5hm4nn
c2af363eef feat(backtests): expand default date range to full Databento availability
- Changed default date range from 5 days (Jan 2024) to 2 years (2022-2023)
- Added SYMBOL_MIN_DATES constant documenting data availability per symbol
- GLD minimum date: 2004-11-18 (ETF launch)
- GC futures minimum date: 1974-01-01
- XAU index minimum date: 1970-01-01
- Added UI hint showing GLD data availability from ETF launch
- Users can now run backtests across the full historical range
2026-03-29 17:53:03 +02:00
Bu5hm4nn
853c80d3a2 feat(event-comparison): use initial portfolio value instead of underlying units
- Changed UI input from 'Underlying units' to 'Initial portfolio value ($)'
- Underlying units are now calculated as initial_value / entry_spot
- Updated default value to workspace gold_value instead of gold_ounces * entry_spot
- Result summary now shows both 'Initial value' and 'Underlying units'
- This allows users to specify how much they invest on day 1, and the system
  automatically calculates the maximum purchasable shares/contracts
2026-03-29 16:12:33 +02:00
Bu5hm4nn
9a3b835c95 feat(DATA-DB-004): add Databento settings UI and independent scenario config
- Updated backtests page with Data Source card
  - Data source selector (databento/yfinance/synthetic)
  - Dataset dropdown (XNAS.BASIC, GLBX.MDP3)
  - Resolution dropdown (ohlcv-1d, ohlcv-1h)
  - Cost estimate display (placeholder for now)

- Added Scenario Configuration card
  - Underlying symbol selector (GLD/GC/XAU)
  - Start/end date inputs
  - Start price input (0 = auto-derive)
  - Underlying units, loan amount, margin call LTV

- BacktestPageService updates:
  - get_historical_prices() with data_source parameter
  - get_cost_estimate() for Databento cost estimation
  - get_cache_stats() for cache status display
  - Support for injected custom provider identity
  - DataSourceInfo for provider metadata

- BacktestSettingsRepository integration:
  - Load/save settings per workspace
  - Default values from BacktestSettings.create_default()

- Test update: TLT validation message changed to reflect
  new multi-symbol support (GLD, GC, XAU)
2026-03-29 11:12:11 +02:00
Bu5hm4nn
bb06fa7e80 feat(PORTFOLIO-003): add premium and spread for physical gold positions 2026-03-28 23:53:46 +01:00
Bu5hm4nn
0e972e9dd6 feat(PORTFOLIO-002): add position storage costs 2026-03-28 23:48:41 +01:00
Bu5hm4nn
63a8482753 feat(DISPLAY-002): GLD mode shows real share prices 2026-03-28 21:59:15 +01:00
Bu5hm4nn
dac0463d55 feat(DISPLAY-002): GLD mode shows real share prices 2026-03-28 21:45:00 +01:00
Bu5hm4nn
20f5086507 feat(DISPLAY-001): add underlying mode switching 2026-03-28 21:44:32 +01:00
Bu5hm4nn
1a39956757 feat(PORTFOLIO-001): add position-level portfolio entries 2026-03-28 21:29:30 +01:00
Bu5hm4nn
3b98ebae69 feat(DATA-004): add underlying instrument selector 2026-03-28 16:40:18 +01:00
Bu5hm4nn
9d06313480 feat(PRICING-002): add GLD/GC=F basis display on overview 2026-03-28 09:18:26 +01:00
Bu5hm4nn
4620234967 feat(EXEC-001): add hedge strategy builder 2026-03-27 22:33:20 +01:00
Bu5hm4nn
477514f838 feat(BT-002): add historical snapshot provider 2026-03-27 18:31:28 +01:00
Bu5hm4nn
1a6760bee3 feat(PORT-003): add historical ltv charts 2026-03-27 16:39:33 +01:00
Bu5hm4nn
3c9ff201e1 feat(BT-003B): add event comparison drilldown 2026-03-26 22:05:31 +01:00
Bu5hm4nn
ff76e326b1 feat(CORE-001D3B): surface alert history degraded state 2026-03-26 15:12:04 +01:00
Bu5hm4nn
09e03f96a8 chore(settings): drop unused last-saved helper 2026-03-26 15:05:28 +01:00
Bu5hm4nn
38d244356c refactor(settings): separate preview validation from internal failures 2026-03-26 15:00:53 +01:00
Bu5hm4nn
e860c40567 fix(settings): reject fractional refresh intervals 2026-03-26 14:05:49 +01:00
Bu5hm4nn
2759d9a36f fix(settings): track dirty state across all inputs 2026-03-26 13:59:56 +01:00
Bu5hm4nn
cfa3cfcc08 fix(settings): clarify last-saved status state 2026-03-26 13:54:56 +01:00
Bu5hm4nn
f7c134a709 fix(settings): preserve whole-dollar loan formatting 2026-03-26 13:34:34 +01:00
Bu5hm4nn
ea3b384103 fix(settings): fail closed on blank loan input 2026-03-26 13:28:30 +01:00
Bu5hm4nn
753e9d3146 fix(CORE-001D3A): accept decimal boundary inputs 2026-03-26 13:19:18 +01:00
Bu5hm4nn
f38d0a53a9 refactor(pre-alpha): fail closed on historical preview fallbacks 2026-03-26 11:55:45 +01:00
Bu5hm4nn
4eec0127da fix(UX-001): reconcile preview validation behavior 2026-03-26 10:39:03 +01:00
Bu5hm4nn
82e52f7162 fix(UX-001): tighten historical stale state handling 2026-03-26 10:32:05 +01:00
Bu5hm4nn
78de8782c4 fix(UX-001): address layout review findings 2026-03-26 10:24:52 +01:00