Return /profit even if no trade is closed

This commit is contained in:
Matthias 2020-05-29 09:03:48 +02:00
parent 62ac5a9914
commit 6261aef314
4 changed files with 22 additions and 18 deletions

View File

@ -546,6 +546,7 @@ class Trade(_DECL_BASE):
def get_best_pair(): def get_best_pair():
""" """
Get best pair with closed trade. Get best pair with closed trade.
:returns: Tuple containing (pair, profit_sum)
""" """
best_pair = Trade.session.query( best_pair = Trade.session.query(
Trade.pair, func.sum(Trade.close_profit).label('profit_sum') Trade.pair, func.sum(Trade.close_profit).label('profit_sum')

View File

@ -281,11 +281,6 @@ class RPC:
best_pair = Trade.get_best_pair() best_pair = Trade.get_best_pair()
if not best_pair:
raise RPCException('no closed trade')
bp_pair, bp_rate = best_pair
# Prepare data to display # Prepare data to display
profit_closed_coin_sum = round(sum(profit_closed_coin), 8) profit_closed_coin_sum = round(sum(profit_closed_coin), 8)
profit_closed_percent = (round(mean(profit_closed_ratio) * 100, 2) if profit_closed_ratio profit_closed_percent = (round(mean(profit_closed_ratio) * 100, 2) if profit_closed_ratio
@ -304,6 +299,8 @@ class RPC:
fiat_display_currency fiat_display_currency
) if self._fiat_converter else 0 ) if self._fiat_converter else 0
first_date = trades[0].open_date if trades else None
last_date = trades[-1].open_date if trades else None
num = float(len(durations) or 1) num = float(len(durations) or 1)
return { return {
'profit_closed_coin': profit_closed_coin_sum, 'profit_closed_coin': profit_closed_coin_sum,
@ -313,13 +310,13 @@ class RPC:
'profit_all_percent': profit_all_percent, 'profit_all_percent': profit_all_percent,
'profit_all_fiat': profit_all_fiat, 'profit_all_fiat': profit_all_fiat,
'trade_count': len(trades), 'trade_count': len(trades),
'first_trade_date': arrow.get(trades[0].open_date).humanize(), 'first_trade_date': arrow.get(first_date).humanize() if first_date else '',
'first_trade_timestamp': int(trades[0].open_date.timestamp() * 1000), 'first_trade_timestamp': int(first_date.timestamp() * 1000) if first_date else 0,
'latest_trade_date': arrow.get(trades[-1].open_date).humanize(), 'latest_trade_date': arrow.get(last_date).humanize() if last_date else '',
'latest_trade_timestamp': int(trades[-1].open_date.timestamp() * 1000), 'latest_trade_timestamp': int(last_date.timestamp() * 1000) if last_date else 0,
'avg_duration': str(timedelta(seconds=sum(durations) / num)).split('.')[0], 'avg_duration': str(timedelta(seconds=sum(durations) / num)).split('.')[0],
'best_pair': bp_pair, 'best_pair': best_pair[0] if best_pair else '',
'best_rate': round(bp_rate * 100, 2), 'best_rate': round(best_pair[1] * 100, 2) if best_pair else 0,
} }
def _rpc_balance(self, stake_currency: str, fiat_display_currency: str) -> Dict: def _rpc_balance(self, stake_currency: str, fiat_display_currency: str) -> Dict:

View File

@ -279,8 +279,12 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
rpc = RPC(freqtradebot) rpc = RPC(freqtradebot)
rpc._fiat_converter = CryptoToFiatConverter() rpc._fiat_converter = CryptoToFiatConverter()
with pytest.raises(RPCException, match=r'.*no closed trade*'): res = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
rpc._rpc_trade_statistics(stake_currency, fiat_display_currency) assert res['trade_count'] == 0
assert res['first_trade_date'] == ''
assert res['first_trade_timestamp'] == 0
assert res['latest_trade_date'] == ''
assert res['latest_trade_timestamp'] == 0
# Create some test data # Create some test data
freqtradebot.enter_positions() freqtradebot.enter_positions()

View File

@ -396,9 +396,8 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, li
) )
rc = client_get(client, f"{BASE_URI}/profit") rc = client_get(client, f"{BASE_URI}/profit")
assert_response(rc, 502) assert_response(rc, 200)
assert len(rc.json) == 1 assert rc.json['trade_count'] == 0
assert rc.json == {"error": "Error querying _profit: no closed trade"}
ftbot.enter_positions() ftbot.enter_positions()
trade = Trade.query.first() trade = Trade.query.first()
@ -406,8 +405,11 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, li
# Simulate fulfilled LIMIT_BUY order for trade # Simulate fulfilled LIMIT_BUY order for trade
trade.update(limit_buy_order) trade.update(limit_buy_order)
rc = client_get(client, f"{BASE_URI}/profit") rc = client_get(client, f"{BASE_URI}/profit")
assert_response(rc, 502) assert_response(rc, 200)
assert rc.json == {"error": "Error querying _profit: no closed trade"} # One open trade
assert rc.json['trade_count'] == 1
assert rc.json['best_pair'] == ''
assert rc.json['best_rate'] == 0
trade.update(limit_sell_order) trade.update(limit_sell_order)