feat: validate annotation-type before returning

this avoids breaking the whole chart due to annotations.
This commit is contained in:
Matthias
2025-05-02 20:58:13 +02:00
parent 064708a354
commit a3359b62d4
3 changed files with 21 additions and 12 deletions

View File

@@ -1172,11 +1172,12 @@ class AwesomeStrategy(IStrategy):
```
Entries will be validated, and won't be passed to the UI if they don't correspond to the expected schema and will log an error if they don't.
!!! Warning "Many annotations"
Using too many annotations can cause the UI to hang, especially when plotting large amounts of historic data.
Use the annotation feature with care.
### Plot annotations example
![FreqUI - plot Annotations](assets/frequi-chart-annotations-dark.png#only-dark)

View File

@@ -1,14 +1,18 @@
from datetime import datetime
from typing import Literal
from pydantic import BaseModel
from pydantic import TypeAdapter
from typing_extensions import Required, TypedDict
class AnnotationType(BaseModel):
type: Literal["area"]
start: None | str | datetime = None
end: None | str | datetime = None
y_start: None | float = None
y_end: None | float = None
color: None | str = None
label: None | str = None
class AnnotationType(TypedDict, total=False):
type: Required[Literal["area"]]
start: str | datetime
end: str | datetime
y_start: float
y_end: float
color: str
label: str
AnnotationTypePA = TypeAdapter(AnnotationType)

View File

@@ -1812,12 +1812,16 @@ class IStrategy(ABC, HyperStrategyMixin):
start_date=dataframe.iloc[0]["date"].to_pydatetime(),
end_date=dataframe.iloc[-1]["date"].to_pydatetime(),
)
annotations_new = []
from freqtrade.ft_types.plot_annotation_type import AnnotationTypePA
annotations_new: list[AnnotationType] = []
for annotation in annotations:
if isinstance(annotation, dict):
# Convert to AnnotationType
try:
annotations_new.append(AnnotationType(**annotation))
AnnotationTypePA.validate_python(annotation)
annotations_new.append(annotation)
except ValidationError as e:
logger.error(f"Invalid annotation data: {annotation}. Error: {e}")
else: