robcarver17 / pysystemtrade

Systematic Trading in python
GNU General Public License v3.0
2.51k stars 800 forks source link

Crash on Futures when the last Contract is expired #1270

Closed localempire closed 9 months ago

localempire commented 9 months ago

Dear Rob, the module 'update_sampled_contracts.py' crashes when the last dated Future is no more in IB. This happens currently with BEL20, BRENT_W.....FTSE100.

Here is the Terminal Debug message: Instrument code?(Return for ALL)FTSE100 2023-10-16 20:16:48 INFO Update-Sampled_Contracts {'instrument_code': 'FTSE100', 'contract_date': '20230900'} Added contract FTSE100 20230900 2023-10-16 20:16:48 DEBUG Update-Sampled_Contracts {'instrument_code': 'FTSE100', 'contract_date': '20230900'} Contract FTSE100/20230900 now sampling 2023-10-16 20:16:48 DEBUG Update-Sampled_Contracts {'component': 'mongoIbBrokerClientIdData'} Locked IB client ID 9 2023-10-16 20:16:53 ERROR ib_insync.wrapper Error 200, reqId 3: Es wurde keine Wertpapierdefinition zu der Anfrage gefunden, contract: Future(symbol='Z', lastTradeDateOrContractMonth='202309', multiplier='1000', exchange='ICEEU', currency='GBP') 2023-10-16 20:16:53 WARNING Update-Sampled_Contracts {'component': 'ibFuturesContractData'} Reqid 3: 200 Es wurde keine Wertpapierdefinition zu der Anfrage gefunden for Future(symbol='Z', lastTradeDateOrContractMonth='202309', multiplier='1000', exchange='ICEEU', currency='GBP') 2023-10-16 20:16:53 WARNING Update-Sampled_Contracts {'instrument_code': 'FTSE100', 'contract_date': '20230900'} Contract is missing can't get expiry 2023-10-16 20:16:53 DEBUG Update-Sampled_Contracts {'instrument_code': 'FTSE100', 'contract_date': '20230900'} Can't find expiry for FTSE100/20230900, could be a connection problem but could be because contract has already expired 2023-10-16 20:16:53 INFO Update-Sampled_Contracts {'instrument_code': 'FTSE100', 'contract_date': '20230900'} Added contract FTSE100 20230900 2023-10-16 20:16:53 DEBUG Update-Sampled_Contracts {'instrument_code': 'FTSE100', 'contract_date': '20230900'} Contract FTSE100/20230900 has expired so now stopped sampling 2023-10-16 20:16:53 DEBUG Update-Sampled_Contracts {'broker': 'IB', 'clientid': 9} Terminating {'ipaddress': 'localhost', 'port': 4001, 'client': 9} 2023-10-16 20:16:53 DEBUG Update-Sampled_Contracts {'component': 'mongoIbBrokerClientIdData'} Released IB client ID 9 Traceback (most recent call last): File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/site-packages/pysystemtrade-1.61.0-py3.8.egg/sysobjects/contract_dates_and_expiries.py", line 49, in from_str as_date = datetime.datetime.strptime(date_as_str, EXPIRY_DATE_FORMAT) File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/_strptime.py", line 568, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/_strptime.py", line 352, in _strptime raise ValueError("unconverted data remains: %s" % ValueError: unconverted data remains: 10:15:00 GB

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/papa/pysystemtrade/sysproduction/update_sampled_contracts.py", line 453, in update_sampled_contracts() File "/home/papa/pysystemtrade/sysproduction/update_sampled_contracts.py", line 45, in update_sampled_contracts update_contracts_object.update_sampled_contracts( File "/home/papa/pysystemtrade/sysproduction/update_sampled_contracts.py", line 73, in update_sampled_contracts update_active_contracts_with_data(data, instrument_code=instrument_code) File "/home/papa/pysystemtrade/sysproduction/update_sampled_contracts.py", line 86, in update_active_contracts_with_data update_active_contracts_for_instrument(instrument_code, data) File "/home/papa/pysystemtrade/sysproduction/update_sampled_contracts.py", line 100, in update_active_contracts_for_instrument update_expiries_and_sampling_status_for_contracts( File "/home/papa/pysystemtrade/sysproduction/update_sampled_contracts.py", line 317, in update_expiries_and_sampling_status_for_contracts update_expiry_and_sampling_status_for_contract( File "/home/papa/pysystemtrade/sysproduction/update_sampled_contracts.py", line 344, in update_expiry_and_sampling_status_for_contract broker_expiry_date = get_contract_expiry_from_broker(contract_object, data=data) File "/home/papa/pysystemtrade/sysproduction/update_sampled_contracts.py", line 390, in get_contract_expiry_from_broker broker_expiry_date = data_broker.get_actual_expiry_date_for_single_contract( File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/site-packages/pysystemtrade-1.61.0-py3.8.egg/sysproduction/data/broker.py", line 181, in get_actual_expiry_date_for_single_contract return self.broker_futures_contract_data.get_actual_expiry_date_for_single_contract( File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/site-packages/pysystemtrade-1.61.0-py3.8.egg/sysbrokers/IB/ib_futures_contracts_data.py", line 106, in get_actual_expiry_date_for_single_contract contract_object_with_ib_data = self.get_contract_object_with_IB_data( File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/site-packages/pysystemtrade-1.61.0-py3.8.egg/sysbrokers/IB/ib_futures_contracts_data.py", line 84, in get_contract_object_with_IB_data futures_contract_with_ib_data.update_expiry_dates_one_at_a_time_with_method( File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/site-packages/pysystemtrade-1.61.0-py3.8.egg/sysobjects/contracts.py", line 246, in update_expiry_dates_one_at_a_time_with_method new_expiries = [ File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/site-packages/pysystemtrade-1.61.0-py3.8.egg/sysobjects/contracts.py", line 247, in method(single_contract, **kwargs) File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/site-packages/pysystemtrade-1.61.0-py3.8.egg/sysbrokers/IB/ib_futures_contracts_data.py", line 126, in _get_actual_expiry_date_given_single_contract_with_ib_metadata expiry_date = expiryDate.from_str(expiry_date) File "/home/papa/.pyenv/versions/3.8.18/lib/python3.8/site-packages/pysystemtrade-1.61.0-py3.8.egg/sysobjects/contract_dates_and_expiries.py", line 52, in from_str "Expiry date %s not in format %s" % date_as_str, EXPIRY_DATE_FORMAT TypeError: not enough arguments for format string

It would be nice to handle this state. I guess other vice I have to remove it from DB at least or to update all Data. I haven't found a method to remove an Instrument from DB.

robcarver17 commented 9 months ago

" I guess other vice I have to remove it from DB at least or to update all Data. I haven't found a method to remove an Instrument from DB."

No you wouldn't have to do that, that doesn't make any sense at all!!! Please read all the documentation carefully it seems you don't have a clear understanding of how the system works.

robcarver17 commented 9 months ago

The code does actually handle the case of expired contracts gracefully; everything you have shown below is in a try: catch: block. But what seems to have happened is that the original exception has a small formatting bug which I have hopefully now fixed.