def test_volatility_surface(self): stocks.fetch_historical('SPX', TEST_DB_NAME) surface1 = structures.volatility_surface( 'SPX', self.test_timestamp.date(), 50, TEST_DB_NAME) surface2 = OrderedDict([ (date(2016, 1, 15), [ (1985.0, 21.43), (1990.0, 21.19), (1995.0, 20.81), (2000.0, 20.67), (2005.0, 20.12), (2010.0, 19.76), (2015.0, 19.41), (2020.0, 19.06), (2025.0, 18.75), (2030.0, 18.39), ]), (date(2016, 2, 19), [ (1985.0, 19.14), (1990.0, 18.95), (1995.0, 18.74), (2000.0, 18.51), (2005.0, 18.33), (2010.0, 18.12), (2015.0, 17.93), (2020.0, 17.71), (2025.0, 17.51), (2030.0, 17.31), ]), (date(2016, 3, 18), [ (1985.0, 18.55), (1990.0, 18.41), (1995.0, 18.23), (2000.0, 18.06), (2005.0, 17.91), (2010.0, 17.74), (2015.0, 17.58), (2020.0, 17.41), (2025.0, 17.25), (2030.0, 17.1), ]), (date(2016, 4, 15), [(2000.0, 17.88), (2025.0, 17.17)]), (date(2016, 6, 17), [(2000.0, 17.31), (2025.0, 16.86)]), (date(2016, 9, 16), [(2000.0, 16.95), (2025.0, 16.6)]), (date(2016, 12, 16), [(2000.0, 16.66), (2025.0, 16.36)]), (date(2017, 1, 20), [(2000.0, 16.71), (2025.0, 16.48)]), (date(2017, 6, 16), [(2000.0, 16.58), (2025.0, 16.36)]), (date(2017, 12, 15), [(2000.0, 16.39), (2025.0, 16.24)]), ]) self.assertEqual(surface1, surface2)
def update_quotes(): """Retrieves all required historical quotes""" logger = logging.getLogger(__name__) logger.info('Updating quotes ...') all_symbols = [ 'VIX', 'VXST', 'VXV', 'VXMT', 'VVIX', 'SPX', 'XIV', 'VXX', 'TLT'] for symbol in all_symbols: stocks.fetch_historical(symbol) structures.update_stocks_vol('SPX') futures.fetch_historical('VX') options.fetch_historical('SPX') logger.info('Done')
def test_volatility_cone(self): stocks.fetch_historical('SPX', TEST_DB_NAME) structures.update_stocks_vol('SPX', TEST_DB_NAME) cone1 = structures.volatility_cone( 'SPX', self.test_timestamp.date(), 252, TEST_DB_NAME) cone2 = np.array([ [ 19.84, 16.89, 15.69, 15.88, 18.30, 15.39], [ 5.82, 8.08, 9.37, 9.64, 10.81, 11.54], [ 8.85, 9.43, 9.98, 10.50, 11.50, 11.69], [ 9.83, 10.54, 11.18, 11.01, 12.26, 11.85], [ 12.32, 12.41, 11.90, 12.57, 12.94, 12.3 ], [ 16.04, 16.64, 18.51, 20.50, 16.94, 15.04], [ 21.94, 26.19, 23.62, 21.59, 17.34, 15.3 ], [ 43.41, 31.95, 25.45, 22.06, 18.30, 15.54], ]) self.assertTrue(np.array_equal(cone1, cone2))
def test_query_historical_multiple_quotes(self): stocks.fetch_historical("SPX", TEST_DB_NAME) stocks.fetch_historical("VIX", TEST_DB_NAME) stocks.fetch_historical("AAPL", TEST_DB_NAME) quotes = stocks.query_historical(["SPX", "VIX", "AAPL"], date(2015, 12, 15), TEST_DB_NAME) self.assertEqual(quotes["SPX"].close, 2043.41) self.assertEqual(quotes["VIX"].close, 20.95)
def test_query_historical_single_quote(self): test_date = date(2015, 12, 15) stocks.fetch_historical("SPX", TEST_DB_NAME) quotes = stocks.query_historical(["SPX"], test_date, TEST_DB_NAME) self._check_quotes(quotes, "SPX", test_date, 2043.41)
def test_fetch_historical_non_existing_symbol(self): lines = stocks.fetch_historical("SPX1", TEST_DB_NAME) self.assertEqual(lines, 0) self._check_database_quotes("SPX1", lines, date(2015, 12, 15), None)
def test_fetch_historical_existing_cboe_vvix(self): lines = stocks.fetch_historical("VVIX", TEST_DB_NAME) self.assertGreater(lines, 0) self._check_database_quotes("VVIX", lines, date(2015, 12, 15), 114.37)
def test_fetch_historical_existing_symbol_cboe(self): lines = stocks.fetch_historical("VXST", TEST_DB_NAME) self.assertGreater(lines, 0) self._check_database_quotes("VXST", lines, date(2015, 12, 15), 23.05)
def test_fetch_historical_existing_symbol_yahoo(self): lines = stocks.fetch_historical("SPX", TEST_DB_NAME) self.assertGreater(lines, 0) self._check_database_quotes("SPX", lines, date(2015, 12, 15), 2043.41)
def setUpClass(cls): database.drop_db(TEST_DB_NAME) stocks.fetch_historical('SPX', TEST_DB_NAME)