From b0a7b64d444ddffdb47ec11166a3126f54f2d5da Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 17 Mar 2023 20:41:11 +0100 Subject: [PATCH 01/32] Close sessions after telegram calls --- freqtrade/rpc/telegram.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 0c0b24f00..fda791313 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -83,6 +83,8 @@ def authorized_only(command_handler: Callable[..., None]) -> Callable[..., Any]: self._send_msg(str(e)) except BaseException: logger.exception('Exception occurred within Telegram module') + finally: + Trade.session.remove() return wrapper From 62c8dd98d57c8d0ece15ff2a31f83a68f0f2d34d Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 17 Mar 2023 20:44:00 +0100 Subject: [PATCH 02/32] Use combination of thread-local and asyncio-aware session context --- freqtrade/persistence/models.py | 25 ++++++++++++++++++++++--- freqtrade/rpc/api_server/deps.py | 15 ++++++++++++--- tests/rpc/test_rpc_telegram.py | 6 ++++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index eee07e61c..2315c0acc 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -2,7 +2,9 @@ This module contains the class to persist trades into SQLite """ import logging -from typing import Any, Dict +import threading +from contextvars import ContextVar +from typing import Any, Dict, Final, Optional from sqlalchemy import create_engine, inspect from sqlalchemy.exc import NoSuchModuleError @@ -19,6 +21,22 @@ from freqtrade.persistence.trade_model import Order, Trade logger = logging.getLogger(__name__) +REQUEST_ID_CTX_KEY: Final[str] = 'request_id' +_request_id_ctx_var: ContextVar[Optional[str]] = ContextVar(REQUEST_ID_CTX_KEY, default=None) + + +def get_request_or_thread_id() -> Optional[str]: + """ + Helper method to get either async context (for fastapi requests), or thread id + """ + id = _request_id_ctx_var.get() + if id is None: + # when not in request context - use thread id + id = str(threading.current_thread().ident) + + return id + + _SQL_DOCS_URL = 'http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls' @@ -53,8 +71,9 @@ def init_db(db_url: str) -> None: # https://docs.sqlalchemy.org/en/13/orm/contextual.html#thread-local-scope # Scoped sessions proxy requests to the appropriate thread-local session. - # We should use the scoped_session object - not a seperately initialized version - Trade.session = scoped_session(sessionmaker(bind=engine, autoflush=False)) + # Since we also use fastAPI, we need to make it aware of the request id, too + Trade.session = scoped_session(sessionmaker( + bind=engine, autoflush=False), scopefunc=get_request_or_thread_id) Order.session = Trade.session PairLock.session = Trade.session diff --git a/freqtrade/rpc/api_server/deps.py b/freqtrade/rpc/api_server/deps.py index aed97367b..eb41d728d 100644 --- a/freqtrade/rpc/api_server/deps.py +++ b/freqtrade/rpc/api_server/deps.py @@ -1,9 +1,11 @@ from typing import Any, Dict, Iterator, Optional +from uuid import uuid4 from fastapi import Depends from freqtrade.enums import RunMode from freqtrade.persistence import Trade +from freqtrade.persistence.models import _request_id_ctx_var from freqtrade.rpc.rpc import RPC, RPCException from .webserver import ApiServer @@ -15,12 +17,19 @@ def get_rpc_optional() -> Optional[RPC]: return None -def get_rpc() -> Optional[Iterator[RPC]]: +async def get_rpc() -> Optional[Iterator[RPC]]: + _rpc = get_rpc_optional() if _rpc: + request_id = str(uuid4()) + ctx_token = _request_id_ctx_var.set(request_id) Trade.rollback() - yield _rpc - Trade.rollback() + try: + yield _rpc + finally: + Trade.session.remove() + _request_id_ctx_var.reset(ctx_token) + else: raise RPCException('Bot is not in the correct state') diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index b1859f581..521e3b66d 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -674,8 +674,9 @@ def test_monthly_handle(default_conf_usdt, update, ticker, fee, mocker, time_mac assert str('Monthly Profit over the last 6 months:') in msg_mock.call_args_list[0][0][0] -def test_profit_handle(default_conf_usdt, update, ticker_usdt, ticker_sell_up, fee, - limit_sell_order_usdt, mocker) -> None: +def test_telegram_profit_handle( + default_conf_usdt, update, ticker_usdt, ticker_sell_up, fee, + limit_sell_order_usdt, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=1.1) mocker.patch.multiple( EXMS, @@ -710,6 +711,7 @@ def test_profit_handle(default_conf_usdt, update, ticker_usdt, ticker_sell_up, f # Update the ticker with a market going up mocker.patch(f'{EXMS}.fetch_ticker', ticker_sell_up) # Simulate fulfilled LIMIT_SELL order for trade + trade = Trade.session.scalars(select(Trade)).first() oobj = Order.parse_from_ccxt_object( limit_sell_order_usdt, limit_sell_order_usdt['symbol'], 'sell') trade.orders.append(oobj) From f5f151fcc54b2255e4dc167276500fd937b54f8c Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 19 Mar 2023 15:06:56 +0100 Subject: [PATCH 03/32] Fix typing error --- freqtrade/rpc/api_server/deps.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/rpc/api_server/deps.py b/freqtrade/rpc/api_server/deps.py index eb41d728d..f5b1bcd74 100644 --- a/freqtrade/rpc/api_server/deps.py +++ b/freqtrade/rpc/api_server/deps.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Iterator, Optional +from typing import Any, AsyncIterator, Dict, Optional from uuid import uuid4 from fastapi import Depends @@ -17,7 +17,7 @@ def get_rpc_optional() -> Optional[RPC]: return None -async def get_rpc() -> Optional[Iterator[RPC]]: +async def get_rpc() -> Optional[AsyncIterator[RPC]]: _rpc = get_rpc_optional() if _rpc: From a2ce288241b8a77523b9608c1418fb306a8d95c0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 7 Jun 2022 21:05:15 +0200 Subject: [PATCH 04/32] Add okx stoploss on exchange (non-working for futures). --- freqtrade/exchange/exchange.py | 1 + freqtrade/exchange/okx.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index e5f897c2a..728e997f1 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1193,6 +1193,7 @@ class Exchange: try: params = self._get_stop_params(side=side, ordertype=ordertype, stop_price=stop_price_norm) + # TODO: reduceOnly is invalid for OKX stop orders if self.trading_mode == TradingMode.FUTURES: params['reduceOnly'] = True if 'stoploss_price_type' in order_types and 'stop_price_type_field' in self._ft_has: diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index e7d658d24..048d4cad5 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -24,6 +24,8 @@ class Okx(Exchange): "ohlcv_candle_limit": 100, # Warning, special case with data prior to X months "mark_ohlcv_timeframe": "4h", "funding_fee_timeframe": "8h", + "stoploss_order_types": {"limit": "limit"}, + "stoploss_on_exchange": True, } _ft_has_futures: Dict = { "tickers_have_quoteVolume": False, @@ -157,3 +159,26 @@ class Okx(Exchange): pair_tiers = self._leverage_tiers[pair] return pair_tiers[-1]['maxNotional'] / leverage + + def _get_stop_params(self, side: BuySell, ordertype: str, stop_price: float) -> Dict: + + params = super()._get_stop_params(side, ordertype, stop_price) + if self.trading_mode == TradingMode.FUTURES and self.margin_mode: + params['tdMode'] = self.margin_mode.value + params['posSide'] = self._get_posSide(side, True) + return params + + def fetch_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: + # TODO: This does not work until the algo-order is actually triggered! + return self.fetch_order( + order_id=order_id, + pair=pair, + params={'stop': True} + ) + + def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: + return self.cancel_order( + order_id=order_id, + pair=pair, + params={'stop': True} + ) From df20757d2116a52265edfbe4624ed545eddb9a23 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 8 Nov 2022 19:58:39 +0100 Subject: [PATCH 05/32] OKX stop: implement proper stoploss fetching --- freqtrade/exchange/okx.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 048d4cad5..8199bd0ea 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -6,7 +6,8 @@ import ccxt from freqtrade.constants import BuySell from freqtrade.enums import CandleType, MarginMode, TradingMode from freqtrade.enums.pricetype import PriceType -from freqtrade.exceptions import DDosProtection, OperationalException, TemporaryError +from freqtrade.exceptions import (DDosProtection, OperationalException, RetryableOrderError, + TemporaryError) from freqtrade.exchange import Exchange, date_minus_candles from freqtrade.exchange.common import retrier @@ -169,12 +170,23 @@ class Okx(Exchange): return params def fetch_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: - # TODO: This does not work until the algo-order is actually triggered! - return self.fetch_order( - order_id=order_id, - pair=pair, - params={'stop': True} - ) + params1 = {'stop': True, 'ordType': 'trigger'} + for method in (self._api.fetch_open_orders, self._api.fetch_closed_orders, + self._api.fetch_canceled_orders): + try: + orders = method(pair, params=params1) + orders_f = [order for order in orders if order['id'] == order_id] + if orders_f: + order = orders_f[0] + if (order['status'] == 'closed' + and order.get('info', {}).get('ordId') is not None): + # Once a order triggered, we fetch the regular followup order. + return self.fetch_order(order['info']['ordId'], pair) + return order + except ccxt.BaseError: + logger.exception() + raise RetryableOrderError( + f'StoplossOrder not found (pair: {pair} id: {order_id}).') def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: return self.cancel_order( From 6c5dc7e0a9b8643dca66e0b1df79696a3eb7cd90 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 8 Nov 2022 20:24:26 +0100 Subject: [PATCH 06/32] OKX: improve stop order handling --- freqtrade/exchange/okx.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 8199bd0ea..4ff7f283b 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -1,5 +1,5 @@ import logging -from typing import Dict, List, Optional, Tuple +from typing import Any, Dict, List, Optional, Tuple import ccxt @@ -10,6 +10,7 @@ from freqtrade.exceptions import (DDosProtection, OperationalException, Retryabl TemporaryError) from freqtrade.exchange import Exchange, date_minus_candles from freqtrade.exchange.common import retrier +from freqtrade.misc import safe_value_fallback2 logger = logging.getLogger(__name__) @@ -179,15 +180,26 @@ class Okx(Exchange): if orders_f: order = orders_f[0] if (order['status'] == 'closed' - and order.get('info', {}).get('ordId') is not None): + and (real_order_id := order.get('info', {}).get('ordId')) is not None): # Once a order triggered, we fetch the regular followup order. - return self.fetch_order(order['info']['ordId'], pair) + order_reg = self.fetch_order(real_order_id, pair) + self._log_exchange_response('fetch_stoploss_order1', order_reg) + order_reg['id_stop'] = order_reg['id'] + order_reg['id'] = order_id + order_reg['type'] = 'stop' + order_reg['status_stop'] = 'triggered' + return order_reg return order except ccxt.BaseError: logger.exception() raise RetryableOrderError( f'StoplossOrder not found (pair: {pair} id: {order_id}).') + def get_order_id_conditional(self, order: Dict[str, Any]) -> str: + if order['type'] == 'stop': + return safe_value_fallback2(order, order, 'id_stop', 'id') + return order['id'] + def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: return self.cancel_order( order_id=order_id, From d84ece7258a0082628f4459c278851fcac7374c9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 9 Nov 2022 20:17:10 +0100 Subject: [PATCH 07/32] Use conditional orders for stop orders --- freqtrade/exchange/okx.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 4ff7f283b..5acf039cb 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -164,14 +164,17 @@ class Okx(Exchange): def _get_stop_params(self, side: BuySell, ordertype: str, stop_price: float) -> Dict: - params = super()._get_stop_params(side, ordertype, stop_price) + params = self._params.copy() + # Verify if stopPrice works for your exchange! + params.update({'stopLossPrice': stop_price}) + if self.trading_mode == TradingMode.FUTURES and self.margin_mode: params['tdMode'] = self.margin_mode.value params['posSide'] = self._get_posSide(side, True) return params def fetch_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: - params1 = {'stop': True, 'ordType': 'trigger'} + params1 = {'stop': True, 'ordType': 'conditional'} for method in (self._api.fetch_open_orders, self._api.fetch_closed_orders, self._api.fetch_canceled_orders): try: @@ -204,5 +207,5 @@ class Okx(Exchange): return self.cancel_order( order_id=order_id, pair=pair, - params={'stop': True} + params={'ordType': 'conditional'} ) From 224f289ec8fc6d91ee32e2a3d90f4749d4da9ca0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 12 Mar 2023 15:19:56 +0100 Subject: [PATCH 08/32] OKX Stop: Add some more okx specific logic --- freqtrade/exchange/exchange.py | 1 - freqtrade/exchange/okx.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 728e997f1..e5f897c2a 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1193,7 +1193,6 @@ class Exchange: try: params = self._get_stop_params(side=side, ordertype=ordertype, stop_price=stop_price_norm) - # TODO: reduceOnly is invalid for OKX stop orders if self.trading_mode == TradingMode.FUTURES: params['reduceOnly'] = True if 'stoploss_price_type' in order_types and 'stop_price_type_field' in self._ft_has: diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 5acf039cb..5acfe7fcc 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -173,7 +173,30 @@ class Okx(Exchange): params['posSide'] = self._get_posSide(side, True) return params + def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool: + """ + OKX uses non-default stoploss price naming. + """ + if not self._ft_has.get('stoploss_on_exchange'): + raise OperationalException(f"stoploss is not implemented for {self.name}.") + + return ( + order.get('stopLossPrice', None) is None + or ((side == "sell" and stop_loss > float(order['stopLossPrice'])) or + (side == "buy" and stop_loss < float(order['stopLossPrice']))) + ) + def fetch_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: + if self._config['dry_run']: + return self.fetch_dry_run_order(order_id) + + try: + params1 = {'stop': True} + order_reg = self._api.fetch_order(order_id, pair, params=params1) + self._log_exchange_response('fetch_stoploss_order1', order_reg) + return order_reg + except ccxt.OrderNotFound: + pass params1 = {'stop': True, 'ordType': 'conditional'} for method in (self._api.fetch_open_orders, self._api.fetch_closed_orders, self._api.fetch_canceled_orders): @@ -192,9 +215,10 @@ class Okx(Exchange): order_reg['type'] = 'stop' order_reg['status_stop'] = 'triggered' return order_reg + order['type'] = 'stoploss' return order except ccxt.BaseError: - logger.exception() + pass raise RetryableOrderError( f'StoplossOrder not found (pair: {pair} id: {order_id}).') @@ -204,8 +228,11 @@ class Okx(Exchange): return order['id'] def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: + params1 = {'stop': True} + # 'ordType': 'conditional' + # return self.cancel_order( order_id=order_id, pair=pair, - params={'ordType': 'conditional'} + params=params1, ) From a7c7f720c0791d3ea2cdd5655626a8eab827813c Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 19 Mar 2023 20:03:34 +0100 Subject: [PATCH 09/32] Add test for okx fetch_stop --- freqtrade/exchange/okx.py | 2 +- tests/exchange/test_okx.py | 54 +++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 5acfe7fcc..7de110acf 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -212,7 +212,7 @@ class Okx(Exchange): self._log_exchange_response('fetch_stoploss_order1', order_reg) order_reg['id_stop'] = order_reg['id'] order_reg['id'] = order_id - order_reg['type'] = 'stop' + order_reg['type'] = 'stoploss' order_reg['status_stop'] = 'triggered' return order_reg order['type'] = 'stoploss' diff --git a/tests/exchange/test_okx.py b/tests/exchange/test_okx.py index fce77f4c7..30e23619b 100644 --- a/tests/exchange/test_okx.py +++ b/tests/exchange/test_okx.py @@ -2,11 +2,13 @@ from datetime import datetime, timedelta, timezone from pathlib import Path from unittest.mock import MagicMock, PropertyMock +import ccxt import pytest from freqtrade.enums import CandleType, MarginMode, TradingMode +from freqtrade.exceptions import RetryableOrderError from freqtrade.exchange.exchange import timeframe_to_minutes -from tests.conftest import get_mock_coro, get_patched_exchange, log_has +from tests.conftest import EXMS, get_mock_coro, get_patched_exchange, log_has from tests.exchange.test_exchange import ccxt_exceptionhandlers @@ -476,3 +478,53 @@ def test_load_leverage_tiers_okx(default_conf, mocker, markets, tmpdir, caplog, exchange.load_leverage_tiers() assert log_has(logmsg, caplog) + + +@pytest.mark.usefixtures("init_persistence") +def test_fetch_stoploss_order_okx(default_conf, mocker): + default_conf['dry_run'] = False + api_mock = MagicMock() + api_mock.fetch_order = MagicMock() + + exchange = get_patched_exchange(mocker, default_conf, api_mock, id='okx') + + exchange.fetch_stoploss_order('1234', 'ETH/BTC') + assert api_mock.fetch_order.call_count == 1 + assert api_mock.fetch_order.call_args_list[0][0][0] == '1234' + assert api_mock.fetch_order.call_args_list[0][0][1] == 'ETH/BTC' + assert api_mock.fetch_order.call_args_list[0][1]['params'] == {'stop': True} + + api_mock.fetch_order = MagicMock(side_effect=ccxt.OrderNotFound) + api_mock.fetch_open_orders = MagicMock(return_value=[]) + api_mock.fetch_closed_orders = MagicMock(return_value=[]) + api_mock.fetch_canceled_orders = MagicMock(creturn_value=[]) + + with pytest.raises(RetryableOrderError): + exchange.fetch_stoploss_order('1234', 'ETH/BTC') + assert api_mock.fetch_order.call_count == 1 + assert api_mock.fetch_open_orders.call_count == 1 + assert api_mock.fetch_closed_orders.call_count == 1 + assert api_mock.fetch_canceled_orders.call_count == 1 + + api_mock.fetch_order.reset_mock() + api_mock.fetch_open_orders.reset_mock() + api_mock.fetch_closed_orders.reset_mock() + api_mock.fetch_canceled_orders.reset_mock() + + api_mock.fetch_closed_orders = MagicMock(return_value=[ + { + 'id': '1234', + 'status': 'closed', + 'info': {'ordId': '123455'} + } + ]) + mocker.patch(f"{EXMS}.fetch_order", MagicMock(return_value={'id': '123455'})) + resp = exchange.fetch_stoploss_order('1234', 'ETH/BTC') + assert api_mock.fetch_order.call_count == 1 + assert api_mock.fetch_open_orders.call_count == 1 + assert api_mock.fetch_closed_orders.call_count == 1 + assert api_mock.fetch_canceled_orders.call_count == 0 + + assert resp['id'] == '1234' + assert resp['id_stop'] == '123455' + assert resp['type'] == 'stoploss' From fb0e824a83fb953b4d6d61a215df72e1cd6b74b9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:56:45 +0000 Subject: [PATCH 10/32] Bump nbconvert from 7.2.9 to 7.2.10 Bumps [nbconvert](https://github.com/jupyter/nbconvert) from 7.2.9 to 7.2.10. - [Release notes](https://github.com/jupyter/nbconvert/releases) - [Changelog](https://github.com/jupyter/nbconvert/blob/main/CHANGELOG.md) - [Commits](https://github.com/jupyter/nbconvert/compare/v7.2.9...v7.2.10) --- updated-dependencies: - dependency-name: nbconvert dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 6d076777f..0bdb6702e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -22,7 +22,7 @@ time-machine==2.9.0 httpx==0.23.3 # Convert jupyter notebooks to markdown documents -nbconvert==7.2.9 +nbconvert==7.2.10 # mypy types types-cachetools==5.3.0.4 From 5ade5777e8cbef0619c3d5b33041401b082be93a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:56:49 +0000 Subject: [PATCH 11/32] Bump filelock from 3.9.0 to 3.10.0 Bumps [filelock](https://github.com/tox-dev/py-filelock) from 3.9.0 to 3.10.0. - [Release notes](https://github.com/tox-dev/py-filelock/releases) - [Changelog](https://github.com/tox-dev/py-filelock/blob/main/docs/changelog.rst) - [Commits](https://github.com/tox-dev/py-filelock/compare/3.9.0...3.10.0) --- updated-dependencies: - dependency-name: filelock dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- requirements-hyperopt.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-hyperopt.txt b/requirements-hyperopt.txt index 904b5d661..4d86da2b6 100644 --- a/requirements-hyperopt.txt +++ b/requirements-hyperopt.txt @@ -5,5 +5,5 @@ scipy==1.10.1 scikit-learn==1.1.3 scikit-optimize==0.9.0 -filelock==3.9.0 +filelock==3.10.0 progressbar2==4.2.0 From 47e84ad106a34e3049a8a5412d2fb7155cff5a44 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:56:54 +0000 Subject: [PATCH 12/32] Bump python-rapidjson from 1.9 to 1.10 Bumps [python-rapidjson](https://github.com/python-rapidjson/python-rapidjson) from 1.9 to 1.10. - [Release notes](https://github.com/python-rapidjson/python-rapidjson/releases) - [Changelog](https://github.com/python-rapidjson/python-rapidjson/blob/master/CHANGES.rst) - [Commits](https://github.com/python-rapidjson/python-rapidjson/compare/v1.9...v1.10) --- updated-dependencies: - dependency-name: python-rapidjson dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9e17424f5..311285217 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,7 +26,7 @@ pyarrow==11.0.0; platform_machine != 'armv7l' py_find_1st==1.1.5 # Load ticker files 30% faster -python-rapidjson==1.9 +python-rapidjson==1.10 # Properly format api responses orjson==3.8.7 From a43502093dc18897ba4dfd2110cb5b556f1a3482 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:57:07 +0000 Subject: [PATCH 13/32] Bump sqlalchemy from 2.0.5.post1 to 2.0.7 Bumps [sqlalchemy](https://github.com/sqlalchemy/sqlalchemy) from 2.0.5.post1 to 2.0.7. - [Release notes](https://github.com/sqlalchemy/sqlalchemy/releases) - [Changelog](https://github.com/sqlalchemy/sqlalchemy/blob/main/CHANGES.rst) - [Commits](https://github.com/sqlalchemy/sqlalchemy/commits) --- updated-dependencies: - dependency-name: sqlalchemy dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9e17424f5..36861d029 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ pandas-ta==0.3.14b ccxt==2.9.12 cryptography==39.0.2 aiohttp==3.8.4 -SQLAlchemy==2.0.5.post1 +SQLAlchemy==2.0.7 python-telegram-bot==13.15 arrow==1.2.3 cachetools==4.2.2 From 7d1559f319ae3987fa155e6b85cd1224f11e452c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:57:13 +0000 Subject: [PATCH 14/32] Bump mkdocs-material from 9.1.2 to 9.1.3 Bumps [mkdocs-material](https://github.com/squidfunk/mkdocs-material) from 9.1.2 to 9.1.3. - [Release notes](https://github.com/squidfunk/mkdocs-material/releases) - [Changelog](https://github.com/squidfunk/mkdocs-material/blob/master/CHANGELOG) - [Commits](https://github.com/squidfunk/mkdocs-material/compare/9.1.2...9.1.3) --- updated-dependencies: - dependency-name: mkdocs-material dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- docs/requirements-docs.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/requirements-docs.txt b/docs/requirements-docs.txt index d384a7ec5..110373844 100644 --- a/docs/requirements-docs.txt +++ b/docs/requirements-docs.txt @@ -1,6 +1,6 @@ markdown==3.3.7 mkdocs==1.4.2 -mkdocs-material==9.1.2 +mkdocs-material==9.1.3 mdx_truly_sane_lists==1.3 pymdown-extensions==9.10 jinja2==3.1.2 From fc7c8cce3cbfea9c905ecc8a503011031f7839c4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:57:28 +0000 Subject: [PATCH 15/32] Bump uvicorn from 0.21.0 to 0.21.1 Bumps [uvicorn](https://github.com/encode/uvicorn) from 0.21.0 to 0.21.1. - [Release notes](https://github.com/encode/uvicorn/releases) - [Changelog](https://github.com/encode/uvicorn/blob/master/CHANGELOG.md) - [Commits](https://github.com/encode/uvicorn/compare/0.21.0...0.21.1) --- updated-dependencies: - dependency-name: uvicorn dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9e17424f5..4eb05db12 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,7 +36,7 @@ sdnotify==0.3.2 # API Server fastapi==0.94.0 pydantic==1.10.6 -uvicorn==0.21.0 +uvicorn==0.21.1 pyjwt==2.6.0 aiofiles==23.1.0 psutil==5.9.4 From 4543a1fe02a31904afca5016035f89571f5ba3da Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:57:33 +0000 Subject: [PATCH 16/32] Bump pre-commit from 3.1.1 to 3.2.0 Bumps [pre-commit](https://github.com/pre-commit/pre-commit) from 3.1.1 to 3.2.0. - [Release notes](https://github.com/pre-commit/pre-commit/releases) - [Changelog](https://github.com/pre-commit/pre-commit/blob/main/CHANGELOG.md) - [Commits](https://github.com/pre-commit/pre-commit/compare/v3.1.1...v3.2.0) --- updated-dependencies: - dependency-name: pre-commit dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 6d076777f..c0dc815e2 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -9,7 +9,7 @@ coveralls==3.3.1 ruff==0.0.255 mypy==1.1.1 -pre-commit==3.1.1 +pre-commit==3.2.0 pytest==7.2.2 pytest-asyncio==0.20.3 pytest-cov==4.0.0 From 29b9be9bd0c14199aaf0c36a59207c06a6c3c79c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:57:47 +0000 Subject: [PATCH 17/32] Bump ruff from 0.0.255 to 0.0.257 Bumps [ruff](https://github.com/charliermarsh/ruff) from 0.0.255 to 0.0.257. - [Release notes](https://github.com/charliermarsh/ruff/releases) - [Changelog](https://github.com/charliermarsh/ruff/blob/main/BREAKING_CHANGES.md) - [Commits](https://github.com/charliermarsh/ruff/compare/v0.0.255...v0.0.257) --- updated-dependencies: - dependency-name: ruff dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 6d076777f..8780a6d22 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,7 +7,7 @@ -r docs/requirements-docs.txt coveralls==3.3.1 -ruff==0.0.255 +ruff==0.0.257 mypy==1.1.1 pre-commit==3.1.1 pytest==7.2.2 From c78342b1943cf58bc2e78a94165d5281d9205cfa Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 03:58:15 +0000 Subject: [PATCH 18/32] Bump pypa/gh-action-pypi-publish from 1.7.1 to 1.8.1 Bumps [pypa/gh-action-pypi-publish](https://github.com/pypa/gh-action-pypi-publish) from 1.7.1 to 1.8.1. - [Release notes](https://github.com/pypa/gh-action-pypi-publish/releases) - [Commits](https://github.com/pypa/gh-action-pypi-publish/compare/v1.7.1...v1.8.1) --- updated-dependencies: - dependency-name: pypa/gh-action-pypi-publish dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 663cfb1be..904387fb2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -425,7 +425,7 @@ jobs: python setup.py sdist bdist_wheel - name: Publish to PyPI (Test) - uses: pypa/gh-action-pypi-publish@v1.7.1 + uses: pypa/gh-action-pypi-publish@v1.8.1 if: (github.event_name == 'release') with: user: __token__ @@ -433,7 +433,7 @@ jobs: repository_url: https://test.pypi.org/legacy/ - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@v1.7.1 + uses: pypa/gh-action-pypi-publish@v1.8.1 if: (github.event_name == 'release') with: user: __token__ From dcca51985d89d5b39e0497dd75249b2387639b2a Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 20 Mar 2023 06:27:39 +0100 Subject: [PATCH 19/32] sqlalchemy - pre-commit --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bc2e0bc0d..ca3da8e90 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,7 +18,7 @@ repos: - types-requests==2.28.11.15 - types-tabulate==0.9.0.1 - types-python-dateutil==2.8.19.10 - - SQLAlchemy==2.0.5.post1 + - SQLAlchemy==2.0.7 # stages: [push] - repo: https://github.com/pycqa/isort From 2de5a59d890e2345eb1a935554b3453ab4047416 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 20 Mar 2023 06:38:42 +0100 Subject: [PATCH 20/32] Add test for dry-run fetching --- tests/exchange/test_okx.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/exchange/test_okx.py b/tests/exchange/test_okx.py index 30e23619b..2f862adda 100644 --- a/tests/exchange/test_okx.py +++ b/tests/exchange/test_okx.py @@ -528,3 +528,19 @@ def test_fetch_stoploss_order_okx(default_conf, mocker): assert resp['id'] == '1234' assert resp['id_stop'] == '123455' assert resp['type'] == 'stoploss' + + default_conf['dry_run'] = True + exchange = get_patched_exchange(mocker, default_conf, api_mock, id='okx') + dro_mock = mocker.patch(f"{EXMS}.fetch_dry_run_order", MagicMock(return_value={'id': '123455'})) + + api_mock.fetch_order.reset_mock() + api_mock.fetch_open_orders.reset_mock() + api_mock.fetch_closed_orders.reset_mock() + api_mock.fetch_canceled_orders.reset_mock() + resp = exchange.fetch_stoploss_order('1234', 'ETH/BTC') + + assert api_mock.fetch_order.call_count == 0 + assert api_mock.fetch_open_orders.call_count == 0 + assert api_mock.fetch_closed_orders.call_count == 0 + assert api_mock.fetch_canceled_orders.call_count == 0 + assert dro_mock.call_count == 1 From 4690244673175e708ed097ca22edcc4e9e432281 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 20 Mar 2023 06:40:57 +0100 Subject: [PATCH 21/32] Enable okx stop-price types --- tests/exchange/test_exchange.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 7c48f1c9d..6e15abaf4 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -1039,9 +1039,9 @@ def test_validate_ordertypes(default_conf, mocker): ('bybit', 'last', True), ('bybit', 'mark', True), ('bybit', 'index', True), - # ('okx', 'last', True), - # ('okx', 'mark', True), - # ('okx', 'index', True), + ('okx', 'last', True), + ('okx', 'mark', True), + ('okx', 'index', True), ('gate', 'last', True), ('gate', 'mark', True), ('gate', 'index', True), From 54d8aa7782160d5cf0da4074bf90e134205885fb Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 20 Mar 2023 06:46:00 +0100 Subject: [PATCH 22/32] Test stoploss_adjust okx --- tests/exchange/test_okx.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/exchange/test_okx.py b/tests/exchange/test_okx.py index 2f862adda..3b97e03f4 100644 --- a/tests/exchange/test_okx.py +++ b/tests/exchange/test_okx.py @@ -544,3 +544,18 @@ def test_fetch_stoploss_order_okx(default_conf, mocker): assert api_mock.fetch_closed_orders.call_count == 0 assert api_mock.fetch_canceled_orders.call_count == 0 assert dro_mock.call_count == 1 + + +@pytest.mark.parametrize('sl1,sl2,sl3,side', [ + (1501, 1499, 1501, "sell"), + (1499, 1501, 1499, "buy") +]) +def test_stoploss_adjust_okx(mocker, default_conf, sl1, sl2, sl3, side): + exchange = get_patched_exchange(mocker, default_conf, id='okx') + order = { + 'type': 'stoploss', + 'price': 1500, + 'stopLossPrice': 1500, + } + assert exchange.stoploss_adjust(sl1, order, side=side) + assert not exchange.stoploss_adjust(sl2, order, side=side) From 8d649988ca4cdb1eb558545c8025bfedd6accb41 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 05:47:47 +0000 Subject: [PATCH 23/32] Bump fastapi from 0.94.0 to 0.95.0 Bumps [fastapi](https://github.com/tiangolo/fastapi) from 0.94.0 to 0.95.0. - [Release notes](https://github.com/tiangolo/fastapi/releases) - [Commits](https://github.com/tiangolo/fastapi/compare/0.94.0...0.95.0) --- updated-dependencies: - dependency-name: fastapi dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 664d1752e..201c46106 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,7 +34,7 @@ orjson==3.8.7 sdnotify==0.3.2 # API Server -fastapi==0.94.0 +fastapi==0.95.0 pydantic==1.10.6 uvicorn==0.21.1 pyjwt==2.6.0 From 3175121030b477514eb603a7ec2ebea5e5673e34 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 05:47:55 +0000 Subject: [PATCH 24/32] Bump ast-comments from 1.0.0 to 1.0.1 Bumps [ast-comments](https://github.com/t3rn0/ast-comments) from 1.0.0 to 1.0.1. - [Release notes](https://github.com/t3rn0/ast-comments/releases) - [Commits](https://github.com/t3rn0/ast-comments/compare/1.0.0...1.0.1) --- updated-dependencies: - dependency-name: ast-comments dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 664d1752e..4251c879a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -56,4 +56,4 @@ schedule==1.1.0 websockets==10.4 janus==1.0.0 -ast-comments==1.0.0 +ast-comments==1.0.1 From cb1f971d4bb1bd9559529b6c5734acfc55a773a6 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 06:39:13 +0000 Subject: [PATCH 25/32] Bump ccxt from 2.9.12 to 3.0.23 Bumps [ccxt](https://github.com/ccxt/ccxt) from 2.9.12 to 3.0.23. - [Release notes](https://github.com/ccxt/ccxt/releases) - [Changelog](https://github.com/ccxt/ccxt/blob/master/CHANGELOG.md) - [Commits](https://github.com/ccxt/ccxt/compare/2.9.12...3.0.23) --- updated-dependencies: - dependency-name: ccxt dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index de38d50c3..6f16c71d0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ numpy==1.24.2 pandas==1.5.3 pandas-ta==0.3.14b -ccxt==2.9.12 +ccxt==3.0.23 cryptography==39.0.2 aiohttp==3.8.4 SQLAlchemy==2.0.7 From a4e4310d400b73c15236ebaab36dfa7876dc9330 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 07:11:18 +0000 Subject: [PATCH 26/32] Bump pytest-asyncio from 0.20.3 to 0.21.0 Bumps [pytest-asyncio](https://github.com/pytest-dev/pytest-asyncio) from 0.20.3 to 0.21.0. - [Release notes](https://github.com/pytest-dev/pytest-asyncio/releases) - [Commits](https://github.com/pytest-dev/pytest-asyncio/compare/v0.20.3...v0.21.0) --- updated-dependencies: - dependency-name: pytest-asyncio dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 25e26f47c..8312e2820 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,7 +11,7 @@ ruff==0.0.257 mypy==1.1.1 pre-commit==3.2.0 pytest==7.2.2 -pytest-asyncio==0.20.3 +pytest-asyncio==0.21.0 pytest-cov==4.0.0 pytest-mock==3.10.0 pytest-random-order==1.1.0 From 4f4bfdac4d2491277decca93cdd30aec9ec29a1a Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 20 Mar 2023 09:00:00 +0100 Subject: [PATCH 27/32] Adjustments to okx stoploss --- freqtrade/exchange/okx.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 7de110acf..3110d8189 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -32,7 +32,7 @@ class Okx(Exchange): _ft_has_futures: Dict = { "tickers_have_quoteVolume": False, "fee_cost_in_contracts": True, - "stop_price_type_field": "tpTriggerPxType", + "stop_price_type_field": "slTriggerPxType", "stop_price_type_value_mapping": { PriceType.LAST: "last", PriceType.MARK: "index", @@ -193,7 +193,7 @@ class Okx(Exchange): try: params1 = {'stop': True} order_reg = self._api.fetch_order(order_id, pair, params=params1) - self._log_exchange_response('fetch_stoploss_order1', order_reg) + self._log_exchange_response('fetch_stoploss_order', order_reg) return order_reg except ccxt.OrderNotFound: pass From 639987cbabc8ae969f280639ffa856af99402064 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 20 Mar 2023 18:19:17 +0100 Subject: [PATCH 28/32] Prevent parameter reuse --- freqtrade/exchange/okx.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 3110d8189..162630ea5 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -197,11 +197,11 @@ class Okx(Exchange): return order_reg except ccxt.OrderNotFound: pass - params1 = {'stop': True, 'ordType': 'conditional'} + params2 = {'stop': True, 'ordType': 'conditional'} for method in (self._api.fetch_open_orders, self._api.fetch_closed_orders, self._api.fetch_canceled_orders): try: - orders = method(pair, params=params1) + orders = method(pair, params=params2) orders_f = [order for order in orders if order['id'] == order_id] if orders_f: order = orders_f[0] From 97c420b2df8c2fe49c5c14b264836b1af58111f7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 20 Mar 2023 19:27:48 +0100 Subject: [PATCH 29/32] Add explicit test for okx lev_prep --- freqtrade/exchange/okx.py | 1 - tests/exchange/test_okx.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 162630ea5..1b9134be3 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -128,7 +128,6 @@ class Okx(Exchange): def _lev_prep(self, pair: str, leverage: float, side: BuySell): if self.trading_mode != TradingMode.SPOT and self.margin_mode is not None: try: - # TODO-lev: Test me properly (check mgnMode passed) res = self._api.set_leverage( leverage=leverage, symbol=pair, diff --git a/tests/exchange/test_okx.py b/tests/exchange/test_okx.py index 3b97e03f4..7a3fa22f0 100644 --- a/tests/exchange/test_okx.py +++ b/tests/exchange/test_okx.py @@ -480,6 +480,38 @@ def test_load_leverage_tiers_okx(default_conf, mocker, markets, tmpdir, caplog, assert log_has(logmsg, caplog) +def test__set_leverage_okx(mocker, default_conf): + + api_mock = MagicMock() + api_mock.set_leverage = MagicMock() + type(api_mock).has = PropertyMock(return_value={'setLeverage': True}) + default_conf['dry_run'] = False + default_conf['trading_mode'] = TradingMode.FUTURES + default_conf['margin_mode'] = MarginMode.ISOLATED + + exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx") + exchange._lev_prep('BTC/USDT:USDT', 3.2, 'buy') + assert api_mock.set_leverage.call_count == 1 + # Leverage is rounded to 3. + assert api_mock.set_leverage.call_args_list[0][1]['leverage'] == 3.2 + assert api_mock.set_leverage.call_args_list[0][1]['symbol'] == 'BTC/USDT:USDT' + assert api_mock.set_leverage.call_args_list[0][1]['params'] == { + 'mgnMode': 'isolated', + 'posSide': 'net'} + + ccxt_exceptionhandlers( + mocker, + default_conf, + api_mock, + "okx", + "_lev_prep", + "set_leverage", + pair="XRP/USDT:USDT", + leverage=5.0, + side='buy' + ) + + @pytest.mark.usefixtures("init_persistence") def test_fetch_stoploss_order_okx(default_conf, mocker): default_conf['dry_run'] = False From 36c45fd14f8daa25240b585757af38cfc3663564 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 21 Mar 2023 19:14:09 +0100 Subject: [PATCH 30/32] Remove unused argument from set_leverage --- freqtrade/exchange/exchange.py | 1 - freqtrade/exchange/kraken.py | 1 - tests/exchange/test_binance.py | 1 - tests/exchange/test_exchange.py | 23 ----------------------- 4 files changed, 26 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index e5f897c2a..f620b5bc1 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -2527,7 +2527,6 @@ class Exchange: self, leverage: float, pair: Optional[str] = None, - trading_mode: Optional[TradingMode] = None, accept_fail: bool = False, ): """ diff --git a/freqtrade/exchange/kraken.py b/freqtrade/exchange/kraken.py index 8a4f7f7e0..b1a19fa69 100644 --- a/freqtrade/exchange/kraken.py +++ b/freqtrade/exchange/kraken.py @@ -158,7 +158,6 @@ class Kraken(Exchange): self, leverage: float, pair: Optional[str] = None, - trading_mode: Optional[TradingMode] = None, accept_fail: bool = False, ): """ diff --git a/tests/exchange/test_binance.py b/tests/exchange/test_binance.py index ba786bb3b..8ada089bd 100644 --- a/tests/exchange/test_binance.py +++ b/tests/exchange/test_binance.py @@ -555,7 +555,6 @@ def test__set_leverage_binance(mocker, default_conf): "set_leverage", pair="XRP/USDT", leverage=5.0, - trading_mode=TradingMode.FUTURES ) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 6e15abaf4..586f023b4 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -3868,29 +3868,6 @@ def test_get_stake_amount_considering_leverage( stake_amount, leverage) == min_stake_with_lev -@pytest.mark.parametrize("exchange_name,trading_mode", [ - ("binance", TradingMode.FUTURES), -]) -def test__set_leverage(mocker, default_conf, exchange_name, trading_mode): - - api_mock = MagicMock() - api_mock.set_leverage = MagicMock() - type(api_mock).has = PropertyMock(return_value={'setLeverage': True}) - default_conf['dry_run'] = False - - ccxt_exceptionhandlers( - mocker, - default_conf, - api_mock, - exchange_name, - "_set_leverage", - "set_leverage", - pair="XRP/USDT", - leverage=5.0, - trading_mode=trading_mode - ) - - @pytest.mark.parametrize("margin_mode", [ (MarginMode.CROSS), (MarginMode.ISOLATED) From ebebcb886c0a3f6ac98460fbf073052c96a9f25e Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 21 Mar 2023 19:28:26 +0100 Subject: [PATCH 31/32] Move build-system to the top of pyproject.toml --- pyproject.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c3ca9e1b0..baf707c68 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,7 @@ +[build-system] +requires = ["setuptools >= 46.4.0", "wheel"] +build-backend = "setuptools.build_meta" + [tool.black] line-length = 100 exclude = ''' @@ -48,10 +52,6 @@ ignore_errors = true module = "telegram.*" implicit_optional = true -[build-system] -requires = ["setuptools >= 46.4.0", "wheel"] -build-backend = "setuptools.build_meta" - [tool.pyright] include = ["freqtrade"] exclude = [ From 8cf3e9f91b50fd7d615822e87a51aa997548dd59 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 21 Mar 2023 19:29:27 +0100 Subject: [PATCH 32/32] Accept "insufficient funds" error on set_leverage from stop calls closes #8341 --- freqtrade/exchange/bybit.py | 2 +- freqtrade/exchange/exchange.py | 10 +++++----- freqtrade/exchange/okx.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/freqtrade/exchange/bybit.py b/freqtrade/exchange/bybit.py index 6f841b608..a4b070741 100644 --- a/freqtrade/exchange/bybit.py +++ b/freqtrade/exchange/bybit.py @@ -114,7 +114,7 @@ class Bybit(Exchange): data = [[x['timestamp'], x['fundingRate'], 0, 0, 0, 0] for x in data] return data - def _lev_prep(self, pair: str, leverage: float, side: BuySell): + def _lev_prep(self, pair: str, leverage: float, side: BuySell, accept_fail: bool = False): if self.trading_mode != TradingMode.SPOT: params = {'leverage': leverage} self.set_margin_mode(pair, self.margin_mode, accept_fail=True, params=params) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index f620b5bc1..99551b054 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1018,10 +1018,10 @@ class Exchange: # Order handling - def _lev_prep(self, pair: str, leverage: float, side: BuySell): + def _lev_prep(self, pair: str, leverage: float, side: BuySell, accept_fail: bool = False): if self.trading_mode != TradingMode.SPOT: - self.set_margin_mode(pair, self.margin_mode) - self._set_leverage(leverage, pair) + self.set_margin_mode(pair, self.margin_mode, accept_fail) + self._set_leverage(leverage, pair, accept_fail) def _get_params( self, @@ -1202,7 +1202,7 @@ class Exchange: amount = self.amount_to_precision(pair, self._amount_to_contracts(pair, amount)) - self._lev_prep(pair, leverage, side) + self._lev_prep(pair, leverage, side, accept_fail=True) order = self._api.create_order(symbol=pair, type=ordertype, side=side, amount=amount, price=limit_rate, params=params) self._log_exchange_response('create_stoploss_order', order) @@ -2544,7 +2544,7 @@ class Exchange: self._log_exchange_response('set_leverage', res) except ccxt.DDoSProtection as e: raise DDosProtection(e) from e - except ccxt.BadRequest as e: + except (ccxt.BadRequest, ccxt.InsufficientFunds) as e: if not accept_fail: raise TemporaryError( f'Could not set leverage due to {e.__class__.__name__}. Message: {e}') from e diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 1b9134be3..a4fcaeca0 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -125,7 +125,7 @@ class Okx(Exchange): return params @retrier - def _lev_prep(self, pair: str, leverage: float, side: BuySell): + def _lev_prep(self, pair: str, leverage: float, side: BuySell, accept_fail: bool = False): if self.trading_mode != TradingMode.SPOT and self.margin_mode is not None: try: res = self._api.set_leverage(