feat: add strategy-parameters to /strategy endpoint
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
from typing import Any
|
from typing import Annotated, Any, Literal
|
||||||
|
|
||||||
from pydantic import AwareDatetime, BaseModel, RootModel, SerializeAsAny, model_validator
|
from pydantic import AwareDatetime, BaseModel, Field, RootModel, SerializeAsAny, model_validator
|
||||||
|
|
||||||
from freqtrade.constants import DL_DATA_TIMEFRAMES, IntOrInf
|
from freqtrade.constants import DL_DATA_TIMEFRAMES, IntOrInf
|
||||||
from freqtrade.enums import MarginMode, OrderTypeValues, SignalDirection, TradingMode
|
from freqtrade.enums import MarginMode, OrderTypeValues, SignalDirection, TradingMode
|
||||||
@@ -527,10 +527,60 @@ class FreqAIModelListResponse(BaseModel):
|
|||||||
freqaimodels: list[str]
|
freqaimodels: list[str]
|
||||||
|
|
||||||
|
|
||||||
|
class __StrategyParameter(BaseModel):
|
||||||
|
param_type: str
|
||||||
|
name: str
|
||||||
|
space: str
|
||||||
|
load: bool
|
||||||
|
optimize: bool
|
||||||
|
|
||||||
|
|
||||||
|
class __IntParameter(__StrategyParameter):
|
||||||
|
param_type: Literal["IntParameter"]
|
||||||
|
value: int
|
||||||
|
low: int
|
||||||
|
high: int
|
||||||
|
|
||||||
|
|
||||||
|
class __RealParameter(__StrategyParameter):
|
||||||
|
param_type: Literal["RealParameter"]
|
||||||
|
value: float
|
||||||
|
low: float
|
||||||
|
high: float
|
||||||
|
|
||||||
|
|
||||||
|
class __DecimalParameter(__RealParameter):
|
||||||
|
param_type: Literal["DecimalParameter"]
|
||||||
|
decimals: int
|
||||||
|
|
||||||
|
|
||||||
|
class __BooleanParameter(__StrategyParameter):
|
||||||
|
param_type: Literal["BooleanParameter"]
|
||||||
|
value: Any
|
||||||
|
opt_range: list[Any]
|
||||||
|
|
||||||
|
|
||||||
|
class __CategoricalParameter(__StrategyParameter):
|
||||||
|
param_type: Literal["CategoricalParameter"]
|
||||||
|
value: Any
|
||||||
|
opt_range: list[Any]
|
||||||
|
|
||||||
|
|
||||||
|
AllParameters = Annotated[
|
||||||
|
__BooleanParameter
|
||||||
|
| __CategoricalParameter
|
||||||
|
| __DecimalParameter
|
||||||
|
| __IntParameter
|
||||||
|
| __RealParameter,
|
||||||
|
Field(discriminator="param_type"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class StrategyResponse(BaseModel):
|
class StrategyResponse(BaseModel):
|
||||||
strategy: str
|
strategy: str
|
||||||
code: str
|
|
||||||
timeframe: str | None
|
timeframe: str | None
|
||||||
|
params: list[AllParameters] | None
|
||||||
|
code: str
|
||||||
|
|
||||||
|
|
||||||
class AvailablePairs(BaseModel):
|
class AvailablePairs(BaseModel):
|
||||||
|
|||||||
@@ -48,14 +48,16 @@ def get_strategy(strategy: str, config=Depends(get_config)):
|
|||||||
strategy_obj = StrategyResolver._load_strategy(
|
strategy_obj = StrategyResolver._load_strategy(
|
||||||
strategy, config_, extra_dir=config_.get("strategy_path")
|
strategy, config_, extra_dir=config_.get("strategy_path")
|
||||||
)
|
)
|
||||||
|
strategy_obj.ft_load_hyper_params()
|
||||||
except OperationalException:
|
except OperationalException:
|
||||||
raise HTTPException(status_code=404, detail="Strategy not found")
|
raise HTTPException(status_code=404, detail="Strategy not found")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=502, detail=str(e))
|
raise HTTPException(status_code=502, detail=str(e))
|
||||||
return {
|
return {
|
||||||
"strategy": strategy_obj.get_strategy_name(),
|
"strategy": strategy_obj.get_strategy_name(),
|
||||||
"code": strategy_obj.__source__,
|
|
||||||
"timeframe": getattr(strategy_obj, "timeframe", None),
|
"timeframe": getattr(strategy_obj, "timeframe", None),
|
||||||
|
"code": strategy_obj.__source__,
|
||||||
|
"params": [p for _, p in strategy_obj.enumerate_parameters()],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user