Skip to content

Commit

Permalink
Do some code optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger committed Jan 9, 2025
1 parent 843c731 commit a7f79ef
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
16 changes: 10 additions & 6 deletions src/kraken_infinity_grid/gridbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
17 changes: 4 additions & 13 deletions src/kraken_infinity_grid/order_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_gridbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
##
Expand Down
1 change: 1 addition & 0 deletions tests/test_order_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit a7f79ef

Please sign in to comment.