This Python project aims to predict stock prices using machine learning techniques. The project utilizes historical stock price data, options data, and energy commodity prices to train a predictive model. The prediction model is built using TensorFlow and Keras libraries, and the data is fetched from various sources including Alpaca API, Yahoo Finance, and Yahoo Finance Options.
The purpose of this project is to develop a predictive model for stock prices, specifically focusing on the energy sector represented by the ticker symbol "XLE." By analyzing historical data and employing machine learning algorithms, the project endeavors to forecast future price movements, aiding investors in making informed decisions.
The algorithm used in this project is a deep neural network regression model implemented using TensorFlow and Keras. The model architecture consists of multiple dense layers with ReLU activation functions. The model is trained to minimize the mean squared error loss function, and the Adam optimizer is used for optimization.
- Data Collection: Historical stock price data is collected from the Alpaca API, options data is fetched using Yahoo Finance Options, and energy commodity prices are obtained from Yahoo Finance.
- Data Preprocessing: The collected data is preprocessed, which includes cleaning, scaling, and splitting into training and testing sets.
- Model Building: A neural network regression model is built using TensorFlow and Keras libraries. The model architecture consists of multiple dense layers.
- Model Training: The model is trained on the training dataset using the Adam optimizer and mean squared error loss function.
- Model Evaluation: The trained model is evaluated on the testing dataset to assess its performance using the mean squared error metric.
- Visualization: Various visualizations such as training and validation loss plots, actual vs predicted stock price plots, energy prices plots, and standard deviation plots are generated to analyze the results.
- Data Retrieval: Utilizes APIs and libraries such as Yahoo Finance, Alpaca Trade API, and Yahoo_fin to fetch historical stock data.
- Options Data Analysis: Extracts and analyzes options data using Yahoo_fin library to identify potential market trends.
- Energy Prices Comparison: Compares and analyzes the spot prices of crude oil, natural gas, and coal alongside stock prices for comprehensive insights.
- Machine Learning Model: Constructs a machine learning model using TensorFlow and Keras to predict future stock prices based on historical data.
- Visualization: Employs visualization libraries such as Matplotlib and HoloViews to visualize stock prices, percentage changes, and standard deviations for better understanding and analysis.
Clone the repo first with git clone URl of the repo.
To run the project, ensure you have the necessary dependencies installed. You can install them using pip:
Make sure to replace requirements.txt with the actual name of your requirements file if it differs.
To run this project, make sure you have the following dependencies installed:
- yfinance
- alpaca_trade_api
- yahoo_fin
- pandas
- hvplot
- requests-html
- tensorflow
- scikit-learn
- matplotlib
- holoviews
- Obtain API keys for services like Alpaca and ensure they are correctly configured in the environment file (alpaca.env).
- Execute the Python script to fetch data, train the machine learning model, and make predictions.
- Analyze the output, including predicted stock prices, visualizations, and performance metrics of the machine learning model.
Contributions are welcome! If you'd like to contribute to this project, feel free to fork the repository, make your changes, and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.