0

我想从 yfinance 模块中获取历史收盘价,并创建一个数据框,其中有一列包含存储在 Holdings 列表中的每个代码的收盘价。除了最后创建该数据框外,我可以做任何事情。有人可以帮忙吗?:

Holdings = ['RL', 'AMC', 'BYND', 'BRK-B', 
            'BBY', 'AYX', 'AAPL', 'KO', 
            'FB', 'RACE', 'INTC', 'PFE', 
            'CRM', 'WFC', 'JPM', 'GOOG']

Hist_Holdings = []

for symbol in Holdings:
    Ticker = yf.Ticker(symbol)
    Hist = Ticker.history(period = "6mo", interval = "1d")
    
    Hist = Hist['Close']
    Hist.columns = [symbol]
    Hist_Holdings.append(Hist)
4

1 回答 1

0

所需的数据框格式未知,但以下代码会将您想要获取的股票与空格连接起来。它速度很快,并以数据帧格式返回数据。下面的代码仅指定收盘价。

import yfinance as yf
import datetime

now_ = datetime.datetime.today()

start = datetime.datetime(now_.year, now_.month - 6, now_.day + 1)
end = datetime.datetime(now_.year, now_.month, now_.day - 1)

Holdings = 'RL AMC BYND BRK-B BBY AYX AAPL KO FB RACE INTC PFE CRM WFC JPM GOOG'
data = yf.download(Holdings, start=start, end=end)['Close']

AAPL    AMC     AYX     BBY     BRK-B   BYND    CRM     FB  GOOG    INTC    JPM     KO  PFE     RACE    RL  WFC
Date                                                                
2020-06-12  84.699997   5.89    141.130005  77.760002   181.210007  144.740005  175.110001  228.580002  1413.180054     59.330002   99.870003   45.599998   32.020874   167.889999  74.769997   27.969999
2020-06-15  85.747498   5.80    142.940002  80.010002   181.550003  154.000000  178.610001  232.500000  1419.849976     60.099998   101.250000  46.299999   31.650854   169.699997  73.739998   28.209999
2020-06-16  88.019997   5.56    145.690002  83.470001   182.300003  151.940002  180.479996  235.649994  1442.719971     60.400002   102.059998  46.770000   31.688805   169.690002  76.419998   28.520000
2020-06-17  87.897499   5.42    150.990005  83.239998   180.860001  156.339996  181.399994  235.529999  1451.119995     60.490002   99.480003   46.580002   31.840607   169.809998  74.480003   27.450001
2020-06-18  87.932503   5.63    160.779999  82.300003   180.729996  158.199997  187.660004  235.940002  1435.959961     60.080002   98.940002   46.990002   31.537003   168.580002  73.940002   27.549999
于 2020-12-12T09:57:51.810 回答