freqtrade_origin/ft_client/freqtrade_client/ft_rest_client.py

477 lines
14 KiB
Python
Raw Permalink Normal View History

2019-04-04 05:08:24 +00:00
"""
2024-03-29 15:16:34 +00:00
A Rest Client for Freqtrade bot
2019-04-04 19:07:44 +00:00
Should not import anything from freqtrade,
2024-03-29 15:16:34 +00:00
so it can be used as a standalone script, and can be installed independently.
2019-04-04 05:08:24 +00:00
"""
2019-04-09 04:40:15 +00:00
import json
2019-04-04 19:07:44 +00:00
import logging
2024-03-31 07:37:52 +00:00
from typing import Any, Dict, List, Optional, Union
from urllib.parse import urlencode, urlparse, urlunparse
2019-04-04 19:07:44 +00:00
2019-04-25 18:32:10 +00:00
import requests
2019-04-04 19:07:44 +00:00
from requests.exceptions import ConnectionError
2020-09-28 17:39:41 +00:00
2019-04-04 19:07:44 +00:00
logger = logging.getLogger("ft_rest_client")
2024-03-31 07:37:52 +00:00
ParamsT = Optional[Dict[str, Any]]
PostDataT = Optional[Union[Dict[str, Any], List[Dict[str, Any]]]]
2019-04-04 05:08:24 +00:00
class FtRestClient:
2024-05-12 14:16:26 +00:00
def __init__(
self, serverurl, username=None, password=None, *, pool_connections=10, pool_maxsize=10
):
2019-04-26 07:55:52 +00:00
self._serverurl = serverurl
self._session = requests.Session()
# allow configuration of pool
adapter = requests.adapters.HTTPAdapter(
2024-05-12 14:16:26 +00:00
pool_connections=pool_connections, pool_maxsize=pool_maxsize
)
2024-05-12 14:16:26 +00:00
self._session.mount("http://", adapter)
self._session.auth = (username, password)
2019-04-25 18:32:10 +00:00
2023-01-21 15:02:07 +00:00
def _call(self, method, apipath, params: Optional[dict] = None, data=None, files=None):
2024-05-12 14:16:26 +00:00
if str(method).upper() not in ("GET", "POST", "PUT", "DELETE"):
raise ValueError(f"invalid method <{method}>")
2019-05-15 05:12:33 +00:00
basepath = f"{self._serverurl}/api/v1/{apipath}"
2019-04-25 18:32:10 +00:00
2024-05-12 14:16:26 +00:00
hd = {"Accept": "application/json", "Content-Type": "application/json"}
2019-04-25 18:32:10 +00:00
# Split url
2019-04-26 07:27:20 +00:00
schema, netloc, path, par, query, fragment = urlparse(basepath)
2019-04-25 18:32:10 +00:00
# URLEncode query string
2019-04-26 08:03:54 +00:00
query = urlencode(params) if params else ""
2019-04-25 18:32:10 +00:00
# recombine url
2019-04-26 07:27:20 +00:00
url = urlunparse((schema, netloc, path, par, query, fragment))
2019-04-26 07:55:52 +00:00
2019-04-25 18:32:10 +00:00
try:
resp = self._session.request(method, url, headers=hd, data=json.dumps(data))
2019-04-26 07:08:03 +00:00
# return resp.text
return resp.json()
2019-04-25 18:32:10 +00:00
except ConnectionError:
2024-06-15 07:13:57 +00:00
logger.warning(f"Connection error - could not connect to {netloc}.")
2019-04-25 18:32:10 +00:00
2024-03-31 07:37:52 +00:00
def _get(self, apipath, params: ParamsT = None):
2019-04-26 07:27:20 +00:00
return self._call("GET", apipath, params=params)
2024-03-31 07:37:52 +00:00
def _delete(self, apipath, params: ParamsT = None):
2020-08-04 17:57:28 +00:00
return self._call("DELETE", apipath, params=params)
2024-03-31 07:37:52 +00:00
def _post(self, apipath, params: ParamsT = None, data: PostDataT = None):
2019-04-26 07:27:20 +00:00
return self._call("POST", apipath, params=params, data=data)
2019-04-26 08:10:01 +00:00
def start(self):
2019-11-20 12:20:39 +00:00
"""Start the bot if it's in the stopped state.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 08:10:01 +00:00
"""
return self._post("start")
def stop(self):
2019-11-20 12:20:39 +00:00
"""Stop the bot. Use `start` to restart.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 08:10:01 +00:00
"""
return self._post("stop")
def stopbuy(self):
"""Stop buying (but handle sells gracefully). Use `reload_config` to reset.
2019-11-20 12:20:39 +00:00
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 08:10:01 +00:00
"""
return self._post("stopbuy")
def reload_config(self):
2019-11-20 12:20:39 +00:00
"""Reload configuration.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 08:10:01 +00:00
"""
return self._post("reload_config")
2019-04-26 08:10:01 +00:00
2019-04-26 07:55:52 +00:00
def balance(self):
2019-11-20 12:20:39 +00:00
"""Get the account balance.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 07:27:20 +00:00
"""
2019-04-26 07:55:52 +00:00
return self._get("balance")
2019-04-26 07:27:20 +00:00
def count(self):
2019-11-20 12:20:39 +00:00
"""Return the amount of open trades.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 07:27:20 +00:00
"""
return self._get("count")
def entries(self, pair=None):
"""Returns List of dicts containing all Trades, based on buy tag performance
Can either be average for all pairs or a specific pair provided
:return: json object
"""
return self._get("entries", params={"pair": pair} if pair else None)
def exits(self, pair=None):
"""Returns List of dicts containing all Trades, based on exit reason performance
Can either be average for all pairs or a specific pair provided
:return: json object
"""
return self._get("exits", params={"pair": pair} if pair else None)
def mix_tags(self, pair=None):
"""Returns List of dicts containing all Trades, based on entry_tag + exit_reason performance
Can either be average for all pairs or a specific pair provided
:return: json object
"""
return self._get("mix_tags", params={"pair": pair} if pair else None)
2020-10-17 15:58:07 +00:00
def locks(self):
"""Return current locks
:return: json object
"""
return self._get("locks")
2021-03-01 18:50:39 +00:00
def delete_lock(self, lock_id):
"""Delete (disable) lock from the database.
:param lock_id: ID for the lock to delete
:return: json object
"""
2021-11-11 11:06:18 +00:00
return self._delete(f"locks/{lock_id}")
2021-03-01 18:50:39 +00:00
2024-05-12 14:16:26 +00:00
def lock_add(self, pair: str, until: str, side: str = "*", reason: str = ""):
2024-03-30 13:37:34 +00:00
"""Lock pair
:param pair: Pair to lock
:param until: Lock until this date (format "2024-03-30 16:00:00Z")
:param side: Side to lock (long, short, *)
:param reason: Reason for the lock
:return: json object
"""
2024-05-12 14:16:26 +00:00
data = [{"pair": pair, "until": until, "side": side, "reason": reason}]
2024-03-30 13:37:34 +00:00
return self._post("locks", data=data)
2019-04-26 07:27:20 +00:00
def daily(self, days=None):
"""Return the profits for each day, and amount of trades.
2019-11-20 12:20:39 +00:00
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 07:27:20 +00:00
"""
return self._get("daily", params={"timescale": days} if days else None)
2023-09-02 15:06:23 +00:00
def weekly(self, weeks=None):
"""Return the profits for each week, and amount of trades.
:return: json object
"""
return self._get("weekly", params={"timescale": weeks} if weeks else None)
def monthly(self, months=None):
"""Return the profits for each month, and amount of trades.
:return: json object
"""
return self._get("monthly", params={"timescale": months} if months else None)
2019-04-26 08:06:46 +00:00
def edge(self):
2019-11-20 12:20:39 +00:00
"""Return information about edge.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 08:06:46 +00:00
"""
return self._get("edge")
2019-04-26 07:55:52 +00:00
def profit(self):
2019-11-20 12:20:39 +00:00
"""Return the profit summary.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 07:55:52 +00:00
"""
return self._get("profit")
2020-12-07 14:07:08 +00:00
def stats(self):
"""Return the stats report (durations, sell-reasons).
:return: json object
"""
return self._get("stats")
2019-04-26 08:03:54 +00:00
def performance(self):
2019-11-20 12:20:39 +00:00
"""Return the performance of the different coins.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 08:03:54 +00:00
"""
return self._get("performance")
2019-04-26 07:55:52 +00:00
def status(self):
2019-11-20 12:20:39 +00:00
"""Get the status of open trades.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 07:55:52 +00:00
"""
return self._get("status")
2019-04-26 08:06:46 +00:00
def version(self):
2019-11-20 12:20:39 +00:00
"""Return the version of the bot.
2019-06-23 20:10:37 +00:00
:return: json object containing the version
2019-04-26 08:06:46 +00:00
"""
return self._get("version")
2019-11-17 13:56:08 +00:00
def show_config(self):
2024-05-12 14:16:26 +00:00
"""Returns part of the configuration, relevant for trading operations.
2019-11-17 13:56:08 +00:00
:return: json object containing the version
"""
return self._get("show_config")
2021-03-02 08:54:00 +00:00
def ping(self):
"""simple ping"""
configstatus = self.show_config()
if not configstatus:
return {"status": "not_running"}
2024-05-12 14:16:26 +00:00
elif configstatus["state"] == "running":
2021-03-02 08:54:00 +00:00
return {"status": "pong"}
else:
2021-03-02 09:19:33 +00:00
return {"status": "not_running"}
2020-08-14 13:44:52 +00:00
def logs(self, limit=None):
"""Show latest logs.
:param limit: Limits log messages to the last <limit> logs. No limit to get the entire log.
2020-08-14 13:44:52 +00:00
:return: json object
"""
return self._get("logs", params={"limit": limit} if limit else 0)
def trades(self, limit=None, offset=None):
"""Return trades history, sorted by id
2020-04-05 14:14:02 +00:00
:param limit: Limits trades to the X last trades. Max 500 trades.
:param offset: Offset by this amount of trades.
2020-04-05 14:14:02 +00:00
:return: json object
"""
params = {}
if limit:
2024-05-12 14:16:26 +00:00
params["limit"] = limit
if offset:
2024-05-12 14:16:26 +00:00
params["offset"] = offset
return self._get("trades", params)
2020-04-05 14:14:02 +00:00
def trade(self, trade_id):
"""Return specific trade
:param trade_id: Specify which trade to get.
:return: json object
"""
2021-11-11 11:06:18 +00:00
return self._get(f"trade/{trade_id}")
2020-08-04 17:57:28 +00:00
def delete_trade(self, trade_id):
"""Delete trade from the database.
Tries to close open orders. Requires manual handling of this asset on the exchange.
:param trade_id: Deletes the trade with this ID from the database.
:return: json object
"""
2021-11-11 11:06:18 +00:00
return self._delete(f"trades/{trade_id}")
2020-08-04 17:57:28 +00:00
2023-01-31 06:24:19 +00:00
def cancel_open_order(self, trade_id):
"""Cancel open order for trade.
:param trade_id: Cancels open orders for this trade.
:return: json object
"""
return self._delete(f"trades/{trade_id}/open-order")
2019-04-26 07:55:52 +00:00
def whitelist(self):
2019-11-20 12:20:39 +00:00
"""Show the current whitelist.
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 07:55:52 +00:00
"""
return self._get("whitelist")
def blacklist(self, *args):
2019-11-20 12:20:39 +00:00
"""Show the current blacklist.
2019-04-26 07:55:52 +00:00
:param add: List of coins to add (example: "BNB/BTC")
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 07:55:52 +00:00
"""
if not args:
return self._get("blacklist")
else:
return self._post("blacklist", data={"blacklist": args})
2019-04-26 10:50:13 +00:00
def forcebuy(self, pair, price=None):
2019-11-20 12:20:39 +00:00
"""Buy an asset.
2019-04-26 10:50:13 +00:00
:param pair: Pair to buy (ETH/BTC)
:param price: Optional - price to buy
2019-06-23 20:10:37 +00:00
:return: json object of the trade
2019-04-26 10:50:13 +00:00
"""
2024-05-12 14:16:26 +00:00
data = {"pair": pair, "price": price}
2019-04-26 10:50:13 +00:00
return self._post("forcebuy", data=data)
2024-05-29 13:59:32 +00:00
def forceenter(
self,
pair,
side,
price=None,
*,
2024-05-29 13:59:32 +00:00
order_type=None,
stake_amount=None,
leverage=None,
enter_tag=None,
):
2022-01-26 19:07:58 +00:00
"""Force entering a trade
:param pair: Pair to buy (ETH/BTC)
:param side: 'long' or 'short'
:param price: Optional - price to buy
:param order_type: Optional keyword argument - 'limit' or 'market'
2024-06-15 07:45:34 +00:00
:param stake_amount: Optional keyword argument - stake amount (as float)
:param leverage: Optional keyword argument - leverage (as float)
:param enter_tag: Optional keyword argument - entry tag (as string, default: 'force_enter')
2022-01-26 19:07:58 +00:00
:return: json object of the trade
"""
2024-05-12 14:16:26 +00:00
data = {
"pair": pair,
"side": side,
}
if price:
2024-05-12 14:16:26 +00:00
data["price"] = price
if order_type:
data["ordertype"] = order_type
if stake_amount:
data["stakeamount"] = stake_amount
if leverage:
data["leverage"] = leverage
if enter_tag:
data["entry_tag"] = enter_tag
return self._post("forceenter", data=data)
2022-01-26 19:07:58 +00:00
2022-08-02 17:53:10 +00:00
def forceexit(self, tradeid, ordertype=None, amount=None):
2022-04-10 13:56:29 +00:00
"""Force-exit a trade.
2019-11-20 12:20:39 +00:00
2019-04-26 10:50:13 +00:00
:param tradeid: Id of the trade (can be received via status command)
2022-08-02 17:53:10 +00:00
:param ordertype: Order type to use (must be market or limit)
:param amount: Amount to sell. Full sell if not given
2019-06-23 20:10:37 +00:00
:return: json object
2019-04-26 10:50:13 +00:00
"""
2024-05-12 14:16:26 +00:00
return self._post(
"forceexit",
data={
"tradeid": tradeid,
"ordertype": ordertype,
"amount": amount,
},
)
2019-04-26 10:50:13 +00:00
def strategies(self):
"""Lists available strategies
:return: json object
"""
return self._get("strategies")
2020-09-17 05:53:22 +00:00
def strategy(self, strategy):
"""Get strategy details
:param strategy: Strategy class name
:return: json object
"""
return self._get(f"strategy/{strategy}")
2023-06-11 06:18:01 +00:00
def pairlists_available(self):
"""Lists available pairlist providers
2023-04-19 19:08:28 +00:00
:return: json object
"""
2023-06-11 06:18:01 +00:00
return self._get("pairlists/available")
2023-04-19 19:08:28 +00:00
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
"""
2024-05-12 14:16:26 +00:00
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, columns=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.
:param columns: List of dataframe columns to return. Empty list will return OHLCV.
:return: json object
"""
params = {
"pair": pair,
"timeframe": timeframe,
}
if limit:
2024-05-12 14:16:26 +00:00
params["limit"] = limit
if columns is not None:
2024-05-12 14:16:26 +00:00
params["columns"] = columns
return self._post("pair_candles", data=params)
return self._get("pair_candles", params=params)
2023-03-12 14:24:27 +00:00
def pair_history(self, pair, timeframe, strategy, timerange=None, freqaimodel=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 freqaimodel: FreqAI model to use for analysis
:param timerange: Timerange to get data for (same format than --timerange endpoints)
:return: json object
"""
2024-05-12 14:16:26 +00:00
return self._get(
"pair_history",
params={
"pair": pair,
"timeframe": timeframe,
"strategy": strategy,
"freqaimodel": freqaimodel,
"timerange": timerange if timerange else "",
},
)
def sysinfo(self):
"""Provides system information (CPU, RAM usage)
:return: json object
"""
return self._get("sysinfo")
2019-04-25 18:32:10 +00:00
def health(self):
"""Provides a quick health check of the running bot.
:return: json object
"""
return self._get("health")