Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't use DEFAULT_CANDLES if Hexital.timeframe is given #49

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions hexital/core/hexital.py
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property

def timeframe(self) -> str | None:
    return timedelta_to_str(self._timeframe) if self._timeframe else None

Can just be updated now:

def timeframe(self) -> str | None:
    return self._timeframe_name if self._timeframe else None

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Hexital:
candle_life: Optional[timedelta] = None
candlestick: Optional[CandlestickType] = None

default_tf: str = DEFAULT_CANDLES
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a private attribute and rename it -> _timeframe_name

This is because it's just _timeframe but the str version. Having it duplicated just means we dont need to call timedelta_to_str or convert_timeframe_to_timedelta regularly.
We make it private because there is a timeframe property to get this information out, we should update this too.


def __init__(
self,
name: str,
Expand All @@ -53,16 +55,19 @@ def __init__(
if candlestick:
self.candlestick = validate_candlesticktype(candlestick)

if self._timeframe is not None:
self.default_tf = self.timeframe
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this is correct, self.timeframe does not exist. We parse timeframe into self._timeframe. As this is just the name version of timeframe we should have:

self._timeframe_name = timedelta_to_str(self._timeframe)


self._candles = {
DEFAULT_CANDLES: CandleManager(
self.default_tf: CandleManager(
candles if isinstance(candles, list) else [],
candle_life=self.candle_life,
timeframe=self._timeframe,
timeframe_fill=self.timeframe_fill,
candlestick=self.candlestick,
)
}
self._candles[DEFAULT_CANDLES].name = DEFAULT_CANDLES
self._candles[self.default_tf].name = self.default_tf
Copy link
Owner

@MerlinR MerlinR Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need this line at all, not your fault of course.


self._indicators = self._validate_indicators(indicators) if indicators else {}

Expand Down Expand Up @@ -92,7 +97,7 @@ def has_reading(self, name: str) -> bool:

def candles(self, name: Optional[str | TimeFrame | timedelta | int] = None) -> List[Candle]:
"""Get a set of candles by using either a Timeframe or Indicator name"""
name_ = name if name else DEFAULT_CANDLES
name_ = name if name else self.default_tf
timeframe_name = self._parse_timeframe(name)

name_ = timeframe_name if timeframe_name else name_
Expand All @@ -118,7 +123,7 @@ def reading(self, name: str, index: int = -1) -> float | dict | None:
"""Attempts to retrieve a reading with a given Indicator name.
`name` can use '.' to find nested reading, E.G `MACD_12_26_9.MACD`
"""
reading = reading_by_index(self._candles[DEFAULT_CANDLES].candles, name, index=index)
reading = reading_by_index(self._candles[self.default_tf].candles, name, index=index)

if reading is not None:
return reading
Expand Down Expand Up @@ -240,7 +245,7 @@ def _validate_indicators(self, indicators: List[dict | Indicator]) -> Dict[str,

for indicator in valid_indicators.values():
if not indicator.timeframe:
indicator.candle_manager = self._candles[DEFAULT_CANDLES]
indicator.candle_manager = self._candles[self.default_tf]
elif indicator.timeframe and indicator.timeframe in self._candles:
indicator.candle_manager = self._candles[indicator.timeframe]
else:
Expand All @@ -251,7 +256,7 @@ def _validate_indicators(self, indicators: List[dict | Indicator]) -> Dict[str,
timeframe_fill=self.timeframe_fill,
candlestick=self.candlestick,
)
manager.append(self._candles[DEFAULT_CANDLES].candles)
manager.append(self._candles[self.default_tf].candles)
self._candles[manager.name] = manager
indicator.candle_manager = self._candles[manager.name]

Expand Down