From 8c53a8ca49c9994855b3b523ef7134e49acb4ebd Mon Sep 17 00:00:00 2001 From: todd <3578666+tgibson11@users.noreply.github.com> Date: Tue, 10 Sep 2024 12:08:01 -0700 Subject: [PATCH 1/4] Hack to identify monthly LME contracts (cherry picked from commit 470d2d0b2cac99d977831ab57305cf6570041066) --- sysbrokers/IB/ib_contracts.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sysbrokers/IB/ib_contracts.py b/sysbrokers/IB/ib_contracts.py index ac732a0ce8..d3ab1bd492 100644 --- a/sysbrokers/IB/ib_contracts.py +++ b/sysbrokers/IB/ib_contracts.py @@ -19,6 +19,19 @@ EUREX_CODES_WITH_DAILYS = ["MSCIWORLD", "MSCIASIA"] EUREX_DAY_FLAG = "D" +LME_CODES = [ + "ALUMINIUM_LME", + "COPPER_LME", + "LEAD_LME", + "NICKEL_LME", + "TIN_LME", + "ZINC_LME" +] +# The day of the month that the third Wednesday must fall between +# Same for any other day of the week, but I couldn't think of a +# good generic variable name +EARLIEST_THIRD_WEDNESDAY = 15 +LATEST_THIRD_WEDNESDAY = 21 def resolve_multiple_expiries( ibcontract_list: list, @@ -40,6 +53,8 @@ def resolve_multiple_expiries( resolved_contract = resolve_multiple_expiries_for_VIX(ibcontract_list) elif code in EUREX_CODES_WITH_DAILYS: resolved_contract = resolve_multiple_expiries_for_EUREX(ibcontract_list) + elif code in LME_CODES: + resolved_contract = resolve_multiple_expiries_for_LME(ibcontract_list) else: raise Exception( "You have specified weekly expiries, but I don't have logic for %s" % code @@ -55,6 +70,12 @@ def resolve_multiple_expiries_for_EUREX(ibcontract_list: list) -> ibContract: return resolved_contract +def resolve_multiple_expiries_for_LME(ibcontract_list: list) -> ibContract: + resolved_contract = resolve_multiple_expiries_for_generic_futures( + ibcontract_list=ibcontract_list, is_monthly_function=_is_lme_symbol_monthly + ) + + return resolved_contract def resolve_multiple_expiries_for_VIX(ibcontract_list: list) -> ibContract: # Get the symbols @@ -104,6 +125,13 @@ def _is_eurex_symbol_monthly(symbol: str): return is_monthly +def _is_lme_symbol_monthly(symbol: str): + # 3rd Wednesday of the month is most liquid + try: + day = int(symbol[-2:]) + except: + raise Exception("IB Local Symbol %s not recognised" % symbol) + return EARLIEST_THIRD_WEDNESDAY <= day <= LATEST_THIRD_WEDNESDAY def _is_eurex_symbol_daily(symbol: str): return symbol[-1] == EUREX_DAY_FLAG From f36f79a57d2ff846065e7d54c071874d3c696847 Mon Sep 17 00:00:00 2001 From: todd <3578666+tgibson11@users.noreply.github.com> Date: Tue, 10 Sep 2024 12:12:55 -0700 Subject: [PATCH 2/4] Update config to ignore weekly LME contracts (cherry picked from commit 08f9c60582df310fe942c52e691e5548d98e8b04) --- sysbrokers/IB/config/ib_config_futures.csv | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sysbrokers/IB/config/ib_config_futures.csv b/sysbrokers/IB/config/ib_config_futures.csv index a098cca551..2fc2e84e4e 100644 --- a/sysbrokers/IB/config/ib_config_futures.csv +++ b/sysbrokers/IB/config/ib_config_futures.csv @@ -4,7 +4,7 @@ Instrument,IBSymbol,IBExchange,IBCurrency,IBMultiplier,priceMagnifier,IgnoreWeek AEX,EOE,FTA,EUR,200,1,FALSE AEX_mini,EOE,FTA,EUR,20,1,FALSE ALUMINIUM,ALI,COMEX,USD,25,1,FALSE -ALUMINIUM_LME,AH,LMEOTC,USD,25,1,FALSE +ALUMINIUM_LME,AH,LMEOTC,USD,25,1,TRUE AMERIBOR-1M,AMB30,CFE,USD,5000,100,FALSE AMERIBOR-3M,AMT3M,CFE,USD,2500,100,FALSE AMERIBOR-T30,AMT1S,CFE,USD,2500,100,FALSE @@ -81,7 +81,7 @@ COCOA,CC,NYBOT,USD,10,1,FALSE COCOA_LDN,C,ICEEUSOFT,GBP,10,1,FALSE COFFEE,KC,NYBOT,USD,37500,100,FALSE COPPER,HG,COMEX,NA,25000,1,FALSE -COPPER_LME,CA,LMEOTC,USD,25,1,FALSE +COPPER_LME,CA,LMEOTC,USD,25,1,TRUE COPPER-micro,MHG,COMEX,USD,2500,1,FALSE COPPER-mini,QC,COMEX,USD,12500,1,FALSE CORN,ZC,CBOT,NA,5000,100,FALSE @@ -302,7 +302,7 @@ KR3,3KTB,KSE,NA,1000000,1,FALSE KRWJPY,KJ,SGX,JPY,25000000,1,FALSE KRWUSD,KRW,CME,USD,125000000,1,FALSE KRWUSD_mini,KU,SGX,USD,25000000,1000,FALSE -LEAD_LME,PB,LMEOTC,USD,25,1,FALSE +LEAD_LME,PB,LMEOTC,USD,25,1,TRUE LEANHOG,HE,CME,NA,40000,100,FALSE LIBOR1,EM,CME,USD,2500,1,FALSE LIVECOW,LE,CME,NA,40000,100,FALSE @@ -354,7 +354,7 @@ NASBIO-mini,BQX,CME,USD,50,1,FALSE NASDAQ,NQ,CME,NA,20,1,FALSE NASDAQ_micro,MNQ,CME,USD,2,1,FALSE NASDAQ_mini,QCN,CME,USD,20,1,FALSE -NICKEL_LME,NI,LMEOTC,USD,6,1,FALSE +NICKEL_LME,NI,LMEOTC,USD,6,1,TRUE NIFTY,NIFTY,SGX,USD,2,1,FALSE NIFTY-IN,NIFTY50,NSE,INR,1,1,FALSE NIKKEI,N225M,OSE.JPN,JPY,100,1,FALSE @@ -492,7 +492,7 @@ SWISSLEAD,SLI,EUREX,CHF,10,1,FALSE TECDAX,TDX,EUREX,EUR,10,1,FALSE TECH60_small,STXSME,SMFE,USD,100,1,FALSE THB,SGXTU,SGX,USD,1000000,1000,FALSE -TIN_LME,SNLME,LMEOTC,USD,5,1,FALSE +TIN_LME,SNLME,LMEOTC,USD,5,1,TRUE TOPIX,MNTPX,OSE.JPN,JPY,1000,1,FALSE TOPIX_Large,TOPX,OSE.JPN,JPY,10000,1,FALSE TOPIX30,TPXC30,OSE.JPN,JPY,1000,1,FALSE @@ -582,4 +582,4 @@ WTI_small,SMO,SMFE,USD,100,1,FALSE YENEUR,RY,CME,JPY,125000,1,FALSE YENEUR-ICE,EJ,ICEUS,JPY,125000,1,FALSE ZAR,ZAR,CME,USD,500000,1,FALSE -ZINC_LME,ZSLME,LMEOTC,USD,25,1,FALSE +ZINC_LME,ZSLME,LMEOTC,USD,25,1,TRUE From d858e967c7b862dc11864879d613d28664ea2918 Mon Sep 17 00:00:00 2001 From: todd <3578666+tgibson11@users.noreply.github.com> Date: Fri, 25 Oct 2024 11:37:32 -0700 Subject: [PATCH 3/4] Black --- sysbrokers/IB/ib_contracts.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sysbrokers/IB/ib_contracts.py b/sysbrokers/IB/ib_contracts.py index d3ab1bd492..717bb63473 100644 --- a/sysbrokers/IB/ib_contracts.py +++ b/sysbrokers/IB/ib_contracts.py @@ -21,18 +21,20 @@ LME_CODES = [ "ALUMINIUM_LME", - "COPPER_LME", + "COPPER_LME", "LEAD_LME", "NICKEL_LME", "TIN_LME", - "ZINC_LME" + "ZINC_LME", ] + # The day of the month that the third Wednesday must fall between # Same for any other day of the week, but I couldn't think of a # good generic variable name EARLIEST_THIRD_WEDNESDAY = 15 LATEST_THIRD_WEDNESDAY = 21 + def resolve_multiple_expiries( ibcontract_list: list, futures_instrument_with_ib_data: futuresInstrumentWithIBConfigData, @@ -77,6 +79,7 @@ def resolve_multiple_expiries_for_LME(ibcontract_list: list) -> ibContract: return resolved_contract + def resolve_multiple_expiries_for_VIX(ibcontract_list: list) -> ibContract: # Get the symbols resolved_contract = resolve_multiple_expiries_for_generic_futures( @@ -125,6 +128,7 @@ def _is_eurex_symbol_monthly(symbol: str): return is_monthly + def _is_lme_symbol_monthly(symbol: str): # 3rd Wednesday of the month is most liquid try: @@ -133,6 +137,7 @@ def _is_lme_symbol_monthly(symbol: str): raise Exception("IB Local Symbol %s not recognised" % symbol) return EARLIEST_THIRD_WEDNESDAY <= day <= LATEST_THIRD_WEDNESDAY + def _is_eurex_symbol_daily(symbol: str): return symbol[-1] == EUREX_DAY_FLAG From 550025db649a8ca1de9b5f392254d7d8f064b331 Mon Sep 17 00:00:00 2001 From: todd <3578666+tgibson11@users.noreply.github.com> Date: Fri, 25 Oct 2024 11:39:12 -0700 Subject: [PATCH 4/4] Black --- sysbrokers/IB/ib_contracts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sysbrokers/IB/ib_contracts.py b/sysbrokers/IB/ib_contracts.py index 717bb63473..1132e5fb2f 100644 --- a/sysbrokers/IB/ib_contracts.py +++ b/sysbrokers/IB/ib_contracts.py @@ -72,6 +72,7 @@ def resolve_multiple_expiries_for_EUREX(ibcontract_list: list) -> ibContract: return resolved_contract + def resolve_multiple_expiries_for_LME(ibcontract_list: list) -> ibContract: resolved_contract = resolve_multiple_expiries_for_generic_futures( ibcontract_list=ibcontract_list, is_monthly_function=_is_lme_symbol_monthly