Reputation: 1285
I've looked at a few different solutions on StackOverflow here, but I haven't been able to successfully implement one of them in my application. Mostly because I'm not entirely sure what each solution is doing, so I'm hoping someone can help me out.
I have a simple script that collects daily stock data using DataReader, resamples that data to weekly data, and then I'm trying to see if there's been a gap down in price from the previous week's closing price to this week's opening price.
My code looks like this, which anyone should be able to implement:
import pandas_datareader.data as web
from datetime import datetime
import pandas as pd
# Inputs for stock data
stocks = ['AAPL']
ex = 'yahoo'
start = datetime(2018, 1, 2)
end = datetime(2019, 7, 2)
# Now, it'll get the daily stock data for each stock, and convert it to weekly data
for i in stocks:
a = web.DataReader(i, ex, start, end)
output = a.resample('W').agg({'Open': 'first',
'High': 'max',
'Low': 'min',
'Close': 'last',
'Volume': 'sum'})
output.index=output.index+pd.DateOffset(days=-6) # -6 Makes it Monday as the start of the week
print(output)
# Now, to iterate over each row, and print each time theres a gap
for index, row in output.iterrows():
currentopen = row['Open']
prevclose = row['Close'].shift(1)
if currentopen > prevclose:
print('The current open gapped up from last weeks close.')
elif currentopen < prevclose:
print('Houston we have a gap down!')
So the current row in the "for" loop represents the current week, and I'm trying to get the "Close" price from the previous row using .shift(). Currently I get the error:
AttributeError: 'numpy.float64' object has no attribute 'shift'
...which I know I'm using the shift wrong in this case. A couple of the solutions I found were below:
get previous row's value and calculate new column pandas python
Comparing previous row values in Pandas DataFrame
...but I'm not sure what I'm reading with those, and how one of them might apply to my app. I'm hoping a more skilled programmer can point me in the right direction with a bit of an explanation so I can learn. Thanks!
Upvotes: 0
Views: 318
Reputation: 142960
I can't test it but I see two methods:
First: use variable to keep value from previous loop in for
prevclose = None
for index, row in output.iterrows():
currentopen = row['Open']
if prevclose:
if currentopen > prevclose:
print('The current open gapped up from last weeks close.')
elif currentopen < prevclose:
print('Houston we have a gap down!')
prevclose = row['Close']
Second: create column with shifted data to have both values in the same row
outer['prevClose'] = outer['Close'].shift()
for index, row in output.iterrows():
currentopen = row['Open']
prevclose = row['prevClose']
if prevclose:
if currentopen > prevclose:
print('The current open gapped up from last weeks close.')
elif currentopen < prevclose:
print('Houston we have a gap down!')
Upvotes: 1