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" !!! Warning "Many annotations"
Using too many annotations can cause the UI to hang, especially when plotting large amounts of historic data. Using too many annotations can cause the UI to hang, especially when plotting large amounts of historic data.
Use the annotation feature with care. Use the annotation feature with care.
### Plot annotations example ### Plot annotations example
![FreqUI - plot Annotations](assets/frequi-chart-annotations-dark.png#only-dark) ![FreqUI - plot Annotations](assets/frequi-chart-annotations-dark.png#only-dark)

View File

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

View File

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