コード例 #1
0
ファイル: test_asset.py プロジェクト: ZhangSteven/risk_report
	def testAverageRating(self):
		positions = getPortfolioPositions('19437', '20200429', 'test')
		blpData = getBlpData('20200429', 'test')

		securitiesWithRatings = compose(
			list
		  , partial(map, lambda p: (getAverageRatingScore(blpData, p), p))
		  , partial(map, lambda t: t[1])
		  , partial(filter, lambda t: ratingsApplicable(t[0]))
		  , lambda blpData, positions: \
		  		map(lambda p: (getAssetType(blpData, p), p), positions)
		)(blpData, positions)

		# There are 154 bonds (155 bonds, but one with special case override)
		self.assertEqual(154, len(securitiesWithRatings))

		# Has 3 credit ratings
		self.assertEqual(11, firstOf( lambda t: t[1]['InvestID'] == 'XS2114413565'
									, securitiesWithRatings)[0])

		# Has 2 credit ratings
		self.assertEqual(11, firstOf( lambda t: t[1]['InvestID'] == 'USY70902AB04'
									, securitiesWithRatings)[0])

		# Has 1 credit ratings
		self.assertEqual(13, firstOf( lambda t: t[1]['InvestID'] == 'XS2127809528'
									, securitiesWithRatings)[0])

		# Has no credit ratings
		self.assertEqual(0, firstOf( lambda t: t[1]['InvestID'] == 'XS2021226985'
									, securitiesWithRatings)[0])
コード例 #2
0
ファイル: test_asset.py プロジェクト: ZhangSteven/risk_report
	def testDIFAssetType(self):
		positions = list(getPortfolioPositions('19437', '20200429', 'test'))
		blpData = getBlpData('20200429', 'test')

		# USD cash on hand position
		isUSDCash = lambda x: \
			x['InvestID'] == 'USD' and int(x['Quantity']) == 8183675
		self.assertEqual( ('Cash', )
						, getAssetType(blpData, firstOf(isUSDCash, positions)))

		# Cash payable position
		isHKDCashPayable = lambda x: \
			x['InvestID'] == 'HKD' and int(x['Quantity']) == -1804761
		self.assertEqual( ('Cash', )
						, getAssetType(blpData, firstOf(isHKDCashPayable, positions)))

		# Equity position
		isEquityPosition = lambda x: x['InvestID'] == '1299 HK'
		self.assertEqual( ('Equity', 'Listed Equities')
						, getAssetType(blpData, firstOf(isEquityPosition, positions)))

		# The bond: T V2.875 PERP B
		isBondPosition = lambda x: x['InvestID'] == 'XS2114413565'
		self.assertEqual( ('Fixed Income', 'Corporate')
						, getAssetType(blpData, firstOf(isBondPosition, positions)))


		# The bond: POSABK V4.5 PERP, the special case, treated as equity in 19437
		isBondPosition2 = lambda x: x['InvestID'] == 'XS1684793018 Perfshs'
		self.assertEqual( ('Equity', 'Listed equities')
						, getAssetType(blpData, firstOf(isBondPosition2, positions)))


		# The callable bond: BCHINA V3.6 PERP
		isCallableBondPosition = lambda x: x['InvestID'] == 'XS2125922349'
		self.assertEqual( ('Fixed Income', 'Additional Tier 1, Contingent Convertibles')
						, getAssetType(blpData, firstOf(isCallableBondPosition, positions)))

		# The iShares A50 China ETF
		isA50Fund = lambda x: x['InvestID'] == '2823 HK'
		self.assertEqual( ('Fund', 'Exchange Traded Funds')
						, getAssetType(blpData, firstOf(isA50Fund, positions)))

		# The LINK REIT (823 HK is treated as special case)
		isREITFund = lambda x: x['InvestID'] == '823 HK'
		self.assertEqual( ('Equity', 'Listed equities')
						, getAssetType(blpData, firstOf(isREITFund, positions)))
