예제 #1
0
 def setUp(self):
     self.longMessage = True  # Print complete error message on failure
     self.maxDiff = None
     self.asset_manager_id = random.randint(1, 2**31-1)
     self.future = generate_future(asset_manager_id=self.asset_manager_id)
     self.asset_id = self.future.asset_id
     self.assets_interface = AssetsInterface()
 def setUp(self):
     self.longMessage = True  # Print complete error message on failure
     self.asset_manager_id = random.randint(1, 2**31 - 1)
     self.fx_forward = generate_fx_forward(
         asset_manager_id=self.asset_manager_id)
     self.asset_id = self.fx_forward.asset_id
     self.assets_interface = AssetsInterface(environment=ENVIRONMENT)
class ForeignExchangeTest(unittest.TestCase):
    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.asset_manager_id = random.randint(1, 2**31 - 1)
        self.fx_forward = generate_fx_forward(
            asset_manager_id=self.asset_manager_id)
        self.asset_id = self.fx_forward.asset_id
        self.assets_interface = AssetsInterface(environment=ENVIRONMENT)

    def tearDown(self):
        pass

    def test_ForeignExchange(self):
        fx = generate_foreignexchange(asset_id='USDJPY')
        self.assertEqual(type(fx), ForeignExchange)
        self.assertEqual(self.fx_forward.pricing_method(), 'Derived')

    def test_ForeignExchangeForward(self):
        self.assertEqual(type(self.fx_forward), ForeignExchangeForward)

    def test_ForeignExchangeSpot(self):
        fx_spot = generate_fx_spot(asset_manager_id=self.asset_manager_id)
        self.assertEqual(type(fx_spot), ForeignExchangeSpot)

    def test_Persistence(self):
        self.assets_interface.new(self.fx_forward)
        fx_forward = self.assets_interface.retrieve(
            asset_manager_id=self.asset_manager_id, asset_id=self.asset_id)
        self.assertEqual(fx_forward, self.fx_forward)
예제 #4
0
 def setUp(self):
     self.longMessage = True  # Print complete error message on failure
     self.asset_manager_id = random.randint(1, 2**31 - 1)
     self.fx_option = generate_fx_option(
         asset_manager_id=self.asset_manager_id)
     self.asset_id = self.fx_option.asset_id
     self.assets_interface = AssetsInterface(environment=STAGE)
예제 #5
0
 def setUp(self):
     self.longMessage = True  # Print complete error message on failure
     self.maxDiff = None  # View the complete diff when there is a mismatch in a test
     self.interface = TransactionsInterface(environment=ENVIRONMENT)
     self.asset_manager_id = 1
     self.assets_interface = AssetsInterface(environment=ENVIRONMENT)
     self.books_interface = BooksInterface(environment=ENVIRONMENT)
     self.asset = generate_asset(asset_manager_id=self.asset_manager_id, fungible=True)
     self.asset_book = generate_book(asset_manager_id=self.asset_manager_id)
     self.counterparty_book = generate_book(asset_manager_id=self.asset_manager_id)
     self.transaction1 = generate_transaction(asset_manager_id=self.asset_manager_id, asset_id=self.asset.asset_id,
                                              asset_book_id=self.asset_book.book_id,
                                              counterparty_book_id=self.counterparty_book.book_id,
                                              transaction_currency='USD',
                                              net_affecting_charges=True,
                                              charge_currency='USD')
     self.transaction2 = generate_transaction(asset_manager_id=self.asset_manager_id, asset_id=self.asset.asset_id,
                                              asset_book_id=self.asset_book.book_id,
                                              counterparty_book_id=self.counterparty_book.book_id,
                                              transaction_currency='USD',
                                              net_affecting_charges=True,
                                              charge_currency='USD')
     self.setup_cache()
     self.interface.new(self.transaction1)
     self.interface.new(self.transaction2)
     self.transaction_id1 = self.transaction1.transaction_id
     self.transaction_id2 = self.transaction2.transaction_id
