Enhance restclient and add tests for new api methods

This commit is contained in:
Matthias 2020-09-11 20:31:02 +02:00
parent bb4993dc20
commit f82d39e1b0
4 changed files with 98 additions and 4 deletions

View File

@ -145,6 +145,12 @@ python3 scripts/rest_client.py help
``` output
Possible commands:
available_pairs
Return available pair (backtest data) based on timeframe / stake_currency selection
:param timeframe: Only pairs with this timeframe available.
:param stake_currency: Only pairs that include this timeframe
balance
Get the account balance.
@ -184,9 +190,27 @@ logs
:param limit: Limits log messages to the last <limit> logs. No limit to get all the trades.
pair_candles
Return live dataframe for <pair><timeframe>.
:param pair: Pair to get data for
:param timeframe: Only pairs with this timeframe available.
:param limit: Limit result to the last n candles.
pair_history
Return historic, analyzed dataframe
:param pair: Pair to get data for
:param timeframe: Only pairs with this timeframe available.
:param strategy: Strategy to analyze and get values for
:param timerange: Timerange to get data for (same format than --timerange endpoints)
performance
Return the performance of the different coins.
plot_config
Return plot configuration if the strategy defines one.
profit
Return the profit summary.
@ -209,6 +233,9 @@ stop
stopbuy
Stop buying (but handle sells gracefully). Use `reload_config` to reset.
strategies
Lists available strategies
trades
Return trades history.
@ -220,7 +247,6 @@ version
whitelist
Show the current whitelist.
```
## Advanced API usage using JWT tokens

View File

@ -166,8 +166,8 @@ class ApiServer(RPC):
""" Helper function to jsonify object for a webserver """
return jsonify(return_value)
def rest_error(self, error_msg):
return jsonify({"error": error_msg}), 502
def rest_error(self, error_msg, error_code=502):
return jsonify({"error": error_msg}), error_code
def register_rest_rpc_urls(self):
"""
@ -531,6 +531,8 @@ class ApiServer(RPC):
pair = request.args.get("pair")
timeframe = request.args.get("timeframe")
limit = request.args.get("limit", type=int)
if not pair or not timeframe:
return self.rest_error("Mandatory parameter missing.", 400)
results = self._analysed_dataframe(pair, timeframe, limit)
return self.rest_dump(results)
@ -556,7 +558,7 @@ class ApiServer(RPC):
strategy = request.args.get("strategy")
if not pair or not timeframe or not timerange or not strategy:
return self.rest_error("Mandatory parameter missing.")
return self.rest_error("Mandatory parameter missing.", 400)
config = deepcopy(self._config)
config.update({

View File

@ -224,6 +224,62 @@ class FtRestClient():
return self._post("forcesell", data={"tradeid": tradeid})
def strategies(self):
"""Lists available strategies
:return: json object
"""
return self._get("strategies")
def plot_config(self):
"""Return plot configuration if the strategy defines one.
:return: json object
"""
return self._get("plot_config")
def available_pairs(self, timeframe=None, stake_currency=None):
"""Return available pair (backtest data) based on timeframe / stake_currency selection
:param timeframe: Only pairs with this timeframe available.
:param stake_currency: Only pairs that include this timeframe
:return: json object
"""
return self._get("available_pairs", params={
"stake_currency": stake_currency if timeframe else '',
"timeframe": timeframe if timeframe else '',
})
def pair_candles(self, pair, timeframe, limit=None):
"""Return live dataframe for <pair><timeframe>.
:param pair: Pair to get data for
:param timeframe: Only pairs with this timeframe available.
:param limit: Limit result to the last n candles.
:return: json object
"""
return self._get("available_pairs", params={
"pair": pair,
"timeframe": timeframe,
"limit": limit,
})
def pair_history(self, pair, timeframe, strategy, timerange=None):
"""Return historic, analyzed dataframe
:param pair: Pair to get data for
:param timeframe: Only pairs with this timeframe available.
:param strategy: Strategy to analyze and get values for
:param timerange: Timerange to get data for (same format than --timerange endpoints)
:return: json object
"""
return self._get("pair_history", params={
"pair": pair,
"timeframe": timeframe,
"strategy": strategy,
"timerange": timerange if timerange else '',
})
def add_arguments():
parser = argparse.ArgumentParser()

View File

@ -818,6 +818,16 @@ def test_api_pair_candles(botclient, ohlcv_history):
timeframe = '5m'
amount = 2
# No pair
rc = client_get(client,
f"{BASE_URI}/pair_candles?limit={amount}&timeframe={timeframe}")
assert_response(rc, 400)
# No timeframe
rc = client_get(client,
f"{BASE_URI}/pair_candles?pair=XRP%2FBTC")
assert_response(rc, 400)
rc = client_get(client,
f"{BASE_URI}/pair_candles?limit={amount}&pair=XRP%2FBTC&timeframe={timeframe}")
assert_response(rc)