feat: add historic_balance api endpoint

This commit is contained in:
Matthias
2025-04-27 15:32:28 +02:00
parent 2d2cee2c58
commit 281b627db3
2 changed files with 30 additions and 1 deletions
+16
View File
@@ -31,6 +31,7 @@ from freqtrade.rpc.api_server.api_schemas import (
ResultMsg,
Stats,
StatusMsg,
WalletsSummary,
WhitelistResponse,
)
from freqtrade.rpc.api_server.deps import get_config, get_rpc
@@ -104,6 +105,21 @@ def stats(rpc: RPC = Depends(get_rpc)):
return rpc._rpc_stats()
@router.get(
"/historic_balance",
response_model=WalletsSummary,
tags=["info"],
)
def api_get_backtest_wallet(rpc: RPC = Depends(get_rpc)):
results = rpc._rpc_get_historic_balance()
return {
"columns": results.columns.tolist(),
"data": results.values.tolist(),
"length": len(results),
}
@router.get("/daily", response_model=DailyWeeklyMonthly, tags=["Trading-info"])
def daily(
timescale: int = Query(7, ge=1, description="Number of days to fetch data for"),
+14 -1
View File
@@ -12,7 +12,7 @@ import psutil
from dateutil.relativedelta import relativedelta
from dateutil.tz import tzlocal
from numpy import inf, int64, isnan, mean, nan
from pandas import DataFrame, NaT
from pandas import DataFrame, NaT, read_sql
from sqlalchemy import func, select
from freqtrade import __version__
@@ -785,6 +785,19 @@ class RPC:
"bot_start_date": format_date(bot_start),
}
def _rpc_get_historic_balance(self) -> DataFrame:
"""
Returns the historic balance of the bot
:return: DataFrame with the balance history
"""
results = read_sql("wallet_balance", con=Trade.session.bind, parse_dates=["timestamp"])
results.loc[:, "total"] = results["price"] * results["balance"]
results = results.rename({"timestamp": "date"}, axis=1)
results.loc[:, "__date_ts"] = results.loc[:, "date"].astype("int64") // 1000 // 1000
results = results.groupby(["date", "__date_ts"]).agg({"total": "sum"}).reset_index()
return results
def __balance_get_est_stake(
self, coin: str, stake_currency: str, amount: float, balance: Wallet
) -> tuple[float, float]: