commit prima di fare cherry-pick della commit 72dc551

This commit is contained in:
Simone Garau
2025-10-30 12:52:58 +01:00
parent 512bc4568e
commit c07938618a
8 changed files with 317 additions and 42 deletions

View File

@@ -33,6 +33,7 @@ class MarketAPIsTool(MarketWrapper, Toolkit):
self.get_product,
self.get_products,
self.get_historical_prices,
self.get_product_aggregated,
self.get_products_aggregated,
self.get_historical_prices_aggregated,
],
@@ -87,6 +88,36 @@ class MarketAPIsTool(MarketWrapper, Toolkit):
"""
return self.handler.try_call(lambda w: w.get_historical_prices(asset_id, limit))
def get_product_aggregated(self, asset_id: str) -> ProductInfo:
"""
Gets product information for a *single* asset from *all available providers* and *aggregates* the results.
This method queries all configured sources (Binance, YFinance, Coinbase, CryptoCompare)
and combines the data using volume-weighted average price (VWAP) to provide
the most accurate and comprehensive price data.
Use this when you need highly reliable price data from multiple sources.
Warning: This uses more API calls (4x) than get_product().
Args:
asset_id (str): The asset ID to retrieve information for (e.g., "BTC", "ETH").
Returns:
ProductInfo: A single ProductInfo object with aggregated data from all providers.
The 'provider' field will list all sources used (e.g., "Binance, YFinance, Coinbase").
Raises:
Exception: If all providers fail to return results.
Example:
>>> tool.get_product_aggregated("BTC")
ProductInfo(symbol="BTC", price=45123.50, provider="Binance, YFinance, Coinbase", ...)
"""
# try_call_all returns dict[str, ProductInfo] where key is provider name
# We need list[ProductInfo] for aggregation, so we extract values
all_products = self.handler.try_call_all(lambda w: w.get_product(asset_id))
return ProductInfo.aggregate_single_asset(all_products)
def get_products_aggregated(self, asset_ids: list[str]) -> list[ProductInfo]:
"""
Gets product information for multiple assets from *all available providers* and *aggregates* the results.
@@ -107,7 +138,7 @@ class MarketAPIsTool(MarketWrapper, Toolkit):
all_products: dict[str, list[ProductInfo]] = {}
for asset in asset_ids:
all_products[asset] = self.handler.try_call_all(lambda w: w.get_product(asset))
return ProductInfo.aggregate(all_products)
return ProductInfo.aggregate_multi_assets(all_products)
def get_historical_prices_aggregated(self, asset_id: str = "BTC", limit: int = 100) -> list[Price]:
"""