'how can i get stock data for one hour in python?
I am new to python. I tried to get data of VIX index of 1 hour from 1990-01-01 until now but i got this error. Could anyone help please?
ticker = '^VIX'
start = dt.datetime(1990, 1, 1)
for i in range(24):
end = dt.datetime(2022,1,1)
prices = web.DataReader(ticker, 'yahoo', start, end)['Close']
returns = prices.pct_change()
last_price = prices[-1]
print(prices)
start=end
KeyError: 'Date'
Solution 1:[1]
All problem is because you use for-loop with start = end so in second loop you try to get data from start='2022,1,1' to end='2022,1,1' and this range doesn't have any data and it makes problem.
You should run it without for-loop
import datetime as dt
import pandas_datareader.data as web
ticker = '^VIX'
start = dt.datetime(1990, 1, 1)
end = dt.datetime(2022, 5, 1) # almost today
data = web.DataReader(ticker, 'yahoo', start, end)
close_prices = data['Close']
print(close_prices)
last_price = close_prices[-1]
print('last:', last_price)
returns = data.pct_change()
print(returns)
close_returns = close_prices.pct_change()
print(close_returns)
EDIT:
DataReader doesn't have option to read 1hour data.
Module yfinance can read 1hour data but only for last 7-8 days.
import yfinance as yf
data = yf.download('^VIX', period="8d", interval='1h')
print(data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
