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)
Example #4
0
    def test_full_in_and_out_path1(self):
        #1 market_order_request, 1 execution_request, 2 fills
        #out
        mor = MarketOrderRequestFactory.create(account=self.account1)
        er = ExecutionRequestFactory.create(goal=self.goal1,
                                            asset=self.ticker1,
                                            volume=100,
                                            order=mor)
        create_apex_orders()

        #in
        fill1_volume = 50
        fill1_price = 10
        fill2_volume = 50
        fill2_price = 15

        order1_etna = OrderETNA.objects.get(ticker=self.ticker1)
        send_etna_order(order1_etna)
        mark_etna_order_as_complete(order1_etna)

        ApexFillFactory.create(volume=fill1_volume,
                               price=fill1_price,
                               etna_order=order1_etna)
        ApexFillFactory.create(volume=fill2_volume,
                               price=fill2_price,
                               etna_order=order1_etna)

        process_apex_fills()

        sum_volume = Execution.objects.filter(distributions__execution_request__goal=self.goal1)\
            .aggregate(sum=Sum('volume'))
        self.assertTrue(sum_volume['sum'] == 100)
Example #5
0
    def setUp(self):
        self.account = ClientAccountFactory.create()
        self.goal = GoalFactory.create(account=self.account)
        self.mor = MarketOrderRequestFactory.create(account=self.account)

        self.asset = TickerFactory.create(symbol='GOOG')
        self.asset2 = TickerFactory.create(symbol='AAPL')
        er1 = ExecutionRequestFactory.create(goal=self.goal,
                                             asset=self.asset,
                                             volume=5,
                                             order=self.mor)
        er2 = ExecutionRequestFactory.create(goal=self.goal,
                                             asset=self.asset,
                                             volume=10,
                                             order=self.mor)
        er3 = ExecutionRequestFactory.create(goal=self.goal,
                                             asset=self.asset2,
                                             volume=10,
                                             order=self.mor)
Example #6
0
 def test_apex_order3(self):
     self.account3 = ClientAccountFactory.create()
     self.goal3 = GoalFactory.create(account=self.account3)
     self.mor3 = MarketOrderRequestFactory.create(account=self.account3)
     er5 = ExecutionRequestFactory.create(goal=self.goal3,
                                          asset=self.asset,
                                          volume=20,
                                          order=self.mor3)
     create_apex_orders()
     etna_orders = OrderETNA.objects.all()
     self.assertTrue(len(etna_orders) == 2)
     self.assertTrue(etna_orders[0].Quantity == 35)
Example #7
0
 def test_apex_order2(self):
     self.account2 = ClientAccountFactory.create()
     self.goal2 = GoalFactory.create(account=self.account2)
     self.mor2 = MarketOrderRequestFactory.create(account=self.account2)
     self.asset3 = TickerFactory.create(symbol='MSFT')
     er4 = ExecutionRequestFactory.create(goal=self.goal2,
                                          asset=self.asset3,
                                          volume=20,
                                          order=self.mor2)
     create_apex_orders()
     etna_order = OrderETNA.objects.all()
     self.assertTrue(len(etna_order) == 3)
     self.assertTrue(etna_order[2].Quantity == 20)
    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)