Alpaca Trading Bot

Alpaca paper trading automation

Available at: kctrading.xyz

Issue / Solution

The Issue: I wanted a robust environment to rigorously backtest and live-evaluate systematic trading strategies in real market conditions. However, doing so manually is highly prone to human error, emotional trades, and lag, while executing arbitrary scripts carries the extreme risk of accidentally touching real-money accounts. I needed a secure sandbox with built-in capital safety gates and continuous execution.

The Solution: I built Alpaca Trading Bot, an automated paper-only trading station deployed 24/7 on an Ubuntu VPS. It combines a continuous Python trading loop with a local SQLite state engine and an interactive Streamlit console. The broker wrapper strictly hardcodes Alpaca\'s simulation API (`paper=True`) to make real-money access impossible, while integrating automated Telegram notifications and risk-based circuit breakers for bulletproof safety.

Overview

Alpaca Trading Bot is a single-user paper trading platform built around Alpaca. The project has two long-running surfaces: a background trading bot and a Streamlit dashboard. The bot can keep evaluating markets on a VPS, while the dashboard gives me a browser-based command center for portfolio metrics, strategy allocation, backtests, logs, safety events, and server health.

System Architecture

The system isolates trading decisions, safety controls, and real-time visualization. It runs as two separate systemd services sharing an SQLite ledger in WAL (Write-Ahead Logging) mode.

System Architecture

VULTR UBUNTU VPS (systemd managed) 24/7 Trading Loop Bot Evaluates signals, audits risk, dispatches yfinance / Alpaca API Streamlit Web Dashboard Renders metrics, equity curve, strategy config, backtests SQLite WAL Database (Write-Ahead Logging) AccountSnapshots | Trades | StrategyConfigs | SafetyEvents Alpaca REST API paper=True yfinance API Historical data & snaps Telegram Bot API Alerts, heartbeats Writes Read/Write

Safety First

The broker wrapper hardcodes Alpaca\'s paper mode with paper=True, so real execution is intentionally impossible by design. The system also includes a master kill switch, daily loss limit, max drawdown limit, runaway trade-rate detection, and safety events that require manual re-enable after an automatic stop. This made the project a practical automation experiment instead of an uncontrolled trading script.

What I Built

Dashboard & Controls

The command center is powered by Streamlit, offering rich visual data tables and charts. It communicates with the backend loop through WAL-mode SQLite reads and configuration updates.

Alpaca Trading Bot portfolio dashboard
Streamlit Portfolio Dashboard: Renders real-time server health and simulated account metrics. It provides equity growth charts, open positions with real-time unrealized P&L, local server log monitors, and index market snapshots.
Alpaca Trading Bot strategy allocation and risk controls
Strategy Controls & Risk Settings: This operational board lets me adjust active strategy models, modify capital allocation percentages, set daily loss limits, configure max drawdown thresholds, and engage the instant master kill switch.

Mobile Interface & Telegram Alerts

Continuous VPS execution requires remote monitoring. I built responsive views and custom integrations so that the entire trading engine can be safely managed from a phone on the move.

Alpaca Trading Bot mobile dashboard with P&L by position
Mobile Dashboard Interface: A optimized responsive view of the Streamlit dashboard, letting me monitor symbol allocations, active trade signals, and open position P&L securely from any mobile web browser.
Alpaca Trading Bot Telegram bot trade alerts
Telegram Bot Alerts: The trading bot dispatches immediate push notifications for every trade event. Alerts clearly communicate the strategy module name, direction (BUY/SELL), quantity, symbol, exact execution fill price, total trade value, and overall strategy equity.

Architecture

SQLite is the shared state layer between the bot and dashboard. WAL mode lets the dashboard read state while the bot writes trades, account snapshots, config, logs, API call counts, heartbeats, and safety events. Alpaca handles paper brokerage and market data, yfinance supports market snapshots and backtests, and systemd runs both processes on an Ubuntu Vultr VPS.

I separated trading logic, broker integration, strategy modules, database access, notification delivery, and dashboard UI into different files. That separation made it easier to add strategies without rewriting the dashboard, and easier to keep the dangerous decisions centralized in the broker and safety layers.

Bot

24/7 service

Runs every cycle, checks safety limits, evaluates strategy signals, and writes state.

Dashboard

Streamlit

View-only demo mode, portfolio charts, market snapshot, backtests, logs, and controls.

Result

This became the infrastructure behind my systematic trading experiments. A paper-trading run recorded +15% total P&L while trading through a U.S.-Iran conflict period, which gave me evidence that the system could operate through stressful news conditions while preserving the safety boundary of simulated execution.

Stack

Python 3.12, Streamlit, alpaca-py, pandas, ta, Plotly, SQLite, psutil, yfinance, Telegram Bot API, systemd, Ubuntu, Vultr, uv.

(go back)