コード例 #3
0
	def testSum(self):
		FX = getFX('20200429', 'USD')['HKD']
		blpData = getBlpData('20200429', 'test')
		positions = list(getPortfolioPositions('19437', '20200429', 'test'))

		
		# Now we test by different country groups
		self.assertAlmostEqual( 394165723.69
							  , compose(
							  		lambda x: x / FX
							  	  , sum
							  	  , partial(map, getGenevaMarketValue)
							  	  , byCountryFilter(blpData, 'China - Mainland')
							  	)(positions)
							  , 2
							  )


		self.assertAlmostEqual( 46753980.08
							  , compose(
							  		lambda x: x / FX
							  	  , sum
							  	  , partial(map, getGenevaMarketValue)
							  	  , byCountryFilter(blpData, 'China - Hong Kong')
							  	)(positions)
							  , 2
							  )


		self.assertAlmostEqual( 10019205.48
							  , compose(
							  		lambda x: x / FX
							  	  , sum
							  	  , partial(map, getGenevaMarketValue)
							  	  , byCountryFilter(blpData, 'China - Macau')
							  	)(positions)
							  , 2
							  )


		self.assertAlmostEqual( 15742145.41
							  , compose(
							  		lambda x: x / FX
							  	  , sum
							  	  , partial(map, getGenevaMarketValue)
							  	  , byCountryFilter(blpData, 'Singapore')
							  	)(positions)
							  , 2
							  )


		self.assertAlmostEqual( 1419901.34
							  , compose(
							  		lambda x: x / FX
							  	  , sum
							  	  , partial(map, getGenevaMarketValue)
							  	  , byCountryFilter(blpData, 'America - others (1)')
							  	)(positions)
							  , 2
							  )


		# Purposedly made wrong, since there are no "XXX" country.
		self.assertAlmostEqual( 0
							  , compose(
							  		lambda x: x / FX
							  	  , sum
							  	  , partial(map, getGenevaMarketValue)
							  	  , byCountryFilter(blpData, 'XXX')
							  	)(positions)
							  , 2
							  )


		# Now test different asset types
		"""
コード例 #4
0
	def testSum2(self):
		blpData = getBlpData('20200429', 'test')
		positions = list(getPortfolioPositions('19437', '20200429', 'test'))

		self.assertAlmostEqual( 12513933.62
							  , getTotalMarketValueFromCountrynAssetType(
									'20200429'
								  , positions
								  , blpData
								  , 'USD'
								  , 'China - Mainland'
								  , 'Fixed Income'
								  , 'Additional Tier 1, Contingent Convertibles'
								)
							  , 2
							  )


		self.assertAlmostEqual( 17208645.91
							  , getTotalMarketValueFromCountrynAssetType(
									'20200429'
								  , positions
								  , blpData
								  , 'USD'
								  , 'China - Hong Kong'
								  , 'Fixed Income'
								  , 'Corporate'
								  , 'Investment Grade'
								  , 'Financial Institution'
								)
							  , 2
							  )


		self.assertAlmostEqual( 5020919.30
							  , getTotalMarketValueFromCountrynAssetType(
									'20200429'
								  , positions
								  , blpData
								  , 'USD'
								  , 'China - Macau'
								  , 'Fixed Income'
								  , 'Corporate'
								  , 'Non-Investment Grade'
								  , 'Non-Financial Institution'
								)
							  , 2
							  )


		self.assertAlmostEqual( 2029888.56
							  , getTotalMarketValueFromCountrynAssetType(
									'20200429'
								  , positions
								  , blpData
								  , 'USD'
								  , 'Asia - others (1)'
								  , 'Fixed Income'
								  , 'Government / Municipal'
								)
							  , 2
							  )


		self.assertAlmostEqual( 26308960.40
							  , getTotalMarketValueFromCountrynAssetType(
									'20200429'
								  , positions
								  , blpData
								  , 'USD'
								  , 'Asia - others (1)'
								  , 'Fixed Income'
								  , 'Corporate'
								)
							  , 2
							  )


		# Purposely made wrong, since there are no such thing as 
		# 'Fixed Income', 'Government', 'Financial'
		self.assertAlmostEqual( 0
							  , getTotalMarketValueFromCountrynAssetType(
									'20200429'
								  , positions
								  , blpData
								  , 'USD'
								  , 'Asia - others (1)'
								  , 'Fixed Income'
								  , 'Government / Municipal'
								  , 'Financial'
								)
							  , 2
							  )
コード例 #5
0
 def testGetPortfolioPositions2(self):
     # Test Bloomberg positions
     positions = compose(list, partial(filterfalse, isGenevaPosition))(
         getPortfolioPositions('ALL', '20200131', 'test'))
     self.assertEqual(97, len(positions))
     self.verifyBlpPosition(positions[2])
コード例 #6
0
 def testGetPortfolioPositions(self):
     # Test Geneva positions
     positions = list(getPortfolioPositions('19437', '20200429', 'test'))
     self.assertEqual(192, len(positions))
     self.verifyInvestmentPosition(positions[3])