Skip to content

Commit

Permalink
Merge pull request #1431 from tgibson11/issue-1403-LME-weekly-contracts
Browse files Browse the repository at this point in the history
Issue #1403 ignore LME weekly contracts
  • Loading branch information
bug-or-feature authored Oct 26, 2024
2 parents 8cf5e70 + 550025d commit 4a72cbc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
12 changes: 6 additions & 6 deletions sysbrokers/IB/config/ib_config_futures.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
34 changes: 34 additions & 0 deletions sysbrokers/IB/ib_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
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,
Expand All @@ -40,6 +55,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
Expand All @@ -56,6 +73,14 @@ 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
resolved_contract = resolve_multiple_expiries_for_generic_futures(
Expand Down Expand Up @@ -105,6 +130,15 @@ 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

Expand Down

0 comments on commit 4a72cbc

Please sign in to comment.