From a7f79efbe5dbcfe836a1d820905b0f0bd1e800a3 Mon Sep 17 00:00:00 2001 From: Benjamin Thomas Schwertfeger Date: Thu, 9 Jan 2025 06:59:15 +0100 Subject: [PATCH] Do some code optimizations --- src/kraken_infinity_grid/gridbot.py | 16 +++++++++------ src/kraken_infinity_grid/order_management.py | 17 ++++------------ tests/test_gridbot.py | 21 ++++++++++++++++++++ tests/test_order_management.py | 1 + 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/kraken_infinity_grid/gridbot.py b/src/kraken_infinity_grid/gridbot.py index 0eb2744..6ca7596 100644 --- a/src/kraken_infinity_grid/gridbot.py +++ b/src/kraken_infinity_grid/gridbot.py @@ -11,6 +11,7 @@ import traceback from datetime import datetime, timedelta from decimal import Decimal +from functools import cache from logging import getLogger from time import sleep from types import SimpleNamespace @@ -569,12 +570,15 @@ def investment(self: Self) -> float: orders=self.orderbook.get_orders(), ) + @property + @cache # noqa: B019 + def amount_per_grid_plus_fee(self: Self) -> float: + """Returns the estimated quote volume of a newly placed buy order.""" + return self.amount_per_grid * (1 + self.fee) + @property def max_investment_reached(self: Self) -> bool: """Returns True if the maximum investment is reached.""" - # TODO: put this as class variable - new_position_value = self.amount_per_grid + self.amount_per_grid * self.fee - - return (self.max_investment <= self.investment + new_position_value) or ( - self.max_investment <= self.investment - ) + return ( + self.max_investment <= self.investment + self.amount_per_grid_plus_fee + ) or (self.max_investment <= self.investment) diff --git a/src/kraken_infinity_grid/order_management.py b/src/kraken_infinity_grid/order_management.py index 1b97157..9133d26 100644 --- a/src/kraken_infinity_grid/order_management.py +++ b/src/kraken_infinity_grid/order_management.py @@ -160,10 +160,7 @@ def __check_n_open_buy_orders(self: OrderManager) -> None: ): fetched_balances: dict[str, float] = self.__s.get_balances() - if ( - fetched_balances["quote_available"] - > self.__s.amount_per_grid + self.__s.amount_per_grid * self.__s.fee - ): + if fetched_balances["quote_available"] > self.__s.amount_per_grid_plus_fee: order_price: float = self.__s.get_order_price( side="buy", last_price=( @@ -236,7 +233,7 @@ def __check_extra_sell_order(self: OrderManager) -> None: if ( fetched_balances["base_available"] * self.__s.ticker.last - > self.__s.amount_per_grid + self.__s.amount_per_grid * self.__s.fee + > self.__s.amount_per_grid_plus_fee ): order_price = self.__s.get_order_price( side="sell", @@ -367,14 +364,9 @@ def new_buy_order( ), ) - # Compute the quote volume for the upcoming buy order. - new_position_value = ( - self.__s.amount_per_grid + self.__s.amount_per_grid * self.__s.fee - ) - # ====================================================================== # Check if there is enough quote balance available to place a buy order. - if current_balances["quote_available"] > new_position_value: + if current_balances["quote_available"] > self.__s.amount_per_grid_plus_fee: LOG.info( "Placing order to buy %s %s @ %s %s.", volume, @@ -460,8 +452,7 @@ def new_sell_order( # noqa: C901 corresponding_buy_order, ) sleep(1) - self.__s.om.handle_arbitrage( - side="sell", + self.__s.om.new_sell_order( order_price=order_price, txid_id_to_delete=txid_id_to_delete, ) diff --git a/tests/test_gridbot.py b/tests/test_gridbot.py index 28dad36..b610755 100644 --- a/tests/test_gridbot.py +++ b/tests/test_gridbot.py @@ -223,6 +223,27 @@ def test_max_investment_reached(instance: KrakenInfinityGridBot) -> None: assert instance.max_investment_reached +def test_amount_per_grid_plus_fee(instance: KrakenInfinityGridBot) -> None: + """Test the amount_per_grid_plus_fee property.""" + instance.amount_per_grid = 100 + instance.fee = 0.01 + assert instance.amount_per_grid_plus_fee == 101.0 + + # Ensure that caching works, i.e., the property is not recomputed + instance.amount_per_grid = 1000 + assert instance.amount_per_grid_plus_fee == 101.0 + + type(instance).amount_per_grid_plus_fee.fget.cache_clear() + instance.amount_per_grid = 200 + instance.fee = 0.02 + assert instance.amount_per_grid_plus_fee == 204.0 + + type(instance).amount_per_grid_plus_fee.fget.cache_clear() + instance.amount_per_grid = 200 + instance.fee = 0.00 + assert instance.amount_per_grid_plus_fee == 200.0 + + # ============================================================================== # on_message ## diff --git a/tests/test_order_management.py b/tests/test_order_management.py index 5e35e72..b29a3b0 100644 --- a/tests/test_order_management.py +++ b/tests/test_order_management.py @@ -49,6 +49,7 @@ def strategy() -> mock.Mock: strategy.ticker = mock.Mock() strategy.ticker.last = 50000.0 strategy.save_exit = sys.exit + strategy.amount_per_grid_plus_fee = strategy.amount_per_grid * (1 + strategy.fee) return strategy