From 2448ef072dcd5e69a77e963c6c8bc8bc931e718d Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Wed, 18 Sep 2019 12:02:04 +0800 Subject: [PATCH] [Mod] use decimal.Decimal to ensure round_to precision, close #2002 --- vnpy/trader/utility.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/vnpy/trader/utility.py b/vnpy/trader/utility.py index 2c7f4b50..38a6e7db 100644 --- a/vnpy/trader/utility.py +++ b/vnpy/trader/utility.py @@ -5,6 +5,7 @@ General utility functions. import json from pathlib import Path from typing import Callable +from decimal import Decimal import numpy as np import talib @@ -109,11 +110,13 @@ def save_json(filename: str, data: dict): ) -def round_to(value: float, target: float): +def round_to(value: float, target: float) -> float: """ Round price to price tick value. """ - rounded = int(round(value / target)) * target + value = Decimal(str(value)) + target = Decimal(str(target)) + rounded = float(int(round(value / target)) * target) return rounded