]> Piment Noir Git Repositories - freqai-strategies.git/commitdiff
perf(qav3): add integer fastpath to round_to_step()
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 31 Aug 2025 11:15:31 +0000 (13:15 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 31 Aug 2025 11:15:31 +0000 (13:15 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
quickadapter/user_data/strategies/Utils.py

index b8e64c073e4ac45c8420070e48b2d1eda3aef9a0..840b323d90b948a0570ca2565ab3912823f2bffe 100644 (file)
@@ -1015,10 +1015,18 @@ def round_to_step(value: float | int, step: int) -> int:
     """
     if not isinstance(value, (int, float)):
         raise ValueError("value must be an integer or float")
-    if not np.isfinite(value):
-        raise ValueError("value must be finite")
     if not isinstance(step, int) or step <= 0:
         raise ValueError("step must be a positive integer")
+    if isinstance(value, (int, np.integer)):
+        q, r = divmod(value, step)
+        twice_r = r * 2
+        if twice_r < step:
+            return q * step
+        if twice_r > step:
+            return (q + 1) * step
+        return int(round(value / step) * step)
+    if not np.isfinite(value):
+        raise ValueError("value must be finite")
     return int(round(float(value) / step) * step)