Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
MFI with apply

Hi I managed to get a list of all rsi and Kama values of my stock list but I can seem to be able to accomplish the same with MFI. Any tip from a python guru here?

    rsi = prices.apply(t.RSI, timeperiod=context.rsi_window_size).iloc[-1]  
#OK  
    kama = prices.apply(t.MA, matype=6,timeperiod=5).iloc[-1]  
#OK  
    mfi = prices.apply(t.MFI, high=highs, volume=volumes, low=lows, close=prices, timeperiod=14).iloc[-1]  
#error  

best, Peter

4 responses

Is this what you're looking for?

import talib

def initialize(context):  
    context.stock = symbol('AAPL')

def handle_data(context, data):

    highs = history(30, '1d', 'high')  
    lows = history(30, '1d', 'low')[symbol('AAPL')]  
    closes = history(30, '1d', 'close_price')[symbol('AAPL')]  
    volumes = history(30, '1d', 'volume')[symbol('AAPL')]  
    print highs.apply(talib.MFI, low=lows, close=closes, volume=volumes, timeperiod=14)  

Tx Ed,

but this will not work for a whole stock universe, so I want to get the MFI for all stocks in my universe, if that makes sense

Peter

You could give this a try:

    highs = history(21, '1d', 'high')  
    lows = history(21, '1d', 'low')  
    closes = history(21, '1d', 'close_price')  
    volumes = history(21, '1d', 'volume')  
    MFI = pd.DataFrame(index=closes.index,columns=closes.columns)  
    for stock in list(closes.columns.values):  
        MFI[stock] = talib.MFI(highs[stock],lows[stock],closes[stock],volumes[stock])  

Peter,
Here is an MFI example algo, I use a function to aggregate the OHLCV data in a pandas panel, then pass the panel to a function. This same sort of thing can be used for any of the talib functions that require OHLCV data.

David