Buffet
Automated trading system that tests strategies, learns from market behavior, and executes decisions with built-in risk controls.
Overview
Buffet is built to answer a hard question: can a trading strategy adapt instead of staying static? It combines backtesting, optimization, and live execution so decisions are informed by evidence rather than guesswork. What makes it impressive is the full loop from research to action, with safety-minded controls that keep risk management central.
Demo
No live demo available for this project. See the repo for setup and run instructions.
Description
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
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.Sample run output
testing_bench.py (local run)
python Code/testing_bench.py
Default Parameters Set
Setting Up Tester
Tester Setup Complete
Testing Strategy
Strategy Tested
Analyzing Strategy
Total PnL: $-369.60 (-36.96%)
Strategy Analzed
Setting Up Optimizer
Optimizer Setup Complete
Optimizing Strategy
Strategy Optimized
Total Optimization Time: 6.912077500000123
{'rsi_high': np.float64(34.62951577028834), 'rsi_low': np.float64(3.0666431160756846), 'position_size': np.float64(7862.260745121215), 'take_profit': np.float64(31.977875597816798), 'stop_loss': np.float64(21.439279662417025), 'rsi_weight': np.float64(1.7018136056060968), 'macd_weight': np.float64(1.9141720056574214), 'bb_weight': np.float64(2.0751066753464897)}
Testing Optimized Parameters
Optimized PnL: $8180.95 (104.05%)
Optimized Testing Complete
Excuting Trades using Optimized Parameters
Latest Signal -2.0751066753464897
Order Size: 8738.000000
Run Time: 57.538496416999806Tech & 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
tinyTemp
Custom tiny temperature monitor that evolved from an ATmega328P prototype into a compact ATtiny85 OLED PCB.
WorldWide Rover
Award-winning internet-controlled rover that lets users drive and monitor a physical robot from anywhere in real time.
BareTag Tool Tracker
Smart tool tracking that helps teams instantly find equipment, prevent loss, and stay coordinated in real time.
HeadHunter
AI-powered job search assistant that helps people find better-fit roles faster, with smarter resume matching and cleaner tracking.
Meal Planner
Personal meal planning app that turns “what should I cook?” into a clear weekly routine with pantry awareness and shopping support.
BrickVision
Turns ideas or 3D models into buildable LEGO-style instructions, making custom creations faster and more accessible.
Pong on Assembly
Classic Pong rebuilt from the ground up in hardware and low-level assembly, showing full-stack systems engineering in action.
Custom Single-Cycle 32-Bit Processor
From-scratch 32-bit processor design that brings core computer architecture concepts to life in working hardware.
ThreadLib Disk Scheduler
C++ user-level threading library paired with a concurrent disk scheduler that coordinates requester threads and SSTF service.
Multilateration 3D Positioning
3D location engine that estimates where an object is in space and validates accuracy through realistic simulation.
RISC-V Encryption
Encryption concepts implemented at the hardware-software boundary, showing security logic at instruction level detail.
PUF Tester
Hardware security testing toolkit for measuring unique device fingerprints and reliability under real test conditions.
tinyTemp
Custom tiny temperature monitor that evolved from an ATmega328P prototype into a compact ATtiny85 OLED PCB.
WorldWide Rover
Award-winning internet-controlled rover that lets users drive and monitor a physical robot from anywhere in real time.