A comprehensive tool for analyzing stock transactions and portfolio performance, specifically designed for integration with Robinhood. This project provides functionalities to filter and analyze stock trades, calculate outstanding stock quantities, and visualize historical stock prices. It integrates with Yahoo Finance to fetch current stock prices and plots various metrics to evaluate stock trading strategies and portfolio performance.
- Transaction Filtering: Filter and display stock transactions from a CSV file in a tabular format.
- Total Analysis: Analyze total bought and sold quantities for specific stocks.
- Outstanding Stock Calculation: Calculate outstanding stock quantities using FIFO order and assess profit/loss based on current prices.
- Historical Data Visualization: Fetch and plot historical stock prices, highlighting key buy/sell points and trends.
- Integration with Robinhood: Optionally generate transaction data from Robinhood using MFA authentication.
To enable MFA for your Robinhood account, you'll need a Time-based One-Time Password (TOTP) application, such as Google Authenticator, Authy, or any other TOTP-compatible app.
- Log in to Robinhood: Open the Robinhood app or website and log in to your account.
- Go to Security and Privacy Settings: Navigate to your Account -> Settings -> Security and privacy.
- Enable Two-Factor Authentication:
- Find the Two-Factor Authentication (2FA) or Multi-Factor Authentication (MFA) option.
- Follow the prompts to enable MFA with Authenticator App.
- Scan the QR Code:
- Robinhood will show you a QR code.
- Open your TOTP app and scan the QR code to add your Robinhood account.
- The app will start generating time-based codes for Robinhood.
- In the same security section where you enabled MFA, you should be able to see or generate a backup key or secret key (usually a 16-character string).
- Copy this secret key.
- Create or Open the
.env
File: If you don’t already have a.env
file in your project directory, create one. This file will store your environment variables. - Add Your Robinhood Credentials:
- Open the
.env
file in a text editor. - Add the following line, replacing
YOUR_TOTP_SECRET
with the secret key you copied earlier:
- Open the
When you run your application, the robin_mfa variable will be used to generate a TOTP code automatically, which is required for logging in to Robinhood.
Run your application and verify that it logs in successfully using the generated TOTP code. If you encounter any issues, double-check that the robin_mfa value in your .env file is correct.
-
Clone the Repository:
git clone https://github.com/uppusaikiran/robinhood_stock_analyzer.git cd robinhood_stock_analyzer
-
Create a Virtual Environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install Dependencies:
Create a
.env
file in the root directory with your Robinhood credentials and MFA code:robin_username=your_robinhood_username robin_password=your_robinhood_password robin_mfa=your_robinhood_mfa_secret
Install the required packages:
pip install -r requirements.txt
-
Run the Analyzer:
Use command-line arguments to specify the stock symbol, start date, and CSV file path.
python stock_analyzer.py --symbol AAPL --start_date 2023-01-01
Arguments:
--symbol
: The stock symbol to analyze (e.g., AAPL).--start_date
: The start date for the analysis in YYYY-MM-DD format (optional).--file
: The path to the CSV file (optional, default is./stock_orders.csv
).--generate_data
: Set toTrue
to generate new transaction data from Robinhood (requires environment variables).
-
Output:
- Displays filtered transactions for the specified stock symbol.
- Analyzes and prints total bought and sold quantities.
- Calculates and prints outstanding stock quantities and detailed sales information.
- Plots historical stock prices and key transaction points.
python stock_analyzer.py --symbol MSFT --start_date 2023-01-01 --file ./my_stock_orders.csv
Feel free to submit issues or pull requests. Contributions are welcome!
This project is licensed under the MIT License. See the LICENSE file for more details.