Using Python and Yfinance to Predict Stock Prices

Click to share! ⬇️

Understanding the financial market and predicting stock prices is an intricate task that involves analyzing a multitude of data. However, with the advent of powerful programming languages like Python and powerful financial libraries like Yfinance, we can simplify the process and make meaningful predictions. This blog post is dedicated to guiding you through the process of predicting stock prices using Python and the Yfinance library.

The beauty of Python lies in its simplicity and the vast array of libraries it provides, making it one of the most preferred languages for data analysis. On the other hand, Yfinance is a popular Python library that allows you to access the financial data available on Yahoo Finance. By combining Python’s data manipulation power with Yfinance’s ability to fetch financial data, we can create a robust system for predicting stock prices.

Whether you are a Python beginner or an experienced data analyst, this tutorial will provide valuable insights into using Python and Yfinance for financial analysis. We will start by explaining the basics and gradually move towards building a stock price prediction model. By the end of this tutorial, you will have a working model that you can use to predict stock prices, along with the knowledge to further refine and improve it. Let’s dive in!

Getting Familiar with Yfinance: Fetching Stock Data

In this section of the tutorial, we’ll take a look at how to fetch stock data using Yfinance. We’ll use five different stock tickers for the demonstration: Apple Inc. (AAPL), First Solar Inc. (FSLR), Nvidia Corporation (NVDA), ARK Innovation ETF (ARKK), and SPDR S&P 500 ETF Trust (SPY). Here are the steps to follow:

  1. Importing the Yfinance Library: First, we need to import the Yfinance library. This is done using the import statement.
import yfinance as yf
  1. Fetching Data for a Single Stock: Next, we’ll fetch the data for Apple Inc. using its ticker symbol ‘AAPL’.
aapl = yf.Ticker("AAPL")
aapl_info = aapl.history(period="1y")
print(aapl_info)
  1. Fetching Data for Multiple Stocks: You can fetch data for multiple stocks at once by passing a list of ticker symbols to the download function.
data = yf.download(['FSLR', 'NVDA', 'ARKK', 'SPY'], period='1y')
print(data)
  1. Accessing Specific Data: You can access specific data such as Open, High, Low, Close, and Volume for each stock.
print(aapl_info['Close'])  # prints the closing prices for AAPL for the last year
  1. Plotting the Data: Python’s Matplotlib library can be used to visualize the fetched data.
import matplotlib.pyplot as plt

aapl_info['Close'].plot()
plt.title('AAPL Close Price')
plt.show()

Remember, practice makes perfect. So, don’t hesitate to experiment with different ticker symbols and time periods to become more familiar with fetching stock data using Yfinance.

Exploratory Data Analysis: Understanding Your Stock Data

Exploratory Data Analysis (EDA) is a crucial step in understanding your data and gaining insights from it. It involves summarizing the main characteristics of the data, often with visual methods. In the context of stock data, EDA can help reveal patterns, spot anomalies, test assumptions, and check for correlations.

Here’s how you might proceed:

Summary Statistics: Start by generating descriptive statistics that summarize the central tendency, dispersion, and shape of your dataset’s distribution.

print(aapl_info.describe())

Check for Missing Data:

Missing data can significantly impact your analysis and model’s performance. Use the following code to identify any missing data:

print(aapl_info.isnull().sum())

Visualize the Price Trends:

Visualizing the stock price trends over time can provide insights into the stock’s volatility and overall trend. You can use the matplotlib library to plot the closing prices over time:

aapl_info['Close'].plot(figsize=(10, 7))
plt.title('AAPL Closing Prices')
plt.xlabel('Date')
plt.ylabel('Closing Price (USD)')
plt.show()

Examine the Volume of Stock Traded:

The volume of a stock traded can indicate investor interest in a particular company. A higher trading volume often means the price is more likely to change:

aapl_info['Volume'].plot(figsize=(10, 7))
plt.title('AAPL Trading Volume')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.show()

Correlation Analysis:

If you’re analyzing multiple stocks, it can be helpful to examine the correlation between their closing prices. This can help identify any relationships between the stocks:

data = yf.download(['AAPL', 'FSLR', 'NVDA', 'ARKK', 'SPY'], period='1y')['Close']
correlation_matrix = data.corr()
print(correlation_matrix)

