12 fix docs #13
@@ -3,21 +3,33 @@ from app.markets.base import ProductInfo, Price
|
|||||||
|
|
||||||
|
|
||||||
def aggregate_history_prices(prices: dict[str, list[Price]]) -> list[Price]:
|
def aggregate_history_prices(prices: dict[str, list[Price]]) -> list[Price]:
|
||||||
"""Aggrega i prezzi storici per symbol calcolando la media"""
|
"""
|
||||||
raise NotImplementedError("Funzione non ancora implementata per problemi di timestamp he deve essere uniformato prima di usare questa funzione.")
|
Aggrega i prezzi storici per symbol calcolando la media
|
||||||
# TODO implementare l'aggregazione dopo aver modificato la classe Price in modo che abbia un timestamp integer
|
|
||||||
# aggregated_prices = []
|
"""
|
||||||
# for timestamp in range(len(next(iter(prices.values())))):
|
max_list_length = max(len(p) for p in prices.values())
|
||||||
# timestamp_prices = [
|
|
||||||
# price_list[timestamp].price
|
# Costruiamo una mappa timestamp_h -> lista di Price
|
||||||
# for price_list in prices.values()
|
timestamped_prices: dict[int, list[Price]] = {}
|
||||||
# if len(price_list) > timestamp and price_list[timestamp].price is not None
|
for _, price_list in prices.items():
|
||||||
# ]
|
for price in price_list:
|
||||||
# if timestamp_prices:
|
time = price.timestamp_ms - (price.timestamp_ms % 3600000) # arrotonda all'ora (non dovrebbe essere necessario)
|
||||||
# aggregated_prices.append(statistics.mean(timestamp_prices))
|
timestamped_prices.setdefault(time, []).append(price)
|
||||||
# else:
|
|
||||||
# aggregated_prices.append(None)
|
# Ora aggregiamo i prezzi per ogni ora
|
||||||
# return aggregated_prices
|
aggregated_prices = []
|
||||||
|
for time, price_list in timestamped_prices.items():
|
||||||
|
price = Price()
|
||||||
|
price.timestamp_ms = time
|
||||||
|
price.high = statistics.mean([p.high for p in price_list])
|
||||||
|
price.low = statistics.mean([p.low for p in price_list])
|
||||||
|
price.open = statistics.mean([p.open for p in price_list])
|
||||||
|
price.close = statistics.mean([p.close for p in price_list])
|
||||||
|
price.volume = statistics.mean([p.volume for p in price_list])
|
||||||
|
aggregated_prices.append(price)
|
||||||
|
|
||||||
|
assert(len(aggregated_prices) <= max_list_length)
|
||||||
|
return aggregated_prices
|
||||||
|
|
||||||
def aggregate_product_info(products: dict[str, list[ProductInfo]]) -> list[ProductInfo]:
|
def aggregate_product_info(products: dict[str, list[ProductInfo]]) -> list[ProductInfo]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ class TestMarketDataAggregator:
|
|||||||
assert len(aggregated) == 2
|
assert len(aggregated) == 2
|
||||||
assert aggregated[0].timestamp_ms == 1685577600000
|
assert aggregated[0].timestamp_ms == 1685577600000
|
||||||
assert aggregated[0].high == pytest.approx(50050.0, rel=1e-3)
|
assert aggregated[0].high == pytest.approx(50050.0, rel=1e-3)
|
||||||
assert aggregated[0].low == pytest.approx(49500.0, rel=1e-3)
|
assert aggregated[0].low == pytest.approx(49550.0, rel=1e-3)
|
||||||
assert aggregated[1].timestamp_ms == 1685581200000
|
assert aggregated[1].timestamp_ms == 1685581200000
|
||||||
assert aggregated[1].high == pytest.approx(50250.0, rel=1e-3)
|
assert aggregated[1].high == pytest.approx(50250.0, rel=1e-3)
|
||||||
assert aggregated[1].low == pytest.approx(49800.0, rel=1e-3)
|
assert aggregated[1].low == pytest.approx(49850.0, rel=1e-3)
|
||||||
|
|||||||
Reference in New Issue
Block a user