In [74]:
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}
In [75]:
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import talib
from talib.abstract import *
from talib import MA_Type
import itable
import pinkfish as pf
# format price data
pd.options.display.float_format = '{:0.3f}'.format
%matplotlib inline
In [76]:
# set size of inline plots
'''note: rcParams can't be in same cell as import matplotlib
or %matplotlib inline
%matplotlib notebook: will lead to interactive plots embedded within
the notebook, you can zoom and resize the figure
%matplotlib inline: only draw static images in the notebook
'''
plt.rcParams["figure.figsize"] = (10, 7)
Some global data
In [77]:
symbol = 'SPY'
start = datetime.datetime(2018, 1, 1)
end = datetime.datetime.now()
Fetch symbol data from cache, if available.
In [78]:
ts = pf.fetch_timeseries(symbol)
In [79]:
ts.tail()
Out[79]:
Select timeseries between start and end.
In [80]:
ts = pf.select_tradeperiod(ts, start, end)
In [81]:
ts.head()
Out[81]:
In [82]:
print('There are {} TA-Lib functions!'.format(len(talib.get_functions())))
Here is a complete listing of the functions by group:
In [83]:
for group, funcs in talib.get_function_groups().items():
print(group)
print('-----------------------------------------')
for func in funcs:
f = Function(func)
print('{} - {}'.format(func, f.info['display_name']))
print()
There are 2 different API that are available with talib, namely Function API and Abstract API. For the Function API, you pass in a price series. For the Abstract API, you pass in a collection of named inputs: 'open', 'high', 'low', 'close', and 'volume'. One or more of these may be used as defaults, but can be changed with the 'price' parameter.
Print the function instance to get documentation. We see that SMA has the parameter 'timeperiod' with default '30'. The input_arrays can be a dataframe with columns named 'open', 'high', 'low', 'close', and 'volume'.
In [84]:
print(SMA)
More information is available through the 'info' property. We observe here that the default price used is 'close'. This can be changed by setting 'price' in the function call, e.g. price='open'.
In [85]:
print(SMA.info)
If we just want to see the inputs, we can print the input_names property.
In [86]:
print(SMA.input_names)
If we just want to see the parameters, we can print the paramters property.
In [87]:
print(SMA.parameters)
If we just want to see the outputs, we can print the output_names property.
In [88]:
print(SMA.output_names)
Create technical indicator: SMA (using defaults: timeperiod=30, price='close')
In [89]:
sma = SMA(ts)
sma.tail()
Out[89]:
Create technical indicator: SMA (using: timeperiod=200, price='close')
In [90]:
sma200 = SMA(ts, timeperiod=200)
sma200.tail()
Out[90]:
Create technical indicator: SMA (using: timeperiod=200, price='open')
In [91]:
sma200 = SMA(ts, timeperiod=200, price='open')
sma200.tail()
Out[91]:
In [92]:
ts['sma200'] = sma200
ts.tail()
Out[92]:
Commodity Channel Index
In [93]:
print(CCI)
In [94]:
print(CCI.input_names)
In [95]:
print(CCI.parameters)
In [96]:
cci = CCI(ts)
ts['cci'] = cci
ts.tail()
Out[96]:
Bollinger Bands
In [97]:
print(BBANDS)
In [98]:
print(BBANDS.input_names)
In [99]:
print(BBANDS.parameters)
Print the available moving average types
In [100]:
attributes = [attr for attr in dir(MA_Type)
if not attr.startswith('__')]
attributes
Out[100]:
In [101]:
MA_Type.__dict__
Out[101]:
In [102]:
print(MA_Type[MA_Type.DEMA])
Set timeperiod=20 and matype=MA_Type.EMA
In [103]:
#upper, middle, lower = BBANDS(ts, timeperiod=20, matype=MA_Type.EMA)
#(for some reason, the abstract API doesn't work for BBANDS, so use the function API)
upper, middle, lower = talib.BBANDS(ts.close, timeperiod=20, matype=MA_Type.EMA)
ts['upper'] = upper; ts['middle'] = middle; ts['lower'] = lower
ts.tail()
Out[103]:
In [104]:
print(MOM)
In [105]:
mom10 = MOM(ts, timeperiod=10)
mom10.tail()
Out[105]:
In [106]:
ts['mom10'] = mom10
ts.head(50)
Out[106]:
In [107]:
m1 = ts['close'].pct_change(periods=10)
ts['m1'] = m1
ts.head(50)
Out[107]:
In [ ]: