11import datetime
2- from typing import Tuple
2+ from typing import Tuple , List
33from ib_insync import IB
44
55from sysbrokers .IB .ib_connection import connectionIB
6- from sysbrokers .IB .ib_contracts import ibContract
6+ from sysbrokers .IB .ib_contracts import ibContract , ibContractDetails
77from sysbrokers .IB .config .ib_instrument_config import (
88 IBconfig ,
99 read_ib_config_from_file ,
10- get_instrument_code_from_broker_code ,
10+ get_instrument_code_from_broker_instrument_identity ,
11+ IBInstrumentIdentity ,
1112)
1213
1314from syscore .constants import arg_not_supplied
@@ -133,21 +134,10 @@ def _get_log_for_contract(self, contract: ibContract) -> logger:
133134 if contract is None :
134135 log_to_use = self .log .setup ()
135136 else :
136- ib_instrument_code = contract .symbol
137137 ib_expiry_str = contract .lastTradeDateOrContractMonth
138-
139- contract_details = self .contract_details (
140- contract , allow_expired = False , allow_multiple_contracts = False
141- )
142-
143- ## FIXME: REPLACE WITH GET EXTENDED CONTRACT DETAILS?
144- ib_exchange = arg_not_supplied
145- ib_multiplier = arg_not_supplied
146-
147- instrument_code = self .get_instrument_code_from_broker_code (
148- ib_instrument_code
138+ instrument_code = self .get_instrument_code_from_broker_contract_object (
139+ contract
149140 )
150-
151141 futures_contract = futuresContract (instrument_code , ib_expiry_str )
152142 log_to_use = futures_contract .specific_log (self .log )
153143
@@ -162,18 +152,27 @@ def broker_message(self, log, msg):
162152 def refresh (self ):
163153 self .ib .sleep (0.00001 )
164154
165- def get_instrument_code_from_broker_code (
166- self ,
167- ib_code : str ,
168- ib_multiplier : float = arg_not_supplied ,
169- ib_exchange : str = arg_not_supplied ,
155+ def get_instrument_code_from_broker_contract_object (
156+ self , broker_contract_object : ibContract
170157 ) -> str :
171- instrument_code = get_instrument_code_from_broker_code (
158+
159+ broker_identity = self .broker_identity_for_contract (broker_contract_object )
160+ instrument_code = self .get_instrument_code_from_broker_identity_for_contract (
161+ broker_identity
162+ )
163+
164+ return instrument_code
165+
166+ def get_instrument_code_from_broker_identity_for_contract (
167+ self , ib_instrument_identity : IBInstrumentIdentity , config = arg_not_supplied
168+ ) -> str :
169+ if config is arg_not_supplied :
170+ config = self .ib_config
171+
172+ instrument_code = get_instrument_code_from_broker_instrument_identity (
173+ ib_instrument_identity = ib_instrument_identity ,
172174 log = self .log ,
173- ib_code = ib_code ,
174- config = self .ib_config ,
175- ib_multiplier = ib_multiplier ,
176- ib_exchange = ib_exchange ,
175+ config = config ,
177176 )
178177 return instrument_code
179178
@@ -191,19 +190,32 @@ def _get_and_set_ib_config_from_file(self) -> IBconfig:
191190
192191 return config_data
193192
194- def contract_details (
193+ def broker_identity_for_contract (
194+ self ,
195+ ib_contract_pattern : ibContract ,
196+ ) -> IBInstrumentIdentity :
197+
198+ contract_details = self .get_contract_details (
199+ ib_contract_pattern = ib_contract_pattern ,
200+ allow_expired = False ,
201+ allow_multiple_contracts = False ,
202+ )
203+
204+ return IBInstrumentIdentity (
205+ ib_code = contract_details .contract .symbol ,
206+ ib_multiplier = contract_details .contract .multiplier ,
207+ ib_exchange = contract_details .contract .exchange ,
208+ )
209+
210+ def get_contract_details (
195211 self ,
196212 ib_contract_pattern : ibContract ,
197213 allow_expired : bool = False ,
198214 allow_multiple_contracts : bool = False ,
199- ) -> Tuple [ibContract , list ]:
200-
201- # contract_details = self.cache.get(
202- # self._contract_details, ib_contract_pattern, allow_expired=allow_expired
203- # )
204- # FIXME TRY WITHOUT CACHING FOR NOW
205- contract_details = self ._contract_details (
206- ib_contract_pattern , allow_expired = allow_expired
215+ ) -> Tuple [ibContractDetails , List [ibContractDetails ]]:
216+
217+ contract_details = self .cache .get (
218+ self ._get_contract_details , ib_contract_pattern , allow_expired = allow_expired
207219 )
208220
209221 if len (contract_details ) == 0 :
@@ -217,9 +229,9 @@ def contract_details(
217229
218230 return contract_details [0 ]
219231
220- def _contract_details (
232+ def _get_contract_details (
221233 self , ib_contract_pattern : ibContract , allow_expired : bool = False
222- ) -> list :
234+ ) -> List [ ibContractDetails ] :
223235 ib_contract_pattern .includeExpired = allow_expired
224236
225237 return self .ib .reqContractDetails (ib_contract_pattern )
0 commit comments