2023-06-01 18:40:12 +00:00
|
|
|
import logging
|
|
|
|
from copy import deepcopy
|
2024-05-12 07:12:53 +00:00
|
|
|
from typing import List
|
2023-06-01 18:40:12 +00:00
|
|
|
|
|
|
|
from fastapi import APIRouter, BackgroundTasks, Depends
|
|
|
|
from fastapi.exceptions import HTTPException
|
|
|
|
|
|
|
|
from freqtrade.constants import Config
|
2023-06-03 04:52:25 +00:00
|
|
|
from freqtrade.enums import CandleType
|
2023-06-01 18:40:12 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2024-01-10 19:08:23 +00:00
|
|
|
from freqtrade.persistence import FtNoDBContext
|
2024-05-12 13:18:32 +00:00
|
|
|
from freqtrade.rpc.api_server.api_schemas import (
|
|
|
|
BackgroundTaskStatus,
|
|
|
|
BgJobStarted,
|
|
|
|
ExchangeModePayloadMixin,
|
|
|
|
PairListsPayload,
|
|
|
|
PairListsResponse,
|
|
|
|
WhitelistEvaluateResponse,
|
|
|
|
)
|
2023-06-01 18:40:12 +00:00
|
|
|
from freqtrade.rpc.api_server.deps import get_config, get_exchange
|
|
|
|
from freqtrade.rpc.api_server.webserver_bgwork import ApiBG
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Private API, protected by authentication and webserver_mode dependency
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
@router.get("/background", response_model=List[BackgroundTaskStatus], tags=["webserver"])
|
2024-05-12 07:12:53 +00:00
|
|
|
def background_job_list():
|
2024-05-12 14:51:11 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"job_id": jobid,
|
|
|
|
"job_category": job["category"],
|
|
|
|
"status": job["status"],
|
|
|
|
"running": job["is_running"],
|
|
|
|
"progress": job.get("progress"),
|
|
|
|
"error": job.get("error", None),
|
|
|
|
}
|
|
|
|
for jobid, job in ApiBG.jobs.items()
|
|
|
|
]
|
2024-05-12 07:12:53 +00:00
|
|
|
|
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
@router.get("/background/{jobid}", response_model=BackgroundTaskStatus, tags=["webserver"])
|
2023-06-01 18:40:12 +00:00
|
|
|
def background_job(jobid: str):
|
|
|
|
if not (job := ApiBG.jobs.get(jobid)):
|
2024-05-12 14:51:11 +00:00
|
|
|
raise HTTPException(status_code=404, detail="Job not found.")
|
2023-06-01 18:40:12 +00:00
|
|
|
|
|
|
|
return {
|
2024-05-12 14:51:11 +00:00
|
|
|
"job_id": jobid,
|
|
|
|
"job_category": job["category"],
|
|
|
|
"status": job["status"],
|
|
|
|
"running": job["is_running"],
|
|
|
|
"progress": job.get("progress"),
|
|
|
|
"error": job.get("error", None),
|
2023-06-01 18:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
@router.get(
|
|
|
|
"/pairlists/available", response_model=PairListsResponse, tags=["pairlists", "webserver"]
|
|
|
|
)
|
2023-06-01 18:40:12 +00:00
|
|
|
def list_pairlists(config=Depends(get_config)):
|
|
|
|
from freqtrade.resolvers import PairListResolver
|
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
pairlists = PairListResolver.search_all_objects(config, False)
|
|
|
|
pairlists = sorted(pairlists, key=lambda x: x["name"])
|
|
|
|
|
|
|
|
return {
|
|
|
|
"pairlists": [
|
|
|
|
{
|
|
|
|
"name": x["name"],
|
|
|
|
"is_pairlist_generator": x["class"].is_pairlist_generator,
|
|
|
|
"params": x["class"].available_parameters(),
|
|
|
|
"description": x["class"].description(),
|
|
|
|
}
|
|
|
|
for x in pairlists
|
|
|
|
]
|
|
|
|
}
|
2023-06-01 18:40:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __run_pairlist(job_id: str, config_loc: Config):
|
|
|
|
try:
|
2024-05-12 14:51:11 +00:00
|
|
|
ApiBG.jobs[job_id]["is_running"] = True
|
2023-06-01 18:40:12 +00:00
|
|
|
from freqtrade.plugins.pairlistmanager import PairListManager
|
2024-05-12 14:51:11 +00:00
|
|
|
|
2024-01-10 19:08:23 +00:00
|
|
|
with FtNoDBContext():
|
|
|
|
exchange = get_exchange(config_loc)
|
|
|
|
pairlists = PairListManager(exchange, config_loc)
|
|
|
|
pairlists.refresh_pairlist()
|
2024-05-12 14:51:11 +00:00
|
|
|
ApiBG.jobs[job_id]["result"] = {
|
|
|
|
"method": pairlists.name_list,
|
|
|
|
"length": len(pairlists.whitelist),
|
|
|
|
"whitelist": pairlists.whitelist,
|
|
|
|
}
|
|
|
|
ApiBG.jobs[job_id]["status"] = "success"
|
2023-06-01 18:40:12 +00:00
|
|
|
except (OperationalException, Exception) as e:
|
|
|
|
logger.exception(e)
|
2024-05-12 14:51:11 +00:00
|
|
|
ApiBG.jobs[job_id]["error"] = str(e)
|
|
|
|
ApiBG.jobs[job_id]["status"] = "failed"
|
2023-06-01 18:40:12 +00:00
|
|
|
finally:
|
2024-05-12 14:51:11 +00:00
|
|
|
ApiBG.jobs[job_id]["is_running"] = False
|
2023-06-01 18:40:12 +00:00
|
|
|
ApiBG.pairlist_running = False
|
|
|
|
|
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
@router.post("/pairlists/evaluate", response_model=BgJobStarted, tags=["pairlists", "webserver"])
|
|
|
|
def pairlists_evaluate(
|
|
|
|
payload: PairListsPayload, background_tasks: BackgroundTasks, config=Depends(get_config)
|
|
|
|
):
|
2023-06-01 18:40:12 +00:00
|
|
|
if ApiBG.pairlist_running:
|
2024-05-12 14:51:11 +00:00
|
|
|
raise HTTPException(status_code=400, detail="Pairlist evaluation is already running.")
|
2023-06-01 18:40:12 +00:00
|
|
|
|
|
|
|
config_loc = deepcopy(config)
|
2024-05-12 14:51:11 +00:00
|
|
|
config_loc["stake_currency"] = payload.stake_currency
|
|
|
|
config_loc["pairlists"] = payload.pairlists
|
2023-06-03 04:57:25 +00:00
|
|
|
handleExchangePayload(payload, config_loc)
|
2023-06-01 18:40:12 +00:00
|
|
|
# TODO: overwrite blacklist? make it optional and fall back to the one in config?
|
|
|
|
# Outcome depends on the UI approach.
|
2024-05-12 14:51:11 +00:00
|
|
|
config_loc["exchange"]["pair_blacklist"] = payload.blacklist
|
2023-06-01 18:40:12 +00:00
|
|
|
# Random job id
|
|
|
|
job_id = ApiBG.get_job_id()
|
|
|
|
|
|
|
|
ApiBG.jobs[job_id] = {
|
2024-05-12 14:51:11 +00:00
|
|
|
"category": "pairlist",
|
|
|
|
"status": "pending",
|
|
|
|
"progress": None,
|
|
|
|
"is_running": False,
|
|
|
|
"result": {},
|
|
|
|
"error": None,
|
2023-06-01 18:40:12 +00:00
|
|
|
}
|
|
|
|
background_tasks.add_task(__run_pairlist, job_id, config_loc)
|
|
|
|
ApiBG.pairlist_running = True
|
|
|
|
|
|
|
|
return {
|
2024-05-12 14:51:11 +00:00
|
|
|
"status": "Pairlist evaluation started in background.",
|
|
|
|
"job_id": job_id,
|
2023-06-01 18:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-03 04:57:25 +00:00
|
|
|
def handleExchangePayload(payload: ExchangeModePayloadMixin, config_loc: Config):
|
|
|
|
"""
|
|
|
|
Handle exchange and trading mode payload.
|
|
|
|
Updates the configuration with the payload values.
|
|
|
|
"""
|
|
|
|
if payload.exchange:
|
2024-05-12 14:51:11 +00:00
|
|
|
config_loc["exchange"]["name"] = payload.exchange
|
2023-06-03 04:57:25 +00:00
|
|
|
if payload.trading_mode:
|
2024-05-12 14:51:11 +00:00
|
|
|
config_loc["trading_mode"] = payload.trading_mode
|
|
|
|
config_loc["candle_type_def"] = CandleType.get_default(
|
|
|
|
config_loc.get("trading_mode", "spot") or "spot"
|
|
|
|
)
|
2023-06-04 11:25:39 +00:00
|
|
|
if payload.margin_mode:
|
2024-05-12 14:51:11 +00:00
|
|
|
config_loc["margin_mode"] = payload.margin_mode
|
2023-06-03 04:57:25 +00:00
|
|
|
|
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
@router.get(
|
|
|
|
"/pairlists/evaluate/{jobid}",
|
|
|
|
response_model=WhitelistEvaluateResponse,
|
|
|
|
tags=["pairlists", "webserver"],
|
|
|
|
)
|
2023-06-01 18:40:12 +00:00
|
|
|
def pairlists_evaluate_get(jobid: str):
|
|
|
|
if not (job := ApiBG.jobs.get(jobid)):
|
2024-05-12 14:51:11 +00:00
|
|
|
raise HTTPException(status_code=404, detail="Job not found.")
|
2023-06-01 18:40:12 +00:00
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
if job["is_running"]:
|
|
|
|
raise HTTPException(status_code=400, detail="Job not finished yet.")
|
2023-06-01 18:40:12 +00:00
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
if error := job["error"]:
|
2023-06-01 18:40:12 +00:00
|
|
|
return {
|
2024-05-12 14:51:11 +00:00
|
|
|
"status": "failed",
|
|
|
|
"error": error,
|
2023-06-01 18:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-05-12 14:51:11 +00:00
|
|
|
"status": "success",
|
|
|
|
"result": job["result"],
|
2023-06-01 18:40:12 +00:00
|
|
|
}
|