Skip to content

Commit

Permalink
Reduce number of calculating the quote value of new positions (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger authored Jan 9, 2025
1 parent 843c731 commit 5aaaedd
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
14 changes: 7 additions & 7 deletions src/kraken_infinity_grid/gridbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,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 @@ -150,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 @@ -572,9 +575,6 @@ def investment(self: Self) -> float:
@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
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
1 change: 1 addition & 0 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 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
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 5aaaedd

Please sign in to comment.