Skip to content

Commit

Permalink
[Tests] add stop loss tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Dec 8, 2024
1 parent bd0f35b commit 0935b10
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,23 @@ async def inner_test_create_and_cancel_stop_orders(self):
price = self.get_order_price(current_price, False)
size = self.get_order_size(await self.get_portfolio(), price)
open_orders = await self.get_open_orders()
stop_loss = await self.create_market_stop_loss_order(current_price, price, size,
trading_enums.TradeOrderSide.SELL)
self.check_created_stop_order(stop_loss, price, size, trading_enums.TradeOrderSide.SELL)
stop_loss_from_get_order = await self.get_order(stop_loss.exchange_order_id, stop_loss.symbol)
self.check_created_stop_order(stop_loss_from_get_order, price, size, trading_enums.TradeOrderSide.SELL)
assert await self.order_in_open_orders(open_orders, stop_loss)
await self.cancel_order(stop_loss)
assert self.exchange_manager.exchange.is_supported_order_type(
trading_enums.TraderOrderType.STOP_LOSS
) is True
stop_loss = await self.create_market_stop_loss_order(
current_price, price, size, trading_enums.TradeOrderSide.SELL
)
try:
self.check_created_stop_order(stop_loss, price, size, trading_enums.TradeOrderSide.SELL)
stop_loss_from_get_order = await self.get_order(stop_loss.exchange_order_id, stop_loss.symbol)
self.check_created_stop_order(stop_loss_from_get_order, price, size, trading_enums.TradeOrderSide.SELL)
assert await self.order_in_open_orders(open_orders, stop_loss)
## for manual checks
# print(f"Stop loss on {stop_loss.symbol} ok {stop_loss.origin_quantity} at {stop_loss.origin_price}")
# await asyncio.sleep(15)
finally:
# don't leave stop_loss as open order
await self.cancel_order(stop_loss)
assert await self.order_not_in_open_orders(open_orders, stop_loss)

async def test_get_my_recent_trades(self):
Expand Down Expand Up @@ -874,7 +884,10 @@ def check_parsed_trade(self, trade: personal_data.Trade):
trade.executed_price, trade.total_cost
)
if "USD" in trade.market:
assert self.MIN_TRADE_USD_VALUE < trade.total_cost < self.MAX_TRADE_USD_VALUE
assert self.MIN_TRADE_USD_VALUE * decimal.Decimal("0.9") < trade.total_cost < self.MAX_TRADE_USD_VALUE, (
f"{self.MIN_TRADE_USD_VALUE * decimal.Decimal('0.9')} < {trade.total_cost} < {self.MAX_TRADE_USD_VALUE} "
f"is FALSE"
)

def check_theoretical_cost(self, symbol, quantity, price, cost):
theoretical_cost = quantity * price
Expand Down Expand Up @@ -971,8 +984,10 @@ async def create_order(self, price, current_price, size, side, order_type,
price=price,
side=side,
)
assert current_order.is_self_managed() is False # will be a real order: can't be self-managed
if push_on_exchange:
current_order = await self._create_order_on_exchange(current_order)
assert current_order.is_self_managed() is False # is a real order: can't be self-managed
if current_order is None:
raise AssertionError("Error when creating order")
return current_order
Expand Down Expand Up @@ -1102,8 +1117,10 @@ def check_created_market_order(self, order, size, side):

def check_created_stop_order(self, order, price, size, side):
self._check_order(order, size, side)
assert order.origin_price == price
assert order.origin_price == price, f"{order.origin_price=} != {price=}"
assert order.side is side
assert order.order_type is trading_enums.TraderOrderType.STOP_LOSS
assert order.is_self_managed() is False # is real stop loss: NOT self-managed
expected_type = personal_data.StopLossOrder
assert isinstance(order, expected_type)

Expand Down
3 changes: 1 addition & 2 deletions additional_tests/exchanges_tests/test_binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ async def test_get_cancelled_orders(self):
await super().test_get_cancelled_orders()

async def test_create_and_cancel_stop_orders(self):
# pass if not implemented
pass
await super().test_create_and_cancel_stop_orders()

async def test_edit_limit_order(self):
# pass if not implemented
Expand Down
3 changes: 1 addition & 2 deletions additional_tests/exchanges_tests/test_bingx.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ async def test_get_cancelled_orders(self):
await super().test_get_cancelled_orders()

async def test_create_and_cancel_stop_orders(self):
# pass if not implemented
pass
await super().test_create_and_cancel_stop_orders()

async def test_edit_limit_order(self):
# pass if not implemented
Expand Down
3 changes: 1 addition & 2 deletions additional_tests/exchanges_tests/test_coinbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ async def test_get_cancelled_orders(self):
await super().test_get_cancelled_orders()

async def test_create_and_cancel_stop_orders(self):
# pass if not implemented
pass
await super().test_create_and_cancel_stop_orders()

async def test_edit_limit_order(self):
# pass if not implemented
Expand Down
3 changes: 1 addition & 2 deletions additional_tests/exchanges_tests/test_kucoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ async def test_get_cancelled_orders(self):
await super().test_get_cancelled_orders()

async def test_create_and_cancel_stop_orders(self):
# pass if not implemented
pass
await super().test_create_and_cancel_stop_orders()

async def test_edit_limit_order(self):
# pass if not implemented
Expand Down

0 comments on commit 0935b10

Please sign in to comment.