Commit Graph

176 Commits

Author SHA1 Message Date
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
shokollm
3c8c85aefc fix: table regex to match tables anywhere in text (not just at start) 2026-04-10 13:53:25 +00:00
shokollm
39b2b558a5 fix: export parseInlineElements and types from markdown.ts 2026-04-10 13:17:07 +00:00
shokollm
7795753aaa fix: render bold and inline code formatting in list items 2026-04-10 13:14:17 +00:00
shokollm
36dcfdb6e2 chore: restrict agent to BSC only, remove chain parameter from search_tokens tool 2026-04-10 12:58:25 +00:00
shokollm
48fc323dac fix: handle native tool_calls from MiniMax API instead of parsing JSON from content 2026-04-10 12:54:29 +00:00
shokollm
0af2de7209 feat: add search_tokens tool for AI to recommend trending tokens 2026-04-10 12:48:49 +00:00
shokollm
e82b8b3549 fix: update token search to use trending endpoint (v2/tokens doesn't exist) 2026-04-10 12:37:27 +00:00
shokollm
6f23b322d3 feat: add token search in modal when confirming address 2026-04-10 12:14:32 +00:00
shokollm
297a185215 feat: implement token address confirmation dialog and limit backtest duration 2026-04-10 11:52:40 +00:00
shokollm
f86ff75525 fix: remove extra closing brace in CSS 2026-04-10 11:32:11 +00:00
shokollm
6f9564790f docs: add ISSUES.md for tracking open issues 2026-04-10 11:13:20 +00:00
shokollm
f43eb11f6f feat: improve backtest with manual refresh and token address confirmation 2026-04-10 10:54:42 +00:00
shokollm
446da96ce4 fix: search for token first to get proper token_id before fetching klines 2026-04-10 10:47:33 +00:00
shokollm
922ef89c1e feat: add backtest progress tracking and fix stop functionality 2026-04-10 10:43:04 +00:00
shokollm
a601ebb08b fix: handle datetime serialization in backtest and show errors in frontend 2026-04-10 10:34:29 +00:00
shokollm
bb40193fc3 fix: add required chain field (bsc) to backtest request 2026-04-10 10:28:16 +00:00
shokollm
3a7d3a3732 feat: set default dates for backtest (yesterday to 30 days ago) 2026-04-10 10:23:35 +00:00
shokollm
0f558a5e8e fix: handle array error format from FastAPI validation errors 2026-04-10 10:21:27 +00:00
shokollm
9e9ff6fa7f fix: handle undefined timeframe in strategy preview 2026-04-10 10:19:28 +00:00
shokollm
4c48932ece fix: support inline formatting in table cells (bold, italic, code, links) 2026-04-10 10:15:21 +00:00
shokollm
bfc85648db fix: improve markdown parser for tables, headings, and line breaks 2026-04-10 10:09:46 +00:00
shokollm
925920eee1 fix: add typing indicator back when waiting for response 2026-04-10 10:05:50 +00:00