quantopian/zipline — pull request #149 by jlowin (2013-04-29 00:58:14+00:00)
Title: Support for TA-Lib transforms

This PR adds support for TA-Lib transforms.

They are used like this:

```python
import zipline.transforms.ta as ta
class Algo(TradingAlgorithm):
def __init__(self, ...):
sid = 'XYZ'
self.close_ma = ta.MA(sid) # MA of closing price with default parameters
self.high_ma = ta.MA(sid, close='high') # MA of high price with default parameters
self.vix_ma = ta.MA(sid, close='vix', timeperiod=10) # MA of a data column called 'vix' with timeperiod of 10

def handle_data(data):
ma_result = self.close_ma.handle_data(data)

```

The reason for the "close=*" syntax is that the abstract functions of TA-Lib accept a dictionary with 'open', 'high', 'low', 'close' and 'volume' keys, and look to specific keys for data. MA looks at the close key. By passing an optional argument like `close = 'high'`, the user can instruct my transform to replace the default data of that key with something arbitrary that also exists in the dataframe.

The docstrings of the ta.* objects are dynamic and reflect not only the TA-Lib and Zipline docs, but the default mappings (for example, the fact that MA will use the 'close' field). So `help(ta.MA)` is your friend here -- or `ta.MA?` for IPython users!

For example, the top of the MA docstring:
```python
"""
#----------------- TALIB docs

MA(real[, timeperiod=?, matype=?])

Moving average (Overlap Studies)

Inputs:
real: (any ndarray)
Parameters:
timeperiod: 30
matype: 0 (Simple Moving Average)
Outputs:
real

#----------------- Default Zipline to TALIB mapping

price : close

#----------------- Zipline docs

TA-Lib keyword arguments must be passed at initialization. For
example, to construct a moving average with timeperiod of 5, pass
"timeperiod=5" during initialization.
"""
```

Many thanks to @mrjbq7, whose work on the python talib made this possible.