Prevent rounding errors in value calculation

This commit is contained in:
2022-11-22 07:20:37 +00:00
parent 3f8fb66656
commit cb88cf33c2

View File

@@ -1,3 +1,5 @@
from decimal import Decimal as D
from pydantic import BaseModel
@@ -62,9 +64,9 @@ def parse_units(x):
def parse_value(x):
if float(x["long"]["units"]) > 0:
return float(x["long"]["units"]) * float(x["long"]["averagePrice"])
return D(x["long"]["units"]) * D(x["long"]["averagePrice"])
elif float(x["short"]["units"]) > 0:
return float(x["short"]["units"]) * float(x["short"]["averagePrice"])
return D(x["short"]["units"]) * D(x["short"]["averagePrice"])
else:
return 0