-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprice_statistics.py
78 lines (58 loc) · 2.64 KB
/
price_statistics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from threading import Lock
from config import logger
from price_evaluator import PriceEvaluator
class PriceStatistics:
def __init__(self):
self._traded_asset_stats = {}
self._stats_read_lock = Lock()
self._evaluator = PriceEvaluator(self)
def process_price(self, asset_price_data):
"""Process asset price data
:type asset_price_data: dict
:param asset_price_data: Asset price data in the following format
{
"symbol": str,
"close": float,
"close_prev_day": float,
"change": float,
"change_percent": float
}
"""
symbol = asset_price_data["symbol"]
if symbol not in self._traded_asset_stats:
self._traded_asset_stats[symbol] = {}
self._traded_asset_stats[symbol]["prev_price"] = self._traded_asset_stats[symbol].get("latest_price", 0.0)
self._traded_asset_stats[symbol]["latest_price"] = asset_price_data["close"]
self._traded_asset_stats[symbol]["prev_day_price"] = asset_price_data["close_prev_day"]
self._traded_asset_stats[symbol]["price_change"] = asset_price_data["change"]
self._traded_asset_stats[symbol]["change_percent"] = asset_price_data["change_percent"]
logger.debug(self)
def get_asset_stats(self):
"""Get current statistics for all symbols
This data is accessed by the threads of PriceEvaluator class.
:rtype: dict
:returns: Asset price statistics in the following format
{
"symbol": {
"prev_price": float,
"latest_price": float,
"prev_day_price": float,
"price_change": float,
"change_percent": float
}
}
"""
self._stats_read_lock.acquire()
stats = self._traded_asset_stats
self._stats_read_lock.release()
return stats
def __str__(self):
result = "+++++++ PriceStatistics +++++++\n"
for symbol in self._traded_asset_stats:
result += f"*** {symbol} ***\n"
result += f"Previous price: {self._traded_asset_stats[symbol]['prev_price']}\n"
result += f"Latest price: {self._traded_asset_stats[symbol]['latest_price']}\n"
result += f"Previous day price: {self._traded_asset_stats[symbol]['prev_day_price']}\n"
result += f"Price change: {self._traded_asset_stats[symbol]['price_change']}\n"
result += f"Price change percent: {self._traded_asset_stats[symbol]['change_percent']}\n\n"
return result