Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 11, 2025

🎯 Issue Reference

Fixes #225

📋 Summary

This PR implements maximum loss protection for the TraderBot by automatically selling all positions at market price when the loss exceeds a configurable threshold.

🚀 Changes Made

Core Features

  • MaximumLossPercentage Setting: Added optional configuration parameter to set the maximum allowable loss percentage
  • Loss Calculation Logic: Implemented real-time loss calculation based on average purchase price vs current market price
  • Market Sell Orders: Added functionality to place immediate market sell orders (not limit orders)
  • Automatic Protection: Integrated maximum loss protection into the main trading loop with highest priority

Technical Implementation

  • TradingSettings.cs: Added MaximumLossPercentage property (optional, nullable decimal)
  • TradingService.cs:
    • Added CalculateCurrentLossPercentage() method to compute real-time portfolio loss
    • Added ShouldTriggerMaximumLossProtection() method for threshold checking
    • Added PlaceMarketSellOrder() method for immediate market execution
    • Integrated protection logic at the beginning of the trading loop (highest priority)
    • Added comprehensive logging with LogCritical for maximum loss events

Configuration Examples

  • Updated appsettings.TMON.json and appsettings.TRUR.json with MaximumLossPercentage: 10.0 (10% loss threshold)

🔧 How It Works

  1. Real-time Monitoring: On every market data update, the bot calculates current portfolio loss percentage
  2. Threshold Check: Compares current loss against the configured MaximumLossPercentage
  3. Emergency Action: When threshold is exceeded:
    • Cancels all active buy/sell orders
    • Places market sell order for all owned lots
    • Clears all position tracking
    • Logs critical protection event
  4. Immediate Execution: Uses market orders (not limit orders) to ensure immediate execution at current market price

📊 Loss Calculation Formula

Average Purchase Price = Total Cost / Total Lots
Current Portfolio Value = Current Market Price × Total Lots
Loss Percentage = ((Total Cost - Current Value) / Total Cost) × 100

🛡️ Safety Features

  • Optional feature (only activates if MaximumLossPercentage is configured)
  • Comprehensive logging for all protection events
  • Fail-safe design: continues normal operation if setting is not configured
  • Priority execution: Loss protection runs before any other trading logic

🎛️ Configuration

Add to your appsettings.json:

{
  "TradingSettings": {
    ...
    "MaximumLossPercentage": 10.0
  }
}

✅ Testing

  • ✅ Code compiles successfully
  • ✅ No breaking changes to existing functionality
  • ✅ Backward compatible (optional feature)
  • ✅ Comprehensive error handling

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #225
@konard konard self-assigned this Sep 11, 2025
konard and others added 2 commits September 11, 2025 11:55
- Add MaximumLossPercentage setting to TradingSettings
- Implement loss calculation based on average purchase price vs current market price
- Add market sell order functionality for immediate execution
- Integrate maximum loss protection into main trading loop
- When loss exceeds threshold, cancel all orders and sell at market price
- Add configuration examples with 10% maximum loss threshold
- Add comprehensive logging for maximum loss protection events

Fixes #225

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Trader Bot: Sell on market price if maximum loss passed Trader Bot: Sell on market price if maximum loss passed Sep 11, 2025
@konard konard marked this pull request as ready for review September 11, 2025 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trader Bot: Sell on market price if maximum loss passed

2 participants