The Fundamentals of Python Finance

automation feed
Oct 6 · 2 min read
Image from Austin Distel , www.unsplash.com

Introduction

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)

Tesla’s share prices over a span of 4 years

Moving Averages

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 .

200-day MA and 100-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.

Summary

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:

automation feed

Written by

automationfeed@gmail.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade