-
Notifications
You must be signed in to change notification settings - Fork 37
/
ib_0dte_iron_condor_bot.py
97 lines (74 loc) · 2.48 KB
/
ib_0dte_iron_condor_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Run `j IBCMacos-3.18.0` in terminal to switch to IBC folder
Then `./gatewaystartmacos.sh -inline` to start TWS
Port 7497 is for connection to TWS using paper trading account
Port 7496 is for connection to TWS using real trading account
Port 4002 is for connection to IB Gateway using paper trading account
Port 4001 is for connection to IB Gateway using real trading account
"""
# pip install ib_insync
# https://ib-insync.readthedocs.io/recipes.html
from datetime import date
from ib_async import Index, Option
from common.ib import setup_ib
# util.startLoop() # uncomment this line when in a notebook
# util.logToConsole("DEBUG")
ib = setup_ib()
# DATE AND TIME
today = date.today()
formatted_date = str(today.strftime("%d/%m/%Y"))
# fetch market price of underlying contract
underlying = Index("SPX", "CBOE", "USD")
ib.qualifyContracts(underlying)
ib.reqMarketDataType(2)
data = ib.reqMktData(underlying, "", False, False)
while data.last != data.last:
ib.sleep(0.01) # Wait until data is in.
market_price = 5 * round(data.last / 5)
print(f"{market_price=}")
# fetch VIX price
VIXIndex = Index("VIX", "CBOE", "USD")
ib.qualifyContracts(VIXIndex)
VIX_data = ib.reqMktData(VIXIndex, "", False, False)
while VIX_data.last != VIX_data.last:
ib.sleep(0.01) # Wait until data is in.
VIX = round(VIX_data.last)
IV = VIX_data.last / 100
print(f"{IV=}")
# fetch treasury yield
TNXIndex = Index("TNX", "CBOE", "USD")
ib.qualifyContracts(TNXIndex)
TNX_data = ib.reqMktData(TNXIndex, "", False, False)
while TNX_data.close != TNX_data.close:
ib.sleep(0.01) # Wait until data is in.
TNX = TNX_data.close / 1000
print(f"{TNX=}")
print(
f"running live.\nMarket price: {market_price}; VIX: {VIX}; Treasury: {TNX} \nDate: {formatted_date}"
)
strike_price_range = 120
min_strike_price = market_price - strike_price_range
max_strike_price = market_price + strike_price_range
put_strikes = [s for s in range(market_price, min_strike_price, 5)]
# Build Call Contracts
call_strikes = [s for s in range(market_price, max_strike_price, 5)]
today = date.today()
date_today = today.strftime("%Y%m%d")
call_contracts = [
Option(
"SPX",
date_today,
strike,
"C",
"SMART",
"100",
"USD",
tradingClass="SPXW",
)
for strike in call_strikes
]
qualified_call_contracts = ib.qualifyContracts(*call_contracts)
ib.reqMarketDataType(2)
call_contract_tickers = [ib.ticker(c) for c in qualified_call_contracts]
print(call_contract_tickers)
ib.disconnect()