Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds method to make order values match the fractional actuality #512

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions dexbot/strategies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,37 @@ def get_own_spread(self):
actual_spread = lowest_own_sell_price / highest_own_buy_price - 1
return actual_spread

def fix_order_price(price=0, quote_amount=0):
"""
Returns a price and amounts that will match what will actually happen on-chain, and
let you continue using float values in the logic.
In case the amounts get really small, some returned value might be zero. If so,
it is an indication that the funds available aren't sufficient, or they are spread into too many orders.
Amounts are rounded to closest possible decimal. Thus the final price might be lower or higher than intended.

:param float | price: the naively calculated price that is to be corrected
:param float | quote_amount: the naively calculated amount
:return dict: values that will result in correctly calculated orders
"""
quote_amount_float = quote_amount
base_amount_float = quote_amount * price
quote_precision = (10 ** self.market['quote']['precision'])
base_precision = (10 ** self.market['base']['precision'])
quote_amount_internal = round(quote_amount_float * quote_precision)
base_amount_internal = round(base_amount_float * (base_precision))
target_price_internal = price * (base_precision / quote_precision)
if base_amount_internal < quote_amount_internal:
quote_amount_internal = round(base_amount_internal / target_price_internal)
else:
base_amount_internal = round(quote_amount_internal * target_price_internal)
quote_amount_float = quote_amount_internal / quote_precision
base_amount_float = base_amount_internal / base_precision
final_price = float(base_amount_float / quote_amount_float)

return {'price': final_price,
'quote_amount': quote_amount_float,
'base_amount': base_amount_float}

def get_updated_order(self, order_id):
""" Tries to get the updated order from the API. Returns None if the order doesn't exist

Expand Down