Downloading Asset Price Data from Yahoo

This section provides code for downloading asset prices from yahoo using the yahoo finance python library

Implementation

The yahoo script given below implements a function named "history" which takes the following inputs:

  • ticker - this the ticker of the asset you wish to pull prices from. It can be a space separated list of tickers. For example "AAPL MSFT" will pull the prices of both Apple and Microsoft.
  • start date - in ISO format, example "2020-01-01"
  • end date in ISO format
  • use_file is a boolen which if set to true, will read the data from a local file if it exists, otherwise it will download the file from yahoo. If this is set to false, it will always download the file from yahoo

When a price file is requested, the system retrieves the file from Yahoo and saves it to a subdirectory named data underneath the current directory. The next time the same data is requested, it will read the file instead of going back to yahoo.

Full Code

import yfinance as yf from pathlib import Path from datetime import datetime, timezone import pandas as pd def to_list(data): list1 = data.to_dict(orient='records') list2 = [] for item in list1: nitem = {} list2.append(nitem) for key in item: key_str = str(key) value = item[key] if key[0] == 'Date': iso_string = value.isoformat() split1 = iso_string.split('T') value = split1[0] nitem[key_str] = value pass pass def read(filename): file_path = Path(filename) if file_path.is_file(): df = pd.read_csv(filename) return df pass def history(ticker_symbol, start, end, usefile=True): root = './' data = None filename = ticker_symbol.replace(' ', '_')+'-'+start+'-'+end+'.json' filename = ticker_symbol.replace(' ', '_')+'-'+start+'-'+end+'.csv' if usefile == True: data = read(root+'data/' + filename ) if data is None: data = yf.download(ticker_symbol, start=start, end=end) data = data.reset_index() #flatten the multi head columns data.columns = [":".join(reversed(col)).strip() for col in data.columns.values] data = data.rename(columns={':Date': 'Date'}) data.to_csv(root+'data/' + filename, index=False) return data return data