HEX
Server: Apache/2.4.18 (Ubuntu)
System: Linux ubuntu 7.0.5-x86_64-linode173 #1 SMP PREEMPT_DYNAMIC Fri May 8 10:12:05 EDT 2026 x86_64
User: root (0)
PHP: 7.2.28-1+ubuntu16.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
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.