From cb88cf33c258e491480c57e82f8db0b69da8357e Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 22 Nov 2022 07:20:37 +0000 Subject: [PATCH] Prevent rounding errors in value calculation --- core/lib/schemas/oanda_s.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/lib/schemas/oanda_s.py b/core/lib/schemas/oanda_s.py index 3b6137b..a36827a 100644 --- a/core/lib/schemas/oanda_s.py +++ b/core/lib/schemas/oanda_s.py @@ -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