How to do a SMA in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
SMAs are by far the most famous technical indicators used in financial analysis.
Here is a simple example of how to compute it in Python using the Pandas library.
We get our data
In order to compute the SMA we first need to get a financial dataset.
Here we take APPLE stock price data from yahoo finance using the yfinance library.
We compute the SMA
Using the Close prices we got back from the yfinance download method we compute the a 10 days close price SMA using the pandas .rolling() method.
The rolling method here is used to get the past 10 days close prices and the mean method is used to compute the average of those prices.
In the end we have the 10 days close price SMA !
Plotting the SMA
In order to see if we did a good job of computing the SMA on the Close prices, we use the .plot() method to see the results.
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