def test_http_400_response(self, mock_urlopen): error = urllib2.HTTPError(url='', code=400, msg='Bad Request', hdrs=None, fp=None) mock_urlopen.side_effect = error self.assertEquals(google.BadResponse, type(google.request_options_chain('INVALID')))
def test_request_option_chain_expirations(self): option_chain = google.request_options_chain('GOOG') expirations = option_chain.expirations self.assertEqual(12, len(expirations)) self.assertListEqual( [16, 8, 2014], [expirations[0].day, expirations[0].month, expirations[0].year]) self.assertEquals('20140816', str(expirations[0])) self.assertListEqual( [15, 1, 2016], [expirations[-1].day, expirations[-1].month, expirations[-1].year]) self.assertEquals('20160115', str(expirations[-1]))
def test_to_list(self): option_chain = google.request_options_chain('GOOG') calls = option_chain.calls puts = option_chain.puts self.assertListEqual([ 'ASK', 'BID', 'CHANGE', 'OPEN_INTEREST', 'VOLUME', 'LAST_PRICE', 'STRIKE', 'PERCENT_CHANGE' ], [item for item in google.OptionChain.FIELDS]) self.assertListEqual( ['58.40', '57.95', '-', '0', '-', '-', '37.86', ''], calls[0].to_list()) self.assertListEqual(['0.01', '-', '-', '700', '-', '-', '37.86', ''], puts[0].to_list())
def _request_option_chains(symbol, dest_dir): option_chain = google.request_options_chain(symbol) if option_chain.is_valid: _write_chain(symbol, option_chain, dest_dir) else: logging.warn( 'Invalid option chain received for symbol: {}'.format(symbol)) return # exclude expiration we already have expirations = [ exp for exp in option_chain.expirations if exp is not option_chain.expiry ] _request_expirations_chains(symbol, expirations, dest_dir)
def _request_expirations_chains(symbol, expirations, dest_dir): for expiry in expirations: # Perform dummy request first google.dummy_request(symbol) option_chain = google.request_options_chain(symbol, expiry_day=expiry.day, expiry_month=expiry.month, expiry_year=expiry.year) if option_chain.is_valid: _write_chain(symbol, option_chain, dest_dir) else: logging.warn('Invalid option chain received for symbol: {0}, ' 'expiry: y={1}, m={2}, d={3} '.format( symbol, expiry.year, expiry.month, expiry.day))
def test_request_option_chain_puts(self): option_chain = google.request_options_chain('GOOG') puts = option_chain.puts self.assertEqual(151, len(puts)) self.assertDictEqual( { 'a': '0.01', 'c': '-', 'b': '-', 'e': 'OPRA', 'name': '', 'oi': '700', 'cid': '597723653891363', 'vol': '-', 'expiry': 'Aug 16, 2014', 'p': '-', 's': 'AAPL140816P00037860', 'strike': '37.86' }, puts[0].data) self.assertDictEqual( { 'a': '44.25', 'c': '0.00', 'b': '43.75', 'e': 'OPRA', 'name': '', 'oi': '1455', 'cid': '247391588317856', 'vol': '-', 'expiry': 'Aug 16, 2014', 'p': '45.00', 's': 'AAPL140816P00140000', 'cs': 'chb', 'cp': '0.00', 'strike': '140.00' }, puts[-1].data)
def test_request_option_chain_calls(self): option_chain = google.request_options_chain('GOOG') calls = option_chain.calls self.assertEqual(151, len(calls)) self.assertDictEqual( { 'a': '58.40', 'c': '-', 'b': '57.95', 'e': 'OPRA', 'name': '', 'oi': '0', 'cid': '985928489129434', 'vol': '-', 'expiry': 'Aug 16, 2014', 'p': '-', 's': 'AAPL140816C00037860', 'strike': '37.86' }, calls[0].data) self.assertDictEqual( { 'a': '0.01', 'c': '0.00', 'b': '-', 'e': 'OPRA', 'name': '', 'oi': '633', 'cid': '791404271676757', 'vol': '-', 'expiry': 'Aug 16, 2014', 'p': '0.01', 's': 'AAPL140816C00140000', 'cs': 'chb', 'cp': '0.00', 'strike': '140.00' }, calls[-1].data)
def test_call_only(self): option_chain = google.request_options_chain('CALL_ONLY') self.assertEqual(1, len(option_chain.calls)) self.assertEqual(0, len(option_chain.puts))
def test_empty_chain(self): # TODO: Find out why not working option_chain = google.request_options_chain('EMPTY') self.assertEqual(google.OptionChainResponse, type(option_chain)) self.assertFalse(option_chain.is_valid)