Support Board
Date/Time: Sat, 18 May 2024 05:11:42 +0000
Post From: Yahoo Data Downloader
[2014-06-14 06:12:38] |
Kiwi - Posts: 375 |
The following Python code will: - download a base file from your chosen date to the end of this year (stops when the yahoo data stops) (put dates in to suit yourself) - every 5 minutes create the data file you plot by appending the current yahoo data for today to the base file (my files are HSIbase and HSI and my data symbol is ^HSI) Just something you can use or mess around with if you like and want something that Sierra isn't providing currently. It is based on Corey Goldberg's excellent and flexibly general purpose ystockquote. # This is based on:
# # ystockquote : Python module - retrieve stock quote data from Yahoo Finance # # Copyright (c) 2007,2008,2013 Corey Goldberg (cgoldberg@gmail.com) # # license: GNU LGPL # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # Requires: Tested under Python 3.4 import time, datetime from urllib.request import Request, urlopen from urllib.parse import urlencode symbol = "^HSI" basedatafile = '/home/john/zRamdisk/SierraChart/Data/HSIbase.dly' datafile = '/home/john/zRamdisk/SierraChart/Data/HSI.dly' startdate = '2007-06-01' enddate = '2014-12-31' seconds_until_repeat = 300 def _request(symbol, stat): url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat) req = Request(url) resp = urlopen(req) content = resp.read().decode().strip() return content def setup_base(): def get_data(start_date, end_date): """ Get historical prices for the given ticker symbol. Date format is 'YYYY-MM-DD' """ params = urlencode({ 's': symbol, 'a': int(start_date[5:7]) - 1, 'b': int(start_date[8:10]), 'c': int(start_date[0:4]), 'd': int(end_date[5:7]) - 1, 'e': int(end_date[8:10]), 'f': int(end_date[0:4]), 'g': 'd', 'ignore': '.csv', }) url = 'http://ichart.yahoo.com/table.csv?%s' % params req = Request(url) resp = urlopen(req) content = str(resp.read().decode('utf-8').strip()) daily_data = content.splitlines() hist_data = [] for day in daily_data[len(daily_data):0:-1]: dd = day.split(',') hist_data.append([dd[0], dd[1], dd[2], dd[3], dd[4], dd[5]]) return hist_data with open(basedatafile, 'w') as f: ll = get_data(startdate, enddate) for l in ll: f.writelines(", ".join(l) +"\n") def update_day(): with open(datafile, 'w') as f: with open(basedatafile, 'r') as rf: for l in rf: f.writelines(l) t = _request(symbol, 'd1l1om').split(',') y = (datetime.date(int(l[0:4]), int(l[5:7]), int(l[8:10])) + datetime.timedelta(days=1)).isoformat() f.writelines(y + ', ' + t[2] + ', ' + t[3].split('"')[1].split()[2] + ', ' + \ t[3].split('"')[1].split()[0] + ', '+t[1] + ', 0') setup_base() while "lifeonearth": update_day() time.sleep(seconds_until_repeat) print("fell out the bottom") |