Skip to content

Commit

Permalink
[#30] Added as_list and as_dict method's to Candle
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinR committed Jan 12, 2025
1 parent a1a1383 commit 45df61a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
35 changes: 35 additions & 0 deletions hexital/core/candle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
86 changes: 86 additions & 0 deletions tests/core/test_candle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 45df61a

Please sign in to comment.