Skip to content

Commit

Permalink
Merge pull request #17 from adocquin/13-some-pairs-only-accept-digit
Browse files Browse the repository at this point in the history
Fixed limit price decimals with limit factor
  • Loading branch information
adocquin authored Mar 28, 2022
2 parents 7df3b0e + 2552c42 commit 74e80c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 11 additions & 5 deletions krakendca/dca.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def handle_dca_logic(self) -> None:
self.pair.name)
print(f"Current {self.pair.name} ask price: {pair_ask_price}.")
# Get limit price based on limit_factor
limit_price = self.get_limit_price(pair_ask_price)
limit_price = self.get_limit_price(pair_ask_price,
self.pair.pair_decimals)
# Reject DCA if limit_price greater than max_price
if self.max_price != -1 and limit_price > self.max_price:
print(f"No DCA for {self.pair.name}: Limit price ({limit_price}) "
Expand All @@ -105,19 +106,24 @@ def handle_dca_logic(self) -> None:
order.save_order_csv(self.orders_filepath)
print("Order information saved to CSV.")

def get_limit_price(self, pair_ask_price: float) -> float:
def get_limit_price(self,
pair_ask_price: float,
pair_decimals: int) -> float:
"""
Calculates wanted limit price from current ask price and limit_factor.
:param pair_ask_price: Pair ask price to adjust li;it price from.
:param pair_ask_price: Pair ask price to adjust limit price from.
:param pair_decimals: Pair maximum number of decimals for price.
:return: The limit price
"""
if round(self.limit_factor, 5) == 1.0:
limit_price = pair_ask_price
else:
limit_price = round(pair_ask_price * self.limit_factor, 2)
limit_price = round(pair_ask_price * self.limit_factor,
pair_decimals)
print(
f"Factor adjusted limit price ({self.limit_factor:.4f}): {limit_price}."
f"Factor adjusted limit price ({self.limit_factor:.4f})"
f": {limit_price}."
)
return limit_price

Expand Down
6 changes: 4 additions & 2 deletions tests/test_dca.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ def test_send_buy_limit_order(self, capfd):
@vcr.use_cassette("tests/fixtures/vcr_cassettes/test_limit_factor.yaml")
def test_limit_factor(self):
self.dca.limit_factor = 0.9
assert self.dca.get_limit_price(3896.01) == 3506.41
assert self.dca.get_limit_price(3896.01, 2) == 3506.41
self.dca.limit_factor = 0.999999
assert self.dca.get_limit_price(3896.01) == 3896.01
assert self.dca.get_limit_price(3896.01, 1) == 3896.01
self.dca.limit_factor = 0.98
assert self.dca.get_limit_price(3896.01, 1) == 3818.1

0 comments on commit 74e80c0

Please sign in to comment.