feat: add trade mode /strategy endpoint
This commit is contained in:
@@ -7,6 +7,7 @@ from fastapi.exceptions import HTTPException
|
|||||||
|
|
||||||
from freqtrade import __version__
|
from freqtrade import __version__
|
||||||
from freqtrade.enums import RunMode, State
|
from freqtrade.enums import RunMode, State
|
||||||
|
from freqtrade.exceptions import OperationalException
|
||||||
from freqtrade.rpc import RPC
|
from freqtrade.rpc import RPC
|
||||||
from freqtrade.rpc.api_server.api_pairlists import handleExchangePayload
|
from freqtrade.rpc.api_server.api_pairlists import handleExchangePayload
|
||||||
from freqtrade.rpc.api_server.api_schemas import (
|
from freqtrade.rpc.api_server.api_schemas import (
|
||||||
@@ -17,6 +18,7 @@ from freqtrade.rpc.api_server.api_schemas import (
|
|||||||
Ping,
|
Ping,
|
||||||
PlotConfig,
|
PlotConfig,
|
||||||
ShowConfig,
|
ShowConfig,
|
||||||
|
StrategyResponse,
|
||||||
SysInfo,
|
SysInfo,
|
||||||
Version,
|
Version,
|
||||||
)
|
)
|
||||||
@@ -139,6 +141,43 @@ def markets(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/strategy/{strategy}", response_model=StrategyResponse, tags=["Strategy"])
|
||||||
|
def get_strategy(
|
||||||
|
strategy: str, config=Depends(get_config), rpc: RPC | None = Depends(get_rpc_optional)
|
||||||
|
):
|
||||||
|
if ":" in strategy:
|
||||||
|
raise HTTPException(status_code=500, detail="base64 encoded strategies are not allowed.")
|
||||||
|
|
||||||
|
if not rpc or config["runmode"] == RunMode.WEBSERVER:
|
||||||
|
# webserver mode
|
||||||
|
config_ = deepcopy(config)
|
||||||
|
from freqtrade.resolvers.strategy_resolver import StrategyResolver
|
||||||
|
|
||||||
|
try:
|
||||||
|
strategy_obj = StrategyResolver._load_strategy(
|
||||||
|
strategy, config_, extra_dir=config_.get("strategy_path")
|
||||||
|
)
|
||||||
|
strategy_obj.ft_load_hyper_params()
|
||||||
|
except OperationalException:
|
||||||
|
raise HTTPException(status_code=404, detail="Strategy not found")
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=502, detail=str(e))
|
||||||
|
else:
|
||||||
|
# trade mode
|
||||||
|
strategy_obj = rpc._freqtrade.strategy
|
||||||
|
if strategy_obj.get_strategy_name() != strategy:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail="Only the currently active strategy is available in trade mode",
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"strategy": strategy_obj.get_strategy_name(),
|
||||||
|
"timeframe": getattr(strategy_obj, "timeframe", None),
|
||||||
|
"code": strategy_obj.__source__,
|
||||||
|
"params": [p for _, p in strategy_obj.enumerate_parameters()],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/sysinfo", response_model=SysInfo, tags=["Info"])
|
@router.get("/sysinfo", response_model=SysInfo, tags=["Info"])
|
||||||
def sysinfo():
|
def sysinfo():
|
||||||
return RPC._rpc_sysinfo()
|
return RPC._rpc_sysinfo()
|
||||||
|
|||||||
@@ -36,31 +36,6 @@ def list_strategies(config=Depends(get_config)):
|
|||||||
return {"strategies": [x["name"] for x in strategies]}
|
return {"strategies": [x["name"] for x in strategies]}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/strategy/{strategy}", response_model=StrategyResponse, tags=["Strategy"])
|
|
||||||
def get_strategy(strategy: str, config=Depends(get_config)):
|
|
||||||
if ":" in strategy:
|
|
||||||
raise HTTPException(status_code=500, detail="base64 encoded strategies are not allowed.")
|
|
||||||
|
|
||||||
config_ = deepcopy(config)
|
|
||||||
from freqtrade.resolvers.strategy_resolver import StrategyResolver
|
|
||||||
|
|
||||||
try:
|
|
||||||
strategy_obj = StrategyResolver._load_strategy(
|
|
||||||
strategy, config_, extra_dir=config_.get("strategy_path")
|
|
||||||
)
|
|
||||||
strategy_obj.ft_load_hyper_params()
|
|
||||||
except OperationalException:
|
|
||||||
raise HTTPException(status_code=404, detail="Strategy not found")
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=502, detail=str(e))
|
|
||||||
return {
|
|
||||||
"strategy": strategy_obj.get_strategy_name(),
|
|
||||||
"timeframe": getattr(strategy_obj, "timeframe", None),
|
|
||||||
"code": strategy_obj.__source__,
|
|
||||||
"params": [p for _, p in strategy_obj.enumerate_parameters()],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/exchanges", response_model=ExchangeListResponse, tags=[])
|
@router.get("/exchanges", response_model=ExchangeListResponse, tags=[])
|
||||||
def list_exchanges(config=Depends(get_config)):
|
def list_exchanges(config=Depends(get_config)):
|
||||||
from freqtrade.exchange import list_available_exchanges
|
from freqtrade.exchange import list_available_exchanges
|
||||||
|
|||||||
Reference in New Issue
Block a user