Updated date:

Why Learn Python for Trading and Investing


I code, trade and invest. Coding is my day job, I trade on the side and invest for the long run.

Python Is Easy to Use and Easy to Learn

Python is widely used for server automation, running web applications, desktop applications, robotics, science, machine learning and more. And, yes, it is very much capable of handling large sets of financial data.

As Python is a scripting language it is easy to do iterative development of software as there is no compilation waiting time. At the same time, it is possible to extend Python code with the code in C or C++ for parts in the application or code library that need better optimization and better speeds. The scientific libraries discussed later on in this article make extensive use of this possibility.

Guido van Rossum developed Python as a programming language that would help him automate his day to day work. He also based it on a programming language that was developed for teaching people how to code. Because of this Python is simple and practical in nature. Yet, if implemented correctly Python-based software can be as powerful as applications build in any other programming language.

Idle: simple but effective

Idle: simple but effective

Getting Started

You can get started quickly. Just go to the website www.python.org. There you can download Python for your operating system. There are two versions of Python:

  • Python 2.x
  • Python 3.x

Either version is fine. If you have never used Python before it is best to immediately start with the latest version.

The install packages typically contain the following component for installation:

  • Python interpreter (cython)
    This is what actually makes your code run.
  • Pip
    Package manager that you can use to install additional libraries.
  • Idle
    Code editor

Once you have installed all component, you can try to run the example script in this article and experience how easy Python is.

Example: Getting and Plotting Historical Financial Pricing Data


# first install wget by typing 'pip install wget pandas pylab' on the command line

import wget
import pandas as pd
import pylab

s = 'xauusd'
url = "http://stooq.com/q/d/l/?s={}&i=d".format(s)
wget.download(url, "./")
df = pd.read_csv('xauusd_d.csv')

Plotting a Basic Line Graph is Easy with Pylab

Price of Gold

Price of Gold

There Are Many Excellent Libraries to Use When Researching Financial Data

Researching trading and investing strategies can require a lot of processing resources. Python itself is slow. For most tasks, this is not a problem and not even noticeable. However, when we want to process large sets of data, like financial data, and we want to test many different scenarios, processing could take a very long time. As mentioned, process intensive parts of the code in a Python application can be replaced with C or C++ code, but luckily in most cases, this is not needed, as there are many libraries that are optimized for process intensive data-science-related tasks. The following Python libraries are typically used:

  • The standard library
    Almost everything can be done with the standard library. Other non-standard libraries build upon this library to implement specific use cases and basically to make complicated stuff easier to implement.
  • SciPy
    This is a combination of libraries used for science, mathematics, and engineering.
  • NumPy
    Part of SciPy and implements among other stuff matrices and vectorization.
  • MatPlotLib
    Part of SciPy and implements advanced plotting capabilities.
  • Pandas
    Part of SciPy. Implements working with data frames and time series.

Besides these libraries there are some additional libraries helpful for data scraping, wrangling, munging and working with APIs:

  • BeautifulSoup
    Library for parsing HTML. Very useful if you want to get data from websites.
  • Mechanize
    This library allows for programmatical access to websites, like filling out a form and posting it, etc.
  • Requests
    Most APIs require authentication when accessing them. This can be accomplished using the tools in the standard library, but the Requests Library makes it almost "Curl" - like simple.

Also very powerful:

  • ScikitLearn
    Library for parsing HTML. Very useful if you want to get data from websites.
  • NLTK
    Natural Language Toolkit, Makes sense out of unstructured text-based data, like for instance, twitter feeds, news, etc.

And to make your life as a researcher of trading strategies even easier, there are many trading related APIs, that have a python library ready to access the data.

  • Pandas DataReader
    The web.DataReader method allows you to pull data from Stooq, Google Finance, Nasdaq, and other sources.
  • Quandl
    "Get millions of financial and economic datasets from hundreds of publishers directly into Python."

Python for All

© 2015 Dave Tromp


Taiwo Kareem from Salford on September 28, 2015:

Yeah, it's good meeting someone with similar interests here. Even though i'm still learning. it's a good opportunity to learn extra.

Dave Tromp (author) from Amsterdam on September 28, 2015:

Taiwokareem, Great to hear. I will look into your module. All it takes is pip install eventsim. This is why I like python. Very strong community. Someone almost always already build a solution that helps you solve your problem. Thanks.

Taiwo Kareem from Salford on September 28, 2015:

Python is a great Programming language because of its simplicity as opposed to C++. I like Ruby as well and even created a module called "eventsim" on the python packaging index for simulating events in the workplace. Simple but useful mainly for simulating things like inter-arrival time, arrival time, service begin and end times etc.

But Python is really my first choice programming language along with Ruby.