I received a good question in response to my previous post on starting to learn Python for finance. Here’s Mark’s question:
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?
About a year ago, I announced my intention to create a training course for Python. This has not happened, and the reason why is that I don’t think it’s possible to effectively teach someone a programming language–you have to do the work to learn it yourself. In this sense, it’s not unlike learning a performing or artistic skill; you can be taught the basics but you don’t stand a chance of understanding them until you actually use the tools, make mistakes, and learn to fix those mistakes. There are a number of pretty good books and courses out there, so I even hesitate to mention the resources I’ve found useful. Undoubtedly, there are other, maybe better resources, but I’ll share the ones I found most useful.
Learning the core language and libraries
I think, to a great extent, and I already laid out a clear roadmap for what you need to learn in my previous post. Let me stress, again, that you need to pay special attention understanding basic data structures and how to manipulate them. Examples, for instance, of how to slice and manipulate strings are going to be critical when you start to deal with financial data. Also, one of the mistakes I made early on was to ignore dictionaries. Don’t do that; dictionaries in Python are your friends (and they are wicked fast, no matter how much data you cram into them!) With this is mind, here are the websites and resources I found most useful for my first steps in learning this language.
Learn Python the Hard Way (website here, and book from Amazon here.) This is an old-school introduction to programming with a good emphasis on basics, repetition, and learning how to learn. Just go here (or buy the book) and patiently do everything he tells you to do. My only complaint is that this is very Python 2.X centric, and I think you probably want to work in 3.X eventually. (This material was written a few years ago, and, to some extent, 3.X has matured. There is still a ton of legacy code in 2.X (most?), and it’s pretty easy to make the transition between the two versions.) If you are a typical Windows user (i.e., you’re terrified of the command prompt) this is also a good introduction to break you out of the way you’ve interacted with computers for years. Nothing is perfect and we could nitpick forever over the details of this course, but it’s certainly one of the best I’ve seen.
Beginning Python from Novice to Professional (book here). For me, I found it most effective to be taught the same concepts more than once. I don’t mind going back to “this is a variable” stage, because you’re going to learn new things each time you cover that ground. This book has generated some controversial Amazon reviews, but I found it to be really fantastic and very accessible. This book will also take you into the core libraries in a bit more depth and get you deeper into the realm of object-oriented programming. If you’re new to that topic, you might find it frustratingly opaque (You’ll have conversations with yourself that go something like, “yes, I understand that dog and human are both instances of class mammals which is a subclass of animals. I even understand that dog and human both share the method eat and the attribute is_hungry, and that dog implements .bark while human, normally does not. This kind of makes sense, but why do I care?” Trust me, you do and will care, but you probably won’t know why until you need to use these concepts.)
You also need a tool (development environment) for writing your code. Technically, you can write Python code in any text editor. I agree with some of the teachers who say you should start learning by doing just this, but I don’t think it’s the best way to do real development work. There are many resources, but I’ve become a big fan of PyCharm. When you finish LPTHW, start looking at IDEs and take a good look at PyCharm.
Also, get comfortable with Jupyter (the new incarnation of the iPython notebook). I find this to be a really useful tool for prototyping code and for digger deeper into data. You might be just as happy working with Jupyter and a text editor as working in a full-fledged IDE, but different people have different preferences.
Sharpening the saw
If you work through the two resources above, you’ll have a good grasp of the basics. Now, start doing things. Build some programs and work on some projects. Here are a few other books I found useful.
You are going to need to learn Pandas to start working with data. I would buy Python for Data Analysis as a reference. I think it’s a book you need, and you will find yourself working through many of the examples. You can also start with 10 Minutes to Pandas (a short online introduction)–a great little tutorial to get you started, but then you’re quickly going to have to dig deeper. You should be able to load a csv file into Python, and/or pull data from Yahoo! Finance, and then you are well on your way to doing “real work”.
As you continue to develop skills, Think Python is a great book to teach you to think like a programmer. Programming isn’t just about learning what commands do, it’s an approach to problem solving. It’s about learning to take complex, real-world tasks and translate them to precise steps and solutions. It’s about understanding errors and reasons for errors. It’s about learning a new way to think, and this is one of the best books I’ve found for teaching yourself those skills.
Along the way, you need to be programming. Don’t just work through these resources and learn what they tell you to do. Find projects, and make projects for yourself. They can be very simple: generate a password for a website, for instance. They can also be much more complex: pull Yahoo! Finance data for two stocks and track P&L for a pairs trade over the past 100 trading days, for instance. You’ll make a lot of mistakes and go down a lot of dead ends, but that is how you learn. Your mistakes are learning opportunities.
Once you feel like you might actually be a programmer, it’s time to take one more pass through the language. The book I found most useful for this is a giant reference manual: Leaning Python. This is a deep and comprehensive look at the language, and I think it would be most useful to someone who already has a decent working grasp. In my opinion, time spent working through this is time well spent.