diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ffe240..9d8ac0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html --- +## 2.0.2 + +*Release Date: -** + +- Added as_list and as_dict method's to Candle + ## 2.0.1 *Release Date: 2025-01-12** diff --git a/hexital/core/candle.py b/hexital/core/candle.py index 41f56c3..0917a8f 100644 --- a/hexital/core/candle.py +++ b/hexital/core/candle.py @@ -120,6 +120,41 @@ def shadow_lower(self) -> float: def high_low(self) -> float: return abs(self.high - self.low) + def as_list(self) -> list: + """ + Generates a list of values from the OHLCV values. + `[timestamp, open, high, low, close, volume]` in that order. + With an optional `timedelta` value at the end being the `timeframe` + + Returns: + list: A list of the `Candle` values + """ + return [self.timestamp, self.open, self.high, self.low, self.close, self.volume] + ( + [self.timeframe] if self.timeframe else [] + ) + + def as_dict(self) -> dict: + """ + Generates a dict of values from the OHLCV values, with the following keys: + `[timestamp, open, high, low, close, volume]` + + Returns: + dict: A list of the `Candle` values + """ + cdl = { + "open": self.open, + "high": self.high, + "low": self.low, + "close": self.close, + "volume": self.volume, + "timestamp": self.timestamp, + } + + if self.timeframe: + cdl["timeframe"] = self.timeframe + + return cdl + @classmethod def from_dict(cls, candle: Dict[str, Any]) -> Candle: """ diff --git a/tests/core/test_candle.py b/tests/core/test_candle.py index 06eba2f..c35f423 100644 --- a/tests/core/test_candle.py +++ b/tests/core/test_candle.py @@ -411,3 +411,89 @@ def test_candle_merge_out_of_timeframe_under(self, merge_candles): main_candle.timestamp == datetime(2023, 10, 3, 9, 0, 30) and main_candle.close == 12536.019 ) + + +class TestCandleTo: + def test_candle_to_list(self): + candle = Candle( + 12331.69043, + 12542.540039, + 12202.410156, + 12536.019531, + 4918240000, + datetime(2023, 10, 3, 9), + ) + expected = [ + datetime(2023, 10, 3, 9), + 12331.69043, + 12542.540039, + 12202.410156, + 12536.019531, + 4918240000, + ] + + assert candle.as_list() == expected + + def test_candle_to_list_timeframe(self): + candle = Candle( + 12331.69043, + 12542.540039, + 12202.410156, + 12536.019531, + 4918240000, + datetime(2023, 10, 3, 9), + timeframe="H1", + ) + expected = [ + datetime(2023, 10, 3, 9), + 12331.69043, + 12542.540039, + 12202.410156, + 12536.019531, + 4918240000, + timedelta(hours=1), + ] + + assert candle.as_list() == expected + + def test_candle_to_dict(self): + candle = Candle( + 12331.69043, + 12542.540039, + 12202.410156, + 12536.019531, + 4918240000, + datetime(2023, 10, 3, 9), + ) + + expected = { + "open": 12331.69043, + "high": 12542.540039, + "low": 12202.410156, + "close": 12536.019531, + "volume": 4918240000, + "timestamp": datetime(2023, 10, 3, 9), + } + assert candle.as_dict() == expected + + def test_candle_to_dict_timeframe(self): + candle = Candle( + 12331.69043, + 12542.540039, + 12202.410156, + 12536.019531, + 4918240000, + datetime(2023, 10, 3, 9), + timeframe="H1", + ) + + expected = { + "open": 12331.69043, + "high": 12542.540039, + "low": 12202.410156, + "close": 12536.019531, + "volume": 4918240000, + "timestamp": datetime(2023, 10, 3, 9), + "timeframe": timedelta(hours=1), + } + assert candle.as_dict() == expected