← Back to Projects

Buffet

Trading AI: paper and live trading on Alpaca, strategy optimization, signal processing, and Qiskit-based quantum optimization. Python, TensorFlow, Alpaca API.

View on GitHub →

Overview

Buffet is an automated trading system that runs a Momentum strategy (RSI, MACD, Bollinger Bands) on crypto or equities, re-optimizes parameters periodically via Bayesian or quantum-kernel search, and executes paper or live market orders on Alpaca with configurable risk (take-profit, stop-loss, position sizing).

Description

Buffet is an automated trading algorithm that derives, backtests, and optimizes strategies and runs them in paper or live mode on Alpaca. It trades a single symbol (e.g. BTC/USD) with no margin: sizing is driven by account buying power and configurable starting capital. The system runs on a fixed schedule (e.g. hourly): each cycle it may re-optimize strategy parameters over a recent window of bars, then evaluate the latest market state and optionally submit a market order (long or short) whose size is derived from signal strength and account state. Demonstrates end-to-end quant workflow: data pipeline, strategy design, classical and quantum-inspired optimization, and broker API integration.

What it is and what it does? The core loop has two phases: parameter update and trade execution. In the parameter-update phase the system fetches historical bars (crypto or stock via Alpaca’s CryptoBarsRequest or StockBarsRequest), computes technical indicators with TA-Lib (Bollinger Bands, MACD, RSI, and an EMA of RSI), and runs a backtest with the current or candidate parameters. That backtest uses a Momentum strategy that combines RSI, MACD, and Bollinger Band signals into a single weighted score; positions are opened and closed according to that score and are liquidated when price hits a take-profit or stop-loss percentage. The backtest produces a PnL and ROI. The system then runs an optimizer over a parameter space (RSI thresholds, position size, take-profit, stop-loss, and the weights of the three indicators). Two optimizers are implemented: a classical Bayesian optimizer that maximizes backtest ROI over that space, and a quantum-inspired optimizer that uses a Qiskit-built feature map (Hadamard plus Rz rotations), a kernel matrix from statevector overlaps, and a UCB-style acquisition to propose and evaluate candidate parameter vectors; the objective for both is the same backtest ROI. If the optimized parameters yield a higher backtest ROI than the default parameters, the system adopts them for the next cycle; otherwise it keeps the previous set. In the execution phase the system fetches the most recent bars, runs the Momentum strategy on the latest bar only to get a single combined signal, and—if the signal is long or short and the system has already completed at least one parameter update—computes an order size from the current account balance, buying power, existing position, and signal strength, then submits a market order (long or short) or a close via Alpaca’s API. If both the default and optimized backtest ROIs are non-positive, the system suppresses trading and does not submit orders.

Capabilities Buffet can run in paper mode (Alpaca paper account) or, with the same code path, against a live account. It supports crypto and stock symbols. The Momentum strategy produces integer signals (e.g. −2 to +2) from RSI (overbought/oversold and crossover with RSI-EMA), MACD (line crossover with signal), and Bollinger Bands (band width diverging or converging); these are combined with configurable weights. Position sizing is rule-based: it respects a maximum position size as a fraction of account value and, for shorts, the current position so that the order does not exceed what is held. Optional risk modules provide Kelly-criterion optimal fraction, Sharpe ratio, and a dynamic order size that uses historical returns and risk-adjusted scaling. Trades from the backtest are recorded (e.g. to CSV) for analysis. The scheduler (e.g. APScheduler BlockingScheduler) runs parameter updates and execution on an interval (e.g. hourly), so the system can run unattended.

Implementation

Data and indicators: Historical bars are requested from Alpaca for a configurable range and timeframe (e.g. 31 days, 1H). TA-Lib computes BBANDS (20-period), MACD (12/26/9), RSI (14), and a 14-period EMA of RSI. The resulting DataFrame is extended with RSI_signal, MACD_flip, and BB_diverging; the backtester folds these into a single Signal column (weighted sum) and steps through time—opens long/short when Signal is non-zero, closes on take-profit or stop-loss. PnL and ROI are aggregated; trades can be written to disk.

Strategy: The Momentum class holds the DataFrame and RSI/MACD/BB parameters and weights. It computes per-row RSI, MACD, and BB signals; evaluate_indicators() fills signal columns; evaluate_latest() returns the weighted sum for the last row. Order size is derived from balance, signal, max position fraction, and current position, with separate caps for long (buying power, max position) and short (current position).

Optimization: BasicOptimizer wraps BayesianOptimization over the parameter space; each evaluation runs Momentum + backtest and returns ROI. QuantumOptimizer uses a Qiskit feature map (one qubit per parameter), builds a kernel from statevector simulation, and uses UCB acquisition with scipy.optimize.minimize; objective is again backtest ROI. The best parameters are re-backtested; if ROI beats the default set, they become active for the next cycle.

Execution: Alpaca is used via alpaca-py (TradingClient). The execution path fetches the latest bars, builds Momentum, takes evaluate_latest() as the signal, and—if long/short and parameters are updated—computes order size from position and buying power and submits a MarketOrderRequest. A lock serializes parameter updates and trade execution.

Demo

No live demo available for this project. See the repo for setup and run instructions.

Tech & tools

  • Python
  • TensorFlow
  • Alpaca API
  • TA-Lib
  • Bayesian optimization
  • Qiskit

Highlights

  • Paper and live trading on Alpaca brokerage
  • Strategy optimization, backtesting, and parameter tuning
  • Signal processing to filter noise in price data
  • Qiskit-based quantum-inspired optimization

More projects

WorldWide Rover

HackUMass XII — Best Embedded System. Autonomous, globally accessible rover: web interface for real-time control, maze navigation, obstacle avoidance. Python/Flask backend, Raspberry Pi, Arduino, React, ngrok.

Pong on Assembly

Interactive Pong on FPGA (Duke ECE 550). Hardware VGA/PS2 implementation plus full assembly version (pong.s) for custom 32-bit processor ISA.

Custom Single-Cycle 32-Bit Processor

Custom ISA and single-cycle datapath in Verilog; synthesis-ready design used as the execution target for Pong assembly (Pong-on-Assembly repo).

Baretag iOS App

iOS app (Swift) for real-time device tracking and management; Flask/Ngrok backend and 3D localization validated in Blender.

Multilateration 3D Positioning

3D multilateration and localization algorithms with Python implementation and Blender-based simulation; feeds into Baretag device positioning.

RISC-V Encryption

Low-level encryption on RISC-V: assembly and C implementations for algorithms and analysis on a RISC-V toolchain.

PUF Tester

Hardware tooling for testing and characterizing Physically Unclonable Functions (PUFs); supports security and fingerprinting use cases.

tinyTemp

Temperature display on ATtiny85: reads sensor via ADC and shows °F and °C on SSD1306 OLED (ECE 304, Spring 2024).

WorldWide Rover

HackUMass XII — Best Embedded System. Autonomous, globally accessible rover: web interface for real-time control, maze navigation, obstacle avoidance. Python/Flask backend, Raspberry Pi, Arduino, React, ngrok.

Pong on Assembly

Interactive Pong on FPGA (Duke ECE 550). Hardware VGA/PS2 implementation plus full assembly version (pong.s) for custom 32-bit processor ISA.