예제 #6
0
class NettingTest(unittest.TestCase):

    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.maxDiff = None  # View the complete diff when there is a mismatch in a test
        self.interface = TransactionsInterface(environment=ENVIRONMENT)
        self.asset_manager_id = 1
        self.assets_interface = AssetsInterface(environment=ENVIRONMENT)
        self.books_interface = BooksInterface(environment=ENVIRONMENT)
        self.asset = generate_asset(asset_manager_id=self.asset_manager_id, fungible=True)
        self.asset_book = generate_book(asset_manager_id=self.asset_manager_id)
        self.counterparty_book = generate_book(asset_manager_id=self.asset_manager_id)
        self.transaction1 = generate_transaction(asset_manager_id=self.asset_manager_id, asset_id=self.asset.asset_id,
                                                 asset_book_id=self.asset_book.book_id,
                                                 counterparty_book_id=self.counterparty_book.book_id,
                                                 transaction_currency='USD',
                                                 net_affecting_charges=True,
                                                 charge_currency='USD')
        self.transaction2 = generate_transaction(asset_manager_id=self.asset_manager_id, asset_id=self.asset.asset_id,
                                                 asset_book_id=self.asset_book.book_id,
                                                 counterparty_book_id=self.counterparty_book.book_id,
                                                 transaction_currency='USD',
                                                 net_affecting_charges=True,
                                                 charge_currency='USD')
        self.setup_cache()
        self.interface.new(self.transaction1)
        self.interface.new(self.transaction2)
        self.transaction_id1 = self.transaction1.transaction_id
        self.transaction_id2 = self.transaction2.transaction_id

    def tearDown(self):
        pass

    def create_transaction_asset(self):
        self.assets_interface.upsert(self.asset)

    def setup_cache(self):
        self.create_transaction_asset()
        self.create_transaction_book(self.asset_book)
        self.create_transaction_book(self.counterparty_book)

    def create_transaction_book(self, book):
        self.books_interface.new(book)

    def test_GenerateNettingSet(self):
        net = self.interface.net_transactions(asset_manager_id=self.asset_manager_id,
                                              transaction_ids=[self.transaction_id1, self.transaction_id2])
        self.assertEqual(type(net), Transaction)
        transaction_ids = {link.linked_transaction_id for link in net.links.get('NettingSet')}
        self.assertEqual(transaction_ids, {self.transaction_id1, self.transaction_id2})

    def test_RetrieveNettingSet(self):
        net = self.interface.net_transactions(asset_manager_id=self.asset_manager_id,
                                              transaction_ids=[self.transaction_id1, self.transaction_id2])
        net_transaction_id, netting_set = self.interface.retrieve_netting_set(asset_manager_id=self.asset_manager_id,
                                                                              transaction_id=net.transaction_id)
        self.assertEqual(net_transaction_id, net.transaction_id)
        self.assertEqual(len(netting_set), 2)
        transaction_ids = {transaction.transaction_id for transaction in netting_set}
        self.assertEqual(transaction_ids, {self.transaction_id1, self.transaction_id2})
예제 #7
0
 def setUp(self):
     self.longMessage = True  # Print complete error message on failure
     asset_manager_id = random.randint(1, 2**31 - 1)
     self.pizza = Pizza(asset_id='pizza1',
                        asset_manager_id=asset_manager_id,
                        size='Large',
                        toppings=['pineapple', 'corn', 'garlic'])
     self.assets_interface = AssetsInterface()
예제 #8
0
class TransferTest(unittest.TestCase):
    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.maxDiff = None  # View the complete diff when there is a mismatch in a test
        self.interface = TransactionsInterface(environment=STAGE)
        self.assets_interface = AssetsInterface(environment=STAGE)
        self.books_interface = BooksInterface(environment=STAGE)
        self.asset_manager_id = random.randint(1, 2**31 - 1)
        self.asset = generate_asset(asset_manager_id=self.asset_manager_id,
                                    fungible=True)
        self.trader_one_book = generate_book(
            asset_manager_id=self.asset_manager_id)
        self.trader_two_book = generate_book(
            asset_manager_id=self.asset_manager_id)
        self.wash_book = generate_book(asset_manager_id=self.asset_manager_id,
                                       book_type='Wash')
        self.setup_cache()

    def setup_cache(self):
        self.create_transaction_asset()
        self.create_transaction_book(self.trader_one_book)
        self.create_transaction_book(self.trader_two_book)
        self.create_transaction_book(self.wash_book)

    def tearDown(self):
        pass

    def create_transaction_asset(self):
        self.assets_interface.upsert(self.asset)

    def create_transaction_book(self, book):
        self.books_interface.new(book)

    def test_BookTransfer(self):
        deliver, receive = self.interface.book_transfer(
            asset_manager_id=self.asset_manager_id,
            source_book_id=self.trader_one_book.book_id,
            target_book_id=self.trader_two_book.book_id,
            wash_book_id=self.wash_book.book_id,
            asset_id=self.asset.asset_id,
            quantity=100,
            price=Decimal('3.14'),
            currency='USD')
        self.assertEqual(deliver.quantity, 100)
        self.assertEqual(receive.quantity, 100)
        self.assertEqual(deliver.transaction_action, 'Deliver')
        self.assertEqual(receive.transaction_action, 'Receive')
        self.assertEqual(deliver.transaction_type, 'Transfer')
        self.assertEqual(deliver.transaction_type, 'Transfer')
        self.assertEqual(deliver.counterparty_book_id, self.wash_book.book_id)
        self.assertEqual(receive.counterparty_book_id, self.wash_book.book_id)
예제 #9
0
 def setUp(self):
     self.longMessage = True  # Print complete error message on failure
     self.maxDiff = None  # View the complete diff when there is a mismatch in a test
     self.interface = TransactionsInterface(environment=ENVIRONMENT)
     self.assets_interface = AssetsInterface(environment=ENVIRONMENT)
     self.books_interface = BooksInterface(environment=ENVIRONMENT)
     self.asset_manager_id = random.randint(1, 2**31 - 1)
     self.asset = generate_asset(asset_manager_id=self.asset_manager_id,
                                 fungible=True)
     self.trader_one_book = generate_book(
         asset_manager_id=self.asset_manager_id)
     self.trader_two_book = generate_book(
         asset_manager_id=self.asset_manager_id)
     self.wash_book = generate_book(asset_manager_id=self.asset_manager_id)
     self.setup_cache()
예제 #10
0
 def setUp(self):
     self.longMessage = True  # Print complete error message on failure
     self.maxDiff = None  # View the complete diff when there is a mismatch in a test
     self.asset_manager_id = random.randint(1, 2**31 - 1)
     self.assets_interface = AssetsInterface(environment=ENVIRONMENT)
     self.books_interface = BooksInterface(environment=ENVIRONMENT)
     self.asset = generate_asset(asset_manager_id=self.asset_manager_id)
     self.asset_book = generate_book(asset_manager_id=self.asset_manager_id)
     self.counterparty_book = generate_book(
         asset_manager_id=self.asset_manager_id)
     self.transaction = generate_transaction(
         asset_manager_id=self.asset_manager_id,
         asset_id=self.asset.asset_id,
         asset_book_id=self.asset_book.book_id,
         counterparty_book_id=self.counterparty_book.book_id)
     self.transaction_id = self.transaction.transaction_id
     self.setup_cache()
class ForeignExchangeTest(unittest.TestCase):

    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.asset_manager_id = random.randint(1, 2**31-1)
        self.fx_forward = generate_fx_forward(asset_manager_id=self.asset_manager_id)
        self.asset_id = self.fx_forward.asset_id
        self.assets_interface = AssetsInterface(environment=STAGE)

    def tearDown(self):
        pass

    def test_ForeignExchange(self):
        fx = generate_foreignexchange(asset_id='USDJPY')
        self.assertEqual(type(fx), ForeignExchange)
        self.assertEqual(self.fx_forward.pricing_method(), 'Derived')

    def test_ForeignExchangeForward(self):
        self.assertEqual(type(self.fx_forward), ForeignExchangeForward)

    def test_ForeignExchangeSpot(self):
        fx_spot = generate_fx_spot(asset_manager_id=self.asset_manager_id)
        self.assertEqual(type(fx_spot), ForeignExchangeSpot)

    def test_Persistence(self):
        self.assets_interface.new(self.fx_forward)
        fx_forward = self.assets_interface.retrieve(asset_manager_id=self.asset_manager_id, asset_id=self.asset_id)
        self.assertEqual(fx_forward, self.fx_forward)

    def test_rates(self):
        examples = [
            (('SGD', 'USD'),
             {'forward_rate': Decimal('1.354'), 'spot_reference': Decimal('1.356')},
             {'fwd_points': Decimal('-20')}),
            (('SGD', 'USD'),
             {'spot_reference': Decimal('1.356'), 'fwd_points': Decimal('-20')},
             {'forward_rate': Decimal('1.354')}),
            (('SGD', 'USD'),
             {'forward_rate': Decimal('1.354'), 'fwd_points': Decimal('-20')},
             {'spot_reference': Decimal('1.356')}),
        ]
        for a, kw, expect in examples:
            expect.update(kw)
            result = calculate_rates(*a, **kw)
            self.assertEqual(result, expect)
예제 #12
0
class ForeignExchangeTest(unittest.TestCase):

    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.asset_manager_id = random.randint(1, 2**31-1)
        self.fx_forward = generate_fx_forward(asset_manager_id=self.asset_manager_id)
        self.asset_id = self.fx_forward.asset_id
        self.assets_interface = AssetsInterface()

    def tearDown(self):
        pass

    def test_ForeignExchangeOption(self):
        self.assertEqual(type(self.fx_forward), ForeignExchangeForward)

    def test_Persistence(self):
        self.assets_interface.new(self.fx_forward)
        fx_forward = self.assets_interface.retrieve(asset_manager_id=self.asset_manager_id, asset_id=self.asset_id)
        self.assertEqual(fx_forward, self.fx_forward)
예제 #13
0
class BondTest(unittest.TestCase):
    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.asset_manager_id = random.randint(1, 2**31 - 1)
        self.bond = generate_bond(asset_manager_id=self.asset_manager_id)
        self.asset_id = self.bond.asset_id
        self.assets_interface = AssetsInterface(environment=ENVIRONMENT)

    def tearDown(self):
        pass

    def test_Bond(self):
        self.assertEqual(type(self.bond), BondGovernment)

    def test_Persistence(self):
        self.assets_interface.new(self.bond)
        bond = self.assets_interface.retrieve(
            asset_manager_id=self.asset_manager_id, asset_id=self.asset_id)
        self.assertEqual(bond, self.bond)
예제 #14
0
class FutureTest(unittest.TestCase):

    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.maxDiff = None
        self.asset_manager_id = random.randint(1, 2**31-1)
        self.future = generate_future(asset_manager_id=self.asset_manager_id)
        self.asset_id = self.future.asset_id
        self.assets_interface = AssetsInterface()

    def tearDown(self):
        pass

    def test_Future(self):
        self.assertEqual(type(self.future), Future)

    def test_Persistence(self):
        self.assets_interface.new(self.future)
        future = self.assets_interface.retrieve(asset_manager_id=self.asset_manager_id, asset_id=self.asset_id)
        self.assertEqual(future, self.future)
