SMA(Short Moving Average) in Python Continued
By: Joseph Hart
Tags: Data Science, Web Scraping, Data Analytics, Data Visualization, Algorithmic Trading

In one of my previous posts, I talked about the SMA strategy and its application to the stock market. I showed the reader how to create the system in python, graph the results, and see the date and price of the buy or sell option.

Today, I’ll show you how I implemented the SMA strategy with web scraping to save new information to my pre-existing file and graph the latest data.

DISCLAIMER: Stocks can be risky, and it is possible to lose money. I have not used this strategy to make any financial gains, investments, or purchases. I do not recommend reading this article and using the plan for your investment purposes and/or financial gain. I am not responsible for your losses if you choose to use this. The strategy below is only an experiment.

I combined the graphing and SMA strategy into one cell to take in the CSV information and creates new SMA30 and SMA100 data. The function works with any stock regardless of price.

The strategy is a nested function inside the graphing function.

All information was stored into variables as presented in the original blog.

I graphed the adjusted closing price, SMA30, and SMA100. These code lines are all in one cell, so it is easier to run and saves time when in the notebook.

The web scraping process is tedious and repetitive, but necessary to not re-download new CSV files every day. The function will scrape five days of past information, so if you miss the sixth day, your table will be missing a day’s data and skew your graph. I realize this is very important to prevent, and a future goal is to run the web scraping code for the stocks every hour to avoid missing data.

The web scraping function is named new_data and takes in the argument stock_name. The stock name must be the abbreviation for the company, not the company name. Example: AAPL is Apple, WMT is Walmart Inc, and TSLA is Tesla. The cell requests the historical data from the requested stocks page and, after some filtering, finds the row of the desired prices. There are patterns in the formatting of the dates and prices, so identifying these and using them to our advantage made scraping much more manageable.

I scraped five dates but will show you two, as the only differences are the variable names and indexes.

The column index date always went up by 7, the open_price column index went up by 7, and adj_close_price index also went up by 7. The quoted numbers in red next to open_price and adj_close_price always went up by 15.

Extracting the price proved more challenging than expected because it did not have a title. I used string manipulation and regular expression manipulation to exclude undesirable characters. I excluded more characters than needed because a stock such as Tesla has more characters than the stock Pfizer. The third, fourth, and fifth date information followed the same pattern as the first and second date information.

Now that I had the five dates of information, I created a new data frame to concatenate my pre-existing data.

For easier access, I created an input function to access stocks I wanted to see.

After inputting the stock name, the web scraping function pulled the information.

I changed the datetime format because web scraping pulls the time. We do not want the time because the CSV file does not concatenate with the time.

I changed the ‘Date’ type to datetime so it would concatenate with the pre-existing CSV file.

Next, I read in the CSV file and checked its ‘Date’ type is in datetime.

I combined the newly web scraped data with my previous data to get a new table.

Now that I had new information, I needed to save it for when I graphed. I saved the file as the stock name I entered in the beginning as a CSV file.

Now that I saved my new file it’s time to graph.

So in one cell it looks like this:

When I ran this cell, python prompted a text box to enter the stock name. As long as I had the stock downloaded where I was running my notebook, it ran.

This post is a continuation of my previous post, and I have future goals for this project. I combined a lot of information at the beginning of this post from the previous one, so if something is confusing, you can find my original post- https://medium.com/analytics-vidhya/sma-short-moving-average-in-python-c656956a08f8.

Again, the strategy used here is not 100% correct, and that is essential information going into any trade. This strategy is only an experiment and should read as so.