How to use futures - 10 common examples

To help you get started, we’ve selected a few futures examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github severb / flowy / tests / test_local.py View on Github external
def test(self):
        lw = LocalWorkflow(wf,
                           activity_workers=16,
                           workflow_workers=2,
                           executor=ThreadPoolExecutor)
        lw.conf_activity('a', examples.activity)
        start = time.time()
        result = lw.run(TIME_SCALE)
        duration = time.time() - start
        lines = [l.strip() for l in wf.__doc__.split("\n")]
        expected = None
        for line in lines:
            if line.startswith('R '):
                expected = int(line.split()[-1].split("-")[-1])
                break
        self.assertEquals(expected, result)
        for line in lines:
            if line.startswith('Duration:'):
                expected_duration = int(line.split()[-1]) * TIME_SCALE * 0.1
                break
        print(expected_duration, duration)
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
history = FutureHistory.objects.filter(future=future)
        self.assertEqual(history.count(), 2)
        self.assertEqual(history[0].price, 275)
        self.assertEqual(history[1].price, 100)  # Got the highest then lowest.
        transactions = SuccessfulTransaction.objects.filter(future=future)
        self.assertEqual(transactions.count(), 2)
        self.assertTrue(self.compare_transactions(
            transactions[0], SuccessfulTransaction(
                buyer=order_1.creator, seller=user, future=future,
                quantity=15, unit_price=275)))
        self.assertTrue(self.compare_transactions(
            transactions[1], SuccessfulTransaction(
                buyer=order_2.creator, seller=user, future=future,
                quantity=75, unit_price=100)))
        # Wallets
        wallet = Wallet.objects.get(user=user)
        wallet_1 = Wallet.objects.get(user=order_1.creator)
        wallet_2 = Wallet.objects.get(user=order_2.creator)
        key = '%s' % future.pk
        self.assertEqual(json.loads(wallet.shares_owned)[key], 5)
        self.assertEqual(json.loads(wallet_1.shares_owned)[key], 15)
        self.assertEqual(json.loads(wallet_2.shares_owned)[key], 75)
        self.assertEqual(json.loads(wallet.shares_owned)['1'], 7)  # Unchanged.
        self.assertEqual(wallet.points, 10000 + (15 * 275) + (75 * 100))
        self.assertEqual(wallet_1.points, 10000 - (15 * 275))
        self.assertEqual(wallet_2.points, 10000 - (75 * 100))
        self.assertEqual(wallet.frozen, 0)
        self.assertEqual(wallet_1.frozen, 0)
        self.assertEqual(wallet_2.frozen, 0)
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
def test_order_5(self):
        """
            A regular sell to multiple people, with some stock still left over.
        """
        user = User.objects.get(pk=1)
        wallet = Wallet.objects.get(user=user)
        wallet.shares_owned = """{"4": 95, "1": 7}"""
        future = Future.objects.get(pk=4)
        # Sell 93 shares at 95 each.
        response = process_order(93, 95, wallet, 'sell', future)
        # Someone wants to buy 75@100, 15@275
        order = Order.objects.all().order_by('-pk')[0]  # My sell.
        order_1 = Order.objects.get(pk=11)  # 15@275
        order_2 = Order.objects.get(pk=5)   # 75@100
        self.assertEqual(order_1.filled, True)
        self.assertEqual(order_2.filled, True)
        self.assertEqual(order.filled, False)
        self.assertEqual(order.quantity, 3)
        self.assertEqual(order_1.filled_by, user)
        self.assertEqual(order_2.filled_by, user)
        future = Future.objects.get(pk=4)
        self.assertEqual(future.last_buy, 100)
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
def test_order_2(self):
        """
            Test that orders are prioritized by price then chronologically.
        """
        user = User.objects.get(pk=8)
        wallet = Wallet.objects.get(user=user)
        # Want to buy future "Jesse Day".
        # There is a sell order of 8 for 600, a buy order of 4 for 100,
        # a sell order of 4 for 325, a buy order of 4 for 275 in the fixture.
        future = Future.objects.get(pk=4)
        response = process_order(4, 600, wallet, 'buy', future)
        # Get my order
        order = Order.objects.all().order_by('-pk')[0]
        self.assertEqual(order.filled, True)
        self.assertEqual(order.unit_price, 600)  # Unchanged
        # Get the matching orders.
        order_1 = Order.objects.get(pk=9)  # The 325 S
        order_2 = Order.objects.get(pk=3)  # The 650 S
        self.assertEqual(order_1.filled, True)
        self.assertEqual(order_2.filled, False)
        self.assertEqual(order_1.filled_by, user)
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
future = Future.objects.get(pk=1)
        self.assertEqual(future.last_buy, 155)
        self.assertEqual(future.ask, 600)
        self.assertEqual(future.bid, 155)
        self.assertEqual(future.volume, 4)
        history = FutureHistory.objects.filter(future=future)
        self.assertEqual(history.count(), 1)
        self.assertEqual(history[0].price, 155)
        transactions = SuccessfulTransaction.objects.filter(future=future)
        self.assertEqual(transactions.count(), 1)
        self.assertTrue(self.compare_transactions(
            transactions[0], SuccessfulTransaction(
                buyer=order_1.creator, seller=user, future=future,
                quantity=4, unit_price=155)))
        # Wallets
        wallet = Wallet.objects.get(user=user)
        wallet_1 = Wallet.objects.get(user=order_1.creator)
        key = '%s' % future.pk
        self.assertEqual(json.loads(wallet.shares_owned)[key], 3)
        self.assertEqual(json.loads(wallet_1.shares_owned)[key], 4)
        self.assertEqual(json.loads(wallet.shares_owned)['4'], 10) # Unchanged.
        self.assertEqual(wallet_1.points, 10000 - (4 * 155))
        self.assertEqual(wallet.points, 10000 + (4 * 155))
        self.assertEqual(wallet.frozen, 0)
        self.assertEqual(wallet_1.frozen, 0)
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
self.assertEqual(history[1].price, 600)
        transactions = SuccessfulTransaction.objects.filter(future=future)
        self.assertEqual(transactions.count(), 2)

        self.assertTrue(self.compare_transactions(
            transactions[0], SuccessfulTransaction(
                buyer=user, seller=order_1.creator, future=future,
                quantity=4, unit_price=325)))
        self.assertTrue(self.compare_transactions(
            transactions[1], SuccessfulTransaction(
                buyer=user, seller=order_2.creator, future=future,
                quantity=8, unit_price=600)))
        # Wallets.
        wallet = Wallet.objects.get(user=user)
        wallet_1 = Wallet.objects.get(user=order_1.creator)
        wallet_2 = Wallet.objects.get(user=order_2.creator)
        key = '%s' % future.pk
        self.assertEqual(json.loads(wallet.shares_owned)[key], 12)
        self.assertEqual(wallet.points, 10000 - (4 * 325 + 8 * 600))
        self.assertEqual(wallet_1.points, 10000 + (4 * 325))
        self.assertEqual(wallet_2.points, 10000 + (8 * 600))
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
def test_order_4(self):
        """
            Regular sell.
        """
        user = User.objects.get(pk=1)
        wallet = Wallet.objects.get(user=user)
        wallet.shares_owned = """{"1": 7, "4": 10}"""
        future = Future.objects.get(pk=1)
        response = process_order(4, 150, wallet, 'sell', future)
        # Someone wants to buy 10@155
        order = Order.objects.all().order_by('-pk')[0]
        order_1 = Order.objects.get(pk=1) # Buy 10@155
        self.assertEqual(order_1.filled, False)
        self.assertEqual(order.filled, True)
        self.assertEqual(order_1.quantity, 6)
        self.assertEqual(order.filled_by, order_1.creator)
        future = Future.objects.get(pk=1)
        self.assertEqual(future.last_buy, 155)
        self.assertEqual(future.ask, 600)
        self.assertEqual(future.bid, 155)
        self.assertEqual(future.volume, 4)
        history = FutureHistory.objects.filter(future=future)
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
self.assertEqual(history[0].price, 325)
        self.assertEqual(history[1].price, 600)
        transactions = SuccessfulTransaction.objects.filter(future=future)
        self.assertEqual(transactions.count(), 2)

        self.assertTrue(self.compare_transactions(
            transactions[0], SuccessfulTransaction(
                buyer=user, seller=order_1.creator, future=future,
                quantity=4, unit_price=325)))
        self.assertTrue(self.compare_transactions(
            transactions[1], SuccessfulTransaction(
                buyer=user, seller=order_2.creator, future=future,
                quantity=8, unit_price=600)))
        # Wallets.
        wallet = Wallet.objects.get(user=user)
        wallet_1 = Wallet.objects.get(user=order_1.creator)
        wallet_2 = Wallet.objects.get(user=order_2.creator)
        key = '%s' % future.pk
        self.assertEqual(json.loads(wallet.shares_owned)[key], 12)
        self.assertEqual(wallet.points, 10000 - (4 * 325 + 8 * 600))
        self.assertEqual(wallet_1.points, 10000 + (4 * 325))
        self.assertEqual(wallet_2.points, 10000 + (8 * 600))
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
def test_simultaneous_sell_short(self):
        """
            Can't short-sell and regular sell the same shares simultaneously.
        """
        user = User.objects.get(pk=1)
        wallet = Wallet.objects.get(user=user)
        wallet.shares_owned = """{"1": 3}"""
        future = Future.objects.get(pk=1)
        response = process_order(4, 200, wallet, 'sell', future)
        # We only own 3 but are trying to sell 4.
        self.assertContains(response, 'two separate orders', status_code=400)
github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
def test_short_sell(self):
        user = User.objects.get(pk=1)
        wallet = Wallet.objects.get(user=user)
        # Want to short-sell "Nigel Richards".
        future = Future.objects.get(pk=1)
        wallet.points = 11000
        wallet.save()
        response = process_order(11, 140, wallet, 'sell', future)
        order = Order.objects.all().order_by('-pk')[0]
        order_1 = Order.objects.get(pk=1)  # Buy 10@155
        self.assertEqual(order_1.filled, True)
        self.assertEqual(order.filled, False)
        self.assertEqual(order.quantity, 1)
        self.assertEqual(order_1.filled_by, user)
        ### Future
        future = Future.objects.get(pk=1)
        self.assertEqual(future.last_buy, 155)
        self.assertEqual(future.ask, 140)
        self.assertEqual(future.bid, None)
        self.assertEqual(future.volume, 10)
        history = FutureHistory.objects.filter(future=future)