Why Learn Python for Trading and Investing

Updated on July 25, 2018
davetromp profile image

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

Python
Python | Source

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

#!/usr/bin/python3

# 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)
print(url)
wget.download(url, "./")
df = pd.read_csv('xauusd_d.csv')
pylab.plot(df['Close'])
pylab.show()

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

What do you use Python for?

See results

© 2015 Dave Tromp

Comments

Submit a Comment

  • taiwokareem profile image

    Taiwo Kareem 

    3 years ago from Salford

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

  • davetromp profile imageAUTHOR

    Dave Tromp 

    3 years ago from Amsterdam

    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.

  • taiwokareem profile image

    Taiwo Kareem 

    3 years ago from Salford

    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.

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, owlcation.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://owlcation.com/privacy-policy#gdpr

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)