Collect and plot gold prices from SBJ site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
import urllib2 | |
from datetime import date, timedelta | |
import matplotlib.pyplot as plt | |
last_buy_price = 0 | |
last_sell_price = 0 | |
def query(date): | |
# date_string's format yyyy-mm-dd | |
date_string = date.strftime('%Y-%m-%d') | |
base_url ='http://www.sacombank-sbj.com/service/tygiatrongnuoc.ashx?d=' | |
page = urllib2.urlopen(base_url + date_string) | |
return page.read() | |
def parse_price(html): | |
it = re.finditer(r'\d\.\d\d\d', html) | |
buy_price = int(it.next().group(0).replace('.','')) | |
sell_price = int(it.next().group(0).replace('.','')) | |
return (buy_price, sell_price) | |
def parse_date(html): | |
match = re.search(r'\d\d-\d\d-\d\d\d\d', html) | |
return match.group(0) | |
def validate_result(date, html): | |
if html.startswith('String was not recognized as a valid DateTime.'): | |
return False | |
resulted_date = parse_date(html) | |
expected_date = date.strftime('%d-%m-%Y') | |
return expected_date == resulted_date | |
def get_price(date): | |
global last_buy_price | |
global last_sell_price | |
html = query(date) | |
if validate_result(date, html): | |
buy, sell = parse_price(html) | |
last_buy_price = buy | |
last_sell_price = sell | |
return {'date': date, 'buy': buy, 'sell': sell} | |
else: | |
return {'date': date, 'buy': last_buy_price, 'sell': last_sell_price} | |
def get_date_list(from_date, to_date): | |
delta = timedelta(days=1) | |
d = from_date | |
while d <= to_date: | |
yield d | |
d = d + delta | |
def plot(prices): | |
buy = [p['buy'] for p in prices] | |
#sell = [p['sell'] for p in prices] | |
dates = [p['date'] for p in prices] | |
fig = plt.figure() | |
ax = fig.add_subplot(1,1,1) | |
ax.plot(dates, buy) | |
#ax2 = fig.add_subplot(1,1,1) | |
#ax2.plot(dates, sell) | |
fig.autofmt_xdate() | |
plt.savefig('test.png') | |
def main(): | |
oldest_available = date(2010, 8, 16) | |
prices = [get_price(d) for d in get_date_list(oldest_available, date.today())] | |
plot(prices) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment