From 0a14d5ec467bdab197d1d100446f7c4dd5fdb698 Mon Sep 17 00:00:00 2001 From: Ork Blutt Date: Sun, 5 Apr 2020 16:14:02 +0200 Subject: [PATCH] trades history RPC --- freqtrade/rpc/api_server.py | 16 ++++++++++++- freqtrade/rpc/rpc.py | 47 +++++++++++++++++++++++++++++++++++++ scripts/rest_client.py | 7 ++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 8f4cc4787..7e0cdd71d 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -173,7 +173,8 @@ class ApiServer(RPC): view_func=self._show_config, methods=['GET']) self.app.add_url_rule(f'{BASE_URI}/ping', 'ping', view_func=self._ping, methods=['GET']) - + self.app.add_url_rule(f'{BASE_URI}/trades', 'trades', + view_func=self._trades, methods=['GET']) # Combined actions and infos self.app.add_url_rule(f'{BASE_URI}/blacklist', 'blacklist', view_func=self._blacklist, methods=['GET', 'POST']) @@ -357,6 +358,19 @@ class ApiServer(RPC): results = self._rpc_balance(self._config['stake_currency'], self._config.get('fiat_display_currency', '')) return self.rest_dump(results) + + @require_login + @rpc_catch_errors + def _trades(self): + """ + Handler for /trades. + + Returns the X last trades in json format + """ + last_trades_number = request.args.get('last_trades_number', 0) + last_trades_number = int(last_trades_number) + results = self._rpc_trade_history(last_trades_number) + return self.rest_dump(results) @require_login @rpc_catch_errors diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index a0f50b070..b0c045e4a 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -226,6 +226,53 @@ class RPC: for key, value in profit_days.items() ] + def _rpc_trade_history( + self, last_trades_number: int) -> List[List[Any]]: + """ Returns the X last trades """ + if last_trades_number > 0: + trades = Trade.get_trades().order_by(Trade.id.desc()).limit(last_trades_number) + else: + trades = Trade.get_trades().order_by(Trade.id.desc()).all() + + output = [] + + for trade in trades: + output.append({ + 'id': trade.id, + 'pair': trade.pair, + 'exchange': trade.exchange, + 'is_open': trade.is_open if trade.is_open is not None else 0, + 'open_rate': trade.open_rate, + 'close_rate': trade.close_rate, + 'fee_open': trade.fee_open, + 'fee_close': trade.fee_close, + 'open_rate_requested': trade.open_rate_requested, + 'open_trade_price': trade.open_trade_price, + 'close_rate_requested': trade.close_rate_requested, + 'close_profit': trade.close_profit, + 'close_profit_abs': trade.close_profit_abs, + 'stake_amount': trade.stake_amount, + 'amount': trade.amount, + 'open_date': trade.open_date, + 'close_date': trade.close_date, + 'open_order_id': trade.open_order_id, + 'stop_loss': trade.stop_loss, + 'stop_loss_pct': trade.stop_loss_pct, + 'initial_stop_loss': trade.initial_stop_loss, + 'initial_stop_loss_pct': trade.initial_stop_loss_pct, + 'stoploss_order_id': trade.stoploss_order_id, + 'stoploss_last_update': trade.stoploss_last_update, + 'max_rate': trade.max_rate, + 'min_rate': trade.min_rate, + 'sell_reason': trade.sell_reason, + 'strategy': trade.strategy, + 'ticker_interval': trade.ticker_interval, + }) + + return { + "trades" : output + } + def _rpc_trade_statistics( self, stake_currency: str, fiat_display_currency: str) -> Dict[str, Any]: """ Returns cumulative profit statistics """ diff --git a/scripts/rest_client.py b/scripts/rest_client.py index ccb33604f..5cbdd8e07 100755 --- a/scripts/rest_client.py +++ b/scripts/rest_client.py @@ -156,6 +156,13 @@ class FtRestClient(): """ return self._get("show_config") + def history(self, number=None): + """Return the amount of open trades. + + :return: json object + """ + return self._get("trades", params={"last_trades_number": number} if number else 0) + def whitelist(self): """Show the current whitelist.