def testGetTaxlotInterestIncome(self): file = join(getCurrentDirectory(), 'samples', 'daily interest 2020-01.txt') positions, _ = readDailyInterestAccrualDetailTxtReport('utf-16', '\t', file) d = getTaxlotInterestIncome(positions) self.assertAlmostEqual(240897.71, d['1104216']) self.assertAlmostEqual(145956.02, d['1109831'])
def testGetRealizedGainLoss(self): file = join(getCurrentDirectory(), 'samples', 'profit loss summary tax lot 2020-01.txt') self.assertAlmostEqual( 5011429.88, compose( lambda d: sum(d.values()), getRealizedGainLoss, lambda t: t[0], partial(readProfitLossSummaryWithTaxLotTxtReport, 'utf-16', '\t'))(file), 2)
def testGetTimeWeightedCapital(self): file = join(getCurrentDirectory(), 'samples', 'cash ledger 2020-01.txt') positions, metadata = readCashLedgerTxtReport('utf-16', '\t', file) self.assertAlmostEqual( 163922587.75, getTimeWeightedCapital(metadata['PeriodEndDate'], list(positions)), 2) file = join(getCurrentDirectory(), 'samples', 'cash ledger 2020-03.txt') positions, metadata = readCashLedgerTxtReport('utf-16', '\t', file) self.assertAlmostEqual( 488290764.18, getTimeWeightedCapital(metadata['PeriodEndDate'], list(positions)), 2)
def testGetFairValueChange(self): file = join(getCurrentDirectory(), 'samples', 'profit loss summary tax lot 2020-01.txt') self.assertAlmostEqual( 8071778.13, compose( lambda d: sum(d.values()), getFairValueChange, lambda t: t[0], partial(readProfitLossSummaryWithTaxLotTxtReport, 'utf-16', '\t'))(file), 2)
def testReadTaxlotTxtReport(self): inputFile = join(getCurrentDirectory(), 'samples', 'taxlot01_text_unicode.txt') positions, metaData = readTaxlotTxtReport('utf-16', '\t', inputFile) self.verifyMetaData(metaData) positions = list(positions) self.assertEqual(38, len(positions)) self.verifyTaxlotPosition1(positions[0]) self.verifyTaxlotPosition2(positions[13])
def testReadInvestmentTxtReport(self): inputFile = join(getCurrentDirectory(), 'samples', 'investment 2020-03.txt') positions, metaData = readInvestmentTxtReport('utf-16', '\t', inputFile) self.verifyMetaData2(metaData) positions = list(positions) self.assertEqual(174, len(positions)) self.verifyInvestmentPosition(positions[10])
def testReadProfitLossTxtReport(self): inputFile = join(getCurrentDirectory(), 'samples', 'profit loss 2020-01.txt') positions, metaData = readProfitLossTxtReport('utf-16', '\t', inputFile) self.verifyMetaData3(metaData) positions = list(positions) self.assertEqual(133, len(positions)) self.verifyProfitLossPosition(positions[0])
def testReadCashLedgerTxtReport(self): inputFile = join(getCurrentDirectory(), 'samples', 'cash ledger 2020-01.txt') positions, metaData = readCashLedgerTxtReport('utf-16', '\t', inputFile) self.verifyMetaData3(metaData) positions = list(positions) self.assertEqual(66, len(positions)) self.verifyCashLedgerPosition(positions[0])
def testReadDailyInterestAccrualDetailTxtReport(self): inputFile = join(getCurrentDirectory(), 'samples', 'daily interest 2020-01.txt') positions, metaData = readDailyInterestAccrualDetailTxtReport( 'utf-16', '\t', inputFile) self.verifyMetaData3(metaData) positions = list(positions) self.assertEqual(19641, len(positions)) self.verifyDailyTaxlotAccrualDetailPosition(positions[2])
def testGetAccumulatedRealizedGainLoss(self): files = [ join(getCurrentDirectory(), 'samples', 'profit loss summary tax lot 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'profit loss summary tax lot 2020-02.txt') ] values = compose( list, partial(map, lambda d: sum(d.values())), partial(getAccumulatedRealizedGainLoss, None), partial(map, lambda t: t[0]), partial( map, partial(readProfitLossSummaryWithTaxLotTxtReport, 'utf-16', '\t')))(files) self.assertEqual(2, len(values)) self.assertAlmostEqual(5011429.88, values[0]) self.assertAlmostEqual(5011429.88 + 8483828.71, values[1])
def testGetAccumulatedTimeWeightedCapital(self): files = [ join(getCurrentDirectory(), 'samples', 'cash ledger 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'cash ledger 2020-02.txt'), join(getCurrentDirectory(), 'samples', 'cash ledger 2020-03.txt') ] sortedCLPositions = compose( partial(sorted, key=lambda t: t[0]), partial(map, lambda t: (t[1]['PeriodEndDate'], list(t[0]))), partial(map, partial(readCashLedgerTxtReport, 'utf-16', '\t')))(files) self.assertEqual('2020-01-31', sortedCLPositions[0][0]) self.assertEqual('2020-02-29', sortedCLPositions[1][0]) L = list(getAccumulatedTimeWeightedCapital(False, sortedCLPositions)) self.assertEqual(3, len(L)) self.assertAlmostEqual(163922587.75, L[0], 2) self.assertAlmostEqual(556735459.34, L[1], 2) self.assertAlmostEqual(1509419832.61, L[2], 2)
def testGetAccumulatedFairValueChange(self): files = [ join(getCurrentDirectory(), 'samples', 'profit loss summary tax lot 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'profit loss summary tax lot 2020-02.txt') ] values = compose( list, partial(map, lambda d: sum(d.values())), partial(getAccumulatedFairValueChange, None), partial(map, lambda t: t[0]), partial( map, partial(readProfitLossSummaryWithTaxLotTxtReport, 'utf-16', '\t')))(files) self.assertEqual(2, len(values)) self.assertAlmostEqual(8071778.13, values[0]) self.assertAlmostEqual(8071778.13 - 6700947.29, values[1])
def testReadProfitLossSummaryWithTaxLotTxtReport(self): inputFile = join(getCurrentDirectory(), 'samples', 'profit loss summary tax lot 2020-01.txt') positions, metaData = readProfitLossSummaryWithTaxLotTxtReport( 'utf-16', '\t', inputFile) self.verifyMetaData3(metaData) positions = list(positions) self.assertEqual(713, len(positions)) self.assertEqual('', positions[0]['TaxLotId']) self.assertEqual('CNY: Chinese Renminbi Yuan', positions[0]['Invest']) self.verifyProfitLossTaxLotPosition(positions[3])
def testReadMultipartInvestmentTxtReport(self): inputFile = join(getCurrentDirectory(), 'samples', '12xxx investment multipart.txt') result = list( readMultipartInvestmentTxtReport('utf-16', '\t', inputFile)) self.assertEqual(6, len(result)) self.assertEqual( ['12229', '12366', '12549', '12550', '12630', '12734'], list(map(lambda t: t[1]['Portfolio'], result))) self.verifyInvestmentPosition2(list(result[0][0])[0]) self.assertEqual(0, len(list(result[3][0]))) self.verifyInvestmentPosition3(list(result[5][0])[-1])
def testResultFromFilesError1(self): investmentFiles = [ join(getCurrentDirectory(), 'samples', 'investment positions 2020-06.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-07.txt') ] profitLossFiles = [ join(getCurrentDirectory(), 'samples', 'profit loss 2020-06.txt') ] try: getResultFromFiles(investmentFiles, profitLossFiles, 177801674041.66, 177800934590.20, 3212689500.00, 7) except ValueError: # expected: inconsistent number of files pass else: self.fail('error should occur')
def testReadMultipartProfitLossSummaryWithTaxLotTxtReport(self): inputFile = join(getCurrentDirectory(), 'samples', '12xxx multipart profit loss summary tax lot.txt') result = list( readMultipartProfitLossSummaryWithTaxLotTxtReport( 'utf-16', '\t', inputFile)) self.assertEqual(6, len(result)) self.assertEqual( ['12229', '12366', '12549', '12550', '12630', '12734'], list(map(lambda t: t[1]['Portfolio'], result))) self.verifyProfitlossSummaryTaxlotPosition(list(result[0][0])[0]) self.assertEqual(0, len(list(result[3][0]))) self.verifyProfitlossSummaryTaxlotPosition2(list(result[5][0])[-1])
def testResultFromFilesError5(self): investmentFiles = [ join(getCurrentDirectory(), 'samples', '12229 investment positions 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-02.txt') ] profitLossFiles = [ join(getCurrentDirectory(), 'samples', 'profit loss 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-02.txt') ] try: getResultFromFiles(investmentFiles, profitLossFiles, 177801674041.66, 177800934590.20, 3212689500.00, 7) except ValueError: # expected: one investment file has a different portfolio pass else: self.fail('error should occur')
def testResultFromFilesError3(self): investmentFiles = [ join(getCurrentDirectory(), 'samples', 'investment positions base USD 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-02.txt') ] profitLossFiles = [ join(getCurrentDirectory(), 'samples', 'profit loss 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-02.txt') ] try: getResultFromFiles(investmentFiles, profitLossFiles, 177801674041.66, 177800934590.20, 3212689500.00, 7) except ValueError: # expected: one investment file has base USD, inconsistent # with others pass else: self.fail('error should occur')
def testGetNavFromPositions(self): inputFile = join(getCurrentDirectory(), 'samples', 'investment positions 2020-08.txt') positions = compose( lambda t: list(t[0]), lambda file: readInvestmentTxtReport('utf-16', '\t', file))( inputFile) self.assertAlmostEqual( 201234832347.59, getNavFromPositions(True, False, getImpairment(), positions), 2) self.assertAlmostEqual( 200800408243.31, getNavFromPositions(False, False, getImpairment(), positions), 2)
def testGetReturnFromPositions(self): inputFile = join(getCurrentDirectory(), 'samples', 'profit loss 2020-07.txt') positions = compose( lambda t: list(t[0]), lambda file: readProfitLossTxtReport('utf-16', '\t', file))( inputFile) realized, total = getReturnFromPositions(True, positions) self.assertAlmostEqual(910273720.75, realized, 2) self.assertAlmostEqual(975551797.10, total, 2) realized, total = getReturnFromPositions(False, positions) self.assertAlmostEqual(910888436.76, realized, 2) self.assertAlmostEqual(976508916.44, total, 2)
def testGetReturnFromPositions2(self): inputFile = join(getCurrentDirectory(), 'samples', 'profit loss 2020-08.txt') positions = compose( lambda t: list(t[0]), lambda file: readProfitLossTxtReport('utf-16', '\t', file))( inputFile) realized, total = getReturnFromPositions(True, positions) self.assertAlmostEqual(834768238.18, realized, 2) self.assertAlmostEqual(893933279.75, total, 2) realized, total = getReturnFromPositions(False, positions) self.assertAlmostEqual(835110810.36, realized, 2) self.assertAlmostEqual(893939455.95, total, 2)
def testGetNavFromPositions2(self): inputFile = join(getCurrentDirectory(), 'samples', 'investment positions 2020-07.txt') positions = compose( lambda t: list(t[0]), lambda file: readInvestmentTxtReport('utf-16', '\t', file))( inputFile) self.assertAlmostEqual( 198014238644.38, getNavFromPositions(True, True, getImpairment(), positions), 2) self.assertAlmostEqual( 195050730381.42, getNavFromPositions(False, True, getImpairment(), positions), 2)
def testReadTxtReport2(self): inputFile = join(getCurrentDirectory(), 'samples', 'taxlot01_text_unicode.txt') positions, metaData = readTxtReport('utf-16', '\t', inputFile) self.verifyMetaData(metaData)
def testReadExcelReport(self): inputFile = join(getCurrentDirectory(), 'samples', 'taxlot01.xlsx') positions, metaData = readExcelReport(inputFile) self.verifyMetaData(metaData) self.assertEqual(38, len(positions))
def testGetResultFromFiles(self): # Sequence of the files does not matter investmentFiles = [ join(getCurrentDirectory(), 'samples', 'investment positions 2020-06.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-07.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-08.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-09.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-10.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-02.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-03.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-04.txt'), join(getCurrentDirectory(), 'samples', 'investment positions 2020-05.txt') ] profitLossFiles = [ join(getCurrentDirectory(), 'samples', 'profit loss 2020-06.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-07.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-08.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-09.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-10.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-01.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-02.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-03.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-04.txt'), join(getCurrentDirectory(), 'samples', 'profit loss 2020-05.txt') ] withCash, withoutCash = (lambda t: (list(t[0]), list(t[1])))( getResultFromFiles(investmentFiles, profitLossFiles, 177801674041.66, 177800934590.20, 3212689500.00, 7)) self.assertAlmostEqual(794354025.64, withCash[0][0], 2) self.assertAlmostEqual(467300058.59, withCash[0][2], 2) self.assertAlmostEqual(178862489321.83, withCash[0][4], 2) self.assertAlmostEqual(795081909.54, withoutCash[0][0], 2) self.assertAlmostEqual(468008054.89, withoutCash[0][2], 2) self.assertAlmostEqual(178202412228.44, withoutCash[0][4], 2) self.assertAlmostEqual(8836311220.42, withCash[9][0], 2) self.assertAlmostEqual(8228027711.85, withCash[9][2], 2) self.assertAlmostEqual(191878687075.53, withCash[9][4], 2) self.assertAlmostEqual(8833423074.48, withoutCash[9][0], 2) self.assertAlmostEqual(8223793885.14, withoutCash[9][2], 2) self.assertAlmostEqual(189813739094.29, withoutCash[9][4], 2)