aboutsummaryrefslogtreecommitdiff
path: root/script.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-01-11 23:06:12 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-01-11 23:06:12 +0100
commit987bd3d3f72e11b8d2b18e5b8fbca44a5d561d05 (patch)
treef6ff0262c3d9eb21379ee35d83e5eb6a8f3ec579 /script.py
parent269ec87c7f2a9b9b8fd6a707881c5477680abf31 (diff)
downloadTrader-987bd3d3f72e11b8d2b18e5b8fbca44a5d561d05.tar.gz
Trader-987bd3d3f72e11b8d2b18e5b8fbca44a5d561d05.tar.zst
Trader-987bd3d3f72e11b8d2b18e5b8fbca44a5d561d05.zip
Cache ticker values
Diffstat (limited to 'script.py')
-rw-r--r--script.py29
1 files changed, 22 insertions, 7 deletions
diff --git a/script.py b/script.py
index 4732533..fe0f0de 100644
--- a/script.py
+++ b/script.py
@@ -1,5 +1,11 @@
1import ccxt 1import ccxt
2 2
3def static_var(varname, value):
4 def decorate(func):
5 setattr(func, varname, value)
6 return func
7 return decorate
8
3max_digits = 18 9max_digits = 18
4current_assets = { 10current_assets = {
5 "ETH": int(2.00000000 * 10**max_digits), 11 "ETH": int(2.00000000 * 10**max_digits),
@@ -21,18 +27,27 @@ market = ccxt.poloniex()
21def formatted_price(value): 27def formatted_price(value):
22 return round(value / 10**max_digits, 8) 28 return round(value / 10**max_digits, 8)
23 29
30@static_var("cache", {})
24def get_ticker(c1, c2, market): 31def get_ticker(c1, c2, market):
32 def invert(ticker):
33 return {
34 "bid": float(1/ticker["ask"]),
35 "ask": float(1/ticker["bid"]),
36 }
37
38 if (c1, c2, market.__class__) in get_ticker.cache:
39 return get_ticker.cache[(c1, c2, market.__class__)]
40 if (c2, c1, market.__class__) in get_ticker.cache:
41 return invert(get_ticker.cache[(c2, c1, market.__class__)])
42
25 try: 43 try:
26 return market.fetch_ticker("{}/{}".format(c1, c2)) 44 get_ticker.cache[(c1, c2, market.__class__)] = market.fetch_ticker("{}/{}".format(c1, c2))
27 except ccxt.ExchangeError: 45 except ccxt.ExchangeError:
28 try: 46 try:
29 ticker = market.fetch_ticker("{}/{}".format(c2, c1)) 47 get_ticker.cache[(c2, c1, market.__class__)] = market.fetch_ticker("{}/{}".format(c2, c1))
30 return {
31 "bid": float(1/ticker["ask"]),
32 "ask": float(1/ticker["bid"]),
33 }
34 except ccxt.ExchangeError: 48 except ccxt.ExchangeError:
35 return None 49 get_ticker.cache[(c1, c2, market.__class__)] = None
50 return get_ticker(c1, c2, market)
36 51
37def assets_value(assets, market, base_currency="BTC"): 52def assets_value(assets, market, base_currency="BTC"):
38 repartition_in_base_currency = {} 53 repartition_in_base_currency = {}