From c59ed542c4884938e04583c45216e830fd4f548a Mon Sep 17 00:00:00 2001 From: BlndMrn Date: Sun, 29 Sep 2024 19:42:52 +0500 Subject: [PATCH] Extend price data by klines OHLC OHLC is already presented in klines so why not to use this information? --- CryptoPrice/common/prices.py | 16 +++++++++++----- CryptoPrice/retrievers/KlineRetriever.py | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CryptoPrice/common/prices.py b/CryptoPrice/common/prices.py index af0b5b4..adc326b 100644 --- a/CryptoPrice/common/prices.py +++ b/CryptoPrice/common/prices.py @@ -7,7 +7,10 @@ @dataclass(frozen=True) class Price: - value: float + high: float + low: float + open: float + close: float asset: str ref_asset: str timestamp: int @@ -16,7 +19,10 @@ class Price: @dataclass(frozen=True) class MetaPrice: - value: float + high: float + low: float + open: float + close: float asset: str ref_asset: str prices: List[Union[Price, MetaPrice]] @@ -41,7 +47,7 @@ def mean_from_meta_price(meta_prices: List[MetaPrice]) -> MetaPrice: for meta_price in meta_prices: if (asset, ref_asset) != (meta_price.asset, meta_price.ref_asset): raise ValueError("asset and ref asset are inconsistent") - cum_value += meta_price.value + cum_value += meta_price.close source.update(meta_price.source) return MetaPrice(cum_value / len(meta_prices), asset, ref_asset, meta_prices, source=source) @@ -66,9 +72,9 @@ def from_price_path(assets: List[str], price_path: List[Price]) -> MetaPrice: for i, price in enumerate(price_path): current_asset, next_asset = assets[i:i + 2] if price.asset == next_asset: - cumulated_price /= price.value + cumulated_price /= price.close else: - cumulated_price *= price.value + cumulated_price *= price.close source.add(price.source) return MetaPrice(cumulated_price, assets[0], assets[-1], price_path, source=source) diff --git a/CryptoPrice/retrievers/KlineRetriever.py b/CryptoPrice/retrievers/KlineRetriever.py index 1f78193..e6f1f50 100644 --- a/CryptoPrice/retrievers/KlineRetriever.py +++ b/CryptoPrice/retrievers/KlineRetriever.py @@ -85,7 +85,7 @@ def _get_closest_price(self, asset: str, ref_asset: str, timestamp: int) -> Opti closest_open_timestamp, self.closest_window) if closest_kline is not None: - return Price(closest_kline.open, asset, ref_asset, closest_kline.open_timestamp, closest_kline.source) + return Price(closest_kline.high, closest_kline.low, closest_kline.open, closest_kline.close, asset, ref_asset, closest_kline.open_timestamp, closest_kline.source) msg = f"no Kline found for {asset}, {ref_asset}, {self.kline_timeframe.name}, {timestamp}," \ f" w={self.closest_window}"