First steps in Python for traders

codeingI’ve written a bit on the difference between high and low level programming languages, and on why I think Python is the right choice for many traders today. In this post, I’ll assume you’re more or less starting from scratch, and will lay out the concepts you are going to need to focus on so that you can use the language effectively. Learning a language is fun, especially at first, but it can soon be frustrating. I want to give you some ideas on where to focus your energy in those first weeks, and give you some confidence to keep forging ahead when the learning process becomes a little bit more complicated.

Core language skills

Python is one of the more approachable languages, in my experience. Code often reads more or less like “human language”, especially if you name your variables intelligently. Which would you rather read: ‘for ticker in ticker_list:” or “for x in z:”? Python also depends on whitespace, which means that indentation matters to define blocks of  code. While this is essential for the interpreter (the program that makes Python programs run on your computer), it’s also very friendly to human eyes. Python code is elegant and easy to read (some examples here), but be careful–this is also a little deceptive.

Though Python might look like English, you’ll soon be reminded that it is not; writing code requires a different level of precision than writing in natural language. Though there are usually several ways to accomplish the same task, there are many, many more ways to do it wrong (or to not accomplish that task!) Don’t be lulled into a sense of complacency early on. Just remember that code is maddeningly precise and that vaunted approachability of Python is a potential liability for new programmers. So that’s the first thing I think the new programmer needs to learn: a respect for the precision of code.

With that in mind, learn the core language. There are a number of good books and decent tutorials out there that will make sure you hit all the important topics, perhaps in a different order. In my opinion, these core topics are:

  • Variables
  • Logic and making decisions (if this, do that, else do this)
  • Loops (doing things over and over)
  • Basic input/output (getting information from the user and writing things to the screen)
  • Basic file operations
  • Core data structures (lists, tuples, dictionaries)

This work will go pretty quickly, and most people can probably start from scratch and get to the point where you can write a basic Python program in a matter of weeks. This is a good and important foundation, but you are going to need to go deeper. Another core skill is learning how to look things up. Python is extremely well documented, and you can find most answers in the online docs. If you have questions that you can’t answer yourself, there’s a large and extremely helpful community here that will answer your questions. Whatever question you have, someone there does have the answer; my only advice would be to take some time writing good questions and be courteous.

Also, you need to learn early on how, mechanically, you will write the language. You can write Python code in any text editor, but there are a number of tools that can make it easier (or harder lol) on the programmer. I am partial to PyCharm as an editor, but this is a polarizing topic–if you find something you like better, use it. Don’t get too caught up in learning all the features and possibilities of that tool at this point though, you just need to focus on learning basic coding skills.

Refining core language skills

Some things in Python might not be super intuitive, and you’ll find that you start to make some mistakes. A good way to supercharge your learning is to keep a list of mistakes, and to resolve that once you make an error twice (or have to look the same thing up twice), make it a learning opportunity. Perhaps you make a flashcard, or simply keep a notebook of tricky language elements, but going back to things you struggle with, even a little bit, will help you learn much faster. Part of this is learning how to learn.

You need to become very comfortable with the built-in functions and core libraries of Python. (Those terms will return rewarding Google searches.) The basic language operations in the first part become much more powerful here, and you’ll find that there are already good solutions to many of the things you want to do. Some of the tutorials make a big deal out of you learning all the “keywords” of the language. There are words which the language defines to have special meaning. Words which are not keywords you can reassign and pretty much do whatever you want with, but don’t get too caught up in this. For instance, there are a number of built-in functions that you will use all the time that are not keywords. If you are a Python programmer, which set of words are more important: exec and global (which are keywords) or print and range (built-in functions).

I think one of the most important things you need to learn here is to dig deeper into the standard data structures. You need to be very familiar with lists and dictionaries: how to access elements, how to change, add, sort, reassign, slice, and do other operations on these data structures. Especially when you start manipulating financial data, these operations are your bread and butter, so they need to be second nature. Most tutorials and books don’t focus a lot of attention on these tools early on, but now you know that you need to. (Hint: you can learn a lot from string indexing, so pay attention when that topic comes up.) Also, spend some time understanding how the language represents numbers, and some of the issues with floating point arithmetic.

Pandas and Numpy

The basic philosophy of Python is that the core language is simple, and then great power is available in add-on libraries. Most of these libraries are free and are well-maintained by an active community of developers. Pandas and Numpy are libraries that have functions to store, manipulate, and analyze large data sets. Used correctly, they can also be very fast, since the operations are carried out at C speed. (As I’ve written before, high level languages tend to be slower than lower level languages. The number crunching speed of these libraries helps, but does not completely solve the problem.) Both of these libraries will be incredibly important to your work with financial data. I’m not going to go into this in depth here, since this post is aimed at someone taking the very first steps, but these are powerful tools that can already do much of what you want to do in Python. Time spent learning these tools is time very well spent. Once you have some basic familiarity with the language and are playing with data structures, you can start to explore what Pandas can do.

Those are some good first steps. I’ll share some ideas on next steps in another post.

AdamHGrimes

Adam Grimes has over two decades of experience in the industry as a trader, analyst and system developer. The author of a best-selling trading book, he has traded for his own account, for a top prop firm, and spent several years at the New York Mercantile Exchange. He focuses on the intersection of quantitative analysis and discretionary trading, and has a talent for teaching and helping traders find their own way in the market.

This Post Has 2 Comments

  1. retailpropshop

    Starting from total scratch, zero programming skills but got the motivation. Hope your not going to go too fast, started this week teaching myself python… Geting familiar with variables and strings :p Feel like a very long journey but if it is forth it at the end…

  2. Mark Reeve

    Hi Adam,
    Thanks again for all your work and all that you share. I am a little behind you in programming abilities but working furiously through my second MOOC on programming in python. I am finding it quite challenging at times (to the point of utter frustration) which is mostly caused by wondering if what I am learning has any relevance to programming in trading? I am wondering what resources you had access to when you embarked upon your (python) programming journey and if they were more trading related? Are there any resources you would recommend to help me through my struggles?
    Kind Regards,
    Mark

Comments are closed.