Esempio n. 1
0
    def testAll(self):
        """
		Add 7 repo positions, 3 of type OPEN, 4 of type fixed term.
		Then close 1, cancel 1, both of type OPEN.
		"""
        clearRepoData()
        self.assertEqual(
            7,
            saveRepoMasterFileToDB(
                join(getCurrentDir(), 'samples',
                     'RepoMaster_20210305_20210305154653.xml')))

        self.assertEqual(
            9,
            saveRepoTradeFileToDB(
                join(getCurrentDir(), 'samples',
                     'RepoTrade_20210305_20210305155621.xml')))

        data = getRepo()
        self.assertEqual(6, len(data))  # 6 not canceled
        self.verifyHKDRepoPosition(data)
        self.verifyUSDRepoPosition(data)
        self.verifyClosedRepoPosition(data)

        # there is one repo canceled
        data = getRepo(status='canceled')
        self.assertEqual(1, len(data))
        self.verifyCanceledRepoPosition(data)

        # Add 2 rerate actions to a repo position 316444
        self.assertEqual(
            2,
            saveRepoRerateFileToDB(
                join(getCurrentDir(), 'samples',
                     'Repo_ReRate_20210305_20210305174826.xml')))

        # add the same file again, 2 more rerate actions added
        self.assertEqual(
            2,
            saveRepoRerateFileToDB(
                join(getCurrentDir(), 'samples',
                     'Repo_ReRate_20210305_20210305174826.xml')))

        self.assertEqual(
            4,
            len(
                list(
                    filter(lambda el: el['Action'] == 'rerate',
                           getRepoTransactionHistory('316444')))))
Esempio n. 2
0
def search_ticket_db(ticket):
	"""
	Search for a ticket# from the database.
	"""
	result = list(filter( lambda p: p['TransactionId'] == ticket
						, getRepo(status='all')))
	if result == []:
		print('ticket {0} not found'.format(ticket))
	elif len(result) > 1:
		print('multiple entries for ticket {0} found'.format(ticket))
	else:
		print(result[0])
Esempio n. 3
0
    def testAddRepoTransaction(self):
        """
		repo master is in testAddRepoMaster()

		Add 2 trades, cancel 1
		"""
        inputFile = join(getCurrentDir(), 'samples',
                         'RepoTrade_20210218_20210218121202.xml')
        self.assertEqual(3, saveRepoTradeFileToDB(inputFile))

        data = getRepo()  # open or closed repo
        self.assertEqual(1, len(data))
        repoName, userTranId = data[0]['RepoName'], data[0]['TransactionId']
        self.assertEqual([userTranId], getUserTranIdsFromRepoName(repoName))

        data = getRepo(status='canceled')
        self.assertEqual(1, len(data))
        self.assertEqual('310952', data[0]['TransactionId'])

        # add the file again, only the cancel record is saved.
        self.assertEqual(1, saveRepoTradeFileToDB(inputFile))

        history = getRepoTransactionHistory('310955')
        self.assertEqual(1, len(history))
        self.assertEqual('open', history[0]['Action'])
        self.assertEqual(0.65, history[0]['InterestRate'])

        # open, cancel, cancel
        history = getRepoTransactionHistory('310952')
        self.assertEqual(3, len(history))
        self.assertEqual('open', history[0]['Action'])
        self.assertEqual(0.75, history[0]['InterestRate'])
        self.assertEqual('2021-01-25', history[0]['Date'])

        self.assertEqual('cancel', history[1]['Action'])
        self.assertEqual('cancel', history[2]['Action'])
Esempio n. 4
0
def getRepoData():
	"""
	[Dictionary] ([String] repo name -> 
				  [Iterable] ( [String] collateral Id
				  			 , [Float] quantity
				  			 , [Float] share of total loan amount
				  			 , [String] settle date
				  			 )
				 )

	"""
	logger.debug('getRepoData()')

	getCollateralInfo = compose(
		lambda t: \
			map( lambda collateral_t: ( collateral_t[0]
									  , collateral_t[1]
									  , collateral_t[2]/t[1]
									  , collateral_t[3]
									  ) 
			   , t[0]
			   )
	  , lambda L: (L, sum(map(lambda t: t[2], L)))
	  , list
	  , partial( map
	  		   , lambda p: ( p['CollateralID']
	  		   			   , p['Quantity']
	  		   			   , p['CollateralValue']
	  		   			   , p['SettleDate']
	  		   			   )
	  		   )
	)

	return \
	compose(
		partial(valmap, getCollateralInfo)
	  , partial(groupbyToolz, lambda p: p['RepoName'])
	)(getRepo())
