The Robinhood AI Trading Bot is a simple Python script that combines OpenAI's intelligence with Robinhood's trading capabilities to help automate and optimize stock trading decisions. By analyzing Relative Strength Index (RSI), Volume-Weighted Average Price (VWAP), Moving Averages, and Robinhood analyst ratings, the bot generates buy, sell, and hold recommendations โ executing trades automatically based on your selected mode.
This project is an experiment to explore how AI can enhance stock trading decisions โ potentially outperforming human traders (or at least me, The Bot Father).
- Start in Demo or Manual Mode before enabling Auto Mode.
- Test thoroughly to fine-tune AI decision-making.
- Monitor trade logs to understand AI-driven actions.
โ
AI-Driven Trading โ Smart, data-backed buy/sell decisions.
โ
Portfolio & Watchlist Integration โ Trade directly from your Robinhood stocks.
โ
Configurable Strategy โ Set trading parameters to fit your risk profile.
โ
Exclusion List โ Prevent trading specific stocks.
โ
Logging & Analytics โ Track bot activity and trading history.
โ
Workday Scheduling โ Align trading activity with market hours.
- Connect Your Accounts: Add your OpenAI API Key and Robinhood credentials.
- Choose a Mode:
- Demo Mode: Simulates trades without execution.
- Manual Mode: Requires confirmation before executing trades.
- Auto Mode: Executes trades automatically (recommended only after testing).
- Monitor and Adjust: Review trade logs and fine-tune settings for optimal performance.
- Authenticate: Logs into OpenAI and Robinhood.
- Fetch Data: Retrieves stocks from your portfolio and watchlist.
- Analyze Market Conditions:
- RSI: Determines overbought/oversold conditions.
- VWAP: Identifies undervalued/overvalued stocks.
- Moving Averages: Evaluates price trends (50-day and 200-day).
- Analyst Ratings: Incorporates Robinhoodโs expert opinions.
- AI-Driven Decisions: Uses OpenAI to generate trading recommendations.
- Trade Execution: Buys, sells, or holds stocks based on AI insights.
- Continuous Monitoring: Repeats analysis and trades as the market evolves.
- Measures momentum on a 0-100 scale.
- Above 70: Overbought (potential sell signal).
- Below 30: Oversold (potential buy signal).
- Calculates the average price adjusted for volume.
- Above VWAP: Overvalued (potential sell signal).
- Below VWAP: Undervalued (potential buy signal).
- 50-day & 200-day moving averages help detect trends.
- Golden Cross (50-day crosses above 200-day): Bullish signal.
- Death Cross (50-day crosses below 200-day): Bearish signal.
- Aggregates buy, hold, and sell recommendations.
- Provides sentiment analysis based on expert insights.
The bot formulates decisions using OpenAI based on:
- RSI, VWAP, moving averages, and analyst ratings.
- User-defined constraints (e.g., budget, stock exclusions, portfolio size).
**Context:**
Today is 2025-02-03T12:23:02Z.
You are a short-term investment advisor managing a stock portfolio.
You analyze market conditions every 3600 seconds and make investment decisions.
**Constraints:**
- Initial budget: 0.22 USD
- Max portfolio size: 20 stocks
- Excluded stocks: VOO, SPY, IVV
**Stock Data:**
```json
{
"AAPL": {
"current_price": 226.79,
"my_quantity": 0.0927,
"my_average_buy_price": 226.43,
"rsi": 40.47,
"vwap": 228.41,
"50_day_mavg_price": 240.23,
"200_day_mavg_price": 219.7,
"analyst_summary": {
"num_buy_ratings": 30,
"num_hold_ratings": 17,
"num_sell_ratings": 5
},
"analyst_ratings": [
{
"published_at": "2025-02-01T01:13:18Z",
"type": "sell",
"text": "Regulators have a keen eye on Apple, and recent regulations have chipped away at parts of Apple\u2019s sticky ecosystem. "
},
{
"published_at": "2025-02-01T01:13:18Z",
"type": "sell",
"text": "Apple\u2019s supply chain is highly concentrated in China and Taiwan, which opens up the firm to geopolitical risk. Attempts to diversify into other regions may pressure profitability or efficiency."
},
{
"published_at": "2025-02-01T01:13:18Z",
"type": "sell",
"text": "Apple is prone to consumer spending and preferences, which creates cyclicality and opens the firm up to disruption."
},
{
"published_at": "2025-02-01T01:13:18Z",
"type": "buy",
"text": "Apple has a stellar balance sheet and sends great amounts of cash flow back to shareholders."
},
{
"published_at": "2025-02-01T01:13:18Z",
"type": "buy",
"text": "We like Apple\u2019s move to in-house chip development, which we think has accelerated its product development and increased its differentiation. "
},
{
"published_at": "2025-02-01T01:13:18Z",
"type": "buy",
"text": "Apple offers an expansive ecosystem of tightly integrated hardware, software, and services, which locks in customers and generates strong profitability."
}
]
},
"MSFT": {
"current_price": 411.67,
"my_quantity": 0.0509,
"my_average_buy_price": 412.57,
"rsi": 32.53,
"vwap": 411.85,
"50_day_mavg_price": 431.36,
"200_day_mavg_price": 425.73,
"analyst_summary": {
"num_buy_ratings": 57,
"num_hold_ratings": 3,
"num_sell_ratings": 0
},
"analyst_ratings": [
{
"published_at": "2025-02-01T01:14:52Z",
"type": "sell",
"text": "Microsoft is not the top player in its key sources of growth, notably Azure and Dynamics."
},
{
"published_at": "2025-02-01T01:14:52Z",
"type": "sell",
"text": "Microsoft lacks a meaningful mobile presence."
},
{
"published_at": "2025-02-01T01:14:52Z",
"type": "sell",
"text": "Momentum is slowing in the ongoing shift to subscriptions, particularly in Office, which is generally considered a mature product."
},
{
"published_at": "2025-02-01T01:14:52Z",
"type": "buy",
"text": "Microsoft has monopoly like positions in various areas (OS, Office) that serve as cash cows to help drive Azure growth."
},
{
"published_at": "2025-02-01T01:14:52Z",
"type": "buy",
"text": "Microsoft 365 continues to benefit from upselling into higher-priced stock-keeping units as customers are willing to pay up for better security and Teams Phone, which should continue over the next several years."
},
{
"published_at": "2025-02-01T01:14:52Z",
"type": "buy",
"text": "Public cloud is widely considered to be the future of enterprise computing, and Azure is a leading service that benefits the evolution to first to hybrid environments, and then ultimately to public cloud environments."
}
]
},
...
}
```
**Response Format:**
Return your decisions in a JSON array with this structure:
```json
[
{"symbol": <symbol>, "decision": <decision>, "quantity": <quantity>},
...
]
```
- <symbol>: Stock symbol.
- <decision>: One of `buy`, `sell`, or `hold`.
- <quantity>: Recommended transaction quantity.
**Instructions:**
- Provide only the JSON output with no additional text.
- Return an empty array if no actions are necessary.
AI-response example:
[
{"symbol": "AAPL", "decision": "sell", "quantity": 0.564172},
{"symbol": "BL", "decision": "hold", "quantity": 0.0},
{"symbol": "EQIX", "decision": "buy", "quantity": 1.0},
...
]
The bot logs its activity and trading decisions in a console log.
Are you sure you want to run the bot in auto mode? (yes/no): yes
[2024-11-01 11:06:58] [INFO] Market is open, running trading bot in auto mode...
[2024-11-01 11:06:58] [INFO] Getting portfolio stocks...
[2024-11-01 11:07:02] [INFO] Portfolio stocks to proceed: NVDA (1.07%), MSFT (0.12%), SNAP (0.25%), NWSA (13.71%), ...
[2024-11-01 11:07:02] [INFO] Prepare portfolio stocks for AI analysis...
[2024-11-01 11:07:07] [INFO] Getting watchlist stocks...
[2024-11-01 11:07:08] [INFO] Watchlist stocks to proceed: VRT, BB, VRNT, PBI, BMBL, IESC, WB, LITE, ...
[2024-11-01 11:07:08] [INFO] Prepare watchlist overview for AI analysis...
[2024-11-01 11:07:09] [INFO] Making AI-based decision...
[2024-11-01 11:07:21] [INFO] Executing decisions...
[2024-11-01 11:07:21] [INFO] NVDA > Decision: sell of 2.012
[2024-11-01 11:07:21] [ERROR] NVDA > Sold 2.012 stocks
[2024-11-01 11:07:21] [INFO] MSFT > Decision: sell of 1.5422
[2024-11-01 11:07:21] [ERROR] MSFT > Error selling: Not enough shares to sell.
[2024-11-01 11:07:22] [INFO] VRT > Decision: buy of 2.09
[2024-11-01 11:07:23] [INFO] VRT > Bought 2.09 stocks
[2024-11-01 11:07:23] [INFO] SNAP > Decision: hold of 0.0323
[2024-11-01 11:07:23] [INFO] VIAV > Decision: hold of 0.0212
[2024-11-01 11:07:24] [INFO] Stocks sold: NVDA (2.0)
[2024-11-01 11:07:24] [INFO] Stocks bought: VRT (2.09)
[2024-11-01 11:07:24] [INFO] Errors: MSFT (Not enough shares to sell.)
[2024-11-01 11:07:24] [INFO] Waiting for 600 seconds...
-
Clone the repository:
git clone https://github.com/siropkin/robinhood-ai-trading-bot.git cd robinhood-ai-trading-bot
-
Install dependencies:
pip install -r requirements.txt
Copy the example config and update it with your details::
cp config.py.example config.py
Fill in config.py with the required parameters:
# 1Password Credentials
OP_SERVICE_ACCOUNT_NAME = "..." # 1Password service account name (for Robinhood MFA secret)
OP_SERVICE_ACCOUNT_TOKEN = "..." # 1Password service account token (for Robinhood MFA secret)
OP_VAULT_NAME = "..." # 1Password vault name (for Robinhood MFA secret)
OP_ITEM_NAME = "..." # 1Password item name (for Robinhood MFA secret)
# Credentials
OPENAI_API_KEY = "..." # OpenAI API key
ROBINHOOD_USERNAME = "..." # Robinhood username
ROBINHOOD_PASSWORD = "..." # Robinhood password
ROBINHOOD_MFA_SECRET = "" # Robinhood MFA secret (if enabled)
# Basic config parameters
MODE = "demo" # Trading mode (demo, auto, manual)
LOG_LEVEL = "INFO" # Log level (DEBUG, INFO)
RUN_INTERVAL_SECONDS = 600 # Trading interval in seconds (if the market is open)
# Robinhood config parameters
TRADE_EXCEPTIONS = [] # List of stocks to exclude from trading (e.g. ["AAPL", "TSLA", "AMZN"])
WATCHLIST_NAMES = [] # Watchlist names (can be empty, or "My First List", "My Second List", etc.)
WATCHLIST_OVERVIEW_LIMIT = 10 # Number of stocks to process in decision-making (e.g. 20)
PORTFOLIO_LIMIT = 10 # Number of stocks to hold in the portfolio
MIN_SELLING_AMOUNT_USD = 1.0 # Minimum sell amount in USD (False - disable setting)
MAX_SELLING_AMOUNT_USD = 10.0 # Maximum sell amount in USD (False - disable setting)
MIN_BUYING_AMOUNT_USD = 1.0 # Minimum buy amount in USD (False - disable setting)
MAX_BUYING_AMOUNT_USD = 10.0 # Maximum buy amount in USD (False - disable setting)
# OpenAI config params
OPENAI_MODEL_NAME = "gpt-4o-mini" # OpenAI model name
If MFA is enabled, you'll need to provide an MFA code. There are two options:
If you prefer to set the MFA secret directly, follow these steps:
- Log in to your Robinhood account on your phone. Important to use your phone because it will display the secret key but not the QR code.
- Navigate to the security settings.
- Enable MFA if it is not already enabled. When setting up MFA, you will be asked to select an authentication method on your phone. Choose "Authenticator app" and Robinhood will provide you with a secret key. This is your
ROBINHOOD_MFA_SECRET
. - Copy this secret key and set it as the
ROBINHOOD_MFA_SECRET
environment variable or paste it directly into theconfig.py
file. - Enter the same secret key into your authentication app on the same PC where you run the script (e.g., Google Authenticator). Note: If you enter the secret on a different device, it will generate a different value.
- After entering the same secret on the same PC, use the generated TOTP number to authenticate with the Robinhood app.
If you do not set the ROBINHOOD_MFA_SECRET
environment variable, the script will attempt to retrieve the MFA secret from 1Password using the following credentials:
# 1Password Credentials
OP_SERVICE_ACCOUNT_NAME = "..." # 1Password service account name (for Robinhood MFA secret)
OP_SERVICE_ACCOUNT_TOKEN = "..." # 1Password service account token (for Robinhood MFA secret)
OP_VAULT_NAME = "..." # 1Password vault name (for Robinhood MFA secret)
OP_ITEM_NAME = "..." # 1Password item name (for Robinhood MFA secret)
To use this feature:
- Ensure the above 1Password credentials are correctly configured in your environment or
config.py
file. - Store your Robinhood MFA secret in the specified 1Password vault and item.
- The script will automatically fetch the MFA secret from 1Password if
ROBINHOOD_MFA_SECRET
is not provided.
For more information on setting up 1Password Service Accounts, read the guide: Get started with 1Password Service Accounts
Start the bot with:
python main.py
Please note: This bot is designed solely for educational purposes. Trading stocks involves significant risks, and you should only invest money you can afford to lose. The author is not liable for any financial losses incurred through the use of this bot.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are highly encouraged and welcomed! Whether youโre looking to enhance the logging system, optimize AI-prompt strategies, or enrich stock data โ thereโs always room for fresh ideas and improvements. Feel free to submit pull requests or open issues to share your suggestions and expertise!
Got questions, feedback, or just want to chat? Reach out via email at goodbotty@proton.me. I'm always happy to hear from you!