Python : download stock prices using the yfinance package

This post shows how to read prices of stock prices with a list of symbols as a string using Python. Splitting data by price types or symbols are illustrated as examples.


Read historical stock prices or indices





Python Jupyter notebook code


The following code downloads collection of stock prices.

import yfinance as yf
import pandas as pd
 
symbols = ['^GSPC','^VIX''^FTSE''^N225''^HSI']
data = yf.download(symbols, start='2022-12-01', end  ='2022-12-06')
print(data)
 
cs


[*********************100%***********************]  5 of 5 completed
              Adj Close                                                      \
                  ^FTSE        ^GSPC          ^HSI         ^N225       ^VIX   
Date                                                                          
2022-12-01  7558.500000  4076.570068  18736.439453  28226.080078  19.840000   
2022-12-02  7556.200195  4071.699951  18675.349609  27777.900391  19.059999   
2022-12-05  7567.500000  3998.840088  19518.289062  27820.400391  20.750000   
 
                  Close                                                      \
                  ^FTSE        ^GSPC          ^HSI         ^N225       ^VIX   
Date                                                                          
2022-12-01  7558.500000  4076.570068  18736.439453  28226.080078  19.840000   
2022-12-02  7556.200195  4071.699951  18675.349609  27777.900391  19.059999   
2022-12-05  7567.500000  3998.840088  19518.289062  27820.400391  20.750000   
 
            ...         Open                                           \
            ...        ^FTSE        ^GSPC          ^HSI         ^N225   
Date        ...                                                         
2022-12-01  ...  7573.100098  4087.139893  19058.900391  28273.130859   
2022-12-02  ...  7558.500000  4040.169922  18785.279297  27983.179688   
2022-12-05  ...  7556.200195  4052.020020  19221.679688  27752.990234   
 
                          Volume                                         
                 ^VIX      ^FTSE       ^GSPC        ^HSI     ^N225 ^VIX  
Date                                                                     
2022-12-01  20.830000  642843000  4527130000  4262000300  71400000    0  
2022-12-02  20.420000  540219900  4012620000  3757394000  79400000    0  
2022-12-05  20.299999  509145400  4280820000  4890142300  63900000    0  
 
[3 rows x 30 columns]
cs


We can split data by type of prices such as Adj Close, Close, Open, and the like.

# Splitting the downloaded data into separate DataFrames
adj_close_df = data['Adj Close']
close_df     = data['Close']
high_df      = data['High']
low_df       = data['Low']
open_df      = data['Open']
volume_df    = data['Volume']
 
# Printing the separate DataFrames
print("Adj Close:"); print(adj_close_df.round(2))
print("\nClose:");   print(close_df.round(2))
print("\nHigh:");    print(high_df.round(2))
print("\nLow:");     print(low_df.round(2))
print("\nOpen:");    print(open_df.round(2))
print("\nVolume:");  print(volume_df)
 
cs


Adj Close:
             ^FTSE    ^GSPC      ^HSI     ^N225   ^VIX
Date                                                  
2022-12-01  7558.5  4076.57  18736.44  28226.08  19.84
2022-12-02  7556.2  4071.70  18675.35  27777.90  19.06
2022-12-05  7567.5  3998.84  19518.29  27820.40  20.75
 
Close:
             ^FTSE    ^GSPC      ^HSI     ^N225   ^VIX
Date                                                  
2022-12-01  7558.5  4076.57  18736.44  28226.08  19.84
2022-12-02  7556.2  4071.70  18675.35  27777.90  19.06
2022-12-05  7567.5  3998.84  19518.29  27820.40  20.75
 
High:
             ^FTSE    ^GSPC      ^HSI     ^N225   ^VIX
Date                                                  
2022-12-01  7599.7  4100.51  19237.45  28423.46  21.06
2022-12-02  7570.5  4080.48  18841.22  27983.18  20.96
2022-12-05  7598.2  4052.45  19539.60  27854.11  21.29
 
Low:
             ^FTSE    ^GSPC      ^HSI     ^N225   ^VIX
Date                                                  
2022-12-01  7552.3  4050.87  18679.35  28226.08  19.80
2022-12-02  7508.0  4026.63  18530.82  27662.12  18.95
2022-12-05  7547.8  3984.49  19035.14  27700.86  19.78
 
Open:
             ^FTSE    ^GSPC      ^HSI     ^N225   ^VIX
Date                                                  
2022-12-01  7573.1  4087.14  19058.90  28273.13  20.83
2022-12-02  7558.5  4040.17  18785.28  27983.18  20.42
2022-12-05  7556.2  4052.02  19221.68  27752.99  20.30
 
Volume:
                ^FTSE       ^GSPC        ^HSI     ^N225  ^VIX
Date                                                         
2022-12-01  642843000  4527130000  4262000300  71400000     0
2022-12-02  540219900  4012620000  3757394000  79400000     0
2022-12-05  509145400  4280820000  4890142300  63900000     0
 
cs


We can also split data by each symbols.

# Create a MultiIndex from the columns
data.columns = data.columns.swaplevel(01)
data.sort_index(axis=1, level=0, inplace=True)
 
# Split the data based on symbols
symbol_dfs = {}
for symbol in symbols:
    # Create a copy of the DataFrame
    symbol_dfs[symbol] = data[symbol].copy()  
    # Divide 'Volume' column by 1000
    symbol_dfs[symbol]['Volume'/= 1000000
    symbol_dfs[symbol] = symbol_dfs[symbol].round(0)
 
# Print the separate DataFrames
for symbol, df in symbol_dfs.items():
    print(f"Data for symbol: {symbol}")
    print(df)
 
cs


Data for symbol: ^GSPC
            Adj Close   Close    High     Low    Open  Volume
Date                                                         
2022-12-01     4077.0  4077.0  4101.0  4051.0  4087.0  4527.0
2022-12-02     4072.0  4072.0  4080.0  4027.0  4040.0  4013.0
2022-12-05     3999.0  3999.0  4052.0  3984.0  4052.0  4281.0
Data for symbol: ^VIX
            Adj Close  Close  High   Low  Open  Volume
Date                                                  
2022-12-01       20.0   20.0  21.0  20.0  21.0     0.0
2022-12-02       19.0   19.0  21.0  19.0  20.0     0.0
2022-12-05       21.0   21.0  21.0  20.0  20.0     0.0
Data for symbol: ^FTSE
            Adj Close   Close    High     Low    Open  Volume
Date                                                         
2022-12-01     7558.0  7558.0  7600.0  7552.0  7573.0   643.0
2022-12-02     7556.0  7556.0  7570.0  7508.0  7558.0   540.0
2022-12-05     7568.0  7568.0  7598.0  7548.0  7556.0   509.0
Data for symbol: ^N225
            Adj Close    Close     High      Low     Open  Volume
Date                                                             
2022-12-01    28226.0  28226.0  28423.0  28226.0  28273.0    71.0
2022-12-02    27778.0  27778.0  27983.0  27662.0  27983.0    79.0
2022-12-05    27820.0  27820.0  27854.0  27701.0  27753.0    64.0
Data for symbol: ^HSI
            Adj Close    Close     High      Low     Open  Volume
Date                                                             
2022-12-01    18736.0  18736.0  19237.0  18679.0  19059.0  4262.0
2022-12-02    18675.0  18675.0  18841.0  18531.0  18785.0  3757.0
2022-12-05    19518.0  19518.0  19540.0  19035.0  19222.0  4890.0
 
cs





No comments:

Post a Comment