mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-11-29 00:23:07 +00:00
feat: validate annotation-type before returning
this avoids breaking the whole chart due to annotations.
This commit is contained in:
@@ -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
|
||||
|
||||

|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user