当前位置: 首页>>代码示例>>Python>>正文


Python Order.create_order方法代码示例

本文整理汇总了Python中models.order.Order.create_order方法的典型用法代码示例。如果您正苦于以下问题:Python Order.create_order方法的具体用法?Python Order.create_order怎么用?Python Order.create_order使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.order.Order的用法示例。


在下文中一共展示了Order.create_order方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_normal_scenario_with_two_users

# 需要导入模块: from models.order import Order [as 别名]
# 或者: from models.order.Order import create_order [as 别名]
    def test_normal_scenario_with_two_users(self):
        # First create some users
        user1 = User.create_user(self.session, "user1", "abcd")
        user2 = User.create_user(self.session, "user2", "abcd")

        # Make sure user1 has 1 BTC for later deposit into a futures contract
        btc = Asset.create_asset("BTC")
        user1.increase_volume_of_asset(self.session, btc, Decimal("1"))

        # Create a futures contract worth 1 BTC in +14 days
        contract, asset = FuturesContract.create_contract(
            self.session, user1, datetime.now() + timedelta(days=14), btc, Decimal("1"), "FUTURE", Decimal("100")
        )
        # This will fail if user1 has insufficient funds (i.e. < 1 BTC)
        assert contract is not None
        assert asset is not None
        self.session.commit()

        # Create an order for this contract (i.e. newly created asset)
        usd = Asset.create_asset("USD")
        ask_order = Order.create_order(
            self.session, user1, Decimal("20"), usd, contract, Decimal("50"), False, OrderType.limit_order.value
        )
        # Make sure we have enough funds
        assert ask_order is not None
        self.session.commit()

        # Put order into market
        assert put_order(self.session, ask_order) is None

        # Create a bid order from user2
        user2.increase_volume_of_asset(self.session, usd, Decimal("20"))
        bid_order1 = Order.create_order(
            self.session, user2, Decimal("20"), usd, contract, Decimal("50"), True, OrderType.limit_order.value
        )
        # Make sure we have enough funds
        assert bid_order1 is not None

        transaction = put_order(self.session, bid_order1)
        assert isinstance(transaction, Transaction)
        assert transaction.ask_order is ask_order
        assert transaction.bid_order is bid_order1

        contract.expire(self.session)
        assert user1.volume_of_asset(self.session, btc) == Decimal("0.5")
        assert user2.volume_of_asset(self.session, btc) == Decimal("0.5")

        # Run it again. Must be idempotent.
        contract.expire(self.session)
        assert user1.volume_of_asset(self.session, btc) == Decimal("0.5")
        assert user2.volume_of_asset(self.session, btc) == Decimal("0.5")
开发者ID:Rub3nC,项目名称:btcex,代码行数:53,代码来源:test_futures.py

示例2: test_cancel_contract

# 需要导入模块: from models.order import Order [as 别名]
# 或者: from models.order.Order import create_order [as 别名]
    def test_cancel_contract(self):
        # Create a user and asset
        user = User.create_user(self.session, "user", "abcd")
        usd = Asset.create_asset("USD")
        self.session.add_all([user, usd])
        self.session.commit()

        # Add funds to user1 so that we can create a contract
        user.increase_volume_of_asset(self.session, usd, Decimal("1"))

        # Let this user create a contract
        contract, asset = FuturesContract.create_contract(
            self.session, user, datetime.now() + timedelta(days=14), usd, Decimal("1"), "FUTURE", Decimal("100")
        )
        assert contract is not None
        assert asset is not None

        # Now, money have been withdrawn from user1's account
        assert user.volume_of_asset(self.session, usd) == Decimal("0")

        # Cancel the contract
        assert contract.cancel(self.session) is True
        assert inspect(contract).deleted is True
        assert user.volume_of_asset(self.session, usd) == Decimal("1")

        # OK, the contract is cancelled.
        # Now do the same thing, but this time, create an order and verify that the cancelled flag is set instead
        contract, asset = FuturesContract.create_contract(
            self.session, user, datetime.now() + timedelta(days=14), usd, Decimal("1"), "FUTURE", Decimal("100")
        )
        assert contract is not None
        assert asset is not None
        assert user.volume_of_asset(self.session, usd) == Decimal("0")

        ask_order = Order.create_order(
            self.session, user, Decimal("20"), usd, contract, Decimal("50"), False, OrderType.limit_order.value
        )
        assert ask_order is not None
        assert user.volume_of_asset(self.session, asset) == Decimal("50")

        # Assert that we cannot cancel the contract if there are created orders
        assert contract.cancel(self.session) is False

        # Put order into market
        assert put_order(self.session, ask_order) is None

        # Assert that we cannot cancel the contract if there are orders in the market
        assert contract.cancel(self.session) is False

        # It should be possible to cancel this order
        assert ask_order.cancel(self.session) is True
        assert user.volume_of_asset(self.session, asset) == Decimal("100")

        # Check that order is in the expected state
        assert contract.cancel(self.session) is True
        assert inspect(contract).deleted is False
        assert contract.cancelled is True
开发者ID:Rub3nC,项目名称:btcex,代码行数:59,代码来源:test_futures.py


注:本文中的models.order.Order.create_order方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。