Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# AGI Trading Platform - Environment Variables Template
# Copy to .env and fill in your values

# ─── Exchange Credentials ──────────────────────────────────────────────────
# Alpaca (paper trading by default)
ALPACA_API_KEY=your_alpaca_api_key_here
ALPACA_SECRET_KEY=your_alpaca_secret_key_here

# Binance (testnet by default)
BINANCE_API_KEY=your_binance_api_key_here
BINANCE_SECRET_KEY=your_binance_secret_key_here

# ─── Database ──────────────────────────────────────────────────────────────
TRADING_DB_HOST=localhost
TRADING_DB_PORT=5432
TRADING_DB_NAME=trading
TRADING_DB_USER=trading_user
TRADING_DB_PASSWORD=changeme

# ─── Redis ─────────────────────────────────────────────────────────────────
TRADING_REDIS_HOST=localhost
TRADING_REDIS_PORT=6379
TRADING_REDIS_PASSWORD=

# ─── Risk Management ───────────────────────────────────────────────────────
TRADING_RISK_MAX_POSITION_SIZE_USD=100000.0
TRADING_RISK_MAX_PORTFOLIO_DRAWDOWN_PCT=10.0
TRADING_RISK_MAX_ORDER_SIZE_USD=50000.0
TRADING_RISK_DAILY_LOSS_LIMIT_USD=20000.0

# ─── Logging ───────────────────────────────────────────────────────────────
TRADING_LOG_LEVEL=INFO
TRADING_LOG_SERIALIZE=false

# ─── AGI Configuration ─────────────────────────────────────────────────────
TRADING_AGI_ENABLED=true
TRADING_AGI_CONFIDENCE_THRESHOLD=0.7

# ─── Infrastructure ────────────────────────────────────────────────────────
GRAFANA_PASSWORD=admin
DB_PASSWORD=changeme
89 changes: 89 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: CD

on:
push:
branches: [main]
tags: ['v*']

permissions:
contents: read

