This detailed breakdown covers the lifecycle of a trade within the application, from signal parsing through multi-order placement, dynamic TP adjustments, breakeven logic, modification handling, and final closure synchronization. The use of strategies, order groups, and scenario-based adjustments are key complexities.

I. Signal Reception and Processing

  1. Source: Trading signals are received as text messages from a specific Telegram channel, identified by TELEGRAM_CHANNEL_ID in the configuration.
  2. Detection: The TelegramClient monitors the designated channel for new messages (events.NewMessage).
  3. Initial Storage: Upon receiving a new message:
  4. Parsing:
  5. Parsing Outcome & Status Update:
  6. Processing Queue: A separate process (process_new_trades) periodically polls the database for signals with status = SignalStatus.PARSED.

II. New Trade Execution (action == "new_trade")

  1. Trigger: A signal with status == SignalStatus.PARSED and action == "new_trade" is picked up by the trade_processor.
  2. Signal Status: The signal status is immediately updated to SignalStatus.PROCESSING.
  3. Strategy Selection: The application uses a trading strategy determined by the CHANNEL_STRATEGY configuration setting (e.g., DefaultStrategy, GTMOStrategy, BENStrategy, BENGabrielStrategy).
  4. Trade Record Creation:
  5. Parameter Calculation:
  6. Order Definition:
  7. Order Placement:
  8. Database Updates (Orders & Positions):
  9. Final Trade Update: