数据框中股票列表的历史股价

2024-03-29 02:06:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Pyhton的新手,在我的数据框架中,我很难获得股票的历史股价:

我有以下数据框

列新代码:MSGE、IT、NVST
专栏日期:2020-04-202020-02-292019-12-18

我很难为每只股票生成从各自日期到年初至今的历史股价,例如MSGE从2020-04-20到今天的股价

我是否需要为每只股票创建一个单独的数据框,或者我可以将它们放在一起

非常感谢你的提示

import pandas as pd
import yfinance as yf
from yahoofinancials import YahooFinancials

assets=df['New_ticker'].tolist()
yahoo_financials=YahooFinancials(assets)
data=yahoo_financials.get_historical_price_data(start_date='2019-01-01',end_date='2020-08-30',time_interval='daily')
prices_df = pd.DataFrame({a: {x['formatted_date']: x['adjclose'] for x in data[a]['prices']} for a in assets})
prices_df

但当我调用价格时,没有得到数据


Tags: 数据importdfdatadateas历史yahoo
1条回答
网友
1楼 · 发布于 2024-03-29 02:06:48

这是我第一次下载并使用yfinance。您可以使用以下代码获得它。另一个代码是通过使用“Alpha Vantage”API使用“panda_datareader”获得的,Alpha Vantage's API是无邮件的,它只需pandas datareader即可获得

# yfinance
import yfinance as yf

import datetime
import pandas as pd

ticker = ["MSGE", "IT", "NVST"]
msge = "MSGE"
now_ = datetime.datetime.today()

start = datetime.datetime(2020, 4, 1)
end = datetime.datetime(now_.year, now_.month, now_.day - 1)

all_df = pd.DataFrame()
for i in ticker:
    data = yf.download(i, start=start, end=end)
    data['symbol'] = i
    all_df = all_df.append(data)

all_df.head()
    Open    High    Low Close   Adj Close   Volume  symbol
Date                            
2020-04-09  100.000000  100.000000  86.510002   88.510002   88.510002   1900    MSGE
2020-04-13  85.000000   86.830002   80.250000   80.250000   80.250000   167400  MSGE
2020-04-14  75.150002   77.559998   75.150002   75.190002   75.190002   5300    MSGE
2020-04-15  72.150002   76.000000   72.150002   76.000000   76.000000   800 MSGE
2020-04-16  76.000000   76.000000   74.599998   74.599998   74.599998   75300   MSGE

import matplotlib.pyplot as plt
# import matplotlib.dates as mdates

fig = plt.figure(figsize=(8,4),dpi=144)
ax = fig.add_subplot(111)

msge = all_df[all_df['symbol'] == 'MSGE']
it = all_df[all_df['symbol'] == 'IT']
nvst = all_df[all_df['symbol'] == 'NVST']

ax.plot(msge.index, msge['Adj Close'], color='g', lw=1.5, label='MSGE')
ax.plot(it.index, it['Adj Close'], color='b', lw=1.5, label='IT')
ax.plot(nvst.index, nvst['Adj Close'], color='orange', lw=1.5, label='NVST')

plt.legend()
plt.show()

enter image description here

# pandas_datareader and Alpha Vantage API
import datetime
import pandas as pd
import pandas_datareader.data as web
import mplfinance as mpf
import matplotlib.pyplot as plt

with open('./alpha_vantage_api_key.txt') as f:
    api_key = f.read()

now_ = datetime.datetime.today()

start = datetime.datetime(2019, 1, 1)
end = datetime.datetime(now_.year, now_.month, now_.day - 1)
symbol = 'MSGE'
df = web.DataReader(symbol, 'av-daily', start, end, api_key=api_key)

df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
df.index = pd.to_datetime(df.index)

df.head()
Open    High    Low Close   Volume
2020-04-09  100.00  100.00  86.51   88.51   1900
2020-04-13  85.00   86.83   80.25   80.25   167421
2020-04-14  75.15   77.56   75.15   75.19   5281
2020-04-15  72.15   76.00   72.15   76.00   807
2020-04-16  76.00   76.00   74.60   74.60   74999

相关问题 更多 >