def test_partial_in_and_out_path1(self):
        #1 market_order_request, 1 execution_request, 2 fills
        #out
        mor = MarketOrderRequestFactory.create(account=self.account1)
        mor2 = MarketOrderRequestFactory.create(account=self.account2)
        er = ExecutionRequestFactory.create(goal=self.goal1,
                                            asset=self.ticker1,
                                            volume=100,
                                            order=mor)
        er2 = ExecutionRequestFactory.create(goal=self.goal2,
                                             asset=self.ticker2,
                                             volume=10,
                                             order=mor2)
        er3 = ExecutionRequestFactory.create(goal=self.goal1,
                                             asset=self.ticker1,
                                             volume=50,
                                             order=mor)

        allocations = create_pre_trade_allocation()

        create_orders()

        account_profile = FAAccountProfile()
        account_profile.append_share_allocation(
            self.ticker1.symbol, allocations[self.ticker1.symbol])
        profile = account_profile.get_profile()
        send_pre_trade("IB", profile)
        order1_ib = Order.objects.get(ticker=self.ticker1)
        order1_ib.__class__ = IBOrder
        order1_ib.m_faProfile = self.ticker1.symbol
        send_order(order1_ib, True)

        account_profile = FAAccountProfile()
        account_profile.append_share_allocation(
            self.ticker2.symbol, allocations[self.ticker2.symbol])
        profile = account_profile.get_profile()
        send_pre_trade("IB", profile)
        order2_ib = Order.objects.get(ticker=self.ticker2)
        order2_ib.__class__ = IBOrder
        order2_ib.m_faProfile = self.ticker2.symbol

        send_order(order2_ib, True)
        orders = []
        orders.append(order1_ib)
        orders.append(order2_ib)
        time.sleep(1 * 60)
        distributions = update_orders(orders)
        mark_order_as_complete(order1_ib)
        mark_order_as_complete(order2_ib)

        process_fills(distributions)

        sum_volume = Execution.objects.filter(distributions__execution_request__goal=self.goal1)\
            .aggregate(sum=Sum('volume'))
        self.assertTrue(sum_volume['sum'] == 150)
        sum_volume = Execution.objects.filter(distributions__execution_request__goal=self.goal2) \
            .aggregate(sum=Sum('volume'))
        self.assertTrue(sum_volume['sum'] == 10)
    def test_full_in_and_out_path1(self):
        #1 market_order_request, 1 execution_request, 2 fills
        #out
        mor = MarketOrderRequestFactory.create(account=self.account1)
        mor2 = MarketOrderRequestFactory.create(account=self.account2)
        er = ExecutionRequestFactory.create(goal=self.goal1,
                                            asset=self.ticker1,
                                            volume=100,
                                            order=mor)
        er2 = ExecutionRequestFactory.create(goal=self.goal2,
                                             asset=self.ticker2,
                                             volume=10,
                                             order=mor2)
        er3 = ExecutionRequestFactory.create(goal=self.goal1,
                                             asset=self.ticker1,
                                             volume=50,
                                             order=mor)

        create_orders()

        #in
        fill1_volume = 50
        fill1_price = 10
        fill2_volume = 10
        fill2_price = 30
        fill3_volume = 100
        fill3_price = 15

        order1_etna = Order.objects.get(ticker=self.ticker1)
        order2_etna = Order.objects.get(ticker=self.ticker2)
        send_order(order1_etna)
        send_order(order2_etna)
        #orders = []
        #orders.append(order1_etna)
        #orders.append(order2_etna)
        #distributions = update_orders(orders)
        mark_order_as_complete(order1_etna)
        mark_order_as_complete(order2_etna)

        FillFactory.create(volume=fill1_volume,
                           price=fill1_price,
                           order=order1_etna)
        FillFactory.create(volume=fill2_volume,
                           price=fill2_price,
                           order=order2_etna)
        FillFactory.create(volume=fill3_volume,
                           price=fill3_price,
                           order=order1_etna)

        process_fills()  #distributions)

        sum_volume = Execution.objects.filter(distributions__execution_request__goal=self.goal1)\
            .aggregate(sum=Sum('volume'))
        self.assertTrue(sum_volume['sum'] == 150)
        sum_volume = Execution.objects.filter(distributions__execution_request__goal=self.goal2) \
            .aggregate(sum=Sum('volume'))
        self.assertTrue(sum_volume['sum'] == 10)
    def test_full_in_and_out_path3(self):
        # test sale as well
        mor1 = MarketOrderRequestFactory.create(account=self.account1)
        ExecutionRequestFactory.create(goal=self.goal1,
                                       asset=self.ticker1,
                                       volume=101,
                                       order=mor1)

        create_orders()

        #in
        fill1a_volume = 50
        fill1a_price = 10
        fill1b_volume = 50
        fill1b_price = 15

        order1_etna = Order.objects.get(ticker=self.ticker1)
        send_order(order1_etna)  #, True)
        #orders = []
        #orders.append(order1_etna)
        #distributions = update_orders(orders)
        mark_order_as_complete(order1_etna)

        FillFactory.create(volume=fill1a_volume,
                           price=fill1a_price,
                           order=order1_etna)
        FillFactory.create(volume=fill1b_volume,
                           price=fill1b_price,
                           order=order1_etna)

        process_fills()  #distributions)
        order1 = Order.objects.get(ticker=self.ticker1)
        self.assertTrue(
            order1.fill_info == Order.FillInfo.PARTIALY_FILLED.value)

        mor2 = MarketOrderRequestFactory.create(account=self.account1)
        ExecutionRequestFactory.create(goal=self.goal1,
                                       asset=self.ticker1,
                                       volume=-60,
                                       order=mor2)
        create_orders()
        fill2_volume = -60
        fill2_price = 10

        order2_etna = Order.objects.get(ticker=self.ticker1,
                                        Status=Order.StatusChoice.New.value)
        send_order(order2_etna)
        mark_order_as_complete(order2_etna)

        FillFactory.create(volume=fill2_volume,
                           price=fill2_price,
                           order=order2_etna)
        process_fills()
        order2 = Order.objects.get(id=order2_etna.id)
        self.assertTrue(order2.fill_info == Order.FillInfo.FILLED.value)
Пример #4
0
    def execute(self, mor):
        approve_mor(mor)
        create_orders()
        orders_etna = Order.objects.is_not_complete()
        for order in orders_etna:
            send_order(order)
            mark_order_as_complete(order)

            ticker = Ticker.objects.get(id=order.ticker.id)
            price = ticker.daily_prices.order_by('-date').first().price

            FillFactory.create(volume=order.FillQuantity,
                               price=price,
                               etna_order=order)
        process_fills()
    def test_full_in_and_out_path2(self):
        # 3 market_order_requests from 3 accounts, for 2 tickers, fill for first ticker in 2 batches,
        # 1 batch for second ticker (2 mors)
        #out

        mor1 = MarketOrderRequestFactory.create(account=self.account1)
        ExecutionRequestFactory.create(goal=self.goal1,
                                       asset=self.ticker1,
                                       volume=100,
                                       order=mor1)

        mor2 = MarketOrderRequestFactory.create(account=self.account2)
        ExecutionRequestFactory.create(goal=self.goal2,
                                       asset=self.ticker2,
                                       volume=25,
                                       order=mor2)

        mor3 = MarketOrderRequestFactory.create(account=self.account3)
        ExecutionRequestFactory.create(goal=self.goal3,
                                       asset=self.ticker2,
                                       volume=25,
                                       order=mor3)

        create_orders()

        #in
        fill1a_volume = 50
        fill1a_price = 10
        fill1b_volume = 50
        fill1b_price = 15

        order1_etna = Order.objects.get(ticker=self.ticker1)
        send_order(order1_etna)  #, True)
        #orders = []
        #orders.append(order1_etna)
        #distributions = update_orders(orders)
        mark_order_as_complete(order1_etna)

        FillFactory.create(volume=fill1a_volume,
                           price=fill1a_price,
                           order=order1_etna)
        FillFactory.create(volume=fill1b_volume,
                           price=fill1b_price,
                           order=order1_etna)

        process_fills()  #distributions)

        sum_volume = Execution.objects.filter(distributions__execution_request__goal=self.goal1)\
            .aggregate(sum=Sum('volume'))
        self.assertTrue(sum_volume['sum'] == 100)

        fill2_3_volume = 50
        fill2_3_price = 13

        order2_3_etna = Order.objects.get(ticker=self.ticker2)
        send_order(order2_3_etna)
        mark_order_as_complete(order2_3_etna)

        FillFactory.create(volume=fill2_3_volume,
                           price=fill2_3_price,
                           order=order2_3_etna)

        process_fills()
        sum_volume = Execution.objects.filter(distributions__execution_request__asset=self.ticker2)\
            .aggregate(sum=Sum('volume'))
        self.assertTrue(sum_volume['sum'] == 50)