Examples using the technical analysis libraries (ta and talib_wrapper)

tia package (pure python)


In [1]:
import tia.analysis.ta as ta
import tia.analysis.model as model

In [2]:
secs = model.load_yahoo_stock(['MSFT', 'INTC'], start='1/1/2000')


C:\Anaconda\lib\site-packages\pandas\tseries\base.py:363: FutureWarning: using '-' to provide set differences with datetimelike Indexes is deprecated, use .difference()
  "use .difference()",FutureWarning)
C:\Anaconda\lib\site-packages\pandas\core\index.py:1196: FutureWarning: using '-' to provide set differences with Indexes is deprecated, use .difference()
  "use .difference()",FutureWarning)

In [3]:
pxs = secs.frame
pxs.head()


Out[3]:
INTC MSFT
open high low close volume adj close dvds open high low close volume adj close dvds
Date
2000-01-03 83.26562 87.375 83.2500 87.0000 57710200 31.97828 NaN 117.3750 118.625 112.0000 116.5625 53228400 41.47075 NaN
2000-01-04 85.43750 87.875 82.2500 82.9375 51019600 30.48504 NaN 113.5625 117.125 112.2500 112.6250 54119000 40.06986 NaN
2000-01-05 83.00000 85.875 80.5000 83.6250 52389000 30.73774 NaN 111.1250 116.375 109.3750 113.8125 64059600 40.49235 NaN
2000-01-06 81.75000 83.375 78.1875 78.7500 55171200 28.94586 NaN 112.1875 113.875 108.3750 110.0000 54976600 39.13594 NaN
2000-01-07 77.50000 82.000 77.3750 82.0000 41096400 30.14045 NaN 108.6250 112.250 107.3125 111.4375 62013600 39.64737 NaN

In [4]:
close = pxs.swaplevel(1, 0, axis=1).close
close.head()


Out[4]:
INTC MSFT
Date
2000-01-03 87.0000 116.5625
2000-01-04 82.9375 112.6250
2000-01-05 83.6250 113.8125
2000-01-06 78.7500 110.0000
2000-01-07 82.0000 111.4375

In [5]:
# moving average calculation
ta.SMA(close, 10).tail()


Out[5]:
INTC MSFT
Date
2015-04-20 31.986 41.851
2015-04-21 32.103 41.962
2015-04-22 32.242 42.119
2015-04-23 32.353 42.305
2015-04-24 32.368 42.920

In [6]:
# macd
ta.MACD(close).tail()


Out[6]:
INTC MSFT
MACD_FAST MACD_SLOW MACD MACD_SIGNAL MACD_HIST MACD_FAST MACD_SLOW MACD MACD_SIGNAL MACD_HIST
Date
2015-04-20 32.025370 31.919124 0.106247 -0.172275 0.278522 41.886691 41.936118 -0.049427 -0.254985 0.205558
2015-04-21 32.087621 31.956966 0.130655 -0.111689 0.242344 42.002585 41.988257 0.014328 -0.201122 0.215450
2015-04-22 32.181833 32.012006 0.169827 -0.055386 0.225213 42.154495 42.062460 0.092034 -0.142491 0.234525
2015-04-23 32.207705 32.037042 0.170662 -0.010176 0.180839 42.336880 42.157093 0.179787 -0.078035 0.257823
2015-04-24 32.188058 32.040224 0.147834 0.021426 0.126408 43.188129 42.580271 0.607858 0.059143 0.548715

In [7]:
# rsi
ta.RSI(close, n=14).tail()


Out[7]:
INTC MSFT
Date
2015-04-20 57.489183 59.218833
2015-04-21 54.419580 56.288763
2015-04-22 56.662369 59.112946
2015-04-23 53.020355 61.772796
2015-04-24 50.332676 79.951610

In [8]:
# determine when 50 crosses the 200
cross = ta.cross_signal(ta.sma(close.MSFT, 50), ta.sma(close.MSFT, 200))
cross.tail()


Out[8]:
Date
2015-04-20   -1
2015-04-21   -1
2015-04-22   -1
2015-04-23   -1
2015-04-24   -1
dtype: float64

ta-lib wrapper

Provide ability to call ta-lib with DataFrame or Series which may contain NaN values.


In [9]:
import tia.analysis.talib_wrapper as talib

In [10]:
talib.MACD(close).tail()


Out[10]:
INTC MSFT
MACD MACD_SIGNAL MACD_HIST MACD MACD_SIGNAL MACD_HIST
Date
2015-04-20 0.106247 -0.172275 0.278522 -0.049427 -0.254985 0.205558
2015-04-21 0.130655 -0.111689 0.242344 0.014328 -0.201122 0.215450
2015-04-22 0.169827 -0.055386 0.225213 0.092034 -0.142491 0.234525
2015-04-23 0.170662 -0.010176 0.180839 0.179787 -0.078035 0.257823
2015-04-24 0.147834 0.021426 0.126408 0.607858 0.059143 0.548715

In [11]:
# average true range
talib.ATR(pxs).tail()


Out[11]:
INTC MSFT
Date
2015-04-20 0.762301 0.769888
2015-04-21 0.757137 0.759181
2015-04-22 0.752342 0.746383
2015-04-23 0.747889 0.750927
2015-04-24 0.727325 1.040146

In [12]:
talib.RSI(close).tail()


Out[12]:
INTC MSFT
Date
2015-04-20 57.489183 59.218833
2015-04-21 54.419580 56.288763
2015-04-22 56.662369 59.112946
2015-04-23 53.020355 61.772796
2015-04-24 50.332676 79.951610