-
Notifications
You must be signed in to change notification settings - Fork 37
/
ib_es_short_straddle_bot.py
executable file
·130 lines (94 loc) · 3.17 KB
/
ib_es_short_straddle_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""
Short straddles with adjustments bot running on IB
Usage:
./ib_es_short_straddle_bot.py -h
./ib_es_short_straddle_bot.py --dte 3 -v # To log INFO messages
./ib_es_short_straddle_bot.py --dte 3 -vv # To log DEBUG messages
"""
import logging
import time
from argparse import ArgumentParser
from datetime import date, timedelta
from typing import Any, Dict
from common import RawTextWithDefaultsFormatter
from common.logger import setup_logging
def parse_args():
parser = ArgumentParser(
description=__doc__, formatter_class=RawTextWithDefaultsFormatter
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
dest="verbose",
help="Increase verbosity of logging output",
)
parser.add_argument(
"--dte",
type=int,
required=True,
help="Days to Expiry",
)
return parser.parse_args()
def determine_trading_day(dte: int) -> str:
from_date = date.today()
target_date = from_date + timedelta(days=dte)
while target_date.weekday() > 4: # 5 = Saturday, 6 = Sunday
target_date += timedelta(days=1)
return target_date
def analyze_option_chain(trading_day: str) -> Dict[str, Any]:
# Analyze option chain and return relevant data
pass
def open_position(option_data: Dict[str, Any]) -> Dict[str, Any]:
# Open the short straddle position
# Return position details
pass
def calculate_break_even_points(position: Dict[str, Any]) -> Dict[str, float]:
# Calculate and return break-even points
pass
def monitor_position(position: Dict[str, Any]) -> Dict[str, Any]:
# Monitor current prices and calculate position value
pass
def check_adjustment_needed(
monitoring_data: Dict[str, Any], initial_difference: float
) -> bool:
# Check if adjustment is needed
pass
def adjust_position(
position: Dict[str, Any], monitoring_data: Dict[str, Any]
) -> Dict[str, Any]:
# Perform position adjustment
# Return updated position
pass
def check_exit_conditions(
monitoring_data: Dict[str, Any], initial_credit: float
) -> bool:
# Check if exit conditions are met
pass
def close_position(position: Dict[str, Any]) -> Dict[str, Any]:
# Close the position and generate summary
pass
def run_bot(dte: int):
trading_day = determine_trading_day(dte)
option_data = analyze_option_chain(trading_day)
position = open_position(option_data)
initial_credit = position["initial_credit"]
initial_difference = position["initial_difference"]
while True:
monitoring_data = monitor_position(position)
if check_adjustment_needed(monitoring_data, initial_difference):
position = adjust_position(position, monitoring_data)
if check_exit_conditions(monitoring_data, initial_credit):
summary = close_position(position)
print(summary)
break
time.sleep(300) # Wait for 5 minutes before next check
def main(args):
logging.debug(f"Running bot with verbosity level: {args.verbose}")
run_bot(args.dte)
if __name__ == "__main__":
args = parse_args()
setup_logging(args.verbose)
main(args)