Esempio n. 5
0
    def testGetRepo(self):
        #-- preprocess: add 2 repo master and 6 transaction
        master1 = self._get_test_repo_master()
        addRepoMaster(master1)
        master2 = {
            "Code": "MMRPE420BS-2",
            "BifurcationCurrency": "USD",
            "AccrualDaysPerMonth": "Actual",
            "AccrualDaysPerYear": "180"
        }
        addRepoMaster(master2)
        transaction1 = {
            "TransactionType": "ReverseRepo_InsertUpdate",
            "UserTranId1": "300734",
            "Portfolio": "12734",
            "LocationAccount": "BOCHK",
            "Investment": "Isin=XS1234567890",
            "EventDate": "2020-12-31T00:00:00",
            "SettleDate": "2020-12-31T00:00:00",
            "OpenEnded": "CALC",
            "ActualSettleDate": "CALC",
            "Quantity": "300000",
            "CounterInvestment": "USD",
            "Price": "95.23",
            "NetCounterAmount": "1818234",
            "RepoName": "MMRPE420BS",
            "Coupon": "0.95",
            "LoanAmount": "1818234",
            "Broker": "BNP-REPO"
        }
        addRepoTransaction(transaction1)
        transaction2 = {
            "TransactionType": "ReverseRepo_InsertUpdate",
            "UserTranId1": "300735",
            "Portfolio": "12734",
            "LocationAccount": "BOCHK",
            "Investment": "Isin=XS1234567890",
            "EventDate": "2020-12-31T00:00:00",
            "SettleDate": "2020-12-31T00:00:00",
            "OpenEnded": "CALC",
            "ActualSettleDate": "2020-03-08T00:00:00",
            "Quantity": "300000",
            "CounterInvestment": "USD",
            "Price": "95.23",
            "NetCounterAmount": "1818234",
            "RepoName": "MMRPE420BS",
            "Coupon": "0.95",
            "LoanAmount": "1818234",
            "Broker": "BOC-REPO"
        }
        addRepoTransaction(transaction2)
        transaction3 = {
            "TransactionType": "Repo_InsertUpdate",
            "UserTranId1": "300736",
            "Portfolio": "12736",
            "LocationAccount": "BOCHK",
            "Investment": "Isin=XS1234567890",
            "EventDate": "2020-12-31T00:00:00",
            "SettleDate": "2020-12-31T00:00:00",
            "ActualSettleDate": "CALC",
            "Quantity": "300000",
            "CounterInvestment": "USD",
            "Price": "95.23",
            "NetCounterAmount": "1818234",
            "RepoName": "MMRPE420BS-2",
            "Coupon": "0.95",
            "LoanAmount": "1818234",
            "Broker": "BNP-REPO"
        }
        addRepoTransaction(transaction3)
        transaction4 = {
            "TransactionType": "Repo_InsertUpdate",
            "UserTranId1": "300737",
            "Portfolio": "中文",
            "LocationAccount": "中國銀行",
            "Investment": "Isin=XS1234567890",
            "EventDate": "2020-12-31T00:00:00",
            "SettleDate": "2020-12-31T00:00:00",
            "ActualSettleDate": "2020-03-08T00:00:00",
            "Quantity": "300000",
            "CounterInvestment": "USD",
            "Price": "95.23",
            "NetCounterAmount": "1818234",
            "RepoName": "MMRPE420BS-2",
            "Coupon": "0.95",
            "LoanAmount": "1818234",
            "Broker": "BOC-REPO"
        }
        addRepoTransaction(transaction4)
        transaction5 = {
            "TransactionType": "Repo_InsertUpdate",
            "UserTranId1": "300738",
            "Portfolio": "12734",
            "LocationAccount": "BOCHK",
            "Investment": "Isin=XS1234567890",
            "EventDate": "2020-12-31T00:00:00",
            "SettleDate": "2020-12-31T00:00:00",
            "ActualSettleDate": "2020-03-10T00:00:00",
            "Quantity": "300000",
            "CounterInvestment": "USD",
            "Price": "95.23",
            "NetCounterAmount": "1818234",
            "RepoName": "MMRPE420BS-2",
            "Coupon": "0.95",
            "LoanAmount": "1818234",
            "Broker": "BOC-REPO"
        }
        addRepoTransaction(transaction5)
        #-- preprocess: create 1 canceled and 1 closed transaction
        cancel_transaction = {"UserTranId1": "300738"}
        cancelRepoTransaction(cancel_transaction)
        transaction6 = {
            "TransactionType": "Repo_InsertUpdate",
            "UserTranId1": "300739",
            "Portfolio": "12734",
            "LocationAccount": "BOCHK",
            "Investment": "Isin=XS1234567890",
            "EventDate": "2020-12-31T00:00:00",
            "SettleDate": "2020-12-31T00:00:00",
            "ActualSettleDate": "2020-03-10T00:00:00",
            "Quantity": "300000",
            "CounterInvestment": "USD",
            "Price": "95.23",
            "NetCounterAmount": "1818234",
            "RepoName": "MMRPE420BS-2",
            "Coupon": "0.95",
            "LoanAmount": "1818234",
            "Broker": "BOC-REPO"
        }
        addRepoTransaction(transaction6)
        close_transaction = {
            "UserTranId1": "300739",
            "ActualSettleDate": "2018-08-31T00:00:00"
        }
        closeRepoTransaction(close_transaction)
        #-- 1. invalid value
        #-- 1.1 wrong status
        with self.assertRaises(ValueError):
            getRepo(status='open',
                    portfolio='all',
                    custodian='all',
                    repoName='all',
                    broker='all',
                    hasHairCut='all')
        #-- 1.2 wrong hasHairCut
        with self.assertRaises(ValueError):
            getRepo(hasHairCut='0')
        #-- 2. normal query status
        #-- 2.1 status: openclose
        res = getRepo()
        with DBConn.get_db(self.unittest_dbmode).connect() as con:
            count = con.execute("""
								SELECT count(transaction_id)  
								FROM repo_transactions
								where 
								(status='open' or status='closed')
								""").scalar()
            con.close()
        self.assertEqual(count, len(res))
        #-- 2.2 status: all parameters inputted with default value
        res = getRepo(status='openclose',
                      portfolio='all',
                      custodian='all',
                      repoName='all',
                      broker='all',
                      hasHairCut='all')
        with DBConn.get_db(self.unittest_dbmode).connect() as con:
            count = con.execute("""
								SELECT count(transaction_id)  
								FROM repo_transactions
								where 
								(status='open' or status='closed')
								""").scalar()
            con.close()
        self.assertEqual(count, len(res))
        #-- 2.3 status: cancel
        res = getRepo(status='canceled',
                      portfolio='all',
                      custodian='all',
                      repoName='all',
                      broker='all',
                      hasHairCut='all')
        with DBConn.get_db(self.unittest_dbmode).connect() as con:
            count = con.execute("""
								SELECT count(transaction_id)  
								FROM repo_transactions
								where 
								(status='canceled')
								""").scalar()
            con.close()
        #-- 2.4 status: all
        res = getRepo(status='all')
        with DBConn.get_db(self.unittest_dbmode).connect() as con:
            count = con.execute("""
								SELECT count(transaction_id)  
								FROM repo_transactions
								""").scalar()
            con.close()
        self.assertEqual(count, len(res))
        #-- 3. normal query portfolio
        #-- 3.1 porfolio: english
        res = getRepo(portfolio='12734')
        with DBConn.get_db(self.unittest_dbmode).connect() as con:
            count = con.execute("""
								SELECT count(transaction_id)  
								FROM repo_transactions
								where 
								(status='open' or status='closed')
								and portfolio='12734'
								""").scalar()
            con.close()
        self.assertEqual(count, len(res))
        #-- 3.2 porfolio: 中文
        res = getRepo(status='all', portfolio='中文')
        self.assertEqual(1, len(res))
        #-- 4. normal query portfolio
        #-- 4.1 custodian: 中國銀行
        res = getRepo(status='all', custodian='中國銀行')
        self.assertEqual(1, len(res))
        #-- 4.2 custodian: english
        res = getRepo(custodian='BOCHK')
        self.assertEqual(4, len(res))
        #-- 4. normal query repoName
        #-- 5.1 repoName: normal value (search status default input 'open')
        res = getRepo(repoName='MMRPE420BS-2')
        self.assertEqual(3, len(res))
        #-- 5.2 repoName: empty result
        res = getRepo(repoName='--10--')
        self.assertEqual(0, len(res))
        #-- 6. normal query repoName
        #-- 6.1 repoName: normal value
        res = getRepo(broker='BOC-REPO')
        self.assertEqual(3, len(res))
        #-- 6.2 repoName: normal and status cancel
        res = getRepo(status='openclose', broker='BOC-REPO')
        with DBConn.get_db(self.unittest_dbmode).connect() as con:
            count = con.execute("""
								SELECT count(transaction_id)
								FROM repo_transactions
								where 
								(status='open' or status='closed')
								and broker='BOC-REPO'
								""").scalar()
            con.close()
        self.assertEqual(count, len(res))
        #-- 6.3 repoName: empty result
        res = getRepo(broker='--REPO--')
        self.assertEqual(0, len(res))
        #-- 7. normal query repoName
        #-- 7.1 hasHairCut: true
        res = getRepo(hasHairCut='True')
        self.assertEqual(0, len(res))
        #-- 7.2 hasHairCut: false and status close
        res = getRepo(status='openclose', hasHairCut='False')
        with DBConn.get_db(self.unittest_dbmode).connect() as con:
            count = con.execute("""
								SELECT count(transaction_id)
								FROM repo_transactions
								where 
								(status='open' or status='closed')
								and haircut = 0
								""").scalar()
            con.close()
        self.assertEqual(count, len(res))