def test_save_limit_order(self, portfolio_namespace): Ohlcv.objects.filter(symbol="BTCUSD", time_frame="1m").update(close=100) share = portfolio_namespace.share form = OrderForm( data={ "symbol": "BTCUSD", "portfolio": portfolio_namespace.portfolio, "shares": 1, "order_type": "limit_order", "trade_type": "sell", "price": 100, }) form.fields["price"].disabled = False form.full_clean() assert form.instance.portfolio.balance == 50000 assert share.shares == 50 assert share.average_price == 1 order = form.save() share = portfolio_namespace.portfolio.cryptoshare_set.get( symbol="BTCUSD") assert not order.filled assert order.portfolio.balance == 50000 assert share.shares == 50 assert share.average_price == 1
def test_save_buy(self, portfolio_namespace): Ohlcv.objects.filter(symbol="BTCUSD", time_frame="1m").update(close=100) share = portfolio_namespace.share form = OrderForm( data={ "symbol": "BTCUSD", "portfolio": portfolio_namespace.portfolio, "shares": 1, "order_type": "market_order", "trade_type": "buy", }) form.full_clean() assert form.instance.portfolio.balance == 50000 assert share.shares == 50 assert share.average_price == 1 order = form.save() share = portfolio_namespace.portfolio.cryptoshare_set.get( symbol="BTCUSD") assert order.filled assert order.portfolio.balance == 49900 assert share.shares == 51 assert share.average_price == Decimal("2.94118") assert order.share == share
def test_save_delete_share(self, portfolio_namespace): Ohlcv.objects.filter(symbol="ETHUSD", time_frame="1m").update(close=100) share = CryptoShareFactory(average_price=1, portfolio=portfolio_namespace.portfolio, shares=50.0, symbol_id="ETHUSD") form = OrderForm( data={ "symbol": "ETHUSD", "portfolio": portfolio_namespace.portfolio, "shares": 50, "order_type": "market_order", "trade_type": "sell", }) form.full_clean() assert form.instance.portfolio.balance == 50000 assert share.shares == 50 assert share.average_price == 1 order = form.save() assert order.filled assert order.portfolio.balance == 55000 with pytest.raises(CryptoShare.DoesNotExist): portfolio_namespace.portfolio.cryptoshare_set.get(symbol="ETHUSD") # Pytest teardown fails if the share is deleted share.save()