Apply a talib function to multiple securities returned by the get_pricing
method
# Import the whole talib library
import talib
# Set some dates for ease of use
start_date = '2009-08-1'
end_date = '2009-10-1'
# Set some securities. Again just to make it easier to reference later on
aapl = symbols('AAPL')
ibm = symbols('IBM')
# Use get_pricing method to get some data
# the result is a Pandas dataframe
aapl_data = get_pricing(symbols=aapl,
start_date=start_date,
end_date=end_date,
frequency='daily',
fields=['high', 'low', 'open_price', 'close_price'])
# display the resulting dataframe
aapl_data.head(5)
# Lets see what type each column is
type(aapl_data['high'])
# Good. Each column is a series. All the Talib functions expect a series as inputs
# Let's run the talib function
aapl_CDLENGULFING = talib.CDLENGULFING(open=aapl_data.open_price,
high=aapl_data.high,
low=aapl_data.low,
close=aapl_data.close_price,
)
aapl_CDLENGULFING
# The Talib functions return a series which is the same length as the inputs
# Lets check the type
type(aapl_CDLENGULFING)
# Now check the length (the result should be the same length as the inputs)
len(aapl_CDLENGULFING)
# Check the input length
len(aapl_data)
# Good. All checks out.
# Notice that the talib functions return a numpy array
# To get the most current (or the last) value simply use Python indexing of -1
aapl_CDLENGULFING[-1]
# Everything works as expected.
# However, we were just getting data for a single security
# Let's try the exact thing with two securities
aapl_ibm_data = get_pricing(symbols=[aapl, ibm],
start_date=start_date,
end_date=end_date,
frequency='daily',
fields=['high', 'low', 'open_price', 'close_price'])
# With 2 securities the result is a pandas panel
# It's really much easier to work with dataframes
# So let's turn it into a dataframe using the to_frame() method
aapl_ibm_data_df = aapl_ibm_data.to_frame()
display(type(aapl_ibm_data), type(aapl_ibm_data_df))
# Lets see what aapl_ibm_data_df looks like
aapl_ibm_data_df.head(5)
# We can get one security (or a slice) at a time by using the XS method
aapl_slice = aapl_ibm_data_df.xs(aapl, level=1)
talib.CDLENGULFING(open=aapl_slice.open_price,
high=aapl_slice.high,
low=aapl_slice.low,
close=aapl_slice.close_price,
)
# Great. That is what we got before.
# However, we probably want to get all the results back at once. Not one at a time
# To do that we want to group by security and then apply our talib function to each group
# One little problem...
# The function in the apply method gets passed a dataframe. Our talib functions expect series.
# The simplest way around that is to make small helper function
def my_CDLENGULFING(df):
# Return the latest value of CDLENGULFING
return talib.CDLENGULFING(
open=df.open_price,
high=df.high,
low=df.low,
close=df.close_price,
)[-1]
# Now we can apply our talib function to each security
cdlengulfing_latest = aapl_ibm_data_df.groupby(level=1).apply(my_CDLENGULFING)
cdlengulfing_latest
# The result above is a series indexed by security. The values are the latest CDLENGULFING