Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 11, 2025

🤖 AI-Powered Solution

This pull request implements automatic profit calculation for the TraderBot, displaying both absolute and relative profit values as requested in issue #201.

📋 Issue Reference

Fixes #201

🚀 Implementation Details

New Components:

  • ProfitCalculationService: Core service for calculating profits using FIFO (First-In-First-Out) methodology
  • ProfitAnalysis: Data model containing comprehensive profit metrics
  • TradeProfit: Model representing individual completed trades

Key Features:

  • Absolute Profit: Displays monetary profit/loss in the trading currency
  • Relative Profit: Shows percentage-based profit/loss calculations
  • Realized vs Unrealized: Separates completed trades from open positions
  • FIFO Matching: Uses industry-standard First-In-First-Out method for trade matching
  • Current Market Integration: Calculates unrealized profits using live market prices
  • Detailed Trade History: Tracks individual trade profitability and duration

Profit Metrics Displayed:

  • Total Absolute Profit (realized + unrealized)
  • Total Relative Profit (percentage)
  • Realized Absolute Profit from completed trades
  • Realized Relative Profit percentage
  • Unrealized Absolute Profit from open positions
  • Unrealized Relative Profit percentage
  • Open position statistics (quantity, average buy price, total invested)
  • Recent completed trades with individual profit metrics

Integration Points:

  • Integrated into TradingService.SyncLots() method for regular profit updates
  • Uses existing operation tracking infrastructure
  • Leverages current market price from order book data
  • Logs comprehensive profit analysis during bot operation

🧪 Testing

  • Comprehensive unit test suite with 6 test scenarios
  • Tests cover empty operations, buy-only scenarios, complex FIFO matching
  • Validates both profitable and loss scenarios
  • Ensures accurate percentage calculations

📊 Usage Example

The bot now automatically logs profit analysis like this:

=== PROFIT ANALYSIS ===
Total Absolute Profit: 245.5000
Total Relative Profit: 12.34%
--- Realized Profit ---
Realized Absolute Profit: 180.0000
Realized Relative Profit: 15.00%
--- Unrealized Profit ---
Unrealized Absolute Profit: 65.5000
Unrealized Relative Profit: 8.76%
--- Position Info ---
Open Position Quantity: 25
Total Invested in Open Positions: 2750.0000
Average Buy Price: 110.0000
Completed Trades: 8
--- Recent Completed Trades ---
Trade: 10 lots @ 100.0000 -> 115.0000, Profit: 150.0000 (15.00%), Period: 45.2min
...
=======================

✅ Test Results

All 6 unit tests pass successfully:

  • ✅ Empty operations handling
  • ✅ Buy-only operations with unrealized profit
  • ✅ Buy and sell operations with realized profit
  • ✅ Complex FIFO matching scenarios
  • ✅ Profitable trade calculations
  • ✅ Loss trade calculations

This PR was implemented by the AI issue solver

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

Issue: #201
@konard konard self-assigned this Sep 11, 2025
konard and others added 2 commits September 11, 2025 12:45
- Add ProfitCalculationService with comprehensive profit analysis
- Calculate realized and unrealized profits using FIFO method
- Display absolute profit amounts and relative percentages
- Integrate with existing TradingService to show profit metrics
- Add comprehensive unit tests covering various scenarios
- Support both absolute (monetary) and relative (percentage) profit values
- Log detailed profit analysis including completed trades and open positions

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

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Trader: add automatic profit calculation (both in relative and absolute values) Trader: add automatic profit calculation (both in relative and absolute values) Sep 11, 2025
@konard konard marked this pull request as ready for review September 11, 2025 09:52
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: add automatic profit calculation (both in relative and absolute values)

2 participants