Skip to content

Commit 1189b59

Browse files
Merge pull request #203 from InjectiveLabs/fix/margin_calculation_floating_point_error
fix/margin calculation floating point error
2 parents c3df74a + 80f11c4 commit 1189b59

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ Note that the [sync client](https://github.com/InjectiveLabs/sdk-python/blob/mas
7878

7979

8080
### Changelogs
81+
**0.6.2.7**
82+
* Fix margin calculation in utils
83+
8184
**0.6.2.1**
8285
* Remove version deps from Pipfile
8386

pyinjective/utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from decimal import Decimal
22
from math import floor
3+
from typing import Union
34

45
"""
56
One thing you may need to pay more attention to is how to deal with decimals on the Injective Exchange.
@@ -56,9 +57,13 @@ def binary_options_quantity_to_backend(quantity, denom) -> int:
5657
return int(exchange_quantity)
5758

5859
def derivative_margin_to_backend(price, quantity, leverage, denom) -> int:
59-
price_tick_size = Decimal(denom.min_price_tick_size) / pow(10, denom.quote)
60-
margin = (price * quantity) / leverage
61-
exchange_margin = floor_to(margin, float(price_tick_size)) * pow(10, 18 + denom.quote)
60+
chain_format_price = Decimal(str(price)) * Decimal(f"1e{denom.quote}")
61+
chain_format_quantity = Decimal(str(quantity)) * Decimal(f"1e{denom.base}")
62+
decimal_leverage = Decimal(str(leverage))
63+
margin = (chain_format_price * chain_format_quantity) / decimal_leverage
64+
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
65+
# in the chain (it might be changed to a min_notional in the future)
66+
exchange_margin = floor_to(margin, denom.min_quantity_tick_size) * Decimal(f"1e18")
6267
return int(exchange_margin)
6368

6469
def binary_options_buy_margin_to_backend(price, quantity, denom) -> int:
@@ -82,7 +87,7 @@ def amount_to_backend(amount, decimals) -> int:
8287
be_amount = amount * pow(10, decimals)
8388
return int(be_amount)
8489

85-
def floor_to(value: float, target: float) -> Decimal:
90+
def floor_to(value: Union[float, Decimal], target: Union[float, Decimal]) -> Decimal:
8691
value_tmp = Decimal(str(value))
8792
target_tmp = Decimal(str(target))
8893
result = int(floor(value_tmp / target_tmp)) * target_tmp

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
AUTHOR = "Injective Labs"
1919
REQUIRES_PYTHON = ">=3.7.0"
20-
VERSION = "0.6.2.6"
20+
VERSION = "0.6.2.7"
2121

2222
REQUIRED = [
2323
"protobuf",

0 commit comments

Comments
 (0)