Aug-20-2021, 07:37 PM
Hi all,
When I download stock data from Yahoo! Finance, it comes in a dataframe with datetime index. What I'm ultimately trying to do here is add buy and sell dates to lists and then subtract them to get duration.
I have a df with column 'Signal' that says 'Yes' if I should buy. Then:
In my debugging effort, I tried this:
So... why can I subtract dates in the latter case but not in the former?
Thanks!
When I download stock data from Yahoo! Finance, it comes in a dataframe with datetime index. What I'm ultimately trying to do here is add buy and sell dates to lists and then subtract them to get duration.
I have a df with column 'Signal' that says 'Yes' if I should buy. Then:
BuyDates = [] SellDates = [] for i in range(len(df)-11): if df.Signal.iloc[i]: BuyDates.append(df.iloc[i+1].name) for j in range(1,11): if df['RSI'].iloc[i+j]>40: SellDates.append(df.iloc[i+j+1].name) break # elif j==10: elif df.iloc[i+j+1].name - df.iloc[i].name > 10: #if next trading day is more than 10 days from start, then SellDates.append(df.iloc[i+j].name)The trade should be up to 10 days long so lines 12-13 are trying to say if tomorrow's date-- (i + j + 1).name --minus start date (i) is greater than 10, append today's date-- (i + j).name --to list SellDates. Line 12 gets: TypeError: '>' not supported between instances of 'Timedelta' and 'int'
In my debugging effort, I tried this:
for i in range(len(BuyDates)): print(BuyDates[i],'-------',SellDates[i],'-------',SellDates[i]-BuyDates[i])That worked! type(SellDates[i]) also comes back as a timestamp, which is what the datetime index is.
So... why can I subtract dates in the latter case but not in the former?
Thanks!