modifica aggregazione

This commit is contained in:
Simone Garau
2025-10-30 14:56:42 +01:00
parent 14b20ed07d
commit 83363f1b75
2 changed files with 34 additions and 33 deletions

View File

@@ -34,33 +34,28 @@ class ProductInfo(BaseModel):
product.provider = provider_name product.provider = provider_name
symbols_infos.setdefault(product.symbol, []).append(product) symbols_infos.setdefault(product.symbol, []).append(product)
# Aggregazione per ogni symbol # Aggregazione per ogni symbol usando aggregate_single_asset
aggregated_products: list[ProductInfo] = [] aggregated_products: list[ProductInfo] = []
for symbol, product_list in symbols_infos.items(): for symbol, product_list in symbols_infos.items():
product = ProductInfo() try:
# Usa aggregate_single_asset per aggregare ogni simbolo
product.id = f"{symbol}_AGGREGATED" aggregated = ProductInfo.aggregate_single_asset(product_list)
product.symbol = symbol
product.currency = next((p.currency for p in product_list if p.currency), "") # aggregate_single_asset calcola il volume medio, ma per multi_assets
# vogliamo il volume totale. Ricalcoliamo il volume come somma dopo il filtro USD
# Raccogliamo i provider che hanno fornito dati # Dobbiamo rifare il filtro USD per contare correttamente
providers = [p.provider for p in product_list if p.provider] currencies = set(p.currency for p in product_list if p.currency)
product.provider = ", ".join(set(providers)) if providers else "AGGREGATED" if len(currencies) > 1:
product_list = [p for p in product_list if p.currency.upper() == "USD"]
# Calcolo del volume medio
volume_sum = sum(p.volume_24h for p in product_list if p.volume_24h > 0) # Volume totale
product.volume_24h = volume_sum / len(product_list) if product_list else 0.0 aggregated.volume_24h = sum(p.volume_24h for p in product_list if p.volume_24h > 0)
# Calcolo del prezzo pesato per volume (VWAP - Volume Weighted Average Price) aggregated_products.append(aggregated)
if volume_sum > 0: except ValueError:
prices_weighted = sum(p.price * p.volume_24h for p in product_list if p.volume_24h > 0) # Se aggregate_single_asset fallisce (es. no USD when currencies differ), salta
product.price = prices_weighted / volume_sum continue
else:
# Se non c'è volume, facciamo una media semplice dei prezzi
valid_prices = [p.price for p in product_list if p.price > 0]
product.price = sum(valid_prices) / len(valid_prices) if valid_prices else 0.0
aggregated_products.append(product)
return aggregated_products return aggregated_products
@staticmethod @staticmethod
@@ -100,6 +95,14 @@ class ProductInfo(BaseModel):
if not assets_list: if not assets_list:
raise ValueError("aggregate_single_asset requires at least one ProductInfo") raise ValueError("aggregate_single_asset requires at least one ProductInfo")
# Controllo valuta: se non sono tutte uguali, filtra solo USD
currencies = set(p.currency for p in assets_list if p.currency)
if len(currencies) > 1:
# Valute diverse: filtra solo USD
assets_list = [p for p in assets_list if p.currency.upper() == "USD"]
if not assets_list:
raise ValueError("aggregate_single_asset: no USD products available when currencies differ")
# Aggregazione per ogni Exchange # Aggregazione per ogni Exchange
aggregated: ProductInfo = ProductInfo() aggregated: ProductInfo = ProductInfo()
first = assets_list[0] first = assets_list[0]

View File

@@ -66,13 +66,13 @@ class TestMarketDataAggregator:
assert btc_info is not None assert btc_info is not None
avg_weighted_price_btc = (50000.0 * 1000.0 + 50100.0 * 1100.0) / (1000.0 + 1100.0) avg_weighted_price_btc = (50000.0 * 1000.0 + 50100.0 * 1100.0) / (1000.0 + 1100.0)
assert btc_info.price == pytest.approx(avg_weighted_price_btc, rel=1e-3) # type: ignore assert btc_info.price == pytest.approx(avg_weighted_price_btc, rel=1e-3) # type: ignore
assert btc_info.volume_24h == pytest.approx(1050.0, rel=1e-3) # type: ignore assert btc_info.volume_24h == pytest.approx(2100.0, rel=1e-3) # type: ignore # Total volume (1000 + 1100)
assert btc_info.currency == "USD" assert btc_info.currency == "USD"
assert eth_info is not None assert eth_info is not None
avg_weighted_price_eth = (4000.0 * 2000.0 + 4050.0 * 2100.0) / (2000.0 + 2100.0) avg_weighted_price_eth = (4000.0 * 2000.0 + 4050.0 * 2100.0) / (2000.0 + 2100.0)
assert eth_info.price == pytest.approx(avg_weighted_price_eth, rel=1e-3) # type: ignore assert eth_info.price == pytest.approx(avg_weighted_price_eth, rel=1e-3) # type: ignore
assert eth_info.volume_24h == pytest.approx(2050.0, rel=1e-3) # type: ignore assert eth_info.volume_24h == pytest.approx(4100.0, rel=1e-3) # type: ignore # Total volume (2000 + 2100)
assert eth_info.currency == "USD" assert eth_info.currency == "USD"
def test_aggregate_product_info_with_no_data(self): def test_aggregate_product_info_with_no_data(self):
@@ -141,12 +141,10 @@ class TestMarketDataAggregator:
assert info is not None assert info is not None
assert info.id == "BTC_AGGREGATED" assert info.id == "BTC_AGGREGATED"
assert info.symbol == "BTC" assert info.symbol == "BTC"
assert info.currency in ["USD", "EUR"] # Can be either, depending on which is found first assert info.currency == "USD" # Only USD products are kept
# When aggregating different currencies, VWAP is calculated # When currencies differ, only USD is aggregated (only Provider1 in this case)
# (100000.0 * 1000.0 + 70000.0 * 800.0) / (1000.0 + 800.0) assert info.price == pytest.approx(100000.0, rel=1e-3) # type: ignore
expected_price = (100000.0 * 1000.0 + 70000.0 * 800.0) / (1000.0 + 800.0) assert info.volume_24h == pytest.approx(1000.0, rel=1e-3) # type: ignore # Only USD volume
assert info.price == pytest.approx(expected_price, rel=1e-3) # type: ignore
assert info.volume_24h == pytest.approx(900.0, rel=1e-3) # type: ignore # Average of volumes
# ===== Tests for aggregate_single_asset ===== # ===== Tests for aggregate_single_asset =====