Add more endpoints to fastapi
This commit is contained in:
		| @@ -1,4 +1,4 @@ | |||||||
| from typing import List | from typing import Dict, List, Union | ||||||
|  |  | ||||||
| from pydantic import BaseModel | from pydantic import BaseModel | ||||||
|  |  | ||||||
| @@ -45,3 +45,48 @@ class Count(BaseModel): | |||||||
|     current: int |     current: int | ||||||
|     max: int |     max: int | ||||||
|     total_stake: float |     total_stake: float | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class PerformanceEntry(BaseModel): | ||||||
|  |     pair: str | ||||||
|  |     profit: float | ||||||
|  |     count: int | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Profit(BaseModel): | ||||||
|  |     profit_closed_coin: float | ||||||
|  |     profit_closed_percent: float | ||||||
|  |     profit_closed_percent_mean: float | ||||||
|  |     profit_closed_ratio_mean: float | ||||||
|  |     profit_closed_percent_sum: float | ||||||
|  |     profit_closed_ratio_sum: float | ||||||
|  |     profit_closed_fiat: float | ||||||
|  |     profit_all_coin: float | ||||||
|  |     profit_all_percent: float | ||||||
|  |     profit_all_percent_mean: float | ||||||
|  |     profit_all_ratio_mean: float | ||||||
|  |     profit_all_percent_sum: float | ||||||
|  |     profit_all_ratio_sum: float | ||||||
|  |     profit_all_fiat: float | ||||||
|  |     trade_count: int | ||||||
|  |     closed_trade_count: int | ||||||
|  |     first_trade_date: str | ||||||
|  |     first_trade_timestamp: int | ||||||
|  |     latest_trade_date: str | ||||||
|  |     latest_trade_timestamp: int | ||||||
|  |     avg_duration: str | ||||||
|  |     best_pair: str | ||||||
|  |     best_rate: float | ||||||
|  |     winning_trades: int | ||||||
|  |     losing_trades: int | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class SellReason(BaseModel): | ||||||
|  |     wins: int | ||||||
|  |     losses: int | ||||||
|  |     draws: int | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Stats(BaseModel): | ||||||
|  |     sell_reasons: Dict[str, SellReason] | ||||||
|  |     durations: Dict[str, Union[str, float]] | ||||||
|   | |||||||
| @@ -1,9 +1,11 @@ | |||||||
|  | from typing import List | ||||||
|  |  | ||||||
| from fastapi import APIRouter, Depends | from fastapi import APIRouter, Depends | ||||||
|  |  | ||||||
| from freqtrade import __version__ | from freqtrade import __version__ | ||||||
| from freqtrade.rpc import RPC | from freqtrade.rpc import RPC | ||||||
|  |  | ||||||
| from .api_models import Balances, Count, Ping, StatusMsg, Version | from .api_models import Balances, Count, PerformanceEntry, Ping, Profit, Stats, StatusMsg, Version | ||||||
| from .deps import get_config, get_rpc | from .deps import get_config, get_rpc | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -34,6 +36,23 @@ def count(rpc: RPC = Depends(get_rpc)): | |||||||
|     return rpc._rpc_count() |     return rpc._rpc_count() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @router.get('/performance', response_model=List[PerformanceEntry], tags=['info']) | ||||||
|  | def performance(rpc: RPC = Depends(get_rpc)): | ||||||
|  |     return rpc._rpc_performance() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @router.get('/profit', response_model=Profit, tags=['info']) | ||||||
|  | def profit(rpc: RPC = Depends(get_rpc), config=Depends(get_config)): | ||||||
|  |     return rpc._rpc_trade_statistics(config['stake_currency'], | ||||||
|  |                                      config.get('fiat_display_currency') | ||||||
|  |                                      ) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @router.get('/stats', response_model=Stats, tags=['info']) | ||||||
|  | def stats(rpc: RPC = Depends(get_rpc)): | ||||||
|  |     return rpc._rpc_stats() | ||||||
|  |  | ||||||
|  |  | ||||||
| @router.get('/show_config', tags=['info']) | @router.get('/show_config', tags=['info']) | ||||||
| def show_config(rpc: RPC = Depends(get_rpc), config=Depends(get_config)): | def show_config(rpc: RPC = Depends(get_rpc), config=Depends(get_config)): | ||||||
|     return RPC._rpc_show_config(config, rpc._freqtrade.state) |     return RPC._rpc_show_config(config, rpc._freqtrade.state) | ||||||
|   | |||||||
| @@ -512,7 +512,7 @@ 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, 200) |     assert_response(rc, 200) | ||||||
|     assert rc.json['trade_count'] == 0 |     assert rc.json()['trade_count'] == 0 | ||||||
|  |  | ||||||
|     ftbot.enter_positions() |     ftbot.enter_positions() | ||||||
|     trade = Trade.query.first() |     trade = Trade.query.first() | ||||||
| @@ -522,9 +522,9 @@ 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, 200) |     assert_response(rc, 200) | ||||||
|     # One open trade |     # One open trade | ||||||
|     assert rc.json['trade_count'] == 1 |     assert rc.json()['trade_count'] == 1 | ||||||
|     assert rc.json['best_pair'] == '' |     assert rc.json()['best_pair'] == '' | ||||||
|     assert rc.json['best_rate'] == 0 |     assert rc.json()['best_rate'] == 0 | ||||||
|  |  | ||||||
|     trade = Trade.query.first() |     trade = Trade.query.first() | ||||||
|     trade.update(limit_sell_order) |     trade.update(limit_sell_order) | ||||||
| @@ -534,32 +534,32 @@ 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) |     assert_response(rc) | ||||||
|     assert rc.json == {'avg_duration': '0:00:00', |     assert rc.json() == {'avg_duration': '0:00:00', | ||||||
|                        'best_pair': 'ETH/BTC', |                          'best_pair': 'ETH/BTC', | ||||||
|                        'best_rate': 6.2, |                          'best_rate': 6.2, | ||||||
|                        'first_trade_date': 'just now', |                          'first_trade_date': 'just now', | ||||||
|                        'first_trade_timestamp': ANY, |                          'first_trade_timestamp': ANY, | ||||||
|                        'latest_trade_date': 'just now', |                          'latest_trade_date': 'just now', | ||||||
|                        'latest_trade_timestamp': ANY, |                          'latest_trade_timestamp': ANY, | ||||||
|                        'profit_all_coin': 6.217e-05, |                          'profit_all_coin': 6.217e-05, | ||||||
|                        'profit_all_fiat': 0.76748865, |                          'profit_all_fiat': 0.76748865, | ||||||
|                        'profit_all_percent': 6.2, |                          'profit_all_percent': 6.2, | ||||||
|                        'profit_all_percent_mean': 6.2, |                          'profit_all_percent_mean': 6.2, | ||||||
|                        'profit_all_ratio_mean': 0.06201058, |                          'profit_all_ratio_mean': 0.06201058, | ||||||
|                        'profit_all_percent_sum': 6.2, |                          'profit_all_percent_sum': 6.2, | ||||||
|                        'profit_all_ratio_sum': 0.06201058, |                          'profit_all_ratio_sum': 0.06201058, | ||||||
|                        'profit_closed_coin': 6.217e-05, |                          'profit_closed_coin': 6.217e-05, | ||||||
|                        'profit_closed_fiat': 0.76748865, |                          'profit_closed_fiat': 0.76748865, | ||||||
|                        'profit_closed_percent': 6.2, |                          'profit_closed_percent': 6.2, | ||||||
|                        'profit_closed_ratio_mean': 0.06201058, |                          'profit_closed_ratio_mean': 0.06201058, | ||||||
|                        'profit_closed_percent_mean': 6.2, |                          'profit_closed_percent_mean': 6.2, | ||||||
|                        'profit_closed_ratio_sum': 0.06201058, |                          'profit_closed_ratio_sum': 0.06201058, | ||||||
|                        'profit_closed_percent_sum': 6.2, |                          'profit_closed_percent_sum': 6.2, | ||||||
|                        'trade_count': 1, |                          'trade_count': 1, | ||||||
|                        'closed_trade_count': 1, |                          'closed_trade_count': 1, | ||||||
|                        'winning_trades': 1, |                          'winning_trades': 1, | ||||||
|                        'losing_trades': 0, |                          'losing_trades': 0, | ||||||
|                        } |                          } | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.usefixtures("init_persistence") | @pytest.mark.usefixtures("init_persistence") | ||||||
| @@ -576,19 +576,19 @@ def test_api_stats(botclient, mocker, ticker, fee, markets,): | |||||||
|  |  | ||||||
|     rc = client_get(client, f"{BASE_URI}/stats") |     rc = client_get(client, f"{BASE_URI}/stats") | ||||||
|     assert_response(rc, 200) |     assert_response(rc, 200) | ||||||
|     assert 'durations' in rc.json |     assert 'durations' in rc.json() | ||||||
|     assert 'sell_reasons' in rc.json |     assert 'sell_reasons' in rc.json() | ||||||
|  |  | ||||||
|     create_mock_trades(fee) |     create_mock_trades(fee) | ||||||
|  |  | ||||||
|     rc = client_get(client, f"{BASE_URI}/stats") |     rc = client_get(client, f"{BASE_URI}/stats") | ||||||
|     assert_response(rc, 200) |     assert_response(rc, 200) | ||||||
|     assert 'durations' in rc.json |     assert 'durations' in rc.json() | ||||||
|     assert 'sell_reasons' in rc.json |     assert 'sell_reasons' in rc.json() | ||||||
|  |  | ||||||
|     assert 'wins' in rc.json['durations'] |     assert 'wins' in rc.json()['durations'] | ||||||
|     assert 'losses' in rc.json['durations'] |     assert 'losses' in rc.json()['durations'] | ||||||
|     assert 'draws' in rc.json['durations'] |     assert 'draws' in rc.json()['durations'] | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_api_performance(botclient, mocker, ticker, fee): | def test_api_performance(botclient, mocker, ticker, fee): | ||||||
| @@ -629,9 +629,9 @@ def test_api_performance(botclient, mocker, ticker, fee): | |||||||
|  |  | ||||||
|     rc = client_get(client, f"{BASE_URI}/performance") |     rc = client_get(client, f"{BASE_URI}/performance") | ||||||
|     assert_response(rc) |     assert_response(rc) | ||||||
|     assert len(rc.json) == 2 |     assert len(rc.json()) == 2 | ||||||
|     assert rc.json == [{'count': 1, 'pair': 'LTC/ETH', 'profit': 7.61}, |     assert rc.json() == [{'count': 1, 'pair': 'LTC/ETH', 'profit': 7.61}, | ||||||
|                        {'count': 1, 'pair': 'XRP/ETH', 'profit': -5.57}] |                          {'count': 1, 'pair': 'XRP/ETH', 'profit': -5.57}] | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_api_status(botclient, mocker, ticker, fee, markets): | def test_api_status(botclient, mocker, ticker, fee, markets): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user