[Mod] use decimal.Decimal to ensure round_to precision, close #2002

This commit is contained in:
vn.py 2019-09-18 12:02:04 +08:00
parent c02b37bde2
commit 2448ef072d

View File

@ -5,6 +5,7 @@ General utility functions.
import json import json
from pathlib import Path from pathlib import Path
from typing import Callable from typing import Callable
from decimal import Decimal
import numpy as np import numpy as np
import talib 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. 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 return rounded