Reputation: 291
I am getting the following error:
Traceback (most recent call last):
File "tests.py", line 101, in <module>
test()
File "tests.py", line 95, in test
print(connorsRSI(df))
File "/Users/WorkFLOW/app.py", line 14, in connorsRSI
if data.loc[index, 'Diff'] > 0:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 1478, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
On this line if data.loc[index, 'Diff'] > 0:
And don't know how to fix it.
Anybody knows any other way to rewrite it?
I know there are other questions like this one but none has helped me solve this error.
-- UPDATE: df.head() ==>
High Low Open Close Volume Adj Close
Date
2019-11-04 98.529999 95.599998 96.349998 98.150002 3841000 98.150002
2019-11-05 98.989998 96.629997 98.129997 98.260002 2719900 98.260002
2019-11-06 99.180000 97.970001 98.680000 98.669998 2447900 98.669998
2019-11-07 99.290001 97.019997 99.290001 97.230003 2156400 97.230003
2019-11-08 98.500000 96.320000 96.910004 98.269997 2306600 98.269997
Full code here:
import pandas_datareader.data as web
def connorsRSI(df):
data = df.copy()
data = data[['Close']]
data['Close'] = round(data['Close'], 2)
data['Diff'] = data['Close'].diff()
data['UpDown'] = 0
for i, index in enumerate(data.index[1:]):
if data.loc[index, 'Diff'] > 0:
if data.loc[data.index[i], 'Diff'] > 0:
data.loc[index, 'UpDown'] = data.loc[data.index[i], 'UpDown'] + 1
else:
data.loc[index, 'UpDown'] = 1
else:
if data.loc[data.index[i], 'Diff'] > 0:
data.loc[index, 'UpDown'] = -1
else:
data.loc[index, 'UpDown'] = data.loc[data.index[i], 'UpDown'] - 1
xpct = data['Diff'][-1]
data2 = data.copy()
data2=data2[-100:]
counter = data2[data2['Diff'] < xpct].count()['Diff']
percentrank = round((counter/(len(data2)))*100,2)
rsi = round(talib.RSI(data['Close'], timeperiod=3)[-1],2)
streak = round(talib.RSI(data['UpDown'], timeperiod=2)[-1],2)
crsi = round((rsi+streak+percentrank)/3,2)
return crsi
def test():
df = web.DataReader('EA', 'yahoo', start, end)
print(connorsRSI(df))
test()
Upvotes: 0
Views: 238
Reputation: 2579
data['UpDown'] = (data['Diff'] > 0)
data['UpDown'] = data.groupby((df1['UpDown'] != data['UpDown'].shift()).cumsum()).cumcount() +1
data.loc[data['Diff'] < 0, 'UpDown'] *= -1
These three lines replace your loop. The first makes a bool to check whether diff is positive or negative. The second line counts consecutive increases or decreases, and finally the third makes consecutive decreases negative.
Upvotes: 1