Commit Graph

148 Commits

Author SHA1 Message Date
shokollm
3013326ded feat: add time labels to X axis of price chart
- Shows time (HH:MM) at 5 points along the X axis
- Legend moved up to make room for time labels
- More bottom padding for better display
2026-04-12 07:07:19 +00:00
shokollm
a82185de60 fix: syntax error in simulate.py finally block 2026-04-12 06:18:11 +00:00
shokollm
cadea23e40 fix: respect candle_delay from config, default to fast tests
- Tests now run with candle_delay=0 for fast execution
- Simulation defaults to candle_delay based on interval (e.g., 30s for 1m)
- Progress saved to DB every 5 seconds during simulation
- User can now see real-time updates while simulation runs

Tests: 14 passing in 0.15s
2026-04-12 05:24:43 +00:00
shokollm
984656c83c test: add full integration test for simulation
test_full_simulation_workflow_generates_signals_and_trades:
- Creates klines with clear price movements
- Uses very low threshold (0.1%) to ensure signals generated
- Verifies signals are NOT empty
- Verifies trade_log is NOT empty
- Verifies BUY signals are generated
- Verifies results contain signals and trade_log

This test ensures the simulation engine is working correctly.
2026-04-12 05:00:46 +00:00
shokollm
1505bc9913 fix: serialize datetime objects to ISO format for JSON storage
The signals contain datetime objects for created_at which can't be serialized to JSON directly. Convert them to ISO format strings before storing.
2026-04-12 04:50:24 +00:00
shokollm
dd61c32ea7 feat: add trade activity dashboard
Shows what happened at each candle:
- BUY/SELL/HOLD actions
- Price at that time
- Reason for action
- Entry price for positions

Trade log is stored in DB and displayed in frontend.
2026-04-12 04:28:40 +00:00
shokollm
01ec8bc539 fix: make SignalChart more robust
- Use ResizeObserver to handle width changes
- Use tick() to ensure DOM is ready before drawing
- Access reactive values in effect to trigger on changes
- Fixed canvas sizing to use percentage width
2026-04-12 04:11:34 +00:00
shokollm
a253aae766 fix: limit klines to 1 hour, fix chart parsing string to number
- Kline data now fetches only last hour of data
- SignalChart converts string 'close' prices to numbers
2026-04-12 03:57:22 +00:00
shokollm
13e899c851 fix: fetch klines synchronously before returning response
The background task wasn't completing before the response was returned.
Now we fetch klines synchronously (await) before returning the simulation.
2026-04-12 03:51:03 +00:00
shokollm
384f84e772 fix: fetch klines synchronously so chart shows immediately
The simulation engine completes in seconds, but the background task wasn't
saving klines to DB. Now we fetch klines first synchronously so the user
can see the price chart immediately after starting a simulation.

Also marks any stuck 'running' simulations as 'stopped'.
2026-04-12 03:18:42 +00:00
shokollm
cd1a41d1d7 feat: show price chart even when no signals 2026-04-12 03:02:51 +00:00
shokollm
6a20cc174f feat: add price chart to simulation and unit tests
Unit tests (13 passing):
- Kline fetching and processing
- Price drop condition triggers buy
- Stop loss and take profit risk management
- Multiple positions (buy again after sell)
- Max candles limit
- Stop interruption handling

Frontend:
- SignalChart now shows price movement even before signals
- Shows candle count even with no signals
- Chart displays buy/sell markers when signals exist
- Canvas-based chart with gradient fill

Backend:
- Simulation stores klines for chart display
- Returns klines in API response
- Simplified simulation run (no periodic saving)
2026-04-12 02:42:52 +00:00
shokollm
ce8a29c0a4 fix: update notice message for klines-based simulation 2026-04-12 02:22:17 +00:00
shokollm
f425ae08d7 feat: klines-based simulation instead of polling
- Fetch historical klines once from AVE API (10 CU per request)
- Process each candle as a time step
- Limit to 500 candles max per simulation
- No continuous polling - processes all data in seconds
- Frontend now selects kline interval (1m, 5m, 15m, 1h)
- Much more efficient API usage
2026-04-12 01:34:20 +00:00
shokollm
d4400f5dcd fix: simulation conditions now check properly from first iteration
The first price check was always skipped because last_price was None.
Now first iteration primes last_price, subsequent iterations check conditions.
2026-04-12 00:53:05 +00:00
shokollm
1591fcb1ca fix: remove check_interval restriction for non-pro plans
Simulation is paper trade only, so no need to restrict check_interval.
Allow 10 second intervals for all users.
2026-04-12 00:23:55 +00:00
shokollm
b0131aa566 fix: stop simulation always updates DB status
- Status was only updated if engine was in memory (race condition)
- Now always sets status to 'stopped' in DB
- Returns 'stopped' instead of 'stopping'
- Cleaned up 3 stale running simulations in DB
2026-04-12 00:15:41 +00:00
shokollm
52adc93b25 fix: show running simulation correctly, stop old ones when starting new
Frontend:
- Load simulations now prioritizes running simulation over most recent
- Clear signals before loading new simulation

Backend:
- When starting new simulation, stop any existing running simulation first
- Previously would return existing running simulation (confusing UX)
2026-04-12 00:10:10 +00:00
shokollm
79c3ec7d16 fix: typo in simulate page svelte 2026-04-12 00:00:47 +00:00
shokollm
3505cf4ade refactor: simplify simulation to run forever as paper trade
- No duration limit - runs forever until user stops
- Only 1 running simulation per bot (returns existing if already running)
- Always paper trade (no auto-execute option)
- Removed Pro upgrade banner
- Removed duration and auto-execute config options
- Simplified API to only require token, chain, check_interval
2026-04-11 23:52:00 +00:00
shokollm
1b1358353f feat: configurable simulation duration and periodic signal saving
Frontend:
- Added duration selector (1min, 5min, 10min, 30min)
- Added check interval selector (10s, 30s, 60s)

Backend:
- Signals are now saved to database every 30 seconds during simulation
- Can stop simulation early to see partial signals
2026-04-11 17:56:27 +00:00
shokollm
726e579f5f fix: get_token_price checking wrong status code
The AVE API returns status: 1 for success, not status: 200.
This was causing get_token_price to always return None, resulting
in no signals being generated during simulation.
2026-04-11 17:45:59 +00:00
shokollm
b111e4d79f fix: make SimulateEngine.stop() synchronous
The stop() method was async but called from a sync context,
causing 'RuntimeError: no running event loop'. Changed to sync
since it just sets flags.
2026-04-11 17:35:18 +00:00
shokollm
0d63a10ac8 fix: correct simulation API field names
The backend expects 'check_interval' not 'interval_seconds',
and 'chain' is required.
2026-04-11 17:22:45 +00:00
shokollm
19f28fc599 feat: use token from strategy config in simulation page
Like the backtest page, simulation now extracts the token from the
bot's strategy config instead of requiring user input. Shows token
name and truncated address.
2026-04-11 17:17:26 +00:00
shokollm
5f7667992e feat: display backtest config in history card
Show token, timeframe, and date range for each backtest in the history list:
- Token: PEPE
- TF: 1h
- Period: 2026-03-11 to 2026-04-09
2026-04-11 17:11:24 +00:00
shokollm
cd4583ca90 feat: add pagination for trade history in backtest
Backend:
- Added pagination to /trades endpoint with page and per_page params
- Returns paginated trades with metadata (page, total_pages, has_next, has_prev)

Frontend:
- Added pagination controls for trade history (Prev/Next buttons)
- Shows current page info and total trades
- Trades are loaded on-demand when expanded

API changes:
- GET /bots/{id}/backtest/{runId}/trades?page=1&per_page=5
- Response includes: trades, total_trades, page, per_page, total_pages, has_next, has_prev
2026-04-11 16:52:45 +00:00
shokollm
6cadb7a67b test: verify stop loss always results in loss
Add test case that ensures when stop loss is triggered after multiple
DCA buys with decreasing prices, the final balance is always less
than the initial balance.
2026-04-11 16:27:04 +00:00
shokollm
02e0b0ccab fix: proper DCA and max_drawdown calculations in backtest engine
Three bugs fixed:

1. **Weighted average entry price for risk management**:
   - Previously, entry_price was overwritten on each buy, causing stop loss
     to be calculated from the latest buy price instead of average
   - Added cost_basis tracking and average_entry_price property
   - Stop loss now correctly uses weighted average across all buys

2. **Portfolio value accumulation in _calculate_metrics**:
   - Bug: running_position = trade['quantity'] was OVERWRITING position
   - Fix: running_position += trade['quantity'] to properly accumulate DCA

3. **Risk management exit reset**:
   - Added cost_basis reset when position is closed

Max drawdown is now correctly bounded by stop loss percentage (~5%)
instead of showing inflated values like 59%.
2026-04-11 15:54:15 +00:00
shokollm
29ec67cced fix: handle floating point precision in take_profit check and final_balance calculation
Two bugs fixed:
1. final_balance was incorrectly calculated as balance + balance when position=0 due to expression structure
2. take_profit check needed epsilon for floating point precision (95 * 1.10 = 104.50000000000001 instead of 104.5)
2026-04-11 15:02:53 +00:00
shokollm
c86e71c3a3 fix: correct final_balance calculation in _calculate_metrics
Bug: The expression was evaluating incorrectly due to operator precedence:
  final_balance = balance + (position * price if condition else balance)

When condition=False (position=0), this became: balance + balance = 2x balance!

Fixed by restructuring to if/else block.
2026-04-11 15:00:52 +00:00
shokollm
44fb840731 fix: correctly track balance in portfolio value calculation for max_drawdown
The bug was that running_balance was set to trade['amount'] which is
the amount SPENT on a buy (not remaining balance), causing inflated
portfolio values and incorrect max drawdown calculation.

Now properly tracks:
- After BUY: balance decreases by amount spent
- After SELL: balance increases by amount received
2026-04-11 14:22:47 +00:00
shokollm
6a5694f74b fix: properly value open positions using last kline price for max_drawdown calculation
- Track last_kline_price during kline processing
- Use last_kline_price instead of entry price for open position valuation
- Add final marked-to-market value to portfolio_values for max_drawdown calculation
- This fixes the issue where max_drawdown exceeded stop_loss percentage
2026-04-11 13:54:16 +00:00
shokollm
680a9322e3 debug: add logging to trace strategy_config in backtest engine 2026-04-11 11:59:37 +00:00
shokollm
9973b8f6e2 feat: make trade history expandable with button 2026-04-11 06:49:58 +00:00
shokollm
30476e782b fix: remove duplicate backtest history section 2026-04-11 06:23:35 +00:00
shokollm
02ca452655 feat: show trades inline in backtest history 2026-04-11 06:16:10 +00:00
shokollm
cb9558d54f feat: show trades inline in backtest card instead of modal 2026-04-11 06:08:43 +00:00
shokollm
638e17eb73 debug: simplify modal to show raw JSON 2026-04-11 05:48:33 +00:00
shokollm
69a8b06462 debug: add debug info to see selectedTrades.length 2026-04-11 05:44:08 +00:00
shokollm
15e72b009c debug: add console logs to viewTrades function 2026-04-11 05:39:49 +00:00
shokollm
19ba0c7cc6 fix: parse JSON string result if needed when retrieving trades 2026-04-11 05:36:47 +00:00
shokollm
847890b634 feat: limit backtest history to latest 5 2026-04-11 05:36:31 +00:00
shokollm
6658a418cc fix: missing newline in backtest.py causing 404 2026-04-11 05:26:51 +00:00
shokollm
5c9e46e693 feat: add trades history modal to backtest page 2026-04-11 05:18:23 +00:00
shokollm
194c4f8a62 fix: use original datetime for created_at instead of converted string 2026-04-11 05:06:21 +00:00
shokollm
7afcb983e8 fix: correct klines status check (1 not 200) and data.points format 2026-04-11 04:56:50 +00:00
shokollm
caef4b36ed feat: auto-fill token from strategy config in backtest page 2026-04-11 04:37:52 +00:00
shokollm
3bf2877df2 fix: append -bsc suffix to token address for klines API 2026-04-10 17:07:14 +00:00
shokollm
145c6710d1 fix: set 1-day range for backtest (start day before end day) 2026-04-10 16:30:22 +00:00