Skip to content

mrin9/tradebot-java

Repository files navigation

Tradebot-Java

Tradebot-Java is a high-performance algorithmic trading engine built with Quarkus (the Supersonic Subatomic Java Framework). It provides a robust, real-time pipeline for market data ingestion, technical indicator calculation, and automated strategy execution, specifically tailored for the XTS API.

🚀 Key Features

  • Live Trading: Real-time socket-based execution with automated instrument subscription.
  • Backtesting: High-fidelity backtesting against historical tick data stored in MongoDB.
  • Data Synchronization: Synchronous master data and historical OHLC sync from XTS.
  • Dynamic Strategies: Pluggable strategy engine with support for complex multi-indicator setups (EMA, VWAP, OBV, etc.).
  • Risk Management: Automated SL/TP, Trailing Stop-Loss (TSL), and Breakeven (BE) logic.
  • Structured Logging: Beautifully formatted, colorized logs for market heartbeats, signals, and trade execution.

🛠 Prerequisites

  • Java: JDK 21 or higher.
  • Build Tool: Maven 3.9+ (included wrapper ./mvnw is recommended).
  • Database: MongoDB 6.0+ (running locally or via URI).
  • API Credentials: Active XTS API credentials (Market and Interactive).

⚙️ Configuration

The project uses a hybrid configuration model:

  1. .env File: Create a .env file in the root directory for secrets (Template in .env.example).
    XTS_ROOT_URL=https://...
    MARKET_API_KEY=your_key
    MARKET_API_SECRET=your_secret
    ...
  2. application.properties: Standard configuration for market constants, strategy defaults, and MongoDB URIs located in src/main/resources/application.properties.

💻 Development Mode

Run the application in dev mode with live reloading:

./mvnw quarkus:dev

To run specific subcommands in dev mode:

./mvnw quarkus:dev -Dquarkus.args="[subcommand] [args]"

🏗 Build & Production Mode

Compile and Package

To build a runnable JAR:

./mvnw clean package

To build without running tests:

./mvnw clean package -DskipTests

This produces the quarkus-run.jar in target/quarkus-app/.

Run in Production

java -jar target/quarkus-app/quarkus-run.jar [subcommand] [args]

Build Uber-JAR (Optional)

./mvnw package -Dquarkus.package.jar.type=uber-jar
java -jar target/*-runner.jar [subcommand] [args]

🚀 Quick Start Examples

1. Synchronization (First Time Setup)

# Sync instrument master data from XTS
./mvnw quarkus:dev -Dquarkus.args="sync-master"

# Sync last 20 days of Nifty history
./mvnw quarkus:dev -Dquarkus.args="sync-history -s 2026-03-19 -e 2026-03-19"

2. Live Trading

# Run live trading with default triple-confirmation strategy
./mvnw quarkus:dev -Dquarkus.args="live"

# Run with specific strategy, budget, and risk parameters
./mvnw quarkus:dev -Dquarkus.args="live --strategy-id triple-confirmation --budget 10-lots --sl 4.0 --targets 3.0 --tsl 0.5 --log-active-indicator"

3. Backtesting

# Run a backtest for a specific date range
./mvnw quarkus:dev -Dquarkus.args="backtest --start 2026-03-19 --end 2026-03-19"

# Run backtest with custom risk parameters
./mvnw quarkus:dev -Dquarkus.args="backtest --start 2026-03-23 --end 2026-03-25 --sl 4.0 --targets 3.0 --budget 3-lots --use-be --verbose"

4. API Server (Default Mode)

The REST API server is the default entry point. If you start the application without any subcommand, it will stay alive to serve the UI on port 8080.

🧩 Totally Decoupled CLI

The CLI is now fully independent of the API server. When you run a subcommand (like backtest or live), the application automatically disables its internal HTTP server.

This means:

  • You can keep the API server running for the UI in one terminal.
  • You can start/stop as many CLI tasks as you want in other terminals.
  • No port conflicts (8080 is reserved only for the base API process).
Goal Command API Server Status
Serve UI / REST java -jar .../quarkus-run.jar Enabled (Port 8080)
Live Trading java -jar .../quarkus-run.jar live Disabled (Task only)
Backtest java -jar .../quarkus-run.jar backtest ... Disabled (Task only)
Data Sync java -jar .../quarkus-run.jar sync-master Disabled (Task only)

Tip

To see live updates in the UI while running a CLI task, simply keep one terminal running the base API server. The UI will fetch the latest state from MongoDB as the independent CLI process updates it.

5. Debugging

# Test XTS socket connectivity and watch incoming stream (reduced output)
./mvnw quarkus:dev -Dquarkus.args="debug-socket --print-mode reduced"

📜 CLI Subcommands

Tradebot-Java is driven by a powerful CLI.

Subcommand Description Example Usage
live Run live trading engine ... live --strategy-id triple-confirmation --budget 10-lots --log-active-indicator
backtest Run historical backtest ... backtest --start 2026-03-23 --end 2026-03-25 --budget 3-lots
sync-master Sync instrument master data ... sync-master
sync-history Sync historical OHLC data ... sync-history -s 2026-03-23 -e 2026-03-25
analyze Analyze crossovers/indicators ... analyze -i NIFTY -d 2026-03-25
debug-socket Test XTS socket connectivity ... debug-socket --print-mode reduced

🧪 Testing

The project uses JUnit 5 and Mockito for testing.

  • Run all tests:
    ./mvnw test
  • Run specific test package:
    ./mvnw test -Dtest=org.tradebot.logic.*

Other Commands

   ./mvnw clean package -DskipTests
   ./mvnw checkstyle:check
   ./mvnw checkstyle:checkstyle   <<< generates html report under target/reports
   ./mvnw spotbugs:check
   ./mvnw spotbugs:spotbugs <<< generates html report under target/reports
   ./mvnw rewrite:run 
   ./mvnw test 
    java -jar target/quarkus-app/quarkus-run.jar backtest --start 2026-03-18
    java -jar target/quarkus-app/quarkus-run.jar live --log-active-indicator

🏗 Architecture Overview

  • org.tradebot.core: Orchestration logic and CLI runners.
  • org.tradebot.services: Core business logic (Ingestion, Resampling, Strategy, Position Mgmt).
  • org.tradebot.xts: Low-level XTS API client and socket handling.
  • org.tradebot.models: Domain models and event definitions.
  • org.tradebot.repository: MongoDB data access layer.

For more information on Quarkus, visit quarkus.io.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages