Consolidate monthly stats to common method
This commit is contained in:
parent
3cb15a2a54
commit
d4dd026310
@ -86,7 +86,7 @@ def stats(rpc: RPC = Depends(get_rpc)):
|
|||||||
|
|
||||||
@router.get('/daily', response_model=Daily, tags=['info'])
|
@router.get('/daily', response_model=Daily, tags=['info'])
|
||||||
def daily(timescale: int = 7, rpc: RPC = Depends(get_rpc), config=Depends(get_config)):
|
def daily(timescale: int = 7, rpc: RPC = Depends(get_rpc), config=Depends(get_config)):
|
||||||
return rpc._rpc_daily_profit(timescale, config['stake_currency'],
|
return rpc._rpc_timeunit_profit(timescale, config['stake_currency'],
|
||||||
config.get('fiat_display_currency', ''))
|
config.get('fiat_display_currency', ''))
|
||||||
|
|
||||||
|
|
||||||
|
@ -283,7 +283,7 @@ class RPC:
|
|||||||
columns.append('# Entries')
|
columns.append('# Entries')
|
||||||
return trades_list, columns, fiat_profit_sum
|
return trades_list, columns, fiat_profit_sum
|
||||||
|
|
||||||
def _rpc_daily_profit(
|
def _rpc_timeunit_profit(
|
||||||
self, timescale: int,
|
self, timescale: int,
|
||||||
stake_currency: str, fiat_display_currency: str,
|
stake_currency: str, fiat_display_currency: str,
|
||||||
timeunit: str = 'days') -> Dict[str, Any]:
|
timeunit: str = 'days') -> Dict[str, Any]:
|
||||||
@ -297,17 +297,22 @@ class RPC:
|
|||||||
if timeunit == 'months':
|
if timeunit == 'months':
|
||||||
start_date = start_date.replace(day=1)
|
start_date = start_date.replace(day=1)
|
||||||
|
|
||||||
|
def time_offset(step: int):
|
||||||
|
if timeunit == 'months':
|
||||||
|
return relativedelta(months=step)
|
||||||
|
return timedelta(**{timeunit: step})
|
||||||
|
|
||||||
profit_units: Dict[date, Dict] = {}
|
profit_units: Dict[date, Dict] = {}
|
||||||
|
|
||||||
if not (isinstance(timescale, int) and timescale > 0):
|
if not (isinstance(timescale, int) and timescale > 0):
|
||||||
raise RPCException('timescale must be an integer greater than 0')
|
raise RPCException('timescale must be an integer greater than 0')
|
||||||
|
|
||||||
for day in range(0, timescale):
|
for day in range(0, timescale):
|
||||||
profitday = start_date - timedelta(**{timeunit: day})
|
profitday = start_date - time_offset(day)
|
||||||
trades = Trade.get_trades(trade_filter=[
|
trades = Trade.get_trades(trade_filter=[
|
||||||
Trade.is_open.is_(False),
|
Trade.is_open.is_(False),
|
||||||
Trade.close_date >= profitday,
|
Trade.close_date >= profitday,
|
||||||
Trade.close_date < (profitday + timedelta(**{timeunit: 1}))
|
Trade.close_date < (profitday + time_offset(1))
|
||||||
]).order_by(Trade.close_date).all()
|
]).order_by(Trade.close_date).all()
|
||||||
curdayprofit = sum(
|
curdayprofit = sum(
|
||||||
trade.close_profit_abs for trade in trades if trade.close_profit_abs is not None)
|
trade.close_profit_abs for trade in trades if trade.close_profit_abs is not None)
|
||||||
@ -318,7 +323,7 @@ class RPC:
|
|||||||
|
|
||||||
data = [
|
data = [
|
||||||
{
|
{
|
||||||
'date': key,
|
'date': f"{key.year}-{key.month:02d}" if timeunit == 'months' else key,
|
||||||
'abs_profit': value["amount"],
|
'abs_profit': value["amount"],
|
||||||
'fiat_value': self._fiat_converter.convert_amount(
|
'fiat_value': self._fiat_converter.convert_amount(
|
||||||
value['amount'],
|
value['amount'],
|
||||||
@ -335,48 +340,6 @@ class RPC:
|
|||||||
'data': data
|
'data': data
|
||||||
}
|
}
|
||||||
|
|
||||||
def _rpc_monthly_profit(
|
|
||||||
self, timescale: int,
|
|
||||||
stake_currency: str, fiat_display_currency: str) -> Dict[str, Any]:
|
|
||||||
first_day_of_month = datetime.now(timezone.utc).date().replace(day=1)
|
|
||||||
profit_months: Dict[date, Dict] = {}
|
|
||||||
|
|
||||||
if not (isinstance(timescale, int) and timescale > 0):
|
|
||||||
raise RPCException('timescale must be an integer greater than 0')
|
|
||||||
|
|
||||||
for month in range(0, timescale):
|
|
||||||
profitmonth = first_day_of_month - relativedelta(months=month)
|
|
||||||
trades = Trade.get_trades(trade_filter=[
|
|
||||||
Trade.is_open.is_(False),
|
|
||||||
Trade.close_date >= profitmonth,
|
|
||||||
Trade.close_date < (profitmonth + relativedelta(months=1))
|
|
||||||
]).order_by(Trade.close_date).all()
|
|
||||||
curmonthprofit = sum(
|
|
||||||
trade.close_profit_abs for trade in trades if trade.close_profit_abs is not None)
|
|
||||||
profit_months[profitmonth] = {
|
|
||||||
'amount': curmonthprofit,
|
|
||||||
'trades': len(trades)
|
|
||||||
}
|
|
||||||
|
|
||||||
data = [
|
|
||||||
{
|
|
||||||
'date': f"{key.year}-{key.month:02d}",
|
|
||||||
'abs_profit': value["amount"],
|
|
||||||
'fiat_value': self._fiat_converter.convert_amount(
|
|
||||||
value['amount'],
|
|
||||||
stake_currency,
|
|
||||||
fiat_display_currency
|
|
||||||
) if self._fiat_converter else 0,
|
|
||||||
'trade_count': value["trades"],
|
|
||||||
}
|
|
||||||
for key, value in profit_months.items()
|
|
||||||
]
|
|
||||||
return {
|
|
||||||
'stake_currency': stake_currency,
|
|
||||||
'fiat_display_currency': fiat_display_currency,
|
|
||||||
'data': data
|
|
||||||
}
|
|
||||||
|
|
||||||
def _rpc_trade_history(self, limit: int, offset: int = 0, order_by_id: bool = False) -> Dict:
|
def _rpc_trade_history(self, limit: int, offset: int = 0, order_by_id: bool = False) -> Dict:
|
||||||
""" Returns the X last trades """
|
""" Returns the X last trades """
|
||||||
order_by = Trade.id if order_by_id else Trade.close_date.desc()
|
order_by = Trade.id if order_by_id else Trade.close_date.desc()
|
||||||
|
@ -579,10 +579,11 @@ class Telegram(RPCHandler):
|
|||||||
except (TypeError, ValueError, IndexError):
|
except (TypeError, ValueError, IndexError):
|
||||||
timescale = 7
|
timescale = 7
|
||||||
try:
|
try:
|
||||||
stats = self._rpc._rpc_daily_profit(
|
stats = self._rpc._rpc_timeunit_profit(
|
||||||
timescale,
|
timescale,
|
||||||
stake_cur,
|
stake_cur,
|
||||||
fiat_disp_cur
|
fiat_disp_cur,
|
||||||
|
'days'
|
||||||
)
|
)
|
||||||
stats_tab = tabulate(
|
stats_tab = tabulate(
|
||||||
[[day['date'],
|
[[day['date'],
|
||||||
@ -618,7 +619,7 @@ class Telegram(RPCHandler):
|
|||||||
except (TypeError, ValueError, IndexError):
|
except (TypeError, ValueError, IndexError):
|
||||||
timescale = 8
|
timescale = 8
|
||||||
try:
|
try:
|
||||||
stats = self._rpc._rpc_daily_profit(
|
stats = self._rpc._rpc_timeunit_profit(
|
||||||
timescale,
|
timescale,
|
||||||
stake_cur,
|
stake_cur,
|
||||||
fiat_disp_cur,
|
fiat_disp_cur,
|
||||||
@ -659,10 +660,11 @@ class Telegram(RPCHandler):
|
|||||||
except (TypeError, ValueError, IndexError):
|
except (TypeError, ValueError, IndexError):
|
||||||
timescale = 6
|
timescale = 6
|
||||||
try:
|
try:
|
||||||
stats = self._rpc._rpc_monthly_profit(
|
stats = self._rpc._rpc_timeunit_profit(
|
||||||
timescale,
|
timescale,
|
||||||
stake_cur,
|
stake_cur,
|
||||||
fiat_disp_cur
|
fiat_disp_cur,
|
||||||
|
'months'
|
||||||
)
|
)
|
||||||
stats_tab = tabulate(
|
stats_tab = tabulate(
|
||||||
[[month['date'],
|
[[month['date'],
|
||||||
|
Loading…
Reference in New Issue
Block a user