mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-29 18:07:38 +08:00
MAINT: Ensure the sign of the result is positive
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import math
|
||||
|
||||
from nose_parameterized import parameterized
|
||||
from unittest import TestCase
|
||||
|
||||
@@ -16,6 +18,8 @@ class BlotterTestCase(TestCase):
|
||||
def test_round_for_minimum_price_variation_buy(self, price, expected):
|
||||
result = round_for_minimum_price_variation(price, is_buy=True)
|
||||
self.assertEqual(result, expected)
|
||||
self.assertEqual(math.copysign(1.0, result),
|
||||
math.copysign(1.0, expected))
|
||||
|
||||
@parameterized.expand([(0.00, 0.00),
|
||||
(0.01, 0.01),
|
||||
@@ -27,3 +31,5 @@ class BlotterTestCase(TestCase):
|
||||
def test_round_for_minimum_price_variation_sell(self, price, expected):
|
||||
result = round_for_minimum_price_variation(price, is_buy=False)
|
||||
self.assertEqual(result, expected)
|
||||
self.assertEqual(math.copysign(1.0, result),
|
||||
math.copysign(1.0, expected))
|
||||
|
||||
@@ -47,7 +47,10 @@ ORDER_STATUS = Enum(
|
||||
# buy: [.0095, .0195) -> round to .01, sell: (.0005, .0105] -> round to .01
|
||||
def round_for_minimum_price_variation(x, is_buy, diff=(0.0095 - .005)):
|
||||
# relies on rounding half away from zero, unlike numpy's bankers' rounding
|
||||
return round(x - (diff if is_buy else -diff), 2)
|
||||
rounded = round(x - (diff if is_buy else -diff), 2)
|
||||
if zp_math.tolerant_equals(rounded, 0.0):
|
||||
return 0.0
|
||||
return rounded
|
||||
|
||||
|
||||
class Blotter(object):
|
||||
|
||||
Reference in New Issue
Block a user