# --- Start of script --- from lxml import etree import logging import gzip import sys import yfinance as yf onlyMultiple = True createQuotes = False if len(sys.argv) > 3: if "quotes" in sys.argv[3].lower(): createQuotes = True if len(sys.argv) > 2: if "all" in sys.argv[2].lower(): onlyMultiple = False if len(sys.argv) > 1: data_file = sys.argv[1] else: print ("\nProvide one or two positional parameters:") print (" Required first argument: GNC data file.") print (" Required second argument: 'all' to display all tickers, not just those with multiple namespaces; else any non blank string.") print (" Optional third argument: 'quotes' to create an importable 'prices.csv' file.\n") exit() namespaces = dict() logger = logging.getLogger("yfinance") logger.disabled = True logger.propagate = False try: with gzip.open(data_file, "rb") as f: context = etree.iterparse(f, tag='{http://www.gnucash.org/XML/gnc}commodity') for event, elem in context: symbol = elem[1].text namespace = elem[0].text elem.clear() if symbol in namespaces: namespaces[symbol] = namespaces[symbol] + ", " +namespace else: namespaces[symbol] = namespace with open("prices.csv", "w", encoding="utf-8") as file: if not file.writable(): createQuotes = False for ticker in sorted(namespaces.keys()): if onlyMultiple: if namespaces[ticker].count(",") > 0: print ("{}:{}".format(ticker, namespaces[ticker])) else: print ("{}:{}".format(ticker, namespaces[ticker])) namespace = namespaces[ticker].split(",")[0] if namespace != "CURRENCY" and createQuotes: try: quotes = yf.Ticker(ticker) info = quotes.info prices = quotes.history(period="1wk", auto_adjust=True) if not prices.empty: currency = "USD" prices.reset_index(inplace=True) prices['Date'] = prices['Date'].dt.strftime('%m/%d/%Y') prices.drop(['Dividends','Stock Splits'], inplace=True, axis=1) prices.to_dict(orient='records') infoAttr = info.keys() if 'financialCurrency' in infoAttr: currency = info['financialCurrency'] if 'currency' in infoAttr: currency = info['currency'] for i in prices.index: close = prices['Close'][i] date = prices['Date'][i] high = prices['High'][i] low = prices['Low'][i] volume = prices['Volume'][i]/100 with open("prices.csv", "a", encoding="utf-8") as file: file.write('"{}","{}","{}",{},"{}"\n'.format(namespace, ticker, date, close, currency)) except Exception as e: pass except Exception as e: print ("Error: {}".format(e)) # --- End of script ---