diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15f64ad38..407894bd2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: strategy: matrix: os: [ ubuntu-18.04, ubuntu-20.04 ] - python-version: ["3.7", "3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10"] steps: - uses: actions/checkout@v2 @@ -115,7 +115,7 @@ jobs: strategy: matrix: os: [ macos-latest ] - python-version: ["3.7", "3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10"] steps: - uses: actions/checkout@v2 @@ -207,7 +207,7 @@ jobs: strategy: matrix: os: [ windows-latest ] - python-version: ["3.7", "3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10"] steps: - uses: actions/checkout@v2 diff --git a/README.md b/README.md index 3a7d42fe9..b67e16010 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Please find the complete documentation on the [freqtrade website](https://www.fr ## Features -- [x] **Based on Python 3.7+**: For botting on any operating system - Windows, macOS and Linux. +- [x] **Based on Python 3.8+**: For botting on any operating system - Windows, macOS and Linux. - [x] **Persistence**: Persistence is achieved through sqlite. - [x] **Dry-run**: Run the bot without paying money. - [x] **Backtesting**: Run a simulation of your buy/sell strategy. @@ -197,7 +197,7 @@ To run this bot we recommend you a cloud instance with a minimum of: ### Software requirements -- [Python >= 3.7](http://docs.python-guide.org/en/latest/starting/installation/) +- [Python >= 3.8](http://docs.python-guide.org/en/latest/starting/installation/) - [pip](https://pip.pypa.io/en/stable/installing/) - [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) - [TA-Lib](https://mrjbq7.github.io/ta-lib/install.html) diff --git a/build_helpers/TA_Lib-0.4.24-cp37-cp37m-win_amd64.whl b/build_helpers/TA_Lib-0.4.24-cp37-cp37m-win_amd64.whl deleted file mode 100644 index ee8d64c6e..000000000 Binary files a/build_helpers/TA_Lib-0.4.24-cp37-cp37m-win_amd64.whl and /dev/null differ diff --git a/build_helpers/install_windows.ps1 b/build_helpers/install_windows.ps1 index de1b1d597..4caefa340 100644 --- a/build_helpers/install_windows.ps1 +++ b/build_helpers/install_windows.ps1 @@ -5,9 +5,6 @@ python -m pip install --upgrade pip wheel $pyv = python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" -if ($pyv -eq '3.7') { - pip install build_helpers\TA_Lib-0.4.24-cp37-cp37m-win_amd64.whl -} if ($pyv -eq '3.8') { pip install build_helpers\TA_Lib-0.4.24-cp38-cp38-win_amd64.whl } diff --git a/docs/advanced-hyperopt.md b/docs/advanced-hyperopt.md index 9ac31bf16..9dbb86b2d 100644 --- a/docs/advanced-hyperopt.md +++ b/docs/advanced-hyperopt.md @@ -105,7 +105,7 @@ You can define your own estimator for Hyperopt by implementing `generate_estimat ```python class MyAwesomeStrategy(IStrategy): class HyperOpt: - def generate_estimator(): + def generate_estimator(dimensions: List['Dimension'], **kwargs): return "RF" ``` @@ -119,13 +119,34 @@ Example for `ExtraTreesRegressor` ("ET") with additional parameters: ```python class MyAwesomeStrategy(IStrategy): class HyperOpt: - def generate_estimator(): + def generate_estimator(dimensions: List['Dimension'], **kwargs): from skopt.learning import ExtraTreesRegressor # Corresponds to "ET" - but allows additional parameters. return ExtraTreesRegressor(n_estimators=100) ``` +The `dimensions` parameter is the list of `skopt.space.Dimension` objects corresponding to the parameters to be optimized. It can be used to create isotropic kernels for the `skopt.learning.GaussianProcessRegressor` estimator. Here's an example: + +```python +class MyAwesomeStrategy(IStrategy): + class HyperOpt: + def generate_estimator(dimensions: List['Dimension'], **kwargs): + from skopt.utils import cook_estimator + from skopt.learning.gaussian_process.kernels import (Matern, ConstantKernel) + kernel_bounds = (0.0001, 10000) + kernel = ( + ConstantKernel(1.0, kernel_bounds) * + Matern(length_scale=np.ones(len(dimensions)), length_scale_bounds=[kernel_bounds for d in dimensions], nu=2.5) + ) + kernel += ( + ConstantKernel(1.0, kernel_bounds) * + Matern(length_scale=np.ones(len(dimensions)), length_scale_bounds=[kernel_bounds for d in dimensions], nu=1.5) + ) + + return cook_estimator("GP", space=dimensions, kernel=kernel, n_restarts_optimizer=2) +``` + !!! Note While custom estimators can be provided, it's up to you as User to do research on possible parameters and analyze / understand which ones should be used. If you're unsure about this, best use one of the Defaults (`"ET"` has proven to be the most versatile) without further parameters. diff --git a/docs/configuration.md b/docs/configuration.md index 340ae2e72..d702fe8f9 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -173,6 +173,7 @@ Mandatory parameters are marked as **Required**, which means that they are requi | `dataformat_ohlcv` | Data format to use to store historical candle (OHLCV) data.
*Defaults to `json`*.
**Datatype:** String | `dataformat_trades` | Data format to use to store historical trades data.
*Defaults to `jsongz`*.
**Datatype:** String | `position_adjustment_enable` | Enables the strategy to use position adjustments (additional buys or sells). [More information here](strategy-callbacks.md#adjust-trade-position).
[Strategy Override](#parameters-in-the-strategy).
*Defaults to `false`.*
**Datatype:** Boolean +| `max_entry_position_adjustment` | Maximum additional order(s) for each open trade on top of the first entry Order. Set it to `-1` for unlimited additional orders. [More information here](strategy-callbacks.md#adjust-trade-position).
[Strategy Override](#parameters-in-the-strategy).
*Defaults to `-1`.*
**Datatype:** Positive Integer or -1 ### Parameters in the strategy @@ -198,6 +199,7 @@ Values set in the configuration file always overwrite values set in the strategy * `ignore_roi_if_buy_signal` * `ignore_buying_expired_candle_after` * `position_adjustment_enable` +* `max_entry_position_adjustment` ### Configuring amount per trade diff --git a/docs/docker_quickstart.md b/docs/docker_quickstart.md index 95df37811..84c1d596a 100644 --- a/docs/docker_quickstart.md +++ b/docs/docker_quickstart.md @@ -126,6 +126,12 @@ All freqtrade arguments will be available by running `docker-compose run --rm fr !!! Note "`docker-compose run --rm`" Including `--rm` will remove the container after completion, and is highly recommended for all modes except trading mode (running with `freqtrade trade` command). +??? Note "Using docker without docker-compose" + "`docker-compose run --rm`" will require a compose file to be provided. + Some freqtrade commands that don't require authentication such as `list-pairs` can be run with "`docker run --rm`" instead. + For example `docker run --rm freqtradeorg/freqtrade:stable list-pairs --exchange binance --quote BTC --print-json`. + This can be useful for fetching exchange information to add to your `config.json` without affecting your running containers. + #### Example: Download data with docker-compose Download backtesting data for 5 days for the pair ETH/BTC and 1h timeframe from Binance. The data will be stored in the directory `user_data/data/` on the host. diff --git a/docs/index.md b/docs/index.md index 292955346..1f8f15704 100644 --- a/docs/index.md +++ b/docs/index.md @@ -11,7 +11,7 @@ ## Introduction -Freqtrade is a crypto-currency algorithmic trading software developed in python (3.7+) and supported on Windows, macOS and Linux. +Freqtrade is a crypto-currency algorithmic trading software developed in python (3.8+) and supported on Windows, macOS and Linux. !!! Danger "DISCLAIMER" This software is for educational purposes only. Do not risk money which you are afraid to lose. USE THE SOFTWARE AT YOUR OWN RISK. THE AUTHORS AND ALL AFFILIATES ASSUME NO RESPONSIBILITY FOR YOUR TRADING RESULTS. @@ -67,7 +67,7 @@ To run this bot we recommend you a linux cloud instance with a minimum of: Alternatively -- Python 3.7+ +- Python 3.8+ - pip (pip3) - git - TA-Lib diff --git a/docs/installation.md b/docs/installation.md index c67eff60b..2a1c3db0a 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -42,7 +42,7 @@ These requirements apply to both [Script Installation](#script-installation) and ### Install guide -* [Python >= 3.7.x](http://docs.python-guide.org/en/latest/starting/installation/) +* [Python >= 3.8.x](http://docs.python-guide.org/en/latest/starting/installation/) * [pip](https://pip.pypa.io/en/stable/installing/) * [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [virtualenv](https://virtualenv.pypa.io/en/stable/installation.html) (Recommended) diff --git a/docs/requirements-docs.txt b/docs/requirements-docs.txt index 0368c90ff..f64a0ea7c 100644 --- a/docs/requirements-docs.txt +++ b/docs/requirements-docs.txt @@ -1,4 +1,4 @@ mkdocs==1.2.3 -mkdocs-material==8.1.8 +mkdocs-material==8.1.9 mdx_truly_sane_lists==1.2 pymdown-extensions==9.1 diff --git a/docs/strategy-callbacks.md b/docs/strategy-callbacks.md index e83fee46f..1904dd206 100644 --- a/docs/strategy-callbacks.md +++ b/docs/strategy-callbacks.md @@ -74,7 +74,7 @@ class AwesomeStrategy(IStrategy): Freqtrade will fall back to the `proposed_stake` value should your code raise an exception. The exception itself will be logged. !!! Tip - You do not _have_ to ensure that `min_stake <= returned_value <= max_stake`. Trades will succeed as the returned value will be clamped to supported range and this acton will be logged. + You do not _have_ to ensure that `min_stake <= returned_value <= max_stake`. Trades will succeed as the returned value will be clamped to supported range and this action will be logged. !!! Tip Returning `0` or `None` will prevent trades from being placed. @@ -579,11 +579,13 @@ The `position_adjustment_enable` strategy property enables the usage of `adjust_ For performance reasons, it's disabled by default and freqtrade will show a warning message on startup if enabled. `adjust_trade_position()` can be used to perform additional orders, for example to manage risk with DCA (Dollar Cost Averaging). +`max_entry_position_adjustment` property is used to limit the number of additional buys per trade (on top of the first buy) that the bot can execute. By default, the value is -1 which means the bot have no limit on number of adjustment buys. + The strategy is expected to return a stake_amount (in stake currency) between `min_stake` and `max_stake` if and when an additional buy order should be made (position is increased). If there are not enough funds in the wallet (the return value is above `max_stake`) then the signal will be ignored. Additional orders also result in additional fees and those orders don't count towards `max_open_trades`. -This callback is **not** called when there is an open order (either buy or sell) waiting for execution. +This callback is **not** called when there is an open order (either buy or sell) waiting for execution, or when you have reached the maximum amount of extra buys that you have set on `max_entry_position_adjustment`. `adjust_trade_position()` is called very frequently for the duration of a trade, so you must keep your implementation as performant as possible. !!! Note "About stake size" @@ -614,7 +616,7 @@ class DigDeeperStrategy(IStrategy): # ... populate_* methods # Example specific variables - max_dca_orders = 3 + max_entry_position_adjustment = 3 # This number is explained a bit further down max_dca_multiplier = 5.5 @@ -656,8 +658,7 @@ class DigDeeperStrategy(IStrategy): return None filled_buys = trade.select_filled_orders('buy') - count_of_buys = len(filled_buys) - + count_of_buys = trade.nr_of_successful_buys # Allow up to 3 additional increasingly larger buys (4 in total) # Initial buy is 1x # If that falls to -5% profit, we buy 1.25x more, average profit should increase to roughly -2.2% @@ -666,15 +667,14 @@ class DigDeeperStrategy(IStrategy): # Total stake for this trade would be 1 + 1.25 + 1.5 + 1.75 = 5.5x of the initial allowed stake. # That is why max_dca_multiplier is 5.5 # Hope you have a deep wallet! - if 0 < count_of_buys <= self.max_dca_orders: - try: - # This returns first order stake size - stake_amount = filled_buys[0].cost - # This then calculates current safety order size - stake_amount = stake_amount * (1 + (count_of_buys * 0.25)) - return stake_amount - except Exception as exception: - return None + try: + # This returns first order stake size + stake_amount = filled_buys[0].cost + # This then calculates current safety order size + stake_amount = stake_amount * (1 + (count_of_buys * 0.25)) + return stake_amount + except Exception as exception: + return None return None diff --git a/docs/utils.md b/docs/utils.md index 4a032db26..c6e795e60 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -59,7 +59,7 @@ $ freqtrade new-config --config config_binance.json ? Do you want to enable Dry-run (simulated trades)? Yes ? Please insert your stake currency: BTC ? Please insert your stake amount: 0.05 -? Please insert max_open_trades (Integer or 'unlimited'): 3 +? Please insert max_open_trades (Integer or -1 for unlimited open trades): 3 ? Please insert your desired timeframe (e.g. 5m): 5m ? Please insert your display Currency (for reporting): USD ? Select exchange binance diff --git a/docs/windows_installation.md b/docs/windows_installation.md index 0832b753c..9a068e152 100644 --- a/docs/windows_installation.md +++ b/docs/windows_installation.md @@ -25,7 +25,7 @@ Install ta-lib according to the [ta-lib documentation](https://github.com/mrjbq7 As compiling from source on windows has heavy dependencies (requires a partial visual studio installation), there is also a repository of unofficial pre-compiled windows Wheels [here](https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib), which need to be downloaded and installed using `pip install TA_Lib-0.4.24-cp38-cp38-win_amd64.whl` (make sure to use the version matching your python version). -Freqtrade provides these dependencies for the latest 3 Python versions (3.7, 3.8, 3.9 and 3.10) and for 64bit Windows. +Freqtrade provides these dependencies for the latest 3 Python versions (3.8, 3.9 and 3.10) and for 64bit Windows. Other versions must be downloaded from the above link. ``` powershell diff --git a/environment.yml b/environment.yml index 84ab5ff6f..50af602e5 100644 --- a/environment.yml +++ b/environment.yml @@ -4,7 +4,7 @@ channels: # - defaults dependencies: # 1/4 req main - - python>=3.7,<3.9 + - python>=3.8,<=3.10 - numpy - pandas - pip @@ -25,9 +25,12 @@ dependencies: - fastapi - uvicorn - pyjwt + - aiofiles + - psutil - colorama - questionary - prompt-toolkit + - python-dateutil # ============================ diff --git a/freqtrade/__main__.py b/freqtrade/__main__.py index ab4c7a110..fc45bdf61 100644 --- a/freqtrade/__main__.py +++ b/freqtrade/__main__.py @@ -3,7 +3,7 @@ __main__.py for Freqtrade To launch Freqtrade as a module -> python -m freqtrade (with Python >= 3.7) +> python -m freqtrade (with Python >= 3.8) """ from freqtrade import main diff --git a/freqtrade/commands/build_config_commands.py b/freqtrade/commands/build_config_commands.py index 9e95cc455..ca3f11a21 100644 --- a/freqtrade/commands/build_config_commands.py +++ b/freqtrade/commands/build_config_commands.py @@ -76,12 +76,9 @@ def ask_user_config() -> Dict[str, Any]: { "type": "text", "name": "max_open_trades", - "message": f"Please insert max_open_trades (Integer or '{UNLIMITED_STAKE_AMOUNT}'):", + "message": "Please insert max_open_trades (Integer or -1 for unlimited open trades):", "default": "3", - "validate": lambda val: val == UNLIMITED_STAKE_AMOUNT or validate_is_int(val), - "filter": lambda val: '"' + UNLIMITED_STAKE_AMOUNT + '"' - if val == UNLIMITED_STAKE_AMOUNT - else val + "validate": lambda val: validate_is_int(val) }, { "type": "select", diff --git a/freqtrade/constants.py b/freqtrade/constants.py index 504c7dce9..d94e8d850 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -371,7 +371,9 @@ CONF_SCHEMA = { 'type': 'string', 'enum': AVAILABLE_DATAHANDLERS, 'default': 'jsongz' - } + }, + 'position_adjustment_enable': {'type': 'boolean'}, + 'max_entry_position_adjustment': {'type': ['integer', 'number'], 'minimum': -1}, }, 'definitions': { 'exchange': { diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index bfff7d06c..004fb2437 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -953,7 +953,7 @@ class Exchange: raise OperationalException(e) from e @retrier - def get_tickers(self, cached: bool = False) -> Dict: + def get_tickers(self, symbols: List[str] = None, cached: bool = False) -> Dict: """ :param cached: Allow cached result :return: fetch_tickers result @@ -963,7 +963,7 @@ class Exchange: if tickers: return tickers try: - tickers = self._api.fetch_tickers() + tickers = self._api.fetch_tickers(symbols) self._fetch_tickers_cache['fetch_tickers'] = tickers return tickers except ccxt.NotSupported as e: diff --git a/freqtrade/exchange/kraken.py b/freqtrade/exchange/kraken.py index 1b069aa6c..f4c8ca275 100644 --- a/freqtrade/exchange/kraken.py +++ b/freqtrade/exchange/kraken.py @@ -1,6 +1,6 @@ """ Kraken exchange subclass """ import logging -from typing import Any, Dict +from typing import Any, Dict, List import ccxt @@ -33,6 +33,12 @@ class Kraken(Exchange): return (parent_check and market.get('darkpool', False) is False) + def get_tickers(self, symbols: List[str] = None, cached: bool = False) -> Dict: + # Only fetch tickers for current stake currency + # Otherwise the request for kraken becomes too large. + symbols = list(self.get_markets(quote_currencies=[self._config['stake_currency']])) + return super().get_tickers(symbols=symbols, cached=cached) + @retrier def get_balances(self) -> dict: if self._config['dry_run']: diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 972b6f6b7..c3c03ed67 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -462,8 +462,8 @@ class FreqtradeBot(LoggingMixin): try: self.check_and_call_adjust_trade_position(trade) except DependencyException as exception: - logger.warning('Unable to adjust position of trade for %s: %s', - trade.pair, exception) + logger.warning( + f"Unable to adjust position of trade for {trade.pair}: {exception}") def check_and_call_adjust_trade_position(self, trade: Trade): """ @@ -471,6 +471,13 @@ class FreqtradeBot(LoggingMixin): If the strategy triggers the adjustment, a new order gets issued. Once that completes, the existing trade is modified to match new data. """ + if self.strategy.max_entry_position_adjustment > -1: + count_of_buys = trade.nr_of_successful_buys + if count_of_buys > self.strategy.max_entry_position_adjustment: + logger.debug(f"Max adjustment entries for {trade.pair} has been reached.") + return + else: + logger.debug("Max adjustment entries is set to unlimited.") current_rate = self.exchange.get_rate(trade.pair, refresh=True, side="buy") current_profit = trade.calc_profit_ratio(current_rate) diff --git a/freqtrade/main.py b/freqtrade/main.py index 6593fbcb6..162b4d029 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -9,8 +9,8 @@ from typing import Any, List # check min. python version -if sys.version_info < (3, 7): # pragma: no cover - sys.exit("Freqtrade requires Python version >= 3.7") +if sys.version_info < (3, 8): # pragma: no cover + sys.exit("Freqtrade requires Python version >= 3.8") from freqtrade.commands import Arguments from freqtrade.exceptions import FreqtradeException, OperationalException diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 8e52a62fa..e173c3367 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -381,7 +381,12 @@ class Backtesting: # Check if we need to adjust our current positions if self.strategy.position_adjustment_enable: - trade = self._get_adjust_trade_entry_for_candle(trade, sell_row) + check_adjust_buy = True + if self.strategy.max_entry_position_adjustment > -1: + count_of_buys = trade.nr_of_successful_buys + check_adjust_buy = (count_of_buys <= self.strategy.max_entry_position_adjustment) + if check_adjust_buy: + trade = self._get_adjust_trade_entry_for_candle(trade, sell_row) sell_candle_time = sell_row[DATE_IDX].to_pydatetime() sell = self.strategy.should_sell(trade, sell_row[OPEN_IDX], # type: ignore diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index f49f3f307..60c54fe40 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -372,7 +372,7 @@ class Hyperopt: } def get_optimizer(self, dimensions: List[Dimension], cpu_count) -> Optimizer: - estimator = self.custom_hyperopt.generate_estimator(dimensions) + estimator = self.custom_hyperopt.generate_estimator(dimensions=dimensions) acq_optimizer = "sampling" if isinstance(estimator, str): diff --git a/freqtrade/optimize/hyperopt_auto.py b/freqtrade/optimize/hyperopt_auto.py index e7843ff55..5bc0af42b 100644 --- a/freqtrade/optimize/hyperopt_auto.py +++ b/freqtrade/optimize/hyperopt_auto.py @@ -91,5 +91,5 @@ class HyperOptAuto(IHyperOpt): def trailing_space(self) -> List['Dimension']: return self._get_func('trailing_space')() - def generate_estimator(self, dimensions: List['Dimension']) -> EstimatorType: - return self._get_func('generate_estimator')(dimensions) + def generate_estimator(self, dimensions: List['Dimension'], **kwargs) -> EstimatorType: + return self._get_func('generate_estimator')(dimensions=dimensions, **kwargs) diff --git a/freqtrade/optimize/hyperopt_interface.py b/freqtrade/optimize/hyperopt_interface.py index 53b4f087c..01ffd7844 100644 --- a/freqtrade/optimize/hyperopt_interface.py +++ b/freqtrade/optimize/hyperopt_interface.py @@ -40,7 +40,7 @@ class IHyperOpt(ABC): IHyperOpt.ticker_interval = str(config['timeframe']) # DEPRECATED IHyperOpt.timeframe = str(config['timeframe']) - def generate_estimator(self) -> EstimatorType: + def generate_estimator(self, dimensions: List[Dimension], **kwargs) -> EstimatorType: """ Return base_estimator. Can be any of "GP", "RF", "ET", "GBRT" or an instance of a class diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 76886d8ae..411db3fa7 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -569,8 +569,8 @@ class LocalTrade(): return float(f"{profit_ratio:.8f}") def recalc_trade_from_orders(self): - # We need at least 2 orders for averaging amounts and rates. - if len(self.orders) < 2: + # We need at least 2 entry orders for averaging amounts and rates. + if len(self.select_filled_orders('buy')) < 2: # Just in case, still recalc open trade value self.recalc_open_trade_value() return diff --git a/freqtrade/resolvers/strategy_resolver.py b/freqtrade/resolvers/strategy_resolver.py index 95177c000..e9fcc3496 100644 --- a/freqtrade/resolvers/strategy_resolver.py +++ b/freqtrade/resolvers/strategy_resolver.py @@ -97,7 +97,8 @@ class StrategyResolver(IResolver): ("sell_profit_offset", 0.0), ("disable_dataframe_checks", False), ("ignore_buying_expired_candle_after", 0), - ("position_adjustment_enable", False) + ("position_adjustment_enable", False), + ("max_entry_position_adjustment", -1), ] for attribute, default in attributes: StrategyResolver._override_attribute_helper(strategy, config, diff --git a/freqtrade/rpc/api_server/api_schemas.py b/freqtrade/rpc/api_server/api_schemas.py index bbd858795..96421ed32 100644 --- a/freqtrade/rpc/api_server/api_schemas.py +++ b/freqtrade/rpc/api_server/api_schemas.py @@ -173,6 +173,8 @@ class ShowConfig(BaseModel): bot_name: str state: str runmode: str + position_adjustment_enable: bool + max_entry_position_adjustment: int class TradeSchema(BaseModel): diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index 4c430dd46..2ebc3d083 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -214,7 +214,8 @@ def reload_config(rpc: RPC = Depends(get_rpc)): @router.get('/pair_candles', response_model=PairHistory, tags=['candle data']) -def pair_candles(pair: str, timeframe: str, limit: Optional[int], rpc: RPC = Depends(get_rpc)): +def pair_candles( + pair: str, timeframe: str, limit: Optional[int] = None, rpc: RPC = Depends(get_rpc)): return rpc._rpc_analysed_dataframe(pair, timeframe, limit) diff --git a/freqtrade/rpc/fiat_convert.py b/freqtrade/rpc/fiat_convert.py index ef9689d0a..f65fd2d54 100644 --- a/freqtrade/rpc/fiat_convert.py +++ b/freqtrade/rpc/fiat_convert.py @@ -77,6 +77,9 @@ class CryptoToFiatConverter: else: return None found = [x for x in self._coinlistings if x['symbol'] == crypto_symbol] + if crypto_symbol == 'eth': + found = [x for x in self._coinlistings if x['id'] == 'ethereum'] + if len(found) == 1: return found[0]['id'] diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index c78ff1079..ed41dbb01 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -136,7 +136,12 @@ class RPC: 'ask_strategy': config.get('ask_strategy', {}), 'bid_strategy': config.get('bid_strategy', {}), 'state': str(botstate), - 'runmode': config['runmode'].value + 'runmode': config['runmode'].value, + 'position_adjustment_enable': config.get('position_adjustment_enable', False), + 'max_entry_position_adjustment': ( + config.get('max_entry_position_adjustment', -1) + if config.get('max_entry_position_adjustment') != float('inf') + else -1) } return val @@ -247,8 +252,11 @@ class RPC: profit_str ] if self._config.get('position_adjustment_enable', False): - filled_buys = trade.select_filled_orders('buy') - detail_trade.append(str(len(filled_buys))) + max_buy_str = '' + if self._config.get('max_entry_position_adjustment', -1) > 0: + max_buy_str = f"/{self._config['max_entry_position_adjustment'] + 1}" + filled_buys = trade.nr_of_successful_buys + detail_trade.append(f"{filled_buys}{max_buy_str}") trades_list.append(detail_trade) profitcol = "Profit" if self._fiat_converter: diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 716694a81..0f0bd7432 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -1347,6 +1347,14 @@ class Telegram(RPCHandler): else: sl_info = f"*Stoploss:* `{val['stoploss']}`\n" + if val['position_adjustment_enable']: + pa_info = ( + f"*Position adjustment:* On\n" + f"*Max enter position adjustment:* `{val['max_entry_position_adjustment']}`\n" + ) + else: + pa_info = "*Position adjustment:* Off\n" + self._send_msg( f"*Mode:* `{'Dry-run' if val['dry_run'] else 'Live'}`\n" f"*Exchange:* `{val['exchange']}`\n" @@ -1356,6 +1364,7 @@ class Telegram(RPCHandler): f"*Ask strategy:* ```\n{json.dumps(val['ask_strategy'])}```\n" f"*Bid strategy:* ```\n{json.dumps(val['bid_strategy'])}```\n" f"{sl_info}" + f"{pa_info}" f"*Timeframe:* `{val['timeframe']}`\n" f"*Strategy:* `{val['strategy']}`\n" f"*Current state:* `{val['state']}`" diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 6f139026e..78dae6c5d 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -108,6 +108,7 @@ class IStrategy(ABC, HyperStrategyMixin): # Position adjustment is disabled by default position_adjustment_enable: bool = False + max_entry_position_adjustment: int = -1 # Number of seconds after which the candle will no longer result in a buy on expired candles ignore_buying_expired_candle_after: int = 0 diff --git a/requirements-dev.txt b/requirements-dev.txt index 59867ff3a..be6c1f091 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -10,14 +10,14 @@ mypy==0.931 pytest==6.2.5 pytest-asyncio==0.17.2 pytest-cov==3.0.0 -pytest-mock==3.6.1 +pytest-mock==3.7.0 pytest-random-order==1.0.4 isort==5.10.1 # For datetime mocking time-machine==2.6.0 # Convert jupyter notebooks to markdown documents -nbconvert==6.4.0 +nbconvert==6.4.1 # mypy types types-cachetools==4.2.9 @@ -26,4 +26,4 @@ types-requests==2.27.7 types-tabulate==0.8.5 # Extensions to datetime library -types-python-dateutil==2.8.8 \ No newline at end of file +types-python-dateutil==2.8.9 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 9101d1689..8ef2d5f7c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,15 +1,14 @@ -numpy==1.21.5; python_version <= '3.7' -numpy==1.22.1; python_version > '3.7' -pandas==1.3.5 +numpy==1.22.1 +pandas==1.4.0 pandas-ta==0.3.14b -ccxt==1.68.20 +ccxt==1.71.46 # Pin cryptography for now due to rust build errors with piwheels cryptography==36.0.1 aiohttp==3.8.1 SQLAlchemy==1.4.31 python-telegram-bot==13.10 -arrow==1.2.1 +arrow==1.2.2 cachetools==4.2.2 requests==2.27.1 urllib3==1.26.8 @@ -33,7 +32,7 @@ sdnotify==0.3.2 # API Server fastapi==0.73.0 -uvicorn==0.17.0 +uvicorn==0.17.1 pyjwt==2.3.0 aiofiles==0.8.0 psutil==5.9.0 @@ -42,6 +41,6 @@ psutil==5.9.0 colorama==0.4.4 # Building config files interactively questionary==1.10.0 -prompt-toolkit==3.0.24 +prompt-toolkit==3.0.26 # Extensions to datetime library python-dateutil==2.8.2 diff --git a/setup.cfg b/setup.cfg index c5c7f2f25..6aaec9d73 100644 --- a/setup.cfg +++ b/setup.cfg @@ -14,7 +14,6 @@ classifiers = Environment :: Console Intended Audience :: Science/Research License :: OSI Approved :: GNU General Public License v3 (GPLv3) - Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 diff --git a/setup.sh b/setup.sh index c642a654d..1df9df606 100755 --- a/setup.sh +++ b/setup.sh @@ -25,7 +25,7 @@ function check_installed_python() { exit 2 fi - for v in 9 10 8 7 + for v in 9 10 8 do PYTHON="python3.${v}" which $PYTHON @@ -219,7 +219,7 @@ function install() { install_redhat else echo "This script does not support your OS." - echo "If you have Python version 3.7 - 3.10, pip, virtualenv, ta-lib you can continue." + echo "If you have Python version 3.8 - 3.10, pip, virtualenv, ta-lib you can continue." echo "Wait 10 seconds to continue the next install steps or use ctrl+c to interrupt this shell." sleep 10 fi @@ -246,7 +246,7 @@ function help() { echo " -p,--plot Install dependencies for Plotting scripts." } -# Verify if 3.7 or 3.8 is installed +# Verify if 3.8+ is installed check_installed_python case $* in diff --git a/tests/rpc/test_fiat_convert.py b/tests/rpc/test_fiat_convert.py index 2fe5d4a56..c87cea259 100644 --- a/tests/rpc/test_fiat_convert.py +++ b/tests/rpc/test_fiat_convert.py @@ -148,10 +148,13 @@ def test_fiat_multiple_coins(mocker, caplog): {'id': 'helium', 'symbol': 'hnt', 'name': 'Helium'}, {'id': 'hymnode', 'symbol': 'hnt', 'name': 'Hymnode'}, {'id': 'bitcoin', 'symbol': 'btc', 'name': 'Bitcoin'}, + {'id': 'ethereum', 'symbol': 'eth', 'name': 'Ethereum'}, + {'id': 'ethereum-wormhole', 'symbol': 'eth', 'name': 'Ethereum Wormhole'}, ] assert fiat_convert._get_gekko_id('btc') == 'bitcoin' assert fiat_convert._get_gekko_id('hnt') is None + assert fiat_convert._get_gekko_id('eth') == 'ethereum' assert log_has('Found multiple mappings in goingekko for hnt.', caplog) diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 27c509c94..46828b325 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -221,9 +221,13 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None: assert '-0.06' == f'{fiat_profit_sum:.2f}' rpc._config['position_adjustment_enable'] = True + rpc._config['max_entry_position_adjustment'] = 3 result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') assert "# Buys" in headers assert len(result[0]) == 5 + # 4th column should be 1/4 - as 1 order filled (a total of 4 is possible) + # 3 on top of the initial one. + assert result[0][4] == '1/4' mocker.patch('freqtrade.exchange.Exchange.get_rate', MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available"))) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index d226280fe..523696759 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -4550,3 +4550,32 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: # Make sure the closed order is found as the second order. order = trade.select_order('buy', False) assert order.order_id == '652' + + +def test_process_open_trade_positions_exception(mocker, default_conf_usdt, fee, caplog) -> None: + default_conf_usdt.update({ + "position_adjustment_enable": True, + }) + freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) + + mocker.patch('freqtrade.freqtradebot.FreqtradeBot.check_and_call_adjust_trade_position', + side_effect=DependencyException()) + + create_mock_trades(fee) + + freqtrade.process_open_trade_positions() + assert log_has_re(r"Unable to adjust position of trade for .*", caplog) + + +def test_check_and_call_adjust_trade_position(mocker, default_conf_usdt, fee, caplog) -> None: + default_conf_usdt.update({ + "position_adjustment_enable": True, + "max_entry_position_adjustment": 0, + }) + freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) + + create_mock_trades(fee) + caplog.set_level(logging.DEBUG) + + freqtrade.process_open_trade_positions() + assert log_has_re(r"Max adjustment entries for .* has been reached\.", caplog)