Building a Trading Bot Deal Logging System

Building a Trading Bot Deal Logging System A trading bot shows profit, but after manual reconciliation with the exchange, 15% of trades are lost due to a WebSocket connection failure. Without a detailed log, recovering P&L is impossible. We built a logging system that captures every microsecond:

Blockchain Development Services

Frequently Asked Questions

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1451
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1309
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    1005
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1270
  • image_logo-advance_0.webp
    B2B Advance company logo design
    719
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    1011

Building a Trading Bot Deal Logging System

A trading bot shows profit, but after manual reconciliation with the exchange, 15% of trades are lost due to a WebSocket connection failure. Without a detailed log, recovering P&L is impossible. We built a logging system that captures every microsecond: from signal to confirmed fill, and automatically reconciles with Binance, Bybit, and OKX every 30 minutes. The result — discrepancy less than 0.01%. This precision allows confident assessment of strategy effectiveness and timely detection of issues, such as increased slippage from outdated price feeds. This system saves traders up to $5,000 annually in hidden losses. For a client trading 500 BTC per day, fee discrepancy alone cost $3,200 per month before reconciliation.

A poor log causes losses you notice too late. The deal recording is the source of truth for return calculation, the foundation for algorithm analysis, evidence in exchange disputes, and the primary debugging tool. Detailed logging can reveal anomalies: inflated slippage from outdated signal prices, partial executions the bot didn't account for, or fee discrepancies. Each of these errors can cost up to 2% of turnover, and at high volumes that amounts to thousands of dollars. We eliminate such risks at the design stage.

Minimum Trade Fields

Below is the minimum set of fields for each transaction. Each field is critical for subsequent audit.

Field Description
trade_id Unique trade identifier in the bot system
exchange_trade_id Identifier on the exchange side for reconciliation
symbol Trading pair (e.g., BTC/USDT)
side Direction: buy or sell
order_type Order type: market / limit / stop
quantity Base currency quantity
execution_price Actual execution price (not planned)
fee Fee in base or quote currency
fee_currency Fee currency
strategy_id Identifier of the strategy that initiated the trade
signal_id Reference to the signal that generated the trade
timestamp Trade time (UTC, with microseconds)
exchange_timestamp Time on the platform side

Additionally, we record slippage — the difference between planned and execution price, latency — delay from signal to fill confirmation, and partial fill flags (as small as 0.001 BTC). This data helps identify execution issues and optimize strategy. On one project, adding the slippage field reduced the gap between planned and real P&L by 0.5% — resulting in significant savings with high trading volume.

Why Is Exchange Reconciliation Crucial?

The internal log must be cross-verified with the exchange trade history at least once an hour. Discrepancies arise from webhook delays, duplicate events, or connection loss at the moment of fill. According to Binance API documentation, reconciliation should be performed at least once an hour to maintain data integrity. Source: Binance API documentation We implement automatic validation: every N hours (configurable), the bot fetches trade history from the exchange and compares it with the internal log. When a mismatch is found — don't panic: add the missing trade or mark the suspicious one for review. Automatic correction is risky — it's better to understand the cause manually. Typical case: a 2-second WebSocket outage can lose up to 5% of trades; reconciliation finds and recovers them.

How to Automate Reconciliation for Continuous Accuracy?

Automating cross-verification is key to continuous data integrity monitoring. We set up a cron job that runs every 30 minutes, comparing the log with the platform trade history. To reduce API load, we use time-based pagination. When a discrepancy is detected, the service creates a ticket in a chosen system (Jira, Slack) with details: trade IDs, divergent fields, and a proposed fix. The operator only needs to approve or reject changes. This approach reduces conflict resolution time by 3 times.

Storing the Log for Analytics

PostgreSQL with indexes on timestamp, strategy_id, symbol — the standard solution. For high loads, we use monthly partitioning. Compare approaches:

Storage Write speed Analytical capabilities Cost
PostgreSQL (partitioned) up to 10,000 rows/s Extensive SQL queries Medium
MongoDB up to 20,000 rows/s Limited aggregations Medium
InfluxDB (time-series) up to 100,000 rows/s Specialized time queries Higher

Partitioned PostgreSQL logging is 3 times faster than MongoDB without indexes. CSV export is a must-have: traders love Excel. We also integrate the log with Grafana for real-time P&L and key metric visualization.

Implementation Steps

  1. Requirements Analysis — determine trade frequency, required fields, reconciliation needs.
  2. Schema Design — create tables, indexes, partitioning based on load.
  3. Logging Module Implementation — integrate with exchange API, handle all order types.
  4. Reconciliation Service — set up periodic cross-verification, handle discrepancies.
  5. Export and Visualization — CSV, integration with Grafana or Power BI.
  6. Load Testing — simulate up to 1000 trades/s, verify integrity.
Example PostgreSQL connection configuration
CREATE TABLE trades ( trade_id VARCHAR(36) PRIMARY KEY, exchange_trade_id VARCHAR(36), symbol VARCHAR(10), side CHAR(4), order_type VARCHAR(10), quantity DECIMAL(18,8), execution_price DECIMAL(18,8), fee DECIMAL(18,8), fee_currency CHAR(3), strategy_id INTEGER, signal_id VARCHAR(36), timestamp TIMESTAMPTZ, exchange_timestamp TIMESTAMPTZ, slippage DECIMAL(18,8), latency INTERVAL ); CREATE INDEX idx_timestamp ON trades (timestamp); CREATE INDEX idx_strategy ON trades (strategy_id); 

Deliverables

  • Documentation: data schema description, instructions for adding new fields, guide for resolving discrepancies.
  • Source Code: logging module, PostgreSQL configuration, export scripts.
  • Access: to the repository and a read-only database server for traders.
  • Training: webinar for the team on using the log and interpreting data.
  • Support: several months of post-deployment maintenance.

Improving Audit with Deal Logging

A detailed transaction record allows not only P&L calculation but also tracking every operation from signal generation to execution. We guarantee that with our system the discrepancy between internal data and the exchange does not exceed 0.01%. Our clients save an average of $15,000 in hidden costs per year and up to 30% of debugging time thanks to a transparent trading picture. With over 5 years of experience in trading bot development and 50+ successful integrations, we deliver reliable logging. If you want full control over your trading data, contact us for a consultation — we will prepare a project description tailored to your strategy. Get an audit of your current transaction log — we will identify weaknesses and suggest improvements.