Photo by 312 Visuals on Unsplash
Exploring Tesla Stock Trend in 2023 Using Python
Basics of gathering and analyzing financial data using Python, yfinance, Pandas and Matplotlib, focusing on Tesla's historical stock information.
Extracting Stock Data from Yahoo Finance
Yahoo Finance
yfinance
Python library, I retrieve stock prices, dividends, and other financial information that can be used for analysis.GitHub Documentation
The full documentation is available in my GitHub.
Data-Science-Files/Stock Performance (tesla).ipynb at main · Akina-Aoki/Data-Science-Files (github.com)
Key Tasks in Extracting Data
In my project, I utilized the yfinance
and pandas
libraries in Python to gather financial information. Additionally, I learned how to download CSV files from Yahoo Finance's website and read them using Python.
Exploring the Ticker Module
I delved into the yfinance
library, which enabled me to access stock data using ticker
symbols. Through the Ticker
module, I retrieved detailed information about specific stocks, including historical prices.
Using the Requests Library
I explored Python's requests
library to download files from URLs. This allowed me to access and retrieve financial data from external sources.
Extract Historical Share Price Data
Method: yfinance module and Python library
Method: CSV file path and pandas library
From here, new projects can be done in the future, and I would like to try using Pandas and Matplotlib to analyze, clean, visualize this data and get some insights. For now, this is how I would extract data from yahoo finance using yfiance module, reading csv file and pandas.
Cleaning the Dataset using Pandas
ConvertDate
Column toDateTime
The data yielded indeed looks clean at first glance. There are no missing values, duplicates does not exist and there are no outliers that might skew statistical analysis and visualization. However, there are some potential areas I want to preproccess.
Ensure that the Date
column is in datetime format. I can use the pd.to
_datetime()
function to convert it if it's not already in datetime format.
Convert datatype from float to integers
Converting values to integers can result in loss of information, especially if the analysis requires high precision. For financial data like stock prices, even small differences can be significant. So, I am not typecasting them, but here is a code to do it.
Data Analysis
So far, I have all the necessary features needed to do Tesla stock analysis. Here are the important features and their definitions.
Calculate Summary Statistics
What is Summary Statistics? Summary statistics are numbers that help describe and summarize the main features of a set of data such as the mean, median, mode, range, and standard deviation. They provide a snapshot of the data's central tendencies and variability.
I can use the describe()
method from the pandas library to calculate summary statistics.
Genaral Observations
Price Trends
The mean values for Open, High, Low, Close, and Adjusted Close prices suggest that the stock generally had moderate to high prices throughout the year.
The minimum and maximum values show the range of price fluctuations experienced by the stock during the year.
Volatility
- The standard deviation values indicate the level of volatility in the stock prices. Higher standard deviations suggest greater price fluctuations, while lower standard deviations imply more stable prices.
Trading Volume
The mean and range of trading volumes indicate the level of activity and liquidity in the market for Tesla stock during 2023.
Higher trading volumes typically indicate increased investor interest and participation in buying and selling the stock.
Data Visualization
Matplotlib is one of the most widely used libraries for creating static, interactive, and animated visualizations in Python. As a beginner, I will be focusing on static graphs first. One reason I'm also using it is beacause Matplotlib integrates well in Jupyter Notebooks.
Trends Visualization
The first subplot displays the trends in Open, High, Low, Close, and Adjusted Close prices over time. The second subplot displays the trading volume over time in a line graph.
Identify Monthly Trends
Using the closing price data, analyse and determine if there are any noticeable trends or patterns in the stock prices over the months.
Monthly Volume Analysis
Analyze the volume of trades to identify patterns. High trading volumes during certain periods may indicate significant market interest. Calculate the average trading volume for each month. This can show the liquidity of the stock over time.
Reporting
Summarize the findings and insights from the analysis.
Summary Statistics
Tesla's stock prices moved up and down quite a bit throughout the year, with some periods of significant volatility. The trading activity was generally high, indicating a lot of interest from investors.
Monthly Closing Prices
From the monthly trends in Tesla stock closing prices for the year 2023 , it can be implied that investors were generally optimistic about Tesla from June to August. Looking at the Long-Term Outlook, investors can assess Tesla's overall trajectory to be growing.
Average Monthly Trading Volume
Higher bars indicate months with greater trading activity, suggesting peakinterest and participation from investors in buying and selling Tesla stockin February 2023 and lowest in January 2024.