Skip to main contentSkip to navigation
Back to Projects
backend
20212 min read

Robinhood Stock Trading Bot

A Python trading bot with configurable strategies and position limits

Python
Robinhood API
Automation

The Challenge

I wanted to build a system that could apply trading rules consistently. Generating a buy or sell signal was only part of the work. The bot also needed to size positions, keep a record of its trades, and provide a way to test strategies before placing real orders.

Key Challenges

  • 1Connecting to Robinhood through an unofficial API wrapper
  • 2Market data needs to be processed in real-time
  • 3Limiting position sizes and tracking daily profit and loss
  • 4Backtesting strategies requires historical data

The Solution

The Python bot uses an unofficial Robinhood API wrapper and supports momentum and mean-reversion strategies. Position sizing depends on portfolio value and configured limits. Trade logs and a paper trading mode provide ways to inspect what a strategy is doing.

Key Features

  • Multiple configurable trading strategies (momentum, mean reversion)
  • Real-time market data processing
  • Position sizing based on portfolio value and risk tolerance
  • Trade logging for analysis
  • Paper trading mode for strategy testing

Technical Highlights

Risk Management Module

Calculates a share count from a position-value limit and checks daily profit and loss against a threshold

python
class RiskManager:
    def __init__(self, max_position_pct=0.05, max_daily_loss_pct=0.02):
        self.max_position_pct = max_position_pct
        self.max_daily_loss_pct = max_daily_loss_pct
        self.daily_pnl = 0
        
    def calculate_position_size(self, portfolio_value, entry_price):
        """Calculate safe position size based on risk parameters"""
        max_position_value = portfolio_value * self.max_position_pct
        shares = int(max_position_value / entry_price)
        return max(shares, 0)
    
    def can_trade(self, portfolio_value):
        """Check if we've hit daily loss limit"""
        max_loss = portfolio_value * self.max_daily_loss_pct
        return abs(self.daily_pnl) < max_loss

The Results

Key Outcomes

  • Implemented configurable trading rules in Python
  • Added error handling around the trading workflow
  • Recorded trades for later analysis

Lessons Learned

1

Backtesting and paper trading help inspect a strategy, but neither establishes how it will perform in live trading

2

An unofficial API adds a dependency that can change outside the bot's control

3

Trade logs make it possible to inspect decisions after the bot has made them