def get(self, item, date): """ Gets contract instance :param item: Contract hash code or Instrument name :param date: current date :return: Instrument / FutureContract / OptionContract class instance """ if isinstance(item, int): # # Get item by hash instrument, or future, or option # if item < FUT_HASH_ROOT: raise NotImplementedError("Unknown asset hash") elif item < FUT_HASH_ROOT + HASH_ROOT_STEP: # Get future contract # Fetch contracts meta information from asset index fut_contract_dic = self.assetindex.get_future_contract(item - FUT_HASH_ROOT) instr_dic = self.assetindex.get_instrument(fut_contract_dic['idinstrument']) # Creating contract classes instr = Instrument(self, instr_dic, date, self.futures_limit, self.options_limit) fut = FutureContract(fut_contract_dic, instr) assert fut.__hash__() == item return fut elif item < OPT_HASH_ROOT + HASH_ROOT_STEP: # Get option contract # Fetch contracts meta information from asset index opt_contract_dic = self.assetindex.get_option_contract(item - OPT_HASH_ROOT) fut_contract_dic = self.assetindex.get_future_contract(opt_contract_dic['idcontract']) instr_dic = self.assetindex.get_instrument(fut_contract_dic['idinstrument']) # Creating contract classes instr = Instrument(self, instr_dic, date, self.futures_limit, self.options_limit) fut = FutureContract(fut_contract_dic, instr) opt = OptionContract(opt_contract_dic, fut) assert opt.__hash__() == item return opt else: raise NotImplementedError("Unknown asset hash") else: # # Fetch Instrument by symbol name sting # data_dict = self.assetindex.get_instrument_info(item) return Instrument(self, data_dict, date, self.futures_limit, self.options_limit)
def test_putcallpair_addoption_call_error_onduplicate(self): pair = PutCallPair() pair.addoption(self.option_contract_put) opt_contract_dict_call = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'c', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract_call = OptionContract(opt_contract_dict_call, self.fut_contract) pair.addoption(option_contract_call) self.assertEqual(pair._put, self.option_contract_put) self.assertEqual(pair._call, option_contract_call) self.assertRaises(ValueError, pair.addoption, option_contract_call)
def test_putcallpair_has_repr(self): pair = PutCallPair() pair.addoption(self.option_contract_put) opt_contract_dict_call = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'c', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract_call = OptionContract(opt_contract_dict_call, self.fut_contract) pair.addoption(option_contract_call) self.assertEqual( pair.__repr__(), "{0} {1} / {2}".format(pair.strike, pair.call.name, pair.put.name))
def test_putcallpair_has_underlying(self): pair = PutCallPair() self.assertEqual(None, pair.underlying) pair.addoption(self.option_contract_put) self.assertEqual(self.fut_contract, pair.underlying) opt_contract_dict_call = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'c', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract_call = OptionContract(opt_contract_dict_call, self.fut_contract) pair = PutCallPair() pair.addoption(option_contract_call) self.assertEqual(self.fut_contract, pair.underlying)
def setUp(self): self.instrument = Instrument self.contract_dict = { '_id': '577a4fa34b01f47f84cab23c', 'contractname': 'F.EPZ16', 'cqgsymbol': 'F.EPZ16', 'expirationdate': datetime(2016, 12, 16, 0, 0), 'idcontract': 6570, 'idinstrument': 11, 'month': 'Z', 'monthint': 12, 'year': 2016 } self.fut_contract = FutureContract(self.contract_dict, self.instrument) self.opt_contract_dict_put = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } self.option_contract_put = OptionContract(self.opt_contract_dict_put, self.fut_contract)
def __init__(self, option_chain_dic, futures_contract, options_limit=0): self._data = option_chain_dic self._fut = futures_contract self._options = OrderedDict() self._atm_index = -1 self._options_limit = options_limit self._expiration = self._data['_id']['date'] if options_limit > 0: all_strikes = np.array( sorted( list(set([x['strikeprice'] for x in self._data['chain']])))) atm_index = np.argmin(np.abs(all_strikes - self.underlying.price)) all_strikes = all_strikes[max(0, atm_index - options_limit):min( len(self._data['chain']), atm_index + options_limit + 1)] for opt_dic in self._data['chain']: if options_limit > 0: if opt_dic['strikeprice'] not in all_strikes: continue option = OptionContract(opt_dic, self._fut) pc_pair = self._options.setdefault(option.strike, PutCallPair()) pc_pair.addoption(option) self._strike_array = np.array(list(self._options.keys()))
def setUp(self): self.assetindex = AssetIndexDicts() self.symbol = 'EP' self.date = datetime(2014, 1, 5, 0, 0, 0) self.futures_limit = 12 self.datasource = DataSourceForTest(self.assetindex, self.futures_limit, 0) self.instrument = self.datasource.get(self.symbol, self.date) self.contract_dict = { '_id': '577a4fa34b01f47f84cab23c', 'contractname': 'F.EPZ16', 'cqgsymbol': 'F.EPZ16', 'expirationdate': datetime(2016, 12, 16, 0, 0), 'idcontract': 6570, 'idinstrument': 11, 'month': 'Z', 'monthint': 12, 'year': 2016 } self.fut_contract = FutureContract(self.contract_dict, self.instrument) self.opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } self.option_contract = OptionContract(self.opt_contract_dict, self.fut_contract)
def test_optioncontract_has_riskfreerate(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) self.assertEqual(option_contract.riskfreerate, 0.255)
def test_optioncontract_has_price_caching(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'c', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) S = option_contract.underlying.price self.assertEqual(2770.0, S) T = option_contract.to_expiration_years X = option_contract.strike r = option_contract.riskfreerate self.assertEqual(r, 0.255) v = option_contract.iv self.assertEqual(v, 0.356) callputflag = option_contract.callorput self.assertEqual(callputflag, 'C') self.assertTrue(np.isnan(option_contract._option_price)) self.assertEqual(option_contract.price, blackscholes(callputflag, S, X, T, r, v)) self.assertFalse(np.isnan(option_contract._option_price)) self.assertNotEqual(option_contract.price, 0) self.assertFalse(np.isnan(option_contract._option_price))
def test_equality(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.assertEqual(option_contract, self.option_contract) self.assertFalse(option_contract is None) self.assertNotEqual(option_contract, None)
def test_constructor(self): opt_contract = OptionContract(self.opt_contract_dict, self.fut_contract) self.assertEqual(type(opt_contract._data), dict) self.assertEqual(type(opt_contract._future_contract), FutureContract)
class OptionContractTestCase(unittest.TestCase): def setUp(self): self.assetindex = AssetIndexDicts() self.symbol = 'EP' self.date = datetime(2014, 1, 5, 0, 0, 0) self.futures_limit = 12 self.datasource = DataSourceForTest(self.assetindex, self.futures_limit, 0) self.instrument = self.datasource.get(self.symbol, self.date) self.contract_dict = { '_id': '577a4fa34b01f47f84cab23c', 'contractname': 'F.EPZ16', 'cqgsymbol': 'F.EPZ16', 'expirationdate': datetime(2016, 12, 16, 0, 0), 'idcontract': 6570, 'idinstrument': 11, 'month': 'Z', 'monthint': 12, 'year': 2016 } self.fut_contract = FutureContract(self.contract_dict, self.instrument) self.opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } self.option_contract = OptionContract(self.opt_contract_dict, self.fut_contract) def test_constructor(self): opt_contract = OptionContract(self.opt_contract_dict, self.fut_contract) self.assertEqual(type(opt_contract._data), dict) self.assertEqual(type(opt_contract._future_contract), FutureContract) def test_optioncontract_has_name(self): self.assertEqual(self.option_contract.name, self.opt_contract_dict['optionname']) def test_optioncontract_has_underlying(self): self.assertEqual(self.option_contract.underlying, self.fut_contract) def test_optioncontract_has_strike(self): self.assertEqual(self.option_contract.strike, self.opt_contract_dict['strikeprice']) def test_optioncontract_has_instrument(self): self.assertEqual(self.option_contract.instrument, self.instrument) def test_optioncontract_has_expiration(self): self.assertEqual(self.option_contract.expiration, self.opt_contract_dict['expirationdate']) def test_optioncontract_has_callorput_uppercase(self): self.assertEqual(self.option_contract.callorput, 'P') def test_optioncontract_has_putorcall_uppercase(self): self.assertEqual(self.option_contract.putorcall, 'P') def test_optioncontract_has_dbid(self): self.assertEqual(self.option_contract.dbid, self.opt_contract_dict['idoption']) def test_optioncontract_has_date(self): self.assertEqual(self.option_contract.date, self.date) def test_optioncontract_has_toexpiration_years(self): self.assertEqual( self.option_contract.to_expiration_years, (self.option_contract.expiration - self.date).total_seconds() / (365.0 * 24 * 60 * 60)) def test_optioncontract_has_toexpiration_days(self): self.assertEqual( self.option_contract.to_expiration_days, (self.option_contract.expiration - self.date).total_seconds() / (24 * 60 * 60)) def test_optioncontract_has_toexpiration_days_iszero_on_expiration_time( self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) self.assertEqual(option_contract.to_expiration_days, 0) def test_optioncontract_has_toexpiration_days_iszero_integer(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 20, 9, 30, 0) self.assertEqual(option_contract.to_expiration_days, 1) def test_optioncontract_has_toexpiration_years_iszero_on_expiration_time( self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) self.assertEqual(option_contract.to_expiration_years, 0) def test_optioncontract_has_riskfreerate(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) self.assertEqual(option_contract.riskfreerate, 0.255) def test_optioncontract_has_iv_and_caching(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) self.assertEqual(option_contract._option_price_data, None) self.assertEqual(option_contract.iv, 0.356) self.assertNotEqual(option_contract._option_price_data, None) def test_optioncontract_has_price_put(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) S = option_contract.underlying.price self.assertEqual(2770.0, S) T = option_contract.to_expiration_years X = option_contract.strike r = option_contract.riskfreerate self.assertEqual(r, 0.255) v = option_contract.iv self.assertEqual(v, 0.356) callputflag = option_contract.callorput self.assertEqual(callputflag, 'P') self.assertEqual(option_contract.price, blackscholes(callputflag, S, X, T, r, v)) self.assertNotEqual(option_contract.price, 0) def test_optioncontract_has_price_call(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'c', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) S = option_contract.underlying.price self.assertEqual(2770.0, S) T = option_contract.to_expiration_years X = option_contract.strike r = option_contract.riskfreerate self.assertEqual(r, 0.255) v = option_contract.iv self.assertEqual(v, 0.356) callputflag = option_contract.callorput self.assertEqual(callputflag, 'C') self.assertEqual(option_contract.price, blackscholes(callputflag, S, X, T, r, v)) self.assertNotEqual(option_contract.price, 0) def test_optioncontract_has_price_caching(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'c', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.instrument.date = datetime(2014, 3, 21, 9, 30, 0) S = option_contract.underlying.price self.assertEqual(2770.0, S) T = option_contract.to_expiration_years X = option_contract.strike r = option_contract.riskfreerate self.assertEqual(r, 0.255) v = option_contract.iv self.assertEqual(v, 0.356) callputflag = option_contract.callorput self.assertEqual(callputflag, 'C') self.assertTrue(np.isnan(option_contract._option_price)) self.assertEqual(option_contract.price, blackscholes(callputflag, S, X, T, r, v)) self.assertFalse(np.isnan(option_contract._option_price)) self.assertNotEqual(option_contract.price, 0) self.assertFalse(np.isnan(option_contract._option_price)) def test_optioncontract_has_pointvalue(self): self.assertEqual(self.option_contract.pointvalue, self.instrument.point_value_options) def test_optioncontract_as_dict(self): self.assertEqual( { 'name': self.option_contract.name, 'dbid': self.option_contract.dbid, 'type': 'O', 'hash': self.option_contract.__hash__() }, self.option_contract.as_dict()) def test_optioncontract_hash(self): self.assertEqual(200000000 + self.option_contract.dbid, self.option_contract.__hash__()) def test_equality(self): opt_contract_dict = { '_id': '577a573e4b01f47f84d0cbd5', 'callorput': 'p', 'cqgsymbol': 'P.US.EPH1427750', 'expirationdate': datetime(2014, 3, 21, 0, 0), 'idcontract': 4736, 'idinstrument': 11, 'idoption': 11558454, 'optionmonth': 'H', 'optionmonthint': 3, 'optionname': 'P.US.EPH1427750', 'optionyear': 2014, 'strikeprice': 2775.0 } option_contract = OptionContract(opt_contract_dict, self.fut_contract) self.assertEqual(option_contract, self.option_contract) self.assertFalse(option_contract is None) self.assertNotEqual(option_contract, None)