diff --git a/goxapi.py b/goxapi.py index 3f3e9bf..eea2c64 100644 --- a/goxapi.py +++ b/goxapi.py @@ -42,6 +42,7 @@ import logging import Queue import time +import datetime import traceback import threading from urllib2 import Request as URLRequest @@ -158,6 +159,7 @@ class GoxConfig(SafeConfigParser): ,["gox", "load_fulldepth", "True"] ,["gox", "load_history", "True"] ,["gox", "history_timeframe", "15"] + ,["gox", "history_length_days", "2"] ,["gox", "secret_key", ""] ,["gox", "secret_secret", ""] ] @@ -727,7 +729,17 @@ def request_history(self): # Gox() will have set this field to the timestamp of the last # known candle, so we only request data since this time - since = self.history_last_candle + endtime = datetime.datetime.now() + if self.history_last_candle: + since = self.history_last_candle + else: + days = self.config.get_int("gox", "history_length_days") + if not days: + days = 2 + starttime = datetime.datetime.now() - datetime.timedelta(days=days) + since = int(time.mktime(starttime.timetuple())) + + def history_thread(): """request trading history""" @@ -743,16 +755,30 @@ def history_thread(): self.debug("requesting history") use_ssl = self.config.get_bool("gox", "use_ssl") proto = {True: "https", False: "http"}[use_ssl] - json_hist = http_request("%s://%s/api/2/%s%s/money/trades%s" % ( - proto, - HTTP_HOST, - self.curr_base, - self.curr_quote, - querystring - )) - history = json.loads(json_hist) + data = [] + while True: + try: + json_hist = http_request("%s://%s/api/2/%s%s/money/trades%s" % ( + proto, + HTTP_HOST, + self.curr_base, + self.curr_quote, + querystring + )) + history = json.loads(json_hist) + if len(history["data"]) == 0: + break + data += history["data"] + querystring = "?since=" + data[-1]["tid"] + self.debug("download: %2.2f (days behind)" % ((time.mktime(endtime.timetuple()) - data[-1]["date"])/86400)) + if int(data[-1]["date"]) > int(time.mktime(endtime.timetuple())): + break + except ValueError: + self.debug("download: bad response, retrying") + time.sleep(1) + if history["result"] == "success": - self.signal_fullhistory(self, history["data"]) + self.signal_fullhistory(self, data) start_thread(history_thread)