[Backend] Backtest Engine - Historical Data Replay #18

Merged
shoko merged 1 commits from fix/issue-7 into main 2026-04-08 13:21:57 +02:00
Owner

Summary

Implement backtest engine for historical strategy testing as described in issue #7.

Changes Made

New files:

  • src/backend/app/services/backtest/ave_client.py - AVE Cloud API client for fetching klines, token prices, batch prices

Modified files:

  • src/backend/app/services/backtest/engine.py - Full BacktestEngine implementation:

    • Condition matching logic (price_drop, price_rise, volume_spike, price_level)
    • Signal generation on condition match
    • Portfolio simulation with buy/sell execution
    • Metrics calculation: total_return, win_rate, max_drawdown, sharpe_ratio, total_trades
  • src/backend/app/api/backtest.py - Backtest API endpoints:

    • POST /api/bots/{id}/backtest - Start backtest (async, background execution)
    • GET /api/bots/{id}/backtest/{run_id} - Get backtest status/results
    • GET /api/bots/{id}/backtests - List all backtests for bot
    • POST /api/bots/{id}/backtest/{run_id}/stop - Stop running backtest
    • JWT authentication and ownership validation on all endpoints

Features Implemented

  1. AVE Cloud Data API Integration - Fetch historical klines from prod.ave-api.com
  2. Condition Matching - Supports price_drop, price_rise, volume_spike, price_level conditions
  3. Portfolio Simulation - Simulates trades based on strategy actions (buy/sell)
  4. Metrics Calculation:
    • total_return %
    • win_rate %
    • total_trades
    • max_drawdown %
    • sharpe_ratio
  5. Async/Background Execution - Backtests run in background without blocking
  6. Results Persistence - Backtest results stored in backtests table, signals in signals table

Dependencies

  • Depends on issue #6 (Chat Interface + CrewAI) which was merged in PR #17

Fixes #7

## Summary Implement backtest engine for historical strategy testing as described in issue #7. ### Changes Made **New files:** - `src/backend/app/services/backtest/ave_client.py` - AVE Cloud API client for fetching klines, token prices, batch prices **Modified files:** - `src/backend/app/services/backtest/engine.py` - Full BacktestEngine implementation: - Condition matching logic (price_drop, price_rise, volume_spike, price_level) - Signal generation on condition match - Portfolio simulation with buy/sell execution - Metrics calculation: total_return, win_rate, max_drawdown, sharpe_ratio, total_trades - `src/backend/app/api/backtest.py` - Backtest API endpoints: - POST /api/bots/{id}/backtest - Start backtest (async, background execution) - GET /api/bots/{id}/backtest/{run_id} - Get backtest status/results - GET /api/bots/{id}/backtests - List all backtests for bot - POST /api/bots/{id}/backtest/{run_id}/stop - Stop running backtest - JWT authentication and ownership validation on all endpoints ### Features Implemented 1. **AVE Cloud Data API Integration** - Fetch historical klines from prod.ave-api.com 2. **Condition Matching** - Supports price_drop, price_rise, volume_spike, price_level conditions 3. **Portfolio Simulation** - Simulates trades based on strategy actions (buy/sell) 4. **Metrics Calculation**: - total_return % - win_rate % - total_trades - max_drawdown % - sharpe_ratio 5. **Async/Background Execution** - Backtests run in background without blocking 6. **Results Persistence** - Backtest results stored in backtests table, signals in signals table ### Dependencies - Depends on issue #6 (Chat Interface + CrewAI) which was merged in PR #17 ## Related Issue Fixes #7
shoko added 1 commit 2026-04-08 11:39:33 +02:00
Implements issue #7 - Backtest Engine for historical strategy testing.

Changes:
- Created AveCloudClient for fetching klines from AVE Cloud Data API
- Implemented BacktestEngine with condition matching (price_drop, price_rise, volume_spike, price_level)
- Implemented signal generation and portfolio simulation
- Calculates metrics: total_return, win_rate, max_drawdown, sharpe_ratio, total_trades
- Implemented async/background backtest execution via FastAPI BackgroundTasks
- Stores results in backtests table and signals table
- All backtest API endpoints with JWT auth and ownership validation

API Endpoints:
- POST /api/bots/{id}/backtest - Start backtest
- GET /api/bots/{id}/backtest/{run_id} - Get status/results
- GET /api/bots/{id}/backtests - List all backtests
- POST /api/bots/{id}/backtest/{run_id}/stop - Stop running backtest
han approved these changes 2026-04-08 13:21:04 +02:00
han left a comment
First-time contributor

lgtm

lgtm
shoko merged commit 0fb16f06e4 into main 2026-04-08 13:21:57 +02:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shoko/randebu#18