Through exploratory data analysis, you can gain a deeper understanding of your stock data and generate insights that could be instrumental in predicting future prices. In the next section, we’ll look at how to prepare this data for machine learning.

Preprocessing Data for Machine Learning

Data preprocessing is a vital step in preparing your data for machine learning. It involves transforming raw data into an understandable format, dealing with missing values, scaling features, and sometimes creating new features that might better represent the underlying data structure.

Let’s walk through these steps in the context of our stock price prediction task:

Handling Missing Values: As previously mentioned, missing data can impact the performance of a machine learning model. If any missing values are found in your data, you can either fill them with a specific value (like the mean or median) or remove the rows or columns with missing data.python

aapl_info = aapl_info.fillna(aapl_info.mean())  # fill missing values with the mean

Feature Engineering:

Often, creating new features from the existing data can help improve model performance. For instance, you might introduce a feature that represents the average closing price of the past few days.

aapl_info['MA_5'] = aapl_info['Close'].rolling(window=5).mean()  # 5-day moving average

Feature Scaling:

Certain machine learning algorithms perform better when the input numerical features are on a similar scale. Min-max scaling and standardization are two common methods.

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler(feature_range=(0,1))
aapl_info_scaled = scaler.fit_transform(aapl_info)

Splitting the Data:

The data needs to be split into a training set and a test set. The training set is used to train the model, while the test set is used to evaluate the model’s performance.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Remember that data preprocessing is a critical part of the machine learning pipeline and requires careful attention. The quality and quantity of the data you feed into your model will directly affect its ability to learn, so make sure to spend ample time on this stage. In the next section, we’ll get into the exciting part: building a stock price prediction model!

Building a Stock Price Prediction Model

In this section, we’ll create a simple stock price prediction model using the Long Short-Term Memory (LSTM) model, a type of recurrent neural network. LSTM is well-suited to predict time series data due to its ability to remember past information.

Import Required Libraries:We’ll need Keras for building the LSTM model and numpy for array manipulations.python

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np

Prepare Data for LSTM:

LSTM requires the input to be in a specific format, usually a 3D array. We’ll write a function to convert our 2D array into a 3D array.

def create_dataset(X, y, time_steps=1):
    Xs, ys = [], []
    for i in range(len(X) - time_steps):
        v = X.iloc[i:(i + time_steps)].values
        Xs.append(v)        
        ys.append(y.iloc[i + time_steps])
    return np.array(Xs), np.array(ys)

TIME_STEPS = 60
X_train, y_train = create_dataset(X_train, y_train, TIME_STEPS)
X_test, y_test = create_dataset(X_test, y_test, TIME_STEPS)

Define the LSTM Model:

We’ll define a sequential model, add an LSTM layer with 50 units, and add a Dense layer to predict the closing prices.

model = Sequential()
model.add(LSTM(units=50, return_sequences=False, input_shape=(X_train.shape[1], 1)))
model.add(Dense(1))

Compile and Train the Model:

After defining the model, we need to compile it with a loss function and an optimizer. Then, we’ll train the model using the training data.

model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, y_train, epochs=20, batch_size=32)

Predict and Evaluate Model Performance:

Finally, we’ll use the trained model to predict the stock prices for the test data, and then we’ll compare these predictions with the actual values.

y_pred = model.predict(X_test)

The above steps will provide you with a basic LSTM model for stock price prediction. In the next section, we’ll discuss how to evaluate this model and improve its performance.

Evaluating the Prediction Model: Understanding Accuracy and Errors

Once you’ve built your stock price prediction model, the next crucial step is to evaluate its performance. This evaluation helps in understanding how well the model is learning and predicting, and if there are any adjustments needed to improve its performance.

Compute Error Metrics: A variety of error metrics can be used to measure the quality of your predictions. Some common ones include Mean Absolute Error (MAE), Mean Squared Error (MSE), and Root Mean Squared Error (RMSE).python

from sklearn.metrics import mean_absolute_error, mean_squared_error
import math

MAE = mean_absolute_error(y_test, y_pred)
MSE = mean_squared_error(y_test, y_pred)
RMSE = math.sqrt(MSE)

print('Mean Absolute Error:', MAE)
print('Mean Squared Error:', MSE)
print('Root Mean Squared Error:', RMSE)

The lower these error metrics, the better your model’s predictions.

Visualize Predictions:

A good way to evaluate the model’s performance is by visualizing the predicted versus the actual stock prices. This can be done using matplotlib.

plt.figure(figsize=(10,6))
plt.plot(y_test, color='blue', label='Actual Stock Price')
plt.plot(y_pred , color='red', label='Predicted Stock Price')
plt.title('Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()

The closer the red line (predicted stock price) is to the blue line (actual stock price), the better your model is performing.

Evaluate Model Accuracy:

Another way to evaluate your model is by calculating the accuracy of the predictions. However, accuracy isn’t typically used in regression problems like ours. Instead, we might consider the R-squared statistic, which provides a measure of how well the predicted values fit the actual values.

from sklearn.metrics import r2_score

r2 = r2_score(y_test, y_pred)
print('R-squared:', r2)

Creating an accurate prediction model is an iterative process. If your model isn’t performing as well as you’d like, consider revisiting your data preprocessing and feature selection steps, or try using different model parameters or architectures. In the next section, we’ll discuss some advanced techniques to improve your model.

Improving Your Model: Advanced Techniques and Considerations

Creating a robust stock prediction model can be a challenging task. If your model is not performing as well as you’d like, there are several advanced techniques and considerations you can use to try and improve its performance:

  1. Hyperparameter Tuning:You can adjust the hyperparameters of your LSTM model, such as the number of LSTM units, batch size, and number of epochs, to see if they improve model performance. You can use techniques like Grid Search or Random Search to systematically find the best hyperparameters.
  2. Additional Features:Consider adding more features that could be relevant to stock prices. This could include other financial indicators, or external factors like news sentiment or macroeconomic indicators.
  3. Different Model Architectures:You could try using different types of models or different architectures. For instance, you might try using a GRU (Gated Recurrent Unit) instead of an LSTM, or use a combination of LSTM and Convolutional Neural Network (CNN) layers.
  4. Ensemble Methods:Ensemble methods combine the predictions of several models to improve accuracy. You might train several different models and combine their predictions, or use techniques like bagging or boosting.
  5. Regularization Techniques:Techniques like dropout or L1/L2 regularization can help prevent overfitting and make your model more generalizable.
  6. Cross-validation:Rather than splitting your data into a training and test set once, you can use cross-validation to train and test your model on different subsets of your data. This can provide a more robust estimate of your model’s performance.

Building a machine learning model is an iterative process, and it’s normal to not get great results on your first try. Don’t be discouraged if your initial model doesn’t perform as well as you’d hoped. With perseverance and by using some of the techniques described above, you’ll be able to improve your model’s performance over time.

Next Steps: Further Resources for Stock Price Prediction

Congratulations! You’ve built a stock price prediction model using Python and Yfinance. But the learning doesn’t stop here. There are many other resources and techniques you can explore to continue improving your model and broadening your knowledge in this field. Here are some suggestions for next steps:

  1. Deep Dive into Financial Analysis: Understanding financial markets and indicators can greatly help improve the performance of your model. Consider taking courses or reading books on financial analysis and investing.
  2. Explore Other Machine Learning Techniques: There are many other machine learning models and techniques that can be used for stock price prediction, such as ARIMA, Prophet, and SVM. Experiment with these models and see how they compare to the LSTM model.
  3. Learn More About Deep Learning: Go deeper into the world of deep learning. Learn about other types of neural networks such as CNNs (Convolutional Neural Networks), RNNs (Recurrent Neural Networks), and Transformer models.
  4. Apply Natural Language Processing (NLP): Consider incorporating news data or social media sentiment into your model. This will require learning about natural language processing (NLP), which can be a very useful skill in financial modeling.
  5. Explore Reinforcement Learning for Trading: Reinforcement learning is another branch of machine learning that can be applied to financial markets. Some algorithms like Q-learning and policy gradients are used to build trading bots.
  6. Join a Community: Join online communities and forums where you can interact with other data scientists and machine learning enthusiasts. Websites like Kaggle, GitHub, and various machine learning subreddits are great places to share your work, get feedback, and learn from others.
  7. Stay Updated with Research: The field of machine learning and AI is rapidly evolving. Keep an eye on the latest research papers and technologies to stay up-to-date.
Click to share! ⬇️