X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=script.py;h=9bc2fe64ac695f7833ec7a9ecd3d0c7e538c506f;hb=4c51aa716c2940b006430cad70739ddb965f5068;hp=473253309441b5da16c5983096f941a05ef20c03;hpb=269ec87c7f2a9b9b8fd6a707881c5477680abf31;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/script.py b/script.py index 4732533..9bc2fe6 100644 --- a/script.py +++ b/script.py @@ -1,4 +1,12 @@ import ccxt +# Put your poloniex api key in market.py +from market import market + +def static_var(varname, value): + def decorate(func): + setattr(func, varname, value) + return func + return decorate max_digits = 18 current_assets = { @@ -16,23 +24,31 @@ repartition_pertenthousand = { "SC": 623, } -market = ccxt.poloniex() def formatted_price(value): return round(value / 10**max_digits, 8) +@static_var("cache", {}) def get_ticker(c1, c2, market): + def invert(ticker): + return { + "bid": float(1/ticker["ask"]), + "ask": float(1/ticker["bid"]), + } + + if (c1, c2, market.__class__) in get_ticker.cache: + return get_ticker.cache[(c1, c2, market.__class__)] + if (c2, c1, market.__class__) in get_ticker.cache: + return invert(get_ticker.cache[(c2, c1, market.__class__)]) + try: - return market.fetch_ticker("{}/{}".format(c1, c2)) + get_ticker.cache[(c1, c2, market.__class__)] = market.fetch_ticker("{}/{}".format(c1, c2)) except ccxt.ExchangeError: try: - ticker = market.fetch_ticker("{}/{}".format(c2, c1)) - return { - "bid": float(1/ticker["ask"]), - "ask": float(1/ticker["bid"]), - } + get_ticker.cache[(c2, c1, market.__class__)] = market.fetch_ticker("{}/{}".format(c2, c1)) except ccxt.ExchangeError: - return None + get_ticker.cache[(c1, c2, market.__class__)] = None + return get_ticker(c1, c2, market) def assets_value(assets, market, base_currency="BTC"): repartition_in_base_currency = {}