def main():
    logging.info("--- SETTING UP ---")
    asset_manager_id = random.randint(1, 2**31 - 1)
    assets_interface = AssetsInterface()

    logging.info("--- CREATING PIZZA ---")
    pizza = Pizza(asset_id='pizza1',
                  asset_manager_id=asset_manager_id,
                  size='Large',
                  toppings=['pineapple', 'corn', 'garlic'])
    logging.info("--- SENDING PIZZA TO AMAAS ---")
    pizza = assets_interface.new(pizza)
    logging.info("RETURNED PIZZA TYPE IS: %s", type(pizza).__name__)
    logging.info("--- CASTING CUSTOM_TYPE BACK TO PIZZA ---")
    asset_attrs = pizza.to_dict()
    client_additional = json.loads(pizza.client_additional)
    size = client_additional.get('size')
    toppings = client_additional.get('toppings')
    pizza = Pizza(size=size, toppings=toppings, **asset_attrs)
    logging.info("PIZZA TYPE IS: %s", type(pizza).__name__)
    logging.info("PIZZA TOPPINGS ARE: %s", ', '.join(pizza.toppings))
예제 #16
0
class ForeignExchangeOptionTest(unittest.TestCase):
    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.asset_manager_id = random.randint(1, 2**31 - 1)
        self.fx_option = generate_fx_option(
            asset_manager_id=self.asset_manager_id)
        self.asset_id = self.fx_option.asset_id
        self.assets_interface = AssetsInterface(environment=STAGE)

    def tearDown(self):
        pass

    def test_ForeignExchangeOption(self):
        self.assertEqual(type(self.fx_option), ForeignExchangeOption)

    def test_Optionality(self):
        self.assertEqual(type(self.fx_option.strike), Decimal)
        self.assertIn(self.fx_option.option_type, ['Put', 'Call'])

    def test_Persistence(self):
        self.assets_interface.new(self.fx_option)
        fx_option = self.assets_interface.retrieve(
            asset_manager_id=self.asset_manager_id, asset_id=self.asset_id)
        self.assertEqual(fx_option, self.fx_option)
예제 #17
0
class CustomAssetTest(unittest.TestCase):

    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        asset_manager_id = random.randint(1, 2**31-1)
        self.pizza = Pizza(asset_id='pizza1', asset_manager_id=asset_manager_id,
                           size='Large', toppings=['pineapple', 'corn', 'garlic'])
        self.assets_interface = AssetsInterface(environment=STAGE)

    def tearDown(self):
        pass

    def test_CustomAsset(self):
        pizza = self.assets_interface.new(self.pizza)
        self.assertEqual(type(pizza), CustomAsset)
        self.assertIsNotNone(pizza.client_additional)
예제 #18
0
def interface_direct_class(data_class):
    """help to direct to the correct interface interacting with DB by class name only"""
    if data_class in ASSET:
        interface = AssetsInterface()
    elif data_class in PARTY:
        interface = PartiesInterface()
    elif data_class in BOOK:
        interface = BooksInterface()
    elif data_class in CORPORATE_ACTION:
        interface = CorporateActionsInterface()
    elif data_class in MARKET_DATA:
        interface = MarketDataInterface()
    elif data_class in TRANSACTION:
        interface = TransactionsInterface()
    else:
        interface = AssetManagersInterface()
    return interface
예제 #19
0
class TransactionsInterfaceTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.transactions_interface = TransactionsInterface(
            environment=ENVIRONMENT)

    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.maxDiff = None  # View the complete diff when there is a mismatch in a test
        self.asset_manager_id = random.randint(1, 2**31 - 1)
        self.assets_interface = AssetsInterface(environment=ENVIRONMENT)
        self.books_interface = BooksInterface(environment=ENVIRONMENT)
        self.asset = generate_asset(asset_manager_id=self.asset_manager_id)
        self.asset_book = generate_book(asset_manager_id=self.asset_manager_id)
        self.counterparty_book = generate_book(
            asset_manager_id=self.asset_manager_id)
        self.transaction = generate_transaction(
            asset_manager_id=self.asset_manager_id,
            asset_id=self.asset.asset_id,
            asset_book_id=self.asset_book.book_id,
            counterparty_book_id=self.counterparty_book.book_id)
        self.transaction_id = self.transaction.transaction_id
        self.setup_cache()

    def tearDown(self):
        pass

    def test_New_MTM(self):
        mtm_result = generate_mtm_result()
        result = self.transactions_interface.new_mtm_result(
            mtm_results=mtm_result,
            asset_manager_id=mtm_result.asset_manager_id)
        self.assertEqual(mtm_result.asset_id, result[0].asset_id)
        self.assertEqual(mtm_result.mtm_value, result[0].mtm_value)

    def test_Amend_MTM(self):
        mtm_result = generate_mtm_result()
        result = self.transactions_interface.new_mtm_result(
            mtm_results=mtm_result,
            asset_manager_id=mtm_result.asset_manager_id)
        self.assertEqual(mtm_result.asset_id, result[0].asset_id)
        self.assertEqual(mtm_result.mtm_value, result[0].mtm_value)
        mtm_result.mtm_value = Decimal(random.random() * 10000)
        amended = self.transactions_interface.amend_mtm_result(
            mtm_results=mtm_result,
            asset_manager_id=mtm_result.asset_manager_id)
        self.assertEqual(amended[0].mtm_value, mtm_result.mtm_value)

    def test_Retrieve_MTM(self):
        mtm_result = generate_mtm_result(asset_manager_id=1)
        result = self.transactions_interface.new_mtm_result(
            mtm_results=mtm_result,
            asset_manager_id=mtm_result.asset_manager_id)
        self.assertEqual(mtm_result.asset_id, result[0].asset_id)
        self.assertEqual(mtm_result.mtm_value, result[0].mtm_value)
        parameters = {
            'book_id': mtm_result.book_id,
            'asset_id': mtm_result.asset_id
        }
        result = self.transactions_interface.retrieve_mtm_result(
            asset_manager_id=mtm_result.asset_manager_id,
            book_id=mtm_result.book_id,
            paramaters=parameters)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].asset_id, mtm_result.asset_id)

    def create_transaction_asset(self):
        self.assets_interface.upsert(self.asset)

    def setup_cache(self):
        self.create_transaction_asset()
        self.create_transaction_book(self.asset_book)
        self.create_transaction_book(self.counterparty_book)

    def create_transaction_book(self, book):
        self.books_interface.new(book)

    def test_New(self):
        self.assertIsNone(self.transaction.created_time)
        transaction = self.transactions_interface.new(self.transaction)
        # TODO - this should be populated by the New call.
        #self.assertIsNotNone(transaction.created_time)
        self.assertEqual(transaction.transaction_id, self.transaction_id)

    def test_Amend(self):
        transaction = self.transactions_interface.new(self.transaction)
        self.assertEqual(transaction.version, 1)
        new_settlement_date = transaction.settlement_date + datetime.timedelta(
            days=1)
        transaction.settlement_date = new_settlement_date
        transaction = self.transactions_interface.amend(transaction)
        self.assertEqual(transaction.settlement_date, new_settlement_date)
        self.assertEqual(transaction.version, 2)

    def test_Partial(self):
        self.transactions_interface.new(self.transaction)
        price = Decimal('3.14')
        updates = {'price': price}
        transaction = self.transactions_interface.partial(
            asset_manager_id=self.asset_manager_id,
            transaction_id=self.transaction_id,
            updates=updates)
        self.assertEqual(transaction.version, 2)
        self.assertEqual(transaction.price, price)

    def test_Retrieve(self):
        self.transactions_interface.new(self.transaction)
        transaction = self.transactions_interface.retrieve(
            self.transaction.asset_manager_id, self.transaction.transaction_id)
        self.assertEqual(type(transaction), Transaction)

    def test_Cancel(self):
        self.transactions_interface.new(self.transaction)
        self.transactions_interface.cancel(self.transaction.asset_manager_id,
                                           self.transaction.transaction_id)
        transaction = self.transactions_interface.retrieve(
            self.transaction.asset_manager_id, self.transaction.transaction_id)
        self.assertEqual(transaction.transaction_id, self.transaction_id)
        self.assertEqual(transaction.transaction_status, 'Cancelled')

    @requests_mock.Mocker()
    def test_Search(self, mocker):
        # This test is somewhat fake - but the integration tests are for the bigger picture
        endpoint = self.transactions_interface.endpoint + '/transactions'
        asset_manager_ids = [self.asset_manager_id, self.asset_manager_id + 1]
        transactions = generate_transactions(
            asset_manager_ids=asset_manager_ids)
        mocker.get(
            endpoint,
            json=[transaction.to_json() for transaction in transactions])
        all_transactions = self.transactions_interface.search(
            self.asset_manager_id)
        self.assertEqual(all_transactions, transactions)

    @requests_mock.Mocker()
    def test_TransactionsByAssetManager(self, mocker):
        # This test is somewhat fake - but the integration tests are for the bigger picture
        endpoint = '%s/transactions/%s' % (
            self.transactions_interface.endpoint, self.asset_manager_id)
        asset_manager_ids = [self.asset_manager_id]
        transactions = generate_transactions(
            asset_manager_ids=asset_manager_ids)
        mocker.get(
            endpoint,
            json=[transaction.to_json() for transaction in transactions])
        results = self.transactions_interface.transactions_by_asset_manager(
            asset_manager_id=self.asset_manager_id)
        self.assertEqual(results, transactions)

    @requests_mock.Mocker()
    def test_PositionSearch(self, mocker):
        # This test is somewhat fake - but the integration tests are for the bigger picture
        endpoint = '%s/positions' % self.transactions_interface.endpoint
        positions = generate_positions(
            asset_manager_ids=[self.asset_manager_id])
        mocker.get(endpoint,
                   json=[position.to_json() for position in positions])
        all_positions = self.transactions_interface.position_search(
            self.asset_manager_id)
        self.assertEqual(all_positions, positions)

    @requests_mock.Mocker()
    def test_PositionsByBook(self, mocker):
        # This test is somewhat fake - but the integration tests are for the bigger picture
        book_id = self.asset_book.book_id
        endpoint = '%s/positions/%s/%s' % (
            self.transactions_interface.endpoint, self.asset_manager_id,
            book_id)
        positions = generate_positions(
            asset_manager_ids=[self.asset_manager_id], book_ids=[book_id])
        mocker.get(endpoint,
                   json=[position.to_json() for position in positions])
        results = self.transactions_interface.positions_by_asset_manager_book(
            asset_manager_id=self.asset_manager_id, book_id=book_id)
        self.assertEqual(positions, results)

    @requests_mock.Mocker()
    def test_PositionsByAssetManager(self, mocker):
        # This test is somewhat fake - but the integration tests are for the bigger picture
        endpoint = '%s/positions/%s' % (self.transactions_interface.endpoint,
                                        self.asset_manager_id)
        positions = generate_positions(
            asset_manager_ids=[self.asset_manager_id])
        mocker.get(endpoint,
                   json=[position.to_json() for position in positions])
        results = self.transactions_interface.positions_by_asset_manager(
            asset_manager_id=self.asset_manager_id)
        self.assertEqual(positions, results)

    @requests_mock.Mocker()
    def test_PositionsByAssetManagerWithBooks(self, mocker):
        # This test is somewhat fake - but the integration tests are for the bigger picture
        endpoint = '%s/positions/%s' % (self.transactions_interface.endpoint,
                                        self.asset_manager_id)
        positions = [
            generate_position(asset_manager_id=self.asset_manager_id,
                              book_id='TEST')
        ]
        mocker.get(endpoint,
                   json=[position.to_json() for position in positions])
        results = self.transactions_interface.positions_by_asset_manager(
            asset_manager_id=self.asset_manager_id, book_ids=['TEST'])
        self.assertEqual(positions, results)

    def test_MultipleLink(self):
        transaction = self.transactions_interface.new(self.transaction)
        links = transaction.links.get('Multiple')
        self.assertEqual(len(links), 3)  # The test script inserts 3 links
        # Add a link
        random_id = random_string(8)
        transaction.add_link('Multiple', linked_transaction_id=random_id)
        transaction = self.transactions_interface.amend(transaction)
        self.assertEqual(len(transaction.links.get('Multiple')), 4)
        transaction.remove_link('Multiple', linked_transaction_id=random_id)
        transaction = self.transactions_interface.amend(transaction)
        self.assertEqual(len(transaction.links.get('Multiple')), 3)

    def test_ChildrenPopulated(self):
        transaction = self.transactions_interface.new(self.transaction)
        retrieved_transaction = self.transactions_interface.retrieve(
            asset_manager_id=self.asset_manager_id,
            transaction_id=self.transaction_id)
        self.assertGreater(len(transaction.charges), 0)
        self.assertGreater(len(transaction.codes), 0)
        self.assertGreater(len(transaction.comments), 0)
        self.assertGreater(len(transaction.links), 0)
        self.assertGreater(len(transaction.parties), 0)
        self.assertGreater(len(transaction.references), 0)
        self.assertEqual(transaction.charges, retrieved_transaction.charges)
        self.assertEqual(transaction.codes, retrieved_transaction.codes)
        self.assertEqual(transaction.comments, retrieved_transaction.comments)
        self.assertEqual(transaction.links, retrieved_transaction.links)
        self.assertEqual(transaction.parties, retrieved_transaction.parties)
        self.assertEqual(transaction.references,
                         retrieved_transaction.references)

    def test_Unicode(self):
        unicode_comment = '日本語入力'
        self.transaction.comments['Unicode'] = Comment(
            comment_value=unicode_comment)
        transaction = self.transactions_interface.new(self.transaction)
        self.assertEqual(
            transaction.comments.get('Unicode').comment_value, unicode_comment)

    def test_Clear(self):
        self.transactions_interface.new(self.transaction)
        count = self.transactions_interface.clear(self.asset_manager_id)
        self.assertEqual(count['transaction_count'], 1)
        self.assertGreater(count['position_count'], 0)
        results = self.transactions_interface.search(
            asset_manager_id=self.asset_manager_id)
        self.assertEqual(len(results), 0)

    def test_CashTransaction(self):
        transaction = generate_cash_transaction(
            asset_manager_id=self.asset_manager_id,
            asset_id='JPY',
            asset_book_id=self.asset_book.book_id,
            counterparty_book_id=self.counterparty_book.book_id)
        self.transactions_interface.new(transaction)
        transaction = self.transactions_interface.retrieve(
            transaction.asset_manager_id, transaction.transaction_id)
        self.assertEqual(type(transaction), CashTransaction)
예제 #20
0
 def setUp(self):
     self.longMessage = True  # Print complete error message on failure
     self.asset_manager_id = random.randint(1, 2**31-1)
     self.bond = generate_bond(asset_manager_id=self.asset_manager_id)
     self.asset_id = self.bond.asset_id
     self.assets_interface = AssetsInterface()
예제 #21
0
class AllocationTest(unittest.TestCase):
    def setUp(self):
        self.longMessage = True  # Print complete error message on failure
        self.maxDiff = None  # View the complete diff when there is a mismatch in a test
        self.transactions_interface = TransactionsInterface(environment=STAGE)
        self.assets_interface = AssetsInterface(environment=STAGE)
        self.books_interface = BooksInterface(environment=STAGE)
        self.asset_manager_id = 1
        self.asset = generate_asset(asset_manager_id=self.asset_manager_id)
        self.asset_book = generate_book(asset_manager_id=self.asset_manager_id)
        self.counterparty_book = generate_book(
            asset_manager_id=self.asset_manager_id)
        self.transaction = generate_transaction(
            asset_manager_id=self.asset_manager_id,
            asset_id=self.asset.asset_id,
            asset_book_id=self.asset_book.book_id,
            counterparty_book_id=self.counterparty_book.book_id)
        self.transaction_id = self.transaction.transaction_id
        self.setup_cache()

    def tearDown(self):
        pass

    def create_transaction_asset(self):
        self.assets_interface.upsert(asset)

    def setup_cache(self):
        self.create_transaction_asset()
        self.create_transaction_book(self.asset_book)
        self.create_transaction_book(self.counterparty_book)

    def create_transaction_book(self, book):
        self.books_interface.new(book)

    def test_Allocations(self):
        transaction = generate_transaction(
            asset_manager_id=self.asset_manager_id,
            asset_id=self.asset.asset_id,
            asset_book_id=self.asset_book.book_id,
            counterparty_book_id=self.counterparty_book.book_id,
            quantity=Decimal('100'))
        transaction.charges['TEST'] = Charge(charge_value=Decimal('10'),
                                             currency='SGD')
        self.transactions_interface.new(transaction)
        allocation_dicts = [{
            'book_id': 'ABC',
            'quantity': Decimal('40')
        }, {
            'book_id': 'XYZ',
            'quantity': Decimal('60')
        }]
        abc_book = generate_book(asset_manager_id=self.asset_manager_id,
                                 book_id='ABC')
        xyz_book = generate_book(asset_manager_id=self.asset_manager_id,
                                 book_id='XYZ')
        self.create_transaction_book(abc_book)
        self.create_transaction_book(xyz_book)
        allocations = self.transactions_interface.allocate_transaction(
            asset_manager_id=self.asset_manager_id,
            transaction_id=transaction.transaction_id,
            allocation_type='asset_manager',
            allocation_dicts=allocation_dicts)
        self.assertEqual(len(allocations), 2)
        book_ids = sorted(
            [allocation.asset_book_id for allocation in allocations])
        self.assertEqual(book_ids, ['ABC', 'XYZ'])
        quantities = sorted(
            [allocation.quantity for allocation in allocations])
        self.assertEqual(quantities, [Decimal('40'), Decimal('60')])
        charges = sorted([
            allocation.charges.get('TEST').charge_value
            for allocation in allocations
        ])
        self.assertEqual(charges, [Decimal('4'), Decimal('6')])

    def test_AllocationWithExplictID(self):
        transaction = generate_transaction(
            asset_manager_id=self.asset_manager_id,
            asset_id=self.asset.asset_id,
            asset_book_id=self.asset_book.book_id,
            counterparty_book_id=self.counterparty_book.book_id,
            quantity=Decimal('100'))
        self.transactions_interface.new(transaction)
        partial_tran_id = transaction.transaction_id[:10]
        allocation_dicts = [{
            'book_id': 'ABC',
            'quantity': Decimal('60'),
            'transaction_id': partial_tran_id + '_ABC'
        }, {
            'book_id': 'XYZ',
            'quantity': Decimal('40'),
            'transaction_id': partial_tran_id + '_XYZ'
        }]
        abc_book = generate_book(asset_manager_id=self.asset_manager_id,
                                 book_id='ABC')
        xyz_book = generate_book(asset_manager_id=self.asset_manager_id,
                                 book_id='XYZ')
        self.create_transaction_book(abc_book)
        self.create_transaction_book(xyz_book)
        allocations = self.transactions_interface.allocate_transaction(
            asset_manager_id=self.asset_manager_id,
            transaction_id=transaction.transaction_id,
            allocation_type='counterparty',
            allocation_dicts=allocation_dicts)
        self.assertEqual(len(allocations), 2)
        book_ids = sorted(
            [allocation.counterparty_book_id for allocation in allocations])
        self.assertEqual(book_ids, ['ABC', 'XYZ'])
        quantities = sorted(
            [allocation.quantity for allocation in allocations])
        self.assertEqual(quantities, [Decimal('40'), Decimal('60')])
        transaction_ids = sorted(
            [allocation.transaction_id for allocation in allocations])
        self.assertEqual(transaction_ids,
                         [partial_tran_id + '_ABC', partial_tran_id + '_XYZ'])

    def test_RetrieveTransactionAllocations(self):
        pass
