forked from himanjim/TradingScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptionStrategies.py
145 lines (111 loc) · 6 KB
/
OptionStrategies.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import enum
import math
class Strategy(enum.Enum):
BULL_CALL_SPREAD = 1
BULL_PUT_SPREAD = 2
BEAR_PUT_SPREAD = 3
BEAR_CALL_SPREAD = 4
def get_bull_call_spreads(options):
itm_options = [x for x in options if x.strike_price < x.spot_price and x.is_call and x.liquidity]
otm_options = [x for x in options if x.strike_price > x.spot_price and x.is_call and x.liquidity]
if len (itm_options) < 0:
print ('No ITM options')
return None, None, None, None, None
if len (otm_options) < 0:
print ('No ITM options')
return None, None, None, None, None
itm_option_to_trade = None
otm_option_to_trade = None
min_debit = math.inf
for itm_option in itm_options:
for otm_option in otm_options:
itm_option.ltp = min (x['price'] for x in itm_option.asks)
otm_option.ltp = max (x['price'] for x in otm_option.bids)
net_debit = itm_option.ltp - otm_option.ltp
if net_debit < min_debit:
itm_option_to_trade = itm_option
otm_option_to_trade = otm_option
min_debit = net_debit
net_debit = itm_option_to_trade.ltp - otm_option_to_trade.ltp
print ('Bull Call Spread: Buy ITM:%d, Sell OTM:%d, max loss(debit):%s, max profit:%s. Need low volatility' % (
itm_option_to_trade.strike_price, otm_option_to_trade.strike_price, net_debit,
(otm_option_to_trade.strike_price - itm_option_to_trade.strike_price - net_debit)))
return itm_option_to_trade, otm_option_to_trade, Strategy.BULL_CALL_SPREAD, -math.inf, net_debit
def get_bull_put_spreads(options):
itm_options = [x for x in options if x.strike_price > x.spot_price and x.is_call is False and x.liquidity]
otm_options = [x for x in options if x.strike_price < x.spot_price and x.is_call is False and x.liquidity]
if len (itm_options) < 0:
print ('No ITM options')
return None, None, None, None, None
if len (otm_options) < 0:
print ('No ITM options')
return None, None, None, None, None
itm_option_to_trade = None
otm_option_to_trade = None
max_credit = -math.inf
for itm_option in itm_options:
for otm_option in otm_options:
otm_option.ltp = min (x['price'] for x in otm_option.asks)
itm_option.ltp = max (x['price'] for x in itm_option.bids)
net_credit = itm_option.ltp - otm_option.ltp
if net_credit > max_credit:
itm_option_to_trade = itm_option
otm_option_to_trade = otm_option
max_credit = net_credit
net_credit = itm_option_to_trade.ltp - otm_option_to_trade.ltp
print ('Bull Put Spread: Buy OTM:%d, Sell ITM:%d, max loss:%s, max profit(credit):%s. Need High volatility' % (
otm_option_to_trade.strike_price, itm_option_to_trade.strike_price,
(otm_option_to_trade.strike_price - itm_option_to_trade.strike_price - net_credit), net_credit))
return itm_option_to_trade, otm_option_to_trade, Strategy.BULL_PUT_SPREAD, net_credit, math.inf
def get_bear_put_spreads(options):
itm_options = [x for x in options if x.strike_price > x.spot_price and x.is_call is False and x.liquidity]
otm_options = [x for x in options if x.strike_price < x.spot_price and x.is_call is False and x.liquidity]
if len (itm_options) < 0:
print ('No ITM options')
return None, None, None, None, None
if len (otm_options) < 0:
print ('No ITM options')
return None, None, None, None, None
itm_option_to_trade = None
otm_option_to_trade = None
min_debit = math.inf
for itm_option in itm_options:
for otm_option in otm_options:
itm_option.ltp = min (x['price'] for x in itm_option.asks)
otm_option.ltp = max (x['price'] for x in otm_option.bids)
net_debit = itm_option.ltp - otm_option.ltp
if net_debit < min_debit:
itm_option_to_trade = itm_option
otm_option_to_trade = otm_option
min_debit = net_debit
net_debit = itm_option_to_trade.ltp - otm_option_to_trade.ltp
print ('Bear Put Spread: Sell OTM:%d, buy ITM:%d, max loss(debit):%s, max profit:%s. Need Low volatility' % (
otm_option_to_trade.strike_price, itm_option_to_trade.strike_price, net_debit,
(itm_option_to_trade.strike_price - otm_option_to_trade.strike_price - net_debit)))
return itm_option_to_trade, otm_option_to_trade, Strategy.BEAR_PUT_SPREAD, -math.inf, net_debit
def get_bear_call_spreads(options):
itm_options = [x for x in options if x.strike_price < x.spot_price and x.is_call and x.liquidity]
otm_options = [x for x in options if x.strike_price > x.spot_price and x.is_call and x.liquidity]
if len (itm_options) < 0:
print ('No ITM options')
return None, None, None, None, None
if len (otm_options) < 0:
print ('No ITM options')
return None, None, None, None, None
itm_option_to_trade = None
otm_option_to_trade = None
max_credit = -math.inf
for itm_option in itm_options:
for otm_option in otm_options:
otm_option.ltp = min (x['price'] for x in otm_option.asks)
itm_option.ltp = max (x['price'] for x in itm_option.bids)
net_credit = itm_option.ltp - otm_option.ltp
if net_credit > max_credit:
itm_option_to_trade = itm_option
otm_option_to_trade = otm_option
max_credit = net_credit
net_credit = itm_option_to_trade.ltp - otm_option_to_trade.ltp
print ('Bear Call Spread: Sell ITM:%d, buy OTM:%d, max loss:%s, max profit(credit):%s. Need High volatility' % (
itm_option_to_trade.strike_price, otm_option_to_trade.strike_price,
(otm_option_to_trade.strike_price - itm_option_to_trade.strike_price - net_credit), net_credit))
return itm_option_to_trade, otm_option_to_trade, Strategy.BEAR_CALL_SPREAD, net_credit, math.inf