Skip to content

Commit

Permalink
use a usual attribute instead of caching a function return value
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger committed Jan 9, 2025
1 parent a7f79ef commit c1e7e16
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 30 deletions.
12 changes: 4 additions & 8 deletions src/kraken_infinity_grid/gridbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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
Expand Down Expand Up @@ -137,7 +136,7 @@ def __init__(
) -> None:
super().__init__(key=key, secret=secret)
LOG.info("Initiate the Kraken Infinity Grid Algorithm instance...")
LOG.info("Config: %s", config)
LOG.debug("Config: %s", config)
self.init_done: bool = False
self.dry_run: bool = dry_run

Expand All @@ -151,6 +150,9 @@ def __init__(
##
self.interval: float = float(config["interval"])
self.amount_per_grid: float = float(config["amount_per_grid"])

self.amount_per_grid_plus_fee: float | None = None

self.max_investment: float = config["max_investment"]
self.n_open_buy_orders: int = config["n_open_buy_orders"]
self.fee: float | None = None
Expand Down Expand Up @@ -570,12 +572,6 @@ 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."""
Expand Down
6 changes: 5 additions & 1 deletion src/kraken_infinity_grid/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,15 @@ def __check_asset_pair_parameter(self: Self) -> None:

self.__s.xsymbol = next(iter(pair_data.keys()))
data = pair_data[self.__s.xsymbol]
self.__s.fee = float(data["fees_maker"][0][1]) / 100

self.__s.altname = data["altname"]
self.__s.zbase_currency = data["base"] # XXBT
self.__s.xquote_currency = data["quote"] # ZEUR
self.__s.cost_decimals = data["cost_decimals"] # 5, i.e., 0.00001 EUR
self.__s.fee = float(data["fees_maker"][0][1]) / 100
self.__s.amount_per_grid_plus_fee = self.__s.amount_per_grid * (
1 + self.__s.fee
)

def __check_configuration_changes(self: Self) -> None:
"""
Expand Down
22 changes: 1 addition & 21 deletions tests/test_gridbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def test_max_investment_reached(instance: KrakenInfinityGridBot) -> None:
instance.amount_per_grid = 1000.0
instance.fee = 0.01
instance.max_investment = 20000.0
instance.amount_per_grid_plus_fee = instance.amount_per_grid * (1 + instance.fee)

# Case where max investment is not reached
instance.orderbook.get_orders.return_value = [
Expand All @@ -223,27 +224,6 @@ 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
##
Expand Down
2 changes: 2 additions & 0 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def test_check_asset_pair_parameter(
},
}
strategy.symbol = "BTC/USD"
strategy.amount_per_grid = 100

setup_manager._SetupManager__check_asset_pair_parameter()

Expand All @@ -253,6 +254,7 @@ def test_check_asset_pair_parameter(
assert strategy.zbase_currency == "XXBT"
assert strategy.xquote_currency == "ZEUR"
assert strategy.cost_decimals == 5
assert strategy.amount_per_grid_plus_fee == pytest.approx(100.26)


def test_check_configuration_changes(
Expand Down

0 comments on commit c1e7e16

Please sign in to comment.