- 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 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 ExchangeError:
- cls.ticker_cache[(c1, c2, market.__class__)] = None
- return cls.get_ticker(c1, c2, market)
-
- @classmethod
- def compute_trades(cls, values_in_base, new_repartition, only=None, market=None):
- base_currency = sum(values_in_base.values()).currency
- for currency in Balance.currencies():
- if currency == base_currency:
- continue
- trade = cls(
- values_in_base.get(currency, Amount(base_currency, 0)),
- new_repartition.get(currency, Amount(base_currency, 0)),
- currency,
- market=market
- )
- if only is None or trade.action == only:
- cls.trades[currency] = trade
- return cls.trades
-
- @classmethod
- def prepare_orders(cls, only=None, compute_value="default"):
- for currency, trade in cls.trades.items():
- if only is None or trade.action == only:
- trade.prepare_order(compute_value=compute_value)