diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-01-11 23:06:12 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-01-11 23:06:12 +0100 |
commit | 987bd3d3f72e11b8d2b18e5b8fbca44a5d561d05 (patch) | |
tree | f6ff0262c3d9eb21379ee35d83e5eb6a8f3ec579 /script.py | |
parent | 269ec87c7f2a9b9b8fd6a707881c5477680abf31 (diff) | |
download | Trader-987bd3d3f72e11b8d2b18e5b8fbca44a5d561d05.tar.gz Trader-987bd3d3f72e11b8d2b18e5b8fbca44a5d561d05.tar.zst Trader-987bd3d3f72e11b8d2b18e5b8fbca44a5d561d05.zip |
Cache ticker values
Diffstat (limited to 'script.py')
-rw-r--r-- | script.py | 29 |
1 files changed, 22 insertions, 7 deletions
@@ -1,5 +1,11 @@ | |||
1 | import ccxt | 1 | import ccxt |
2 | 2 | ||
3 | def static_var(varname, value): | ||
4 | def decorate(func): | ||
5 | setattr(func, varname, value) | ||
6 | return func | ||
7 | return decorate | ||
8 | |||
3 | max_digits = 18 | 9 | max_digits = 18 |
4 | current_assets = { | 10 | current_assets = { |
5 | "ETH": int(2.00000000 * 10**max_digits), | 11 | "ETH": int(2.00000000 * 10**max_digits), |
@@ -21,18 +27,27 @@ market = ccxt.poloniex() | |||
21 | def formatted_price(value): | 27 | def 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", {}) | ||
24 | def get_ticker(c1, c2, market): | 31 | def 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 | ||
37 | def assets_value(assets, market, base_currency="BTC"): | 52 | def assets_value(assets, market, base_currency="BTC"): |
38 | repartition_in_base_currency = {} | 53 | repartition_in_base_currency = {} |