-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pandas 2.1.0 and mfi indicator #731
Comments
Hello @zakcali,
Nice! Glad you find a work around and shared the code. 😎 Like many indicators, including mfi, are in need of an update. So this really helps! Thanks 😎 Kind Regards, |
this one runs faster, by using numpy arrays instead of pandas.series on my 376 items xu100 market. Please ensure that you are not sending an empty array. Calculate money flow indexdef calculate_mfi(high, low, close, volume, period):
typical_price = (high + low + close) / 3
money_flow = typical_price * volume
mf_sign = np.where(typical_price > np.roll(typical_price, shift=1), 1, -1)
signed_mf = money_flow * mf_sign
# Calculate gain and loss using vectorized operations
positive_mf = np.maximum(signed_mf, 0)
negative_mf = np.maximum(-signed_mf, 0)
mf_avg_gain = np.convolve(positive_mf, np.ones(period), mode='full')[:len(positive_mf)] / period
mf_avg_loss = np.convolve(negative_mf, np.ones(period), mode='full')[:len(negative_mf)] / period
epsilon = 1e-10 # Small epsilon value to avoid division by zero
mfi = 100 - 100 / (1 + mf_avg_gain / (mf_avg_loss + epsilon))
return mfi
df = pd.read_csv(file_name)
if len(df.index) == 0: # faster than if df.empty:
df_is_empty = True
else:
df_is_empty = False
if df_is_empty:
df["MFI"] = np.nan
else:
df["MFI"] = calculate_mfi(high=df["High"].values, low=df["Low"].values, close=df["Close"].values, volume=df["Volume"].values, period=MFI_PERIOD) |
Hello @zakcali Thanks for submitting a faster version. It is now available on the development branch. KJ |
@twopirllc any idea when this will be in main release? i cannot use dev branches and this is blocking pandas upgrade |
Hello @carissssa, A new main release will occur after outstanding PRs are completed and some other bugs and maintenance issues have been taken care of. When, however, is unknowable at this time. 😕
What is preventing the use of the dev branch? Kind Regards, |
Which version are you running? The lastest version is on Github. Pip is for major releases.
pandas-ta 0.3.14b
python 3.12
pandas 2.1.0
Do you have TA Lib also installed in your environment?
No
Have you tried the development version? Did it resolve the issue?
no, because is not compatible with python 3.12
Describe the bug
[A clear and concise description of what the bug is.](futurewarning: setting an item of incompatible dtype is deprecated and will raise in a future error of pandas)
To Reproduce
Expected behavior
pandas 2.0.3 doesn't give that warning
Screenshots
Reason is updated pandas behaviour
Additional context
I find a function from: https://gist.github.com/quantra-go-algo/d3ced509e55ceb15eb0a6bd283e6853f#file-ti_mfi-py
and fine tuned with chatgpt to execute faster than original code and pandas-ta:
I couldn't correct the pandas-ta bug, so I embedded below code to my python program
Thanks for using Pandas TA!
The text was updated successfully, but these errors were encountered: