diff --git a/supervisor/core/trailing_orders.py b/supervisor/core/trailing_orders.py index 564194b..3b97853 100644 --- a/supervisor/core/trailing_orders.py +++ b/supervisor/core/trailing_orders.py @@ -20,7 +20,7 @@ class TrailingShell: # Don't grow a table larger than this amount. Helps cap memory usage. MAX_TABLE_LEN = 200 - def __init__(self, order, offset: int, tick_size: float, test=True): + def __init__(self, order, offset: int, tick_size: float, test=True, init_ws=True): self.tick_size = tick_size self.exited = False self.test = test @@ -39,7 +39,8 @@ def __init__(self, order, offset: int, tick_size: float, test=True): self.__reset() - self.connect() + if init_ws: + self.connect() def __del__(self): self.exit() diff --git a/tests/unit/test_trailing.py b/tests/unit/test_trailing.py index 0d7aa4d..18f6b6c 100644 --- a/tests/unit/test_trailing.py +++ b/tests/unit/test_trailing.py @@ -1,17 +1,50 @@ import unittest +from unittest.mock import Mock, patch from supervisor.core.orders import Order from supervisor.core.trailing_orders import TrailingShell from supervisor.core.utils.math import to_nearest -class Tests(unittest.TestCase): +class TrailingShellMethodsTests(unittest.TestCase): def setUp(self) -> None: self.order = Order(order_type='Stop', qty=228, stop_px=900, side='Sell') - self.trailing_order = TrailingShell(order=self.order, offset=10, tick_size=0.5, test=True) + self.trailing_order = TrailingShell(order=self.order, offset=10, tick_size=0.5, test=True, init_ws=False) def tearDown(self): - pass + self.trailing_order.exit() + + def test_calculate_new_price_for_sale_order(self): + """ + expected price: + 15000 * (1 - (10 / 100)) + = 13500 + """ + + new_price = self.trailing_order.calculate_new_price(15000) + self.assertEqual(13500, new_price) + + def test_calculate_new_price_for_buy_order(self): + """ + expected price: + 15000 * (1 + (10 / 100)) + = 16500 + """ + + self.order.side = 'Buy' + new_price = self.trailing_order.calculate_new_price(15000) + self.assertEqual(16500, new_price) + + def test_calculate_new_price_with_rounding(self): + """ + expected price: + 1234 * (1 - (10 / 100)) + = 1110.6000000000001 + correct price after rounding is 1110.5 + """ + + new_price = self.trailing_order.calculate_new_price(1234) + self.assertEqual(1110.5, new_price) def test_update_max_price(self): self.trailing_order.initial_price = 1000