]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - script.py
Add log-in api key to market
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / script.py
index 473253309441b5da16c5983096f941a05ef20c03..9bc2fe64ac695f7833ec7a9ecd3d0c7e538c506f 100644 (file)
--- 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 = {}