+ fees_cache = {}
+ @classmethod
+ def fetch_fees(cls, market):
+ if market.__class__ not in cls.fees_cache:
+ cls.fees_cache[market.__class__] = market.fetch_fees()
+ return cls.fees_cache[market.__class__]
+
+ ticker_cache = {}
+ ticker_cache_timestamp = time.time()
+ @classmethod
+ def get_ticker(cls, c1, c2, market, refresh=False):
+ def invert(ticker):
+ return {
+ "inverted": True,
+ "average": (1/ticker["bid"] + 1/ticker["ask"]) / 2,
+ "original": ticker,
+ }
+ def augment_ticker(ticker):
+ ticker.update({
+ "inverted": False,
+ "average": (ticker["bid"] + ticker["ask"] ) / 2,
+ })
+
+ if time.time() - cls.ticker_cache_timestamp > 5:
+ cls.ticker_cache = {}
+ cls.ticker_cache_timestamp = time.time()
+ elif not refresh:
+ if (c1, c2, market.__class__) in cls.ticker_cache:
+ return cls.ticker_cache[(c1, c2, market.__class__)]
+ if (c2, c1, market.__class__) in cls.ticker_cache:
+ return invert(cls.ticker_cache[(c2, c1, market.__class__)])
+
+ try:
+ cls.ticker_cache[(c1, c2, market.__class__)] = market.fetch_ticker("{}/{}".format(c1, c2))
+ augment_ticker(cls.ticker_cache[(c1, c2, market.__class__)])
+ except ccxt.ExchangeError:
+ try:
+ cls.ticker_cache[(c2, c1, market.__class__)] = market.fetch_ticker("{}/{}".format(c2, c1))
+ augment_ticker(cls.ticker_cache[(c2, c1, market.__class__)])
+ except ccxt.ExchangeError:
+ cls.ticker_cache[(c1, c2, market.__class__)] = None
+ return cls.get_ticker(c1, c2, market)