예제 #22
0
 def setUpClass(cls):
     cls.assets_interface = AssetsInterface(environment=ENVIRONMENT)
예제 #23
0
def _import_transaction(asset_manager_id, rowdata):
    charge_columns = [
        c for c in rowdata.keys() if c.startswith(CHARGES_PREFIX)
    ]
    charges = {
        column.replace(CHARGES_PREFIX, ''): rowdata.pop(column)
        for column in charge_columns if rowdata.get(column)
    }
    party_columns = [c for c in rowdata.keys() if c.startswith(PARTIES_PREFIX)]
    parties = {
        column.replace(PARTIES_PREFIX, ''): rowdata.pop(column)
        for column in party_columns if rowdata.get(column)
    }
    rate_columns = [c for c in rowdata.keys() if c.startswith(RATES_PREFIX)]
    rates = {
        column.replace(RATES_PREFIX, ''): rowdata.pop(column)
        for column in rate_columns if rowdata.get(column)
    }
    rowdata = {to_snake_case(key): value for key, value in rowdata.items()}

    asset_type = rowdata.pop('asset_type')
    if not asset_type:
        return
    asset_id = rowdata['asset_id']
    settlement_date = parse(rowdata['settlement_date'])
    if asset_type in ['ForeignExchangeSpot', 'ForeignExchangeForward']:
        underlying = asset_id
        # this should be handled by our SDK ideally
        prefix, model = ('SPT', ForeignExchangeSpot) \
                         if asset_type == 'ForeignExchangeSpot' \
                         else ('FWD', ForeignExchangeForward)
        asset_id = f'{prefix}{asset_id}{settlement_date.strftime("%Y%m%d")}'
        rowdata['asset_id'] = asset_id
        params = {
            'asset_manager_id': asset_manager_id,
            'asset_id': asset_id,
            'underlying': underlying,
            'settlement_date': rowdata['settlement_date'],
            'currency': rowdata['transaction_currency']
        }
        if asset_type == 'ForeignExchangeForward':
            params['fixing_date'] = rowdata.get('fixing_date')
            params['forward_rate'] = rowdata['price']
        asset = model(**params)
        asset.references['CCY Pair'] = Reference(underlying,
                                                 reference_primary=True)
        asset_api = AssetsInterface(environment=ENVIRONMENT)
        existing_asset = asset_api.search(asset_manager_id=asset_manager_id,
                                          asset_ids=[asset_id])
        asset = asset_api.new(asset) if not existing_asset \
                                     else asset_api.amend(asset)

    transaction = Transaction(**rowdata)
    transaction_api = TransactionsInterface(environment=ENVIRONMENT)
    existing_transaction = transaction_api.search(
        asset_manager_id=asset_manager_id,
        transaction_ids=[rowdata['transaction_id']])

    for party_type, party_id in parties.items():
        transaction.parties[party_type] = TransactionParty(party_id)
    for charge_type, charge_value in charges.items():
        transaction.charges[charge_type] = Charge(
            charge_value, rowdata['transaction_currency'])
    for rate_type, rate_value in rates.items():
        transaction.rates[rate_type] = Rate(rate_value)
    if not existing_transaction:
        transaction_api.new(transaction)
    else:
        transaction_api.amend(transaction)

    return transaction
