Skip to content

Commit 6a26389

Browse files
committed
when updating symbol data, calculate start/end times on a per-symbol basis
1 parent 52082a2 commit 6a26389

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

pytradelib/data.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2+
import sys
23
import pytz
34
import pandas as pd
4-
from datetime import datetime
5+
from pandas.tseries.offsets import DateOffset
6+
from datetime import date, datetime
57
from collections import defaultdict
68

79
from pytradelib.store import CSVStore
@@ -40,16 +42,21 @@ def initialize_store(self):
4042
self._store.set_dfs(self._provider.download(symbols))
4143

4244
def update_store(self):
43-
# early return if data is already up-to-date
44-
last_trade_date = self._store.get_end_date(self._store.symbols[0])
45-
today = pd.Timestamp(datetime.now().strftime('%Y-%m-%d'), tz=pytz.UTC)
46-
if last_trade_date == today:
47-
return
45+
last_trading_day = pd.Timestamp(date.today(), tz=pytz.UTC)
46+
while last_trading_day.weekday() > 4:
47+
last_trading_day = last_trading_day - DateOffset(days=1)
48+
49+
symbols = {}
50+
for symbol in self._store.symbols:
51+
latest_dt = self._store.get_end_date(symbol)
52+
if latest_dt != last_trading_day:
53+
symbols[symbol] = {'start': latest_dt, 'end': last_trading_day}
54+
55+
if not symbols:
56+
return []
4857

49-
symbols = dict([(symbol, {'start': self._store.get_end_date(symbol),
50-
'end': datetime.now()})
51-
for symbol in self._store.symbols])
5258
self._store.set_dfs(self._provider.download(symbols))
59+
return symbols.keys()
5360

5461
def analyze(self):
5562
results = self._store.analyze()
@@ -60,6 +67,6 @@ def analyze(self):
6067

6168

6269
if __name__ == '__main__':
63-
data_manager = DataManager(CSVStore(), QuandlDailyWikiProvider(batch_size=30))
64-
data_manager.update_store()
70+
data_manager = DataManager(CSVStore(), QuandlDailyWikiProvider())
71+
# data_manager.update_store()
6572
data_manager.analyze()

pytradelib/quandl/wiki.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class QuandlDailyWikiProvider(object):
11-
def __init__(self, api_key=None, batch_size=100, sleep=None):
11+
def __init__(self, api_key=None, batch_size=20, sleep=20):
1212
self._api_key = api_key
1313
self._downloader = Downloader(batch_size=batch_size, sleep=sleep)
1414

@@ -26,9 +26,10 @@ def download(self, symbols, start=None, end=None):
2626
csv = self._downloader.download(url)
2727
return csv_to_df(csv)
2828
elif isinstance(symbols, (list, tuple)):
29-
urls = [self._construct_url(symbol, start, end) for symbol in symbols]
29+
urls = [self._construct_url(symbol, start, end)
30+
for symbol in symbols]
3031
elif isinstance(symbols, dict):
31-
urls = [self._construct_url(symbol, d['start'], d['end'])\
32+
urls = [self._construct_url(symbol, d['start'], d['end'])
3233
for symbol, d in symbols.items()]
3334
else:
3435
raise Exception('symbols must be a string, a list of strings, or a dict of string to start/end dates')

0 commit comments

Comments
 (0)