Skip to content

nixikanius/trading-bot

Repository files navigation

Trading Bot

Bot service for automated trading through exchange brokers API based on trading signals (e.g. from TradingView).

Features

  • Multiple Broker Support - Handle multiple brokers (T-Investments and Finam)
  • Multiple Account Support - Handle multiple trading accounts with individual broker configuration
  • Automatic Position Reconciliation - Smart position management to match signal state
  • Position Size Calculation - Dynamic sizing based on available capital and leverage
  • Stop-Loss & Take-Profit - Automatic order placement with price validation
  • Reserve Capital Support - Configurable capital reserves per strategy
  • Profit & Slippage Tracking - Calculate and report trading performance metrics
  • Instrument Auto-Detection - Automatic currency and instrument type detection
  • Telegram Notifications - Real-time trading notifications with detailed order information
  • Asynchronous Signal Processing - Non-blocking signal processing with ThreadPoolExecutor
  • Smart Queue Management - One signal per account+instrument, with intelligent queuing and replacement
  • Context-Aware Logging - Unique signal IDs for tracking across threads and modules
  • Comprehensive Timing - Track queue wait time, processing time, and total system time

Trading Logic

Signal Processing Flow

  1. Signal Reception: Bot receives trading signal via webhook
  2. Queue Management: Signal is queued for processing
  3. Asynchronous Processing: Signal is processed in background thread
  4. Position Analysis: Determines current position for instrument
    • If current trading position is equal to the signal position, the signal is ignored
  5. Size Calculation: Calculates position size based on available funds, reserve capital and leverage
  6. Order Execution: Places market orders to ensure position:
    • Opens long position (with closing short position if exists)
    • Opens short position (with closing long position if exists)
    • Closes all positions (flat)
  7. Risk Management: Places stop-loss and take-profit orders
  8. Performance Tracking: Calculates profit, slippage and other metrics
  9. Notifications: Sends Telegram notifications with results

Queue Logic

  • One Signal Per Key: Only one signal can be processed at a time per account+instrument
  • Smart Queuing: New signals wait if one is already processing
  • Signal Replacement: New signals replace waiting signals (not processing ones)
  • No Interruption: Processing signals complete fully before next one starts

Installation

  1. Clone the repository:
git clone https://github.com/nixikanius/traiding-bot
cd traiding-bot
  1. Prepare config:
cp config.example.yml config.yml

Edit config.yml with your settings.

Running

Docker

docker run -it --name trading-bot \
  -p 8000:8000 \
  -v $(PWD)/config.yml:/app/config.yml:ro \
  nixikanius/trading-bot:latest

Server starts on http://127.0.0.1:8000

Local Development

# Install dependencies
make install

# Run development server with auto-reload
make run

Debug Logging: Set log_level: "DEBUG" in config.yml to see detailed request/response logging for debugging purposes.

HTTP API Endpoints

Health Check

GET /healthz

Send Signal

POST /signals/enqueue/<account>

Where <account> is the account name from config (the key in the accounts dictionary).

Queue Status

GET /signals/queue

Returns current signal queue status with processing and waiting signals.

Trading Signal Format

Each signal can include the following parameters:

  • signal_id - unique signal ID (optional, generated by the bot if not provided)
  • position - position state: "long", "short", or "flat"
  • bar_index - bar index (optional)
  • entry_time - signal entry time in ISO 8601 format to calculate slippage (optional)
  • entry_price - signal entry price to calculate slippage (optional)
  • stop_price - stop-loss price (optional)
  • limit_price - take-profit price (optional)
  • reserve_capital - additional sum of money added to available funds for position size calculation (in instrument currency, default: 0)
  • capital_leverage_percent - capital percentage used for position size calculation (default: 100)
  • instrument - instrument code (FIGI for T-Investments, symbol for Finam)

Long Position

{
  "position": "long",
  "bar_index": 20567,
  "entry_time": "2025-09-26T17:30:00",
  "entry_price": 290,
  "stop_price": 275,
  "limit_price": 320,
  "reserve_capital": 0,
  "capital_leverage_percent": 100,
  "instrument": "BBG004730N88"
}

Short Position

{
  "position": "short",
  "bar_index": 204112,
  "entry_time": "2025-08-15T10:00:00",
  "entry_price": 2708.5,
  "stop_price": 2750.5,
  "limit_price": 2600,
  "reserve_capital": 6000000,
  "capital_leverage_percent": 250,
  "instrument": "IMOEXF@RTSX"
}

Flat Position (Close All Open Positions)

{
  "position": "flat",
  "bar_index": 10567,
  "entry_time": "2025-09-15T15:15:00",
  "entry_price": 270,
  "figi": "BBG004730N88"
}

Versioning

The project uses A.B.C version format, where change of:

  • A indicates major changes that highly likely break backward compability (be sure to test before upgrade);
  • B indicates changes that may break backward compability (read the changelog and test before upgrade if necessary);
  • C indicates bug fixes, minor improvements or new functionality that do not break backward compability.

License

MIT License

About

Bot service for automated trading

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors