Notebook
In [1]:
from quantopian.research.experimental import continuous_future, history
from quantopian.pipeline import Pipeline, CustomFactor
import pandas as pd
import numpy as np
import talib
In [11]:
eur = continuous_future('EC', offset=0, roll='volume', adjustment='mul')
gbp = continuous_future('BP', offset=0, roll='volume', adjustment='mul')
jpy = continuous_future('JY', offset=0, roll='volume', adjustment='mul')
chf = continuous_future('SF', offset=0, roll='volume', adjustment='mul')
cad = continuous_future('CD', offset=0, roll='volume', adjustment='mul')
aud = continuous_future('AD', offset=0, roll='volume', adjustment='mul')
nzd = continuous_future('NZ', offset=0, roll='volume', adjustment='mul')

ccy = aud

start = '2015-01-01'
end ='2016-01-01'

data = get_pricing(ccy, start_date=start, end_date=end, start_offset=14)

df = {'close': data['close_price'],
      'high': data['high'],
      'low': data['low'],
      'open': data['open_price'],
      'price': data['price']}

df = pd.DataFrame(df)

close = df['close']
high = df['high']
low = df['low']
price = df['price']

close.plot()
Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f7898440650>
In [12]:
df.head(10)
Out[12]:
close high low open price
2014-12-11 00:00:00+00:00 0.8035 0.8142 0.7986 0.8083 0.8035
2014-12-12 00:00:00+00:00 0.8015 0.8064 0.7996 0.8025 0.8015
2014-12-15 00:00:00+00:00 0.7976 0.8035 0.7966 0.8015 0.7976
2014-12-16 00:00:00+00:00 0.7986 0.8045 0.7966 0.7976 0.7986
2014-12-17 00:00:00+00:00 0.7888 0.8005 0.7878 0.7986 0.7888
2014-12-18 00:00:00+00:00 0.7937 0.7976 0.7888 0.7898 0.7937
2014-12-19 00:00:00+00:00 0.7917 0.7966 0.7898 0.7947 0.7917
2014-12-22 00:00:00+00:00 0.7908 0.7947 0.7898 0.7908 0.7908
2014-12-23 00:00:00+00:00 0.7878 0.7917 0.7868 0.7898 0.7878
2014-12-24 00:00:00+00:00 0.7878 0.7898 0.7878 0.7888 0.7878
In [23]:
atr1 = talib.ATR(high, low, close, timeperiod=1)
atr7 = talib.ATR(high, low, close, timeperiod=7)
atr9 = talib.ATR(high, low, close, timeperiod=9)
atr14 = talib.ATR(high, low, close, timeperiod=14)

atr_array = {'atr1': atr1,
             'atr7': atr7,
             'atr9': atr9,
             'atr14': atr14}

atr = pd.DataFrame(atr_array, index=df.index)
atr.head(20)
Out[23]:
atr1 atr14 atr7 atr9
2014-12-11 00:00:00+00:00 NaN NaN NaN NaN
2014-12-12 00:00:00+00:00 0.0068 NaN NaN NaN
2014-12-15 00:00:00+00:00 0.0069 NaN NaN NaN
2014-12-16 00:00:00+00:00 0.0079 NaN NaN NaN
2014-12-17 00:00:00+00:00 0.0127 NaN NaN NaN
2014-12-18 00:00:00+00:00 0.0088 NaN NaN NaN
2014-12-19 00:00:00+00:00 0.0068 NaN NaN NaN
2014-12-22 00:00:00+00:00 0.0049 NaN 0.007829 NaN
2014-12-23 00:00:00+00:00 0.0049 NaN 0.007410 NaN
2014-12-24 00:00:00+00:00 0.0020 NaN 0.006637 0.006856
2014-12-26 00:00:00+00:00 0.0030 NaN 0.006118 0.006427
2014-12-29 00:00:00+00:00 0.0049 NaN 0.005944 0.006257
2014-12-30 00:00:00+00:00 0.0078 NaN 0.006209 0.006429
2014-12-31 00:00:00+00:00 0.0059 NaN 0.006165 0.006370
2015-01-02 00:00:00+00:00 0.0107 0.006714 0.006813 0.006851
2015-01-05 00:00:00+00:00 0.0068 0.006720 0.006811 0.006846
2015-01-06 00:00:00+00:00 0.0069 0.006733 0.006824 0.006852
2015-01-07 00:00:00+00:00 0.0058 0.006667 0.006677 0.006735
2015-01-08 00:00:00+00:00 0.0058 0.006605 0.006552 0.006631
2015-01-09 00:00:00+00:00 0.0108 0.006904 0.007159 0.007094
In [21]:
atr['atr1'].plot()
atr['atr9'].plot()
Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f7898508f90>
In [45]:
class Tsl(CustomFactor):    
    
    inputs = [high, low, close, atr['atr7']]
    window_length = 20
    
    def compute(self, today, assets, out, high, low, close, atr7):

        hl2 = (high + low)/2. 
        up = hl2 + atr7*2  
        dn = hl2 - atr*2  
        
        supertrend = {}  
        if close[-2] < dn[-2] and close[-2] > dn[-2]:  
            supertrend[-1] = up[-1]  
        elif close[-2] >= up[-2] and close[-2] < up[-2]:  
            supertrend[-1] = dn[-1] 
        else:
            return
        
        out[:] = supertrend[-1]
        
tsl2 = Tsl()

AttributeErrorTraceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in run_code(self, code_obj, result)
   2893             if result is not None:
   2894                 result.error_in_exec = sys.exc_info()[1]
-> 2895             self.showtraceback()
   2896         else:
   2897             outflag = 0

/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in showtraceback(self, exc_tuple, filename, tb_offset, exception_only)
   1826                                             value, tb, tb_offset=tb_offset)
   1827 
-> 1828                     self._showtraceback(etype, value, stb)
   1829                     if self.call_pdb:
   1830                         # drop into debugger

/usr/local/lib/python2.7/dist-packages/ipykernel/zmqshell.pyc in _showtraceback(self, etype, evalue, stb)
    541             u'traceback' : stb,
    542             u'ename' : unicode_type(etype.__name__),
--> 543             u'evalue' : py3compat.safe_unicode(evalue),
    544         }
    545 

/usr/local/lib/python2.7/dist-packages/ipython_genutils/py3compat.pyc in safe_unicode(e)
     63     """
     64     try:
---> 65         return unicode_type(e)
     66     except UnicodeError:
     67         pass

/build/src/qexec_repo/zipline_repo/zipline/errors.pyc in __str__(self)
     29 
     30     def __str__(self):
---> 31         msg = self.msg.format(**self.kwargs)
     32         return msg
     33 

AttributeError: 'NoneType' object has no attribute 'format'
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: