Stock prices can be obtained easily from Yahoo Finance , Quandl and many other sources. In today’s tutorial , we will use Python to retrieve these information. Firstly, you will have to install the additional modules. Run these commands on terminal/ cmd prompt.
pip install pandas
pip install matplotlib
pip install pandas_datareader.data
Pandas is a library often used in Data Science mainly for manipulation and analyzing data whereas, Matplotlib is a tool for us to plot graphs in Python. Lastly pandas_datareader help us obtain information from Yahoo Finance.
Retrieving the data and plotting the graph
Using Yahoo Finance and choosing a ticker symbol that you want to analyze i.e TSLA , we will plot a graph of closing price(df[Adj Close]) against time(df.index) . We will expect to see a graph (below)
Moving average(MA) is a tool widely used by investors , it smooth out fluctuations in price actions. A MA is formed by computing the average (mean) price of a security over a specified number of periods. Usually investors prefer to use a 200-day MA and a 50-day MA , and whenever 50-day MA > 200-day MA it implies a buy signal whereas the reverse implies a sell signal.
Now, we will create a function that plots the 100-day MA and 200-day MA .
The graph should look something like the above. It is recommended that the period ,n (e.g n-day MA) be much smaller compared to the time frame(x-axis) to ensure that the n-day MA line is more evident.
In this tutorial, we have learnt how to plot a stock’s closing price as well as it’s moving averages, in the subsequent tutorial, we will learn more about other indicators that investors use.
Source code can be found here: