The quickstart guide to financial coding with Python

Defining fintech, installing Python 3 and relevant packages, and pulling and charting live stock market data

Image by julianreherhorst from Pixabay.

intech, or financial technology, is all the rage, and its prevalence is growing exponentially. Broadly speaking, fintech can be defined as

…the technology and innovation that aims to compete with traditional financial methods in the delivery of financial services. It is an emerging industry that uses technology to improve activities in finance.” ~Wikipedia

While you may not be running an algo hedge fund or a bank or a corporate finance department tomorrow, it still pays to familiarize yourself somewhat with this world, especially as it upends traditional investing, banking, and valuation activities, among others. Often at the heart of fintech is a collection of things: data, computations/analysis, and of course, the code behind it. Python is one of the programming languages particularly well-suited to the task of analyzing financial data.

This is a quickstart guide on how to get going on a box running an Arch Linux distro, such as Manjaro. But really, this guide can be easily adapted to your specific computing environment. I include links to installation methods for other operating systems like Windows, Mac OS, and other Linux variants in the resources section at the end of this article. As for the actual financial coding we’ll cover, that is operating system agnostic. Also, please note that some familiarity with computing concepts is assumed.

Installing Python 3

irst, check to see if Python is already installed. The latest version of the language is Python 3. It is incompatible with the previous version, Python 2. A discussion of the differences is beyond the scope of this overview. For our purposes, we will be working with Python 3. Further reading regarding the distinctions can be found here.

On your machine, open a terminal window (Ctrl+Alt+T or use your GUI) and enter the following at the prompt (don’t forget to hit return after each command entered):

python

The response should include information on the version, whether it is the default, and a short menu of further options, like so:

Python 3.7.4 (default, Oct  4 2019, 06:57:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

If Python 3 is not installed (if your machine responds with Python 2.x, for instance), issue a terminate command by keying Ctrl+D, then enter the following at the resulting prompt:

sudo pacman -S python

Follow the usual procedure and onscreen instructions to install a package on your machine. Pacman is the default package manager on Manjaro. Ubuntu, on the other hand, uses APT. Some guides suggest using pip/pip3, the Python package installer. However, on Linux boxes at least, it’s probably best to rely on the system’s default package manager to grab what we need. Where it comes to Windows and Mac OS, pip/pip3 may be required after all to install the necessary packages mentioned next. Do some supplemental reading and adjust as required.

Installing additional packages for processing financial data

ith the Python 3 package successfully installed, we will add a couple of additional packages to be able to fluidly work with financial data. Just a heads-up, if any of the packages from the list below are already installed for whatever reason, you may get a warning from your system that looks like the following:

sudo pacman -S python-numpy
[sudo] password for username:
warning: python-numpy-1.17.2-1 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...
Packages (1) python-numpy-1.17.2-1Total Installed Size: 20.67 MiB
Net Upgrade Size: 0.00 MiB
:: Proceed with installation? [Y/n] n

Simply enter n and hit return to avoid installation of that specific package. Also note that critical dependencies should be automatically installed alongside the main package requested.

These additional packages are:

  • pandas, a software library intended for data manipulation and analysis, especially for manipulating numerical tables and time series
sudo pacman -S python-pandas
sudo pacman -S python-matplotlib
  • NumPy, a software library geared towards offering mathematical functions to operate on large, multi-dimensional arrays and matrices (we’re going to install this one for good measure)
sudo pacman -S python-numpy
  • alpha_vantage, a module for grabbing financial investments data from Alpha Vantage APIs maintained by a company I randomly came across called Alpha Vantage Inc, which provides free access to APIs for retrieving real-time and historical stock, forex (FX), and digital/crypto currencies data (this one will need to be installed using AUR and corresponding syntax, as follows)
yaourt -S python-alpha_vantage

Proceed with the installation as you would with any other package acquired via AUR. If you don’t know what AUR is or don’t have it installed, do some further reading here and get that taken care of before continuing with this guide.

Pulling and displaying some actual data

ow that everything’s installed, enter the following at the terminal prompt:

python

Next, examine the ensuing code, which will pull and plot a few months’ worth of daily closing price data for the Vanguard Total International Stock ETF (VXUS), adapted from an Alpha Vantage API introductory article:

# Pulling and plotting VXUS ETF price data# Import the software libraries whose functionality we will rely on
from alpha_vantage.timeseries import TimeSeries
from alpha_vantage.techindicators import TechIndicators
from matplotlib.pyplot import figure
import matplotlib.pyplot as plt
# Input your unique Alpha Vantage API key on the next line
key = 'INPUT YOUR KEY HERE'
# Choose your output format (pandas tabular), or default to JSON (python dict)
ts = TimeSeries(key, output_format='pandas')
# Get the data, which returns a tuple of 100 records
# vxus_data is a pandas dataframe, vxus_meta_data is a dict
vxus_data, vxus_meta_data = ts.get_daily(symbol='VXUS')
# Visualize the data by determining the plot size and style
figure(num=None, figsize=(15, 6), dpi=80, facecolor='w', edgecolor='k')
vxus_data['4. close'].plot()
plt.tight_layout()
plt.grid()
plt.show()

In the above, lines following hash/pound symbols “#” are comments. Additionally, it is recommended that you input your own unique Alpha Vantage API key into the code where applicable. You can request one of those here, then type it in in place of the INPUT YOUR KEY HERE bit (but keep the single quotes). That being said, the code above has been working for me without entering a key, FYI.

To briefly explain what’s happening in the code, you are importing real financial time series data via Alpha Vantage for VXUS, organizing it, and plotting a formatted depiction of it.

Copy and paste the chunk of code above after the “>>>” symbols in your terminal window (tip: use Ctrl+Shift+V for pasting). As you perform the paste, you may get a warning message from your system akin to the one shown in the screenshot here:

Click Paste and the code will be pasted into your terminal window. Once that operation has completed, hit return and, if all is well, a new window containing a chart with the data graphed in chronological order will appear on your screen.

Pretty cool, right? You’ve just created your own stock chart with Python and a few Python-specific software library packages utilizing the same underlying data that platforms like Yahoo! Finance use. Much more can be done in terms of chart formatting and technical analysis, to be sure. We’re only getting started.

To terminate the script you’ve run, close the chart window, then click inside your terminal window and issue the keystroke Ctrl+D. The terminal emulator will return you to your normal command prompt. As an aside, be aware that you can save scripts like these in text files with the .py file extension and run them from the terminal by filename (no need to copy and paste), but that’s a story for another time.

Keep learning

Resources and further reading

  • How to install Python 3 on various operating systems, including other flavors of Linux, Windows, and Mac OS
  • Alpha Vantage API documentation, which discusses how to pull other data, for instance
  • A list of financial data API providers

WTFinance

There’s more to it than money.

Philip Valenta, MSF

Written by

Intersectionist :: https://philipvalenta.com

WTFinance

WTFinance is home to insightful articles on finance, fintech, behavioral finance, economics, and financial politics, as well as civics and equity. There’s more to it than money.

Philip Valenta, MSF

Written by

Intersectionist :: https://philipvalenta.com

WTFinance

WTFinance is home to insightful articles on finance, fintech, behavioral finance, economics, and financial politics, as well as civics and equity. There’s more to it than money.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store