feat: implement stop-loss and take-profit risk management #36

Merged
shoko merged 1 commits from fix/issue-28 into main 2026-04-09 11:39:50 +02:00
Owner

Summary

  • Implement stop-loss and take-profit checks in backtest engine after a trade is executed
  • Implement stop-loss and take-profit checks in simulate engine after a trade is executed
  • Both engines now check risk management on each tick when a position is open
  • Exit reasons (stop_loss, take_profit, manual) are recorded in trade history

Implementation Details

Backtest Engine (src/backend/app/services/backtest/engine.py)

  • Added risk_management config parsing with stop_loss_percent and take_profit_percent
  • Added entry_price and entry_time tracking after buy execution
  • Added _check_risk_management() method to check if stop-loss or take-profit is hit
  • Added _execute_risk_exit() method to execute sell when risk management triggers
  • Risk management is checked on each tick before condition evaluation
  • Sell trades now include exit_reason field (stop_loss, take_profit, or manual)

Simulate Engine (src/backend/app/services/simulate/engine.py)

  • Added risk_management config parsing with stop_loss_percent and take_profit_percent
  • Added position tracking (position, entry_price, entry_time, current_balance, trades)
  • Added _check_risk_management() method similar to backtest engine
  • Added _execute_risk_exit() method to generate sell signal when risk management triggers
  • Buy actions now track entry price and position size
  • Risk management is checked on each price update before condition evaluation

Acceptance Criteria

  • Backtest engine respects stop_loss_percent
  • Backtest engine respects take_profit_percent
  • Simulate engine respects stop_loss_percent
  • Simulate engine respects take_profit_percent
  • Results show exit reasons (stop-loss, take-profit, or end of period)

Fixes #28

## Summary - Implement stop-loss and take-profit checks in backtest engine after a trade is executed - Implement stop-loss and take-profit checks in simulate engine after a trade is executed - Both engines now check risk management on each tick when a position is open - Exit reasons (stop_loss, take_profit, manual) are recorded in trade history ## Implementation Details ### Backtest Engine (`src/backend/app/services/backtest/engine.py`) - Added `risk_management` config parsing with `stop_loss_percent` and `take_profit_percent` - Added `entry_price` and `entry_time` tracking after buy execution - Added `_check_risk_management()` method to check if stop-loss or take-profit is hit - Added `_execute_risk_exit()` method to execute sell when risk management triggers - Risk management is checked on each tick before condition evaluation - Sell trades now include `exit_reason` field (stop_loss, take_profit, or manual) ### Simulate Engine (`src/backend/app/services/simulate/engine.py`) - Added `risk_management` config parsing with `stop_loss_percent` and `take_profit_percent` - Added position tracking (`position`, `entry_price`, `entry_time`, `current_balance`, `trades`) - Added `_check_risk_management()` method similar to backtest engine - Added `_execute_risk_exit()` method to generate sell signal when risk management triggers - Buy actions now track entry price and position size - Risk management is checked on each price update before condition evaluation ## Acceptance Criteria - [x] Backtest engine respects stop_loss_percent - [x] Backtest engine respects take_profit_percent - [x] Simulate engine respects stop_loss_percent - [x] Simulate engine respects take_profit_percent - [x] Results show exit reasons (stop-loss, take-profit, or end of period) Fixes #28
shoko added 1 commit 2026-04-09 11:14:31 +02:00
han approved these changes 2026-04-09 11:38:55 +02:00
han left a comment
First-time contributor

lgtm

lgtm
shoko merged commit dd25d38e7e into main 2026-04-09 11:39:50 +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#36