Prevent rounding errors in value calculation

This commit is contained in:
Mark Veidemanis 2022-11-22 07:20:37 +00:00
parent 3f8fb66656
commit cb88cf33c2
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 4 additions and 2 deletions

View File

@ -1,3 +1,5 @@
from decimal import Decimal as D
from pydantic import BaseModel from pydantic import BaseModel
@ -62,9 +64,9 @@ def parse_units(x):
def parse_value(x): def parse_value(x):
if float(x["long"]["units"]) > 0: 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: 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: else:
return 0 return 0