Merge pull request #4677 from mads03dk/get_trade

Add API endpoint for getting a specific trade
This commit is contained in:
Matthias 2021-04-16 19:57:58 +02:00 committed by GitHub
commit c52edcff4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 4 deletions

View File

@ -125,6 +125,7 @@ python3 scripts/rest_client.py --config rest_config.json <command> [optional par
| `stopbuy` | Stops the trader from opening new trades. Gracefully closes open trades according to their rules. | `stopbuy` | Stops the trader from opening new trades. Gracefully closes open trades according to their rules.
| `reload_config` | Reloads the configuration file. | `reload_config` | Reloads the configuration file.
| `trades` | List last trades. | `trades` | List last trades.
| `trade/<tradeid>` | Get specific trade.
| `delete_trade <trade_id>` | Remove trade from the database. Tries to close open orders. Requires manual handling of this trade on the exchange. | `delete_trade <trade_id>` | Remove trade from the database. Tries to close open orders. Requires manual handling of this trade on the exchange.
| `show_config` | Shows part of the current configuration with relevant settings to operation. | `show_config` | Shows part of the current configuration with relevant settings to operation.
| `logs` | Shows last log messages. | `logs` | Shows last log messages.
@ -181,7 +182,7 @@ count
Return the amount of open trades. Return the amount of open trades.
daily daily
Return the amount of open trades. Return the profits for each day, and amount of trades.
delete_lock delete_lock
Delete (disable) lock from the database. Delete (disable) lock from the database.
@ -214,7 +215,7 @@ locks
logs logs
Show latest logs. Show latest logs.
:param limit: Limits log messages to the last <limit> logs. No limit to get all the trades. :param limit: Limits log messages to the last <limit> logs. No limit to get the entire log.
pair_candles pair_candles
Return live dataframe for <pair><timeframe>. Return live dataframe for <pair><timeframe>.
@ -234,6 +235,9 @@ pair_history
performance performance
Return the performance of the different coins. Return the performance of the different coins.
ping
simple ping
plot_config plot_config
Return plot configuration if the strategy defines one. Return plot configuration if the strategy defines one.
@ -270,6 +274,11 @@ strategy
:param strategy: Strategy class name :param strategy: Strategy class name
trade
Return specific trade
:param trade_id: Specify which trade to get.
trades trades
Return trades history. Return trades history.

View File

@ -89,6 +89,14 @@ def trades(limit: int = 0, rpc: RPC = Depends(get_rpc)):
return rpc._rpc_trade_history(limit) return rpc._rpc_trade_history(limit)
@router.get('/trade/{tradeid}', response_model=OpenTradeSchema, tags=['info', 'trading'])
def trade(tradeid: int = 0, rpc: RPC = Depends(get_rpc)):
try:
return rpc._rpc_trade_status([tradeid])[0]
except (RPCException, KeyError):
raise HTTPException(status_code=404, detail='Trade not found.')
@router.delete('/trades/{tradeid}', response_model=DeleteTrade, tags=['info', 'trading']) @router.delete('/trades/{tradeid}', response_model=DeleteTrade, tags=['info', 'trading'])
def trades_delete(tradeid: int, rpc: RPC = Depends(get_rpc)): def trades_delete(tradeid: int, rpc: RPC = Depends(get_rpc)):
return rpc._rpc_delete(tradeid) return rpc._rpc_delete(tradeid)

View File

@ -127,7 +127,7 @@ class FtRestClient():
return self._delete("locks/{}".format(lock_id)) return self._delete("locks/{}".format(lock_id))
def daily(self, days=None): def daily(self, days=None):
"""Return the amount of open trades. """Return the profits for each day, and amount of trades.
:return: json object :return: json object
""" """
@ -195,7 +195,7 @@ class FtRestClient():
def logs(self, limit=None): def logs(self, limit=None):
"""Show latest logs. """Show latest logs.
:param limit: Limits log messages to the last <limit> logs. No limit to get all the trades. :param limit: Limits log messages to the last <limit> logs. No limit to get the entire log.
:return: json object :return: json object
""" """
return self._get("logs", params={"limit": limit} if limit else 0) return self._get("logs", params={"limit": limit} if limit else 0)
@ -208,6 +208,14 @@ class FtRestClient():
""" """
return self._get("trades", params={"limit": limit} if limit else 0) return self._get("trades", params={"limit": limit} if limit else 0)
def trade(self, trade_id):
"""Return specific trade
:param trade_id: Specify which trade to get.
:return: json object
"""
return self._get("trade/{}".format(trade_id))
def delete_trade(self, trade_id): def delete_trade(self, trade_id):
"""Delete trade from the database. """Delete trade from the database.
Tries to close open orders. Requires manual handling of this asset on the exchange. Tries to close open orders. Requires manual handling of this asset on the exchange.

View File

@ -522,6 +522,26 @@ def test_api_trades(botclient, mocker, fee, markets):
assert rc.json()['trades_count'] == 1 assert rc.json()['trades_count'] == 1
def test_api_trade_single(botclient, mocker, fee, ticker, markets):
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets),
fetch_ticker=ticker,
)
rc = client_get(client, f"{BASE_URI}/trade/3")
assert_response(rc, 404)
assert rc.json()['detail'] == 'Trade not found.'
create_mock_trades(fee)
Trade.query.session.flush()
rc = client_get(client, f"{BASE_URI}/trade/3")
assert_response(rc)
assert rc.json()['trade_id'] == 3
def test_api_delete_trade(botclient, mocker, fee, markets): def test_api_delete_trade(botclient, mocker, fee, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot, (True, False)) patch_get_signal(ftbot, (True, False))