Random Notes with Python

I admit I had a lot of trouble just getting up and running in Python. It probably should have been easier but programming languages seem to have gotten a bit more complex since Fortran and C++ where all you need was an editor and a compiler. So I’m sharing these notes to hopefully help anyone else out there who has an interest in using Python specifically for back testing trading strategies. Python can be used for modelling and forecasting and a whole host of other things but for my project I basically needed to download data, arrange it, run some calculations, and produce an equity curve. I wanted to test a simple VXX strategy based on Realized / Implied Divergence I read online somewhere.

1) Download & Install Anaconda 64-bit: https://store.continuum.io/cshop/anaconda/ This is what’s known as a ‘distribution’ for further reference. Anaconda comes pre-installed with most ‘modules’ one needs to turn it into a back testing engine. Things like NumPy, Matplotlib, SciPy, Pandas are already included and are necessary. It also utilizes IPython which I found to be an excellent tool since it splits bits of code into cells. There is no need to compile and run the entire code which saves time and helps error checking!


Figure 1

2) To run Ipython, find that little search button by moving the mouse all the way to the right. Yeah I know, most of you know this but I admit it took me awhile in Windows 8. Search for Ipython and run Ipython Notebook. This will open a command prompt and in turn open your browser to Ipython Notebook.

3) Next step is to get data from the Internet. There’s actually a lot of ways of doing this. If you want to use Pandas (which I believe is the ‘standard’ method) here is a tutorial: http://nbviewer.ipython.org/github/twiecki/financial-analysis-python-tutorial/blob/master/1.%20Pandas%20Basics.ipynb I utilized another module called tradingwithPython from this site: http://www.tradingwithpython.com/?page_id=504 There you can download an executable and it installs automatically. Figure 1 is the code to download data from Yahoo. I think if you spend 5 minutes and have a little bit of programming background, the relevant lines of code should become apparent and then you can transform it to your needs. The DataFrame is pretty awesome as it automatically aligns the data by date! Otherwise the starting dates for the respective securities would be off and your back test won’t make much sense!

4) Figure 2 below is the guts of the strategy. It’s a bit more complicated to figure out but feel free to ask questions and I’ll do my best. This is really where some of the ‘learning’ occurs and going back from Ipython to Google search really helps. Running code in individual cells really shine as you don’t have to compile previous sections over again which may be very time consuming.

Figure 2

Figure 2

5) Downloading external modules was another relatively difficult task for me that should have taken 5 minutes lol. I downloaded the DX Analytics module: http://dx-analytics.com/index.html#installation-usage It has some really cool mean-variance optimization functions and some derivative modelling functions I haven’t really gotten into yet. The trick is putting the unzipped folder in the right place! I finally found out under C:\users\…\Anaconda\Lib\site-packages and just copied the whole folder into there. Ipython was able to find it when I imported any of the functions. Simple but it took me awhile lol.

These are just some notes which hopefully I will edit to and add in the future. This isn’t meant to be a complete tutorial but if you’re looking for a complete example, I learned a lot from going through this example strategy: http://nbviewer.ipython.org/urls/dl.dropboxusercontent.com/u/11352905/notebooks/trading%20with%20python%20example.ipynb?create=1 Hopefully it helps you too!

Edit (1/17/15)

I came across a great little function to calculate peak to trough draw downs of an equity curve or stock. The output is the number of points, dollars, percentage of the maximum drawdown as well as the duration. I can use this function to optimize variables using minimal drawdown as a constraint!

SPY Maximum DD was 74 points and lasted 1656 days from 1994 to 2015.

SPY Maximum DD was 74 points and lasted 1656 days from 1994 to 2015. 

~ by largecaptrader on January 13, 2015.

6 Responses to “Random Notes with Python”

  1. […] http://largecaptrader.com/2015/01/13/random-notes-with-python/ […]

    • great post and thanks for sharing. I have never used python before. This may not be what you were trying to achieve; but I think you would want to see a chart of something like this: =EXP(cumulative sum of the LN(returns)). Will try this later to graph the growth of a $1 over this dataset.

      • Thanks for pointing this out! I have to admit, when I first started quant trading and backtesting it took me a long time to realize why working with LN returns makes sense over arithmetic returns! FWIW, it actually slightly increased the Sharpe. Will fix the posting shortly!

  2. […] Random Notes with Python [Large Cap Trader] I admit I had a lot of trouble just getting up and running in Python. It probably should have been easier but programming languages seem to have gotten a bit more complex since Fortran and C++ where all you nee… […]

  3. […] what the top 10 hedge funds were. Thinking can be dangerous. So can leverage. In the previous Python post, I tested a simple RV timing tool in VXX and combined with TLT ETF as a proxy for bonds. When you […]

  4. […] continuation of an old post on Python, I’ve been playing around with an awesome new library built by P. Morissette simply […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: