Last active
November 20, 2016 08:07
-
-
Save sawadyrr5/7c05302c421dde5cc141787856b1d58d to your computer and use it in GitHub Desktop.
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/local/bin python | |
# -*- coding: UTF-8 -*- | |
from pandas_datareader.base import _DailyBaseReader | |
import pandas as pd | |
from datetime import datetime | |
import urllib.request | |
class StooqDataReader(_DailyBaseReader): | |
@property | |
def url(self): | |
return 'http://stooq.com/q/d/l/?s={symbol}&d1={datefrom}&d2={dateto}&i=d' | |
def _get_params(self, symbol): | |
params = dict( | |
symbol=symbol, | |
datefrom=datetime.strftime(self.start, '%Y%m%d'), | |
dateto=datetime.strftime(self.end, '%Y%m%d') | |
) | |
return params | |
def read(self): | |
# Use _DailyBaseReader's definition | |
result = self._read_one_data(self.url, params=self._get_params(self.symbols)) | |
return result | |
def _read_one_data(self, url, params): | |
url = url.format(**params) | |
response = urllib.request.urlopen(url) | |
try: | |
result = pd.read_csv(response, encoding='Shift_JIS') | |
result['Symbol'] = self.symbols | |
except pd.parser.CParserError: | |
result = pd.DataFrame() | |
if not result.empty: | |
result.set_index(['Symbol', 'Date'], inplace=True) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment