Skip to content

Commit

Permalink
Write tests for trailing-stop.
Browse files Browse the repository at this point in the history
  • Loading branch information
forkcs committed May 12, 2020
1 parent a97d4e7 commit 5cc98e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
5 changes: 3 additions & 2 deletions supervisor/core/trailing_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
39 changes: 36 additions & 3 deletions tests/unit/test_trailing.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 5cc98e0

Please sign in to comment.