예제 #24
0
 def setUpClass(cls):
     cls.assets_interface = AssetsInterface()
예제 #25
0
 def setUpClass(cls):
     cls.assets_interface = AssetsInterface(environment=STAGE)
from amaascore.config import DEFAULT_LOGGING
from amaascore.assets.bond import BondGovernment
from amaascore.assets.interface import AssetsInterface
from amaascore.books.book import Book
from amaascore.books.interface import BooksInterface
from amaascore.core.reference import Reference
from amaascore.parties.broker import Broker
from amaascore.parties.company import Company
from amaascore.parties.interface import PartiesInterface
from amaascore.transactions.interface import TransactionsInterface
from amaascore.transactions.transaction import Transaction

logging.config.dictConfig(DEFAULT_LOGGING)

# Create the interfaces
assets_interface = AssetsInterface()
books_interface = BooksInterface()
parties_interface = PartiesInterface()
transaction_interface = TransactionsInterface()


def create_jgb(asset_manager_id):
    jgb_symbol = 'JB355'
    jgb_references = {
        'ISIN': Reference(reference_value='JP1234567890'),
        'Ticker': Reference(reference_value=jgb_symbol)
    }

    jgb = BondGovernment(asset_manager_id=asset_manager_id,
                         asset_id=jgb_symbol,
                         currency='JPY',
예제 #27
0
 def setUpClass(cls):
     cls.assets_interface = AssetsInterface(environment='local')