Skip to content

Commit 9a1f18e

Browse files
committed
[#55] Over calc candlesticks on prepend
1 parent ba94593 commit 9a1f18e

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ The project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html
66

77
---
88

9+
## 3.0.1
10+
11+
*Release Date: -*
12+
13+
- CandlestickType now re-calculates after prepending to ensure prev transformed are still correct
14+
15+
---
16+
917
## 3.0.0
1018

1119
*Release Date: 2025-04-08*

hexital/core/candle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Candle:
2424
timeframe: Optional[timedelta]
2525
aggregation_factor: int
2626
tag: Optional[str] = None
27-
refs: Dict[str, Any]
27+
refs: Dict[str, Sequence | None]
2828
_start_timestamp: Optional[datetime] = None
2929
_end_timestamp: Optional[datetime] = None
3030

hexital/core/candlestick_type.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import List, Optional
55

66
from hexital.core.candle import Candle
7+
from hexital.utils.indexing import valid_index
78
from hexital.utils.weakreflist import WeakList
89

910

@@ -57,27 +58,37 @@ def transform(self, mode: CalcMode = CalcMode.INSERT, index: Optional[int] = Non
5758
for index in range(start_index, len(self.candles)):
5859
candle = self.candles[index]
5960

60-
if mode == CalcMode.PREPEND and self.acronym in candle.refs:
61-
break
62-
6361
trans_cdl = self.transform_candle(candle)
6462

6563
# None Candles never added to derived
6664
if not trans_cdl:
6765
candle.refs[self.acronym] = None
6866
continue
6967

70-
for cdl in trans_cdl if isinstance(trans_cdl, Sequence) else [trans_cdl]:
71-
cdl.tag = self.acronym
72-
73-
if mode != CalcMode.PREPEND:
74-
self.derived_candles.append(cdl)
75-
else:
76-
self.derived_candles.insert(self._derived_idx, cdl)
77-
78-
self._derived_idx += 1
68+
if candles := self._insert_derived_candles(mode, trans_cdl):
69+
candle.refs[self.acronym] = candles
70+
else:
71+
break
7972

80-
candle.refs[self.acronym] = trans_cdl
73+
def _insert_derived_candles(
74+
self, mode: CalcMode, candles: Candle | Sequence[Candle]
75+
) -> Sequence:
76+
candle_ = candles if isinstance(candles, Sequence) else [candles]
77+
78+
for cdl in candle_:
79+
cdl.tag = self.acronym
80+
81+
if mode == CalcMode.PREPEND:
82+
if (
83+
valid_index(self._derived_idx, len(self.derived_candles))
84+
and cdl == self.derived_candles[self._derived_idx]
85+
):
86+
return []
87+
self.derived_candles.insert(self._derived_idx, cdl)
88+
else:
89+
self.derived_candles.append(cdl)
90+
self._derived_idx += 1
91+
return candle_
8192

8293
def _find_transform_index(self) -> int:
8394
"""Optimisation method, to find where to start calculating the indicator from

hexital/utils/weakreflist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def reset(self):
3333
def __getitem__(self, idx: slice | SupportsIndex) -> T | List[T]:
3434
if self._dirty:
3535
self.flush()
36-
if isinstance(idx, int):
36+
if isinstance(idx, SupportsIndex):
3737
return self._refs[idx]()
3838
else:
3939
return [v() for v in self._refs[idx]]

0 commit comments

Comments
 (0)