1
1
import datetime
2
- from typing import Tuple
2
+ from typing import Tuple , List
3
3
from ib_insync import IB
4
4
5
5
from sysbrokers .IB .ib_connection import connectionIB
6
- from sysbrokers .IB .ib_contracts import ibContract
6
+ from sysbrokers .IB .ib_contracts import ibContract , ibContractDetails
7
7
from sysbrokers .IB .config .ib_instrument_config import (
8
8
IBconfig ,
9
9
read_ib_config_from_file ,
10
- get_instrument_code_from_broker_code ,
10
+ get_instrument_code_from_broker_instrument_identity ,
11
+ IBInstrumentIdentity ,
11
12
)
12
13
13
14
from syscore .constants import arg_not_supplied
@@ -133,21 +134,10 @@ def _get_log_for_contract(self, contract: ibContract) -> logger:
133
134
if contract is None :
134
135
log_to_use = self .log .setup ()
135
136
else :
136
- ib_instrument_code = contract .symbol
137
137
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
149
140
)
150
-
151
141
futures_contract = futuresContract (instrument_code , ib_expiry_str )
152
142
log_to_use = futures_contract .specific_log (self .log )
153
143
@@ -162,18 +152,27 @@ def broker_message(self, log, msg):
162
152
def refresh (self ):
163
153
self .ib .sleep (0.00001 )
164
154
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
170
157
) -> 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 ,
172
174
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 ,
177
176
)
178
177
return instrument_code
179
178
@@ -191,19 +190,32 @@ def _get_and_set_ib_config_from_file(self) -> IBconfig:
191
190
192
191
return config_data
193
192
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 (
195
211
self ,
196
212
ib_contract_pattern : ibContract ,
197
213
allow_expired : bool = False ,
198
214
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
207
219
)
208
220
209
221
if len (contract_details ) == 0 :
@@ -217,9 +229,9 @@ def contract_details(
217
229
218
230
return contract_details [0 ]
219
231
220
- def _contract_details (
232
+ def _get_contract_details (
221
233
self , ib_contract_pattern : ibContract , allow_expired : bool = False
222
- ) -> list :
234
+ ) -> List [ ibContractDetails ] :
223
235
ib_contract_pattern .includeExpired = allow_expired
224
236
225
237
return self .ib .reqContractDetails (ib_contract_pattern )
0 commit comments