Assume you already installed and run the first demo project. Here is another simple project – download stock historical data from Yahoo finance server (aka yfinance). Please note that Yahoo does not want this kind of transaction, thus, it just sometimes kill these program transactions. In this case, just wait for 20-30 seconds and try again.

 

YouTube demo video here:

 

Project source code here. Copy and paste into your new project. Project name is not important. First file name is usually default to main.py, where .py is python file extension.

# Note: This is ver 2 of the source code. Use yfinance module instead of pandas_datareader. This is different than the Youtube video
# import pandas_datareader as dr
import yfinance as yf

start_date = '2000-01-02'
end_date = '2022-06-22'

to_write_csv = True

stock_list = ['0700.HK', '9988.HK']

for stock_num in stock_list:
    stockname = 'HK' + stock_num[0:4]
    print('-->' + stockname + '<--')

    # df = dr.data.get_data_yahoo(stock_num, start_date, end_date).reset_index()
    df = yf.download(stock_num, start_date, end_date).reset_index()
    df2 = df.dropna(axis=0, how="any", inplace=False)
    print(df2)
    df3 = df2[['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']]
    df3 = df3.round(decimals=4)
    print(df3)

    if to_write_csv:
        writefilepath = 'C:/pythonprojects/stocks/' + stockname + '.csv'
        df3.to_csv(writefilepath, mode='a', header=["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"], index=False)

    print('* end of ' + stockname)
    print('')

 

  • Line 1, import module pandas_datareader into your PyCharm project. Note the actual download module is pandas-datareader. Please watch the video above.
  • Line 3-4, change the date in yyyy-mm-dd format.
  • Line 8, add or change the stock code. e.g. ‘GOOGL’, ‘AMD’, etc. For Taiwan stock code, its format is similar to HK stock code format e.g. “2330.TW” for Taiwan Semiconductor Manufacturing Company.
  • Line 11, comment or change this line if the above is not Hong Kong stock code.
  • Line 17, change the column order sequence after data download, to match manual download data.
  • Line 18, round data to 4 decimal places.
  • Line 22, change the path and csv file name to save. In this example, file name will be “HK0700.csv” and “HK9988.csv”
  • Line 23, add header name on first row. Can be omitted. File is save in append mode.
  • For python new developer, remember to add space and keep the same indent after for loop and if statement.
  • All print statements are optional. They just output a message in debug console.

 

**** Note: Since Dec 2022, Yahoo finance server does not response correctly with pandas_datareader, and no data can be downloaded. To fix this problem, we need to change line 1 to (or add to line 2):

import yfinance as yf

Then change line 14 to:

df = yf.download(stock_num, start_date, end_date).reset_index()

Line 4 require to change to a future date, e.g.:
start_date = '2022-12-21'
end_date = '2022-12-22'
if today date is 2022-12-21