jobs:
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
environment: staging
if: github.ref == 'refs/heads/main'
permissions:
contents: read
steps:
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to container registry
uses: docker/login-action@v3
with:
registry: ${{ secrets.REGISTRY_URL }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Build and push trading-engine
uses: docker/build-push-action@v5
with:
context: .
file: docker/Dockerfile.trading-engine
push: true
tags: |
${{ secrets.REGISTRY_URL }}/trading-engine:staging
${{ secrets.REGISTRY_URL }}/trading-engine:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Deploy to staging Kubernetes
run: |
echo "Deploying to staging..."
# kubectl apply -f infrastructure/kubernetes/deployments/
env:
KUBECONFIG_DATA: ${{ secrets.STAGING_KUBECONFIG }}

deploy-production:
name: Deploy to Production
runs-on: ubuntu-latest
environment: production
needs: [deploy-staging]
if: startsWith(github.ref, 'refs/tags/v')
permissions:
contents: read
steps:
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to container registry
uses: docker/login-action@v3
with:
registry: ${{ secrets.REGISTRY_URL }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Build and push trading-engine
uses: docker/build-push-action@v5
with:
context: .
file: docker/Dockerfile.trading-engine
push: true
tags: |
${{ secrets.REGISTRY_URL }}/trading-engine:latest
${{ secrets.REGISTRY_URL }}/trading-engine:${{ github.ref_name }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Deploy to production Kubernetes
run: |
echo "Deploying to production..."
# kubectl apply -f infrastructure/kubernetes/deployments/
env:
KUBECONFIG_DATA: ${{ secrets.PRODUCTION_KUBECONFIG }}
90 changes: 90 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: CI

on:
push:
branches: [main, 'copilot/**']
pull_request:
branches: [main]

permissions:
contents: read

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: pip

- name: Install linting dependencies
run: |
pip install ruff mypy

- name: Run ruff
run: ruff check . --ignore E501

- name: Run mypy (shared layer only)
run: mypy shared/ --ignore-missing-imports --no-strict-optional
continue-on-error: true

test:
name: Test
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: pip

- name: Install dependencies
run: |
pip install pydantic>=2.6.0 pydantic-settings>=2.2.0 loguru>=0.7.0 pyyaml>=6.0.1
pip install pytest>=8.0.0 pytest-asyncio>=0.23.0 pytest-cov>=4.1.0 pytest-mock>=3.12.0

- name: Run unit tests
run: |
pytest tests/unit/ -v --cov=trading_engine --cov=shared --cov-report=xml
env:
PYTHONPATH: ${{ github.workspace }}

- name: Upload coverage
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage.xml
if: always()

build:
name: Build Docker Image
runs-on: ubuntu-latest
needs: [lint, test]
permissions:
contents: read
steps:
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build trading-engine image
uses: docker/build-push-action@v5
with:
context: .
file: docker/Dockerfile.trading-engine
push: false
tags: trading-engine:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
__pycache__/
*.pyc
*.pyo
.env
*.egg-info/
dist/
build/
.pytest_cache/
.coverage
coverage.xml
*.log
logs/
.mypy_cache/
.ruff_cache/
node_modules/
130 changes: 129 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,129 @@
# rag7
# AGI Trading Platform

An AI-powered algorithmic trading platform with AGI orchestration, multi-exchange connectivity, real-time risk management, and TimescaleDB-backed market data storage.

> **⚠️ Paper / testnet mode is enabled by default.** No real capital is at risk during development.

---

## Architecture Overview

```
┌─────────────────────────────────────────────────────────┐
│ FastAPI Application (main.py) │
├──────────────┬──────────────┬───────────────────────────┤
│ trading_engine│ dataops │ shared │
│ ├ connectors │ ├ ingestion │ ├ models (Pydantic v2) │
│ ├ execution │ ├ storage │ ├ common (config/logger) │
│ ├ risk_mgmt │ └ processing │ └ exceptions │
│ └ portfolio │ │ │
└──────────────┴──────────────┴───────────────────────────┘
│ │
Alpaca / Binance Redis · Kafka · TimescaleDB
```

| Layer | Responsibility |
|---|---|
| `shared/` | Pydantic models, config (pydantic-settings), structured logging (loguru), exceptions |
| `trading_engine/` | Exchange connectors (Alpaca, Binance), order lifecycle, pre-trade risk, portfolio P&L |
| `dataops/` | WebSocket market data collection, TimescaleDB persistence, Kafka stream processing |

---

## Quick Start

### Prerequisites

- Python 3.12+
- Docker & Docker Compose

### 1. Clone and configure

```bash
cp .env.example .env
# Edit .env with your API keys (leave as placeholders for paper trading)
```

### 2. Start infrastructure

```bash
docker compose up -d postgres redis kafka
```

### 3. Install Python dependencies

```bash
pip install -r requirements.txt
```

### 4. Run the API server

```bash
export PYTHONPATH=$(pwd)
uvicorn main:app --reload --port 8000
```

The API will be available at <http://localhost:8000>.
Interactive docs: <http://localhost:8000/docs>

### 5. Run tests

```bash
pytest tests/unit/ -v
```

---

## Component Reference

### `shared/models/`

| Model | Description |
|---|---|
| `Order` | Full order lifecycle with fills, status transitions |
| `Position` | Open position with entry price and unrealised P&L |
| `Portfolio` | Aggregate portfolio with drawdown tracking |
| `OHLCV` | Candlestick bar |
| `OrderBook` | Level-2 order book snapshot |
| `TradingSignal` | Directional signal from a single model |
| `RiskAssessment` | Pre-trade risk evaluation result |
| `AGIDecision` | Composite action from the AGI orchestrator |

### `trading_engine/`

- **`AlpacaConnector`** – paper/live trading via alpaca-py
- **`BinanceConnector`** – spot trading via python-binance (testnet default)
- **`OrderManager`** – async order registry with exchange sync
- **`RiskEngine`** – configurable pre-trade limit checks
- **`PortfolioManager`** – thread-safe position and P&L tracking

### `dataops/`

- **`MarketDataCollector`** – WebSocket feed with Redis pub/sub fan-out
- **`TimeSeriesDB`** – asyncpg + TimescaleDB for OHLCV and trade storage
- **`StreamProcessor`** – aiokafka producer/consumer with auto-reconnect

---

## Configuration

All settings live in `config.yaml` and can be overridden by environment variables prefixed with `TRADING_`:

```bash
TRADING_DB_HOST=my-db-server
TRADING_RISK_MAX_ORDER_SIZE_USD=25000
TRADING_LOG_LEVEL=DEBUG
```

See `.env.example` for all available variables.

---

## Paper Trading Note

Both exchange connectors default to paper/testnet mode:

- **Alpaca**: `ALPACA_PAPER=true` (default) → paper-api.alpaca.markets
- **Binance**: `BINANCE_TESTNET=true` (default) → testnet.binance.vision

Set these to `false` only when you are ready to trade with real capital and have reviewed all risk limits in `config.yaml`.
Loading