diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-12 13:57:28 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-12 13:57:28 +0100 |
commit | 9f54fd9acf98692ff7601fd3236c46745eb26e15 (patch) | |
tree | 28b7778dcca169fa6594849034b51e303fd22f40 /portfolio.py | |
parent | f320eb8aafbceafbbfca02617db4846b3571e598 (diff) | |
download | Trader-9f54fd9acf98692ff7601fd3236c46745eb26e15.tar.gz Trader-9f54fd9acf98692ff7601fd3236c46745eb26e15.tar.zst Trader-9f54fd9acf98692ff7601fd3236c46745eb26e15.zip |
Add method to wait for portfolio update
Diffstat (limited to 'portfolio.py')
-rw-r--r-- | portfolio.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/portfolio.py b/portfolio.py index 21a9834..b77f975 100644 --- a/portfolio.py +++ b/portfolio.py | |||
@@ -1,5 +1,5 @@ | |||
1 | import time | 1 | import time |
2 | from datetime import datetime | 2 | from datetime import datetime, timedelta |
3 | from decimal import Decimal as D, ROUND_DOWN | 3 | from decimal import Decimal as D, ROUND_DOWN |
4 | # Put your poloniex api key in market.py | 4 | # Put your poloniex api key in market.py |
5 | from json import JSONDecodeError | 5 | from json import JSONDecodeError |
@@ -14,12 +14,19 @@ class Portfolio: | |||
14 | URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json" | 14 | URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json" |
15 | liquidities = {} | 15 | liquidities = {} |
16 | data = None | 16 | data = None |
17 | last_date = None | ||
17 | 18 | ||
18 | @classmethod | 19 | @classmethod |
19 | def repartition(cls, liquidity="medium"): | 20 | def wait_for_recent(cls, delta=4): |
20 | cls.parse_cryptoportfolio() | 21 | cls.repartition(refetch=True) |
22 | while cls.last_date is None or datetime.now() - cls.last_date > timedelta(delta): | ||
23 | time.sleep(30) | ||
24 | cls.repartition(refetch=True) | ||
25 | |||
26 | @classmethod | ||
27 | def repartition(cls, liquidity="medium", refetch=False): | ||
28 | cls.parse_cryptoportfolio(refetch=refetch) | ||
21 | liquidities = cls.liquidities[liquidity] | 29 | liquidities = cls.liquidities[liquidity] |
22 | cls.last_date = sorted(liquidities.keys())[-1] | ||
23 | return liquidities[cls.last_date] | 30 | return liquidities[cls.last_date] |
24 | 31 | ||
25 | @classmethod | 32 | @classmethod |
@@ -34,8 +41,8 @@ class Portfolio: | |||
34 | cls.data = None | 41 | cls.data = None |
35 | 42 | ||
36 | @classmethod | 43 | @classmethod |
37 | def parse_cryptoportfolio(cls): | 44 | def parse_cryptoportfolio(cls, refetch=False): |
38 | if cls.data is None: | 45 | if refetch or cls.data is None: |
39 | cls.get_cryptoportfolio() | 46 | cls.get_cryptoportfolio() |
40 | 47 | ||
41 | def filter_weights(weight_hash): | 48 | def filter_weights(weight_hash): |
@@ -57,7 +64,8 @@ class Portfolio: | |||
57 | weights_hash = portfolio_hash["weights"] | 64 | weights_hash = portfolio_hash["weights"] |
58 | weights = {} | 65 | weights = {} |
59 | for i in range(len(weights_hash["_row"])): | 66 | for i in range(len(weights_hash["_row"])): |
60 | weights[weights_hash["_row"][i]] = dict(filter( | 67 | date = datetime.strptime(weights_hash["_row"][i], "%Y-%m-%d") |
68 | weights[date] = dict(filter( | ||
61 | filter_weights, | 69 | filter_weights, |
62 | map(clean_weights(i), weights_hash.items()))) | 70 | map(clean_weights(i), weights_hash.items()))) |
63 | return weights | 71 | return weights |
@@ -69,6 +77,7 @@ class Portfolio: | |||
69 | "medium": medium_liquidity, | 77 | "medium": medium_liquidity, |
70 | "high": high_liquidity, | 78 | "high": high_liquidity, |
71 | } | 79 | } |
80 | cls.last_date = max(max(medium_liquidity.keys()), max(high_liquidity.keys())) | ||
72 | 81 | ||
73 | class Computation: | 82 | class Computation: |
74 | computations = { | 83 | computations = { |