I have a time series containing stock price data.

I would like to calculate the Money Flow Index (MFI) for each row.

Given that the MFI, uses the previous approx. 14 rows to calculate the current MFI, what would be the best approach to doing this?

The below calculates the current MFI for a given DataFrame, but I would like to implement this for each row in the data frame

# Typical Price =(high price + low price + closing price) / 3

# Raw money flow = typical price x volume
tp['Raw']=tp['Price'] * hst['Volume']

# Identify flow (upwards or downwards)
tp['Direction'] = np.where(np.nan_to_num(tp['Raw'].shift(1))>tp['Raw'], 'up', 'down')
# Money flow ratio = (14-day Positive Money Flow) / (14-day Negative Money Flow)
if tp.loc[tp['Direction']=='down']['Price'].sum(axis=0) != 0:
    mfr=tp.loc[tp['Direction']=='up']['Price'].sum(axis=0) / tp.loc[tp['Direction']=='down']['Price'].sum(axis=0)

# MFI = 100 - 100 / (1 + money flow ratio)
100-(100/(1+ mfr))

I managed to work this using the 'apply' function, but this function only allows for 1 argument, whilst I would need to pass more than 1 argument to compute the above.


1 Answer 1


Worked with the below to add a column that calculates the above function

quote['MFITEST']=quote['Date'].apply(lambda x: calc_mfi(quote,14,x))
  • 1
    $\begingroup$ See also currying. $\endgroup$
    – Emre
    Aug 22, 2016 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.