Python Tips for Algorithmic Trading [Reading Files, The Command Line]

Python Tips for Automated Trading

Python is the fastest growing and most popular programming language. This has drawn many quantitative traders, developers, and analysts to the easy-to-use and simple to understand scripting language. This post will cover a few python tips including reading text files, formatting dates and more essentials for automated trading.

Python has many popular finance and trading-based libraries for those that are interested in beginning their algo trading journey with python. The most popular trading library is probably talib which is a technical analysis library that has all the functions, indicators and math transforms an algo trader would need to start. Here is the talib documentation, if curious.

Reminder Build Alpha provides an easy way to get into automated trading without any programming. It does offer the ability to add custom signals via python for those of you that eventually want to learn or incorporate python into your process.

Is Python Good for algo trading

Python is an excellent choice for those traders that want to code themselves. The ease of learning, writing and understanding python code skyrockets it up the potential programming language list. Additionally, there are many good algo trading libraries that one can use freely. This saves the algo trader from having to recreate the wheel.

It is important to note that python does have its limitations when it comes to very large data sets or the need for speed. Other programming languages, like C++, are better suited for high frequency trading. I compare Python and C++ in my Algo Trading Guide.

The most popular python library, pandas, was created by a quantitative trading firm. AQR Capital Management began the project in 2008 before open sourcing it in 2009.

Why Python?

Python’s beauty is its simplicity. Python’s simplicity is its beauty. Unlike other programming languages, python does not use brackets to identify code blocks or semi-colons to end statements. Python simply relies on neatly written and indented code for the interpreter to grasp.

On top of the ease of use, Python’s mainstream popularity is due to the growing number of public libraries with python solutions. This gives many python coders an easy introduction to solving a problem or often a solution. For instance, no need to re-code a technical indicator if someone has already added it to the aforementioned talib library. You can simply access or use the code from talib.

Python Read in Financial Data

The first tutorial goes over how to read in a text file, format dates, and create new columns inside a pandas data frame. A data frame is a structure that stores your data in a convenient “table” for easy access. There are a few parts, but I will break down the code below.

The first thing we will do is import pandas library and call the built-in read_csv function. The read_csv function’s first input is the name of the file you desire to read in and store in your pandas’ data frame. The delimiter option allows you to specify the character that separates your text fields within your file.

import pandas as pd

df = pd.read_csv("ES.txt",delimiter=',')

Just like that, we have read a text file into a pandas’ data frame that we can now work with.

However, if we were to plot our data frame (closing prices) now the x-axis would simply be the number of bars as we did not specify an index column. In trading and time series analysis it is often nice to have dates as your x-axis.

Python Working with Dates

In the next few lines of code, I import a built-in python library that can read string dates (“12/30/2007”) and convert them into Python “DateTime” objects. To simplify this… we convert dates into Python dates.

I accomplish this by setting the built-in pandas index column to a list of newly Python formatted dates. I essentially loop through each string date, convert it, and add it to our data frame’s index. I then delete the original string Dates column.

from dateutil import parser

df.index = [parser.parse(d) for d in df['Date']]

del df['Date']

Now we can plot our closing prices and our x-axis will be dates.

df['Close'].plot()

To watch the video tutorial please head to YouTube: Python – Read in Text Files, Format Dates

Python Plotting Financial Data

In the code below I create a new column called “Range”. Notice how Python understands I want to do the calculation on all the highs and lows inside our data frame without me specifying so!

df['Range'] = df['High'] - df['Low']

Finally, the line below plots our Close and Range in two separate plots. This is from a previous tutorial video.

df[['Close','Range']].plot(subplots=True)

Python using the command line

The second part of this tutorial is to make our lives easier. Let’s say that we wanted to run that last program on a bunch of different stocks whenever we wanted. It would be quite annoying to open up the file or notebook and change the filename in our read_csv function every time.

Instead, we can create a filename variable and put the filename variable inside the read_csv function. Ideally, this filename variable could be dynamically set with user input from the command line.

This code is tricky and has a few moving parts. Below is the code and then I will explain what we did.

symbol = 'ES'

import sys,getopt

myopts,args = getopt.getopt(sys.argv[1:],"s")

for o,a in myopts:

        if o == '-s':

            symbol = str(a).upper()

            filename = "%s.csv" % symbol

            df  = pd.read_csv(filename,delimiter=',')

First, we created a symbol variable that will accept our user input. Second, we imported some built-in libraries and called the getopt function to read user input. We also specified that our desired input would be preceded by the “s” option.

We then wrote a simple for loop to read through all the command line inputs (which in this example is only one, but this template will allow you to create multiple command line input options). We then said, “if the command line option is ‘s’ then set the symbol variable to whatever follows it”. We also morphed “whatever follows it” into an upper case, string variable to avoid case-sensitive user input.

We then set our filename variable and proceeded to read our text file into our data frame (df) as before.

This is complicated, but a major time saver. Please review this video Python – Command Line Basics as the extra 3 minutes might save you hours of our life by utilizing tricks like this!

Python for Algo Trading with Build Alpha

Build Alpha is algorithmic trading software that creates thousands of systematic trading strategies with the click of the button. Everything is point and click and made for traders without any programming capabilities.

However, I recently added the ability to add custom entry or exit signals via python for those that want to get even more creative or have existing indicators they would want to throw into the Build Alpha engine.

This is the fastest road to algo trading with the most flexibility. It allows you to test without wasting time coding up every single idea, but still permitting you to add unique ideas via python whenever inspiration strikes. To see how you can add custom signals via python to Build Alpha please head here: buildalpha.com/python

This is complicated, but a major time saver. Please review the video as the extra 3 minutes might save you hours of our life by utilizing tricks like this!

Remember for those of you who don’t want to learn programming you can use research tools like Build Alpha to save even more time.

Thanks for reading,
Dave


2 thoughts on “Python Tips – Reading Text Files, Working with dates, the command line

  1. I have a CSV file containing many columns, one of which contains datetime in the format: dd-mm-yyyy hh:mm:ss. My issue is that whenever I read the file, the ‘seconds’ field of the time value is lost and it’s affecting the subsequent computation. I was wondering if you had a solution for this?

    1. Yeah, you should be able to use something like this

      df = pd.read(“filename.csv”,delimiter=’,’,index_col=’Date’,parse_dates=True)

      Thanks,
      David

Leave a Reply

Your email address will not be published. Required fields are marked *