This Python script is designed for stock analysis using the yfinance library, enabling users to analyze historical stock data comprehensively. The script encompasses various functionalities, including fetching stock data, plotting closing prices, displaying moving averages, and visualizing the Relative Strength Index (RSI). Users can interactively input a stock symbol and date range to gain insights into historical stock performance.
-
Fetching Historical Stock Data: Utilizes the
yfinancelibrary to download historical stock data for a specified stock symbol within a given date range. -
Plotting Closing Prices: Displays a visually intuitive plot of historical closing prices for the selected stock.
-
Moving Averages: Calculates and plots 20-day and 50-day moving averages alongside closing prices for a more nuanced analysis.
-
Relative Strength Index (RSI): Computes and plots the RSI for the chosen stock, aiding in identifying overbought or oversold conditions.
-
Input Validation: Ensures accurate analysis by validating user input for stock symbols and date formats.
Make sure you have the following before running the script:
-
Python 3.x: Install Python from the official website.
- Windows: Make sure to check the option to add Python to your PATH during installation.
- macOS: Check the version using
python3 --version. Install or update using Homebrew if needed (brew install python). - Linux (Debian/Ubuntu):
sudo apt update sudo apt install python3
-
Virtual Environment (Optional but recommended):
pip install -r requirements.txt python3 -m venv venv
-
Clone the repository:
git clone https://github.com/your-username/stock-analysis-tool.git -
Install the required packages: pip install -r requirements.txt If you encounter errors installing TA-Lib, please follow the instructions here to resolve the issue.
-
Run the script:
python trade.pyFollow the on-screen prompts to enter the stock symbol and date range.
Execute the script and follow the interactive prompts to analyze historical stock data, gaining valuable insights into stock performance.
-
Windows:
- Visit the official Python website here.
- Download the latest version of Python for Windows.
- Run the installer, making sure to check the option to add Python to your PATH during installation.
-
macOS:
- macOS usually comes with Python pre-installed. You can check the version by opening a terminal and typing
python3 --version. - If you need to update or install Python, you can use Homebrew (
brew install python).
- macOS usually comes with Python pre-installed. You can check the version by opening a terminal and typing
-
Linux (Debian/Ubuntu):
- Open a terminal and run the following commands:
sudo apt update sudo apt install python3
- Open a terminal and run the following commands:
-
Creating a virtual environment helps isolate your project dependencies from the system-wide Python installation.
pip install -r requirements.txt python3 -m venv venv
To activate the virtual environment, use the following commands:
- On Windows:
.\\venv\\Scripts\\activate - On macOS/Linux:
source venv/bin/activate
-
Ensure you are in the project directory where
requirements.txtis located. -
Activate the virtual environment (if not already activated):
-
Install dependencies:
pip install -r requirements.txt
To run your Python script, follow these steps:
-
Ensure the virtual environment is activated:
- On Windows:
.\\venv\\Scripts\\activate - On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Run the script:
python /src/trade.py
Contributions are welcome! Feel free to open issues or pull requests for any improvements or additional features.
This project is licensed under the MIT License - see the LICENSE.txt file for details.