]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - script.py
Fetch assets from market
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / script.py
index 473253309441b5da16c5983096f941a05ef20c03..b31fd610f88df84a5dfce49a1e8bb46c8db7a321 100644 (file)
--- a/script.py
+++ b/script.py
@@ -1,12 +1,14 @@
 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 = {
-        "ETH":  int(2.00000000 * 10**max_digits),
-        "BTC":  int(1.23000000 * 10**max_digits),
-        "ZEC":  int(2.00000000 * 10**max_digits),
-        "SC" :  int(2.000000000 * 10**max_digits),
-        }
 
 repartition_pertenthousand = {
         "BTC":  2857,
@@ -16,23 +18,39 @@ 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 fetch_balances(market):
+    balances = {}
+    fetched_balance = market.fetch_balance()
+    for key, value in fetched_balance["total"].items():
+        if value > 0:
+            balances[key] = int(value * 10**max_digits)
+    return balances
 
 def assets_value(assets, market, base_currency="BTC"):
     repartition_in_base_currency = {}
@@ -91,4 +109,5 @@ def give_orders(current_assets, repartition_pertenthousand, market, base_currenc
     for currency, value in new_repartition.items():
         print("holding {} {}".format(formatted_price(value), currency))
 
+current_assets = fetch_balances(market)
 give_orders(current_assets, repartition_pertenthousand, market)