File: //opt/trading-bot/README.md
# Crypto Trading System (Phase 1)
An automated crypto trading system focusing on a market-neutral cash-and-carry strategy.
> [!NOTE]
> Paper trading is the default mode. Live trading is NOT implemented or enabled in Phase 1.
## Features
- **Cash-and-Carry Strategy**: Exploits funding rate differentials between spot and perpetual markets.
- **Risk Engine**: Enforces hard safety limits on exposure, daily loss, and drawdown.
- **Paper Trading**: Default mode for safe testing with simulated fills, slippage, and fees.
- **MySQL Backend**: Robust storage using SQLAlchemy for snapshots, signals, orders, and portfolio state.
- **FastAPI Monitoring**: Local HTTP endpoints for status, portfolio, and manual "FLATTEN" commands.
- **Split Ingestion Loops**: High-cadence price polling and low-cadence funding polling.
## Setup
### 1. Prerequisites
- Python 3.10+
- MySQL Server
### 2. Installation
```bash
pip install -r requirements.txt
```
### 3. Configuration
Copy `.env.example` to `.env` and fill in your MySQL details and exchange API keys (keys are optional for paper trading if using public data).
```bash
cp .env.example .env
```
### 4. Database Setup
Create the database (name: `TradingSystem`) and the system will automatically create the tables on the first run.
## Running the System
### Start the Bot
```bash
python main.py
```
This starts:
- Market Snapshot Loop (Prices)
- Funding Snapshot Loop
- Trading Runner (Strategy -> Risk -> Execution)
- FastAPI Monitoring Server (Port 8000)
### Monitoring
- **Status**: `GET http://localhost:8000/status`
- **Portfolio**: `GET http://localhost:8000/portfolio`
- **Positions**: `GET http://localhost:8000/positions`
- **Flatten All**: `POST http://localhost:8000/flatten`
## Verification
### Running Tests
```bash
pytest tests/
```
### Manual Flatten
You can manually close all positions via the CLI or the `/flatten` endpoint.
```bash
curl -X POST http://localhost:8000/flatten
```
## Safety Controls
- **Exposure Cap**: Total USD value across all positions.
- **Daily Loss Limit**: Halts trading if realized loss exceeds the threshold.
- **Drawdown Throttle**: Automatically reduces sizing when drawdown exceeds the threshold.
- **Stale Data Kill-switch**: Automatically stops trading if market data is older than the configured threshold.
- **Manual Flatten**: Immediate closure of all positions.