-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathtest_core.py
More file actions
388 lines (349 loc) · 12.5 KB
/
test_core.py
File metadata and controls
388 lines (349 loc) · 12.5 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import pytest
from optionlab import Inputs, run_strategy
from optionlab.models import Outputs
from optionlab.black_scholes import get_bs_info
COVERED_CALL_RESULT = {
"probability_of_profit": 0.5472008423945267,
"expected_profit_if_profitable": 1448.28,
"expected_loss_if_unprofitable": -1703.74,
"profit_ranges": [(164.9, float("inf"))],
"per_leg_cost": [-16899.0, 409.99999999999994],
"strategy_cost": -16489.0,
"minimum_return_in_the_domain": -9590.000000000002,
"maximum_return_in_the_domain": 2011.0,
"implied_volatility": [0.0, 0.456],
"in_the_money_probability": [1.0, 0.256866624586934],
"probability_of_touch": [1.0, 0.5277250352054264],
"delta": [1.0, -0.30713817729665704],
"gamma": [0.0, 0.013948977387090415],
"theta": [0.0, 0.19283555235589467],
"vega": [0.0, 0.1832408146218486],
"rho": [0.0, -0.04506390742751745],
}
PROB_100_ITM_RESULT = {
"probability_of_profit": 1.0,
"expected_profit_if_profitable": 492.57,
"profit_ranges": [(0.0, float("inf"))],
"per_leg_cost": [-750.0, 990.0],
"strategy_cost": 240.0,
"minimum_return_in_the_domain": 240.0,
"maximum_return_in_the_domain": 740.0000000000018,
"implied_volatility": [0.494, 0.482],
"in_the_money_probability": [0.54558925139931, 0.465831136209786],
"probability_of_touch": [1.0, 0.9661799112521838],
"delta": [0.6039490632362865, -0.525237550169406],
"gamma": [0.015297136732317718, 0.015806160944019643],
"theta": [-0.21821351060901806, 0.22301627833773927],
"vega": [0.20095091693287098, 0.20763771616023433],
"rho": [0.08536880237502181, -0.07509774107468528],
}
NAKED_CALL = {
"probability_of_profit": 0.8389215512144531,
"expected_profit_if_profitable": 113.49,
"expected_loss_if_unprofitable": -717.5,
"profit_ranges": [(0.0, 176.14)],
"per_leg_cost": [114.99999999999999],
"strategy_cost": 114.99999999999999,
"minimum_return_in_the_domain": -6991.999999999999,
"maximum_return_in_the_domain": 114.99999999999999,
"implied_volatility": [0.256],
"in_the_money_probability": [0.1832371984432129],
"probability_of_touch": [0.3741546603689868],
"delta": [-0.20371918274704337],
"gamma": [0.023104402361599465],
"theta": [0.091289876347897],
"vega": [0.12750177318341913],
"rho": [-0.02417676577711979],
"probability_of_profit_target": 0.8197909190785164,
"profit_target_ranges": [(0.0, 175.15)],
"probability_of_loss_limit": 0.14307836806156238,
"loss_limit_ranges": [(177.15, float("inf"))],
}
def test_black_scholes():
stock_price = 100.0
strike = 105.0
interest_rate = 1.0
dividend_yield = 0.0
volatility = 20.0
days_to_maturity = 60
interest_rate = interest_rate / 100
dividend_yield = dividend_yield / 100
volatility = volatility / 100
time_to_maturity = days_to_maturity / 365
bs = get_bs_info(
stock_price, strike, interest_rate, volatility, time_to_maturity, dividend_yield
)
assert bs.call_price == 1.44
assert bs.call_delta == 0.2942972000055033
assert bs.call_theta == -8.780589609657586
assert bs.call_rho == 0.04600635174517672
assert bs.call_itm_prob == 0.2669832523577367
assert round(bs.call_prob_of_touch, 12) == 0.540374479063
assert bs.put_price == 6.27
assert bs.put_delta == -0.7057027999944967
assert bs.put_theta == -7.732314219179215
assert bs.put_rho == -0.12631289052524033
assert bs.put_itm_prob == 0.7330167476422633
assert bs.put_prob_of_touch == 1.0
assert bs.gamma == 0.042503588182705464
assert bs.vega == 0.13973782416231934
def test_covered_call(nvidia):
inputs = Inputs.model_validate(
nvidia
| {
# The covered call strategy is defined
"strategy": [
{"type": "stock", "n": 100, "action": "buy"},
{
"type": "call",
"strike": 185.0,
"premium": 4.1,
"n": 100,
"action": "sell",
"expiration": nvidia["target_date"],
},
],
}
)
outputs = run_strategy(inputs)
assert isinstance(outputs, Outputs)
assert outputs.model_dump(
exclude={"data", "inputs"},
exclude_none=True,
exclude_defaults=True,
) == pytest.approx(COVERED_CALL_RESULT)
def test_covered_call_w_days_to_target(nvidia):
inputs = Inputs.model_validate(
nvidia
| {
"start_date": None,
"target_date": None,
"days_to_target_date": 24, # 32 days minus 9 non-business days plus 1 to consider the expiration date
"strategy": [
{"type": "stock", "n": 100, "action": "buy"},
{
"type": "call",
"strike": 185.0,
"premium": 4.1,
"n": 100,
"action": "sell",
},
],
}
)
outputs = run_strategy(inputs)
# Print useful information on screen
assert isinstance(outputs, Outputs)
assert outputs.model_dump(
exclude={"data", "inputs"},
exclude_none=True,
exclude_defaults=True,
) == pytest.approx(COVERED_CALL_RESULT)
def test_covered_call_w_prev_position(nvidia):
inputs = Inputs.model_validate(
nvidia
| {
# The covered call strategy is defined
"strategy": [
{"type": "stock", "n": 100, "action": "buy", "prev_pos": 158.99},
{
"type": "call",
"strike": 185.0,
"premium": 4.1,
"n": 100,
"action": "sell",
"expiration": nvidia["target_date"],
},
]
}
)
outputs = run_strategy(inputs)
assert outputs.model_dump(
exclude={"data", "inputs"},
exclude_none=True,
exclude_defaults=True,
) == {
"probability_of_profit": 0.7048129541301169,
"expected_profit_if_profitable": 2013.63,
"expected_loss_if_unprofitable": -1350.06,
"profit_ranges": [(154.9, float("inf"))],
"per_leg_cost": [-15899.0, 409.99999999999994],
"strategy_cost": -15489.0,
"minimum_return_in_the_domain": -8590.000000000002,
"maximum_return_in_the_domain": 3011.0,
"implied_volatility": [0.0, 0.456],
"in_the_money_probability": [1.0, 0.256866624586934],
"probability_of_touch": [1.0, 0.5277250352054264],
"delta": [1.0, -0.30713817729665704],
"gamma": [0.0, 0.013948977387090415],
"theta": [0.0, 0.19283555235589467],
"vega": [0.0, 0.1832408146218486],
"rho": [0.0, -0.04506390742751745],
}
def test_100_perc_itm(nvidia):
inputs = Inputs.model_validate(
nvidia
| {
# The covered call strategy is defined
"strategy": [
{
"type": "call",
"strike": 165.0,
"premium": 12.65,
"n": 100,
"action": "buy",
"prev_pos": 7.5,
"expiration": nvidia["target_date"],
},
{
"type": "call",
"strike": 170.0,
"premium": 9.9,
"n": 100,
"action": "sell",
"expiration": nvidia["target_date"],
},
]
}
)
outputs = run_strategy(inputs)
assert outputs.model_dump(
exclude={"data", "inputs"},
exclude_none=True,
exclude_defaults=True,
) == pytest.approx(PROB_100_ITM_RESULT)
def test_naked_call():
inputs = Inputs.model_validate(
{
"stock_price": 164.04,
"volatility": 0.272,
"start_date": "2021-11-22",
"target_date": "2021-12-17",
"interest_rate": 0.0002,
"min_stock": 82.02,
"max_stock": 246.06,
"profit_target": 100.0,
"loss_limit": -100.0,
"model": "black-scholes",
# The naked call strategy is defined
"strategy": [
{
"type": "call",
"strike": 175.00,
"premium": 1.15,
"n": 100,
"action": "sell",
}
],
}
)
outputs = run_strategy(inputs)
assert isinstance(outputs, Outputs)
assert outputs.model_dump(
exclude={"data", "inputs"}, exclude_none=True
) == pytest.approx(NAKED_CALL)
def test_3_legs(nvidia):
inputs = Inputs.model_validate(
nvidia
| {
"strategy": [
{"type": "stock", "n": 100, "action": "buy", "prev_pos": 158.99},
{
"type": "call",
"strike": 165.0,
"premium": 12.65,
"n": 100,
"action": "buy",
"prev_pos": 7.5,
"expiration": nvidia["target_date"],
},
{
"type": "call",
"strike": 170.0,
"premium": 9.9,
"n": 100,
"action": "sell",
"expiration": nvidia["target_date"],
},
]
}
)
outputs = run_strategy(inputs)
assert outputs.model_dump(
exclude={"data", "inputs"},
exclude_none=True,
exclude_defaults=True,
) == {
"probability_of_profit": 0.6790581742719213,
"expected_profit_if_profitable": 2956.8,
"expected_loss_if_unprofitable": -1404.83,
"profit_ranges": [(156.6, float("inf"))],
"per_leg_cost": [-15899.0, -750.0, 990.0],
"strategy_cost": -15659.0,
"minimum_return_in_the_domain": -8760.000000000002,
"maximum_return_in_the_domain": 11740.0,
"implied_volatility": [0.0, 0.494, 0.482],
"in_the_money_probability": [1.0, 0.54558925139931, 0.465831136209786],
"probability_of_touch": [1.0, 1.0, 0.9661799112521838],
"delta": [1.0, 0.6039490632362865, -0.525237550169406],
"gamma": [0.0, 0.015297136732317718, 0.015806160944019643],
"theta": [0.0, -0.21821351060901806, 0.22301627833773927],
"vega": [0.0, 0.20095091693287098, 0.20763771616023433],
"rho": [0.0, 0.08536880237502181, -0.07509774107468528],
}
def test_calendar_spread():
stock_price = 127.14 # Apple stock
volatility = 0.427
start_date = "2021-01-18"
target_date = "2021-01-29"
interest_rate = 0.0009
min_stock = stock_price - round(stock_price * 0.5, 2)
max_stock = stock_price + round(stock_price * 0.5, 2)
strategy = [
{
"type": "call",
"strike": 127.00,
"premium": 4.60,
"n": 1000,
"action": "sell",
},
{
"type": "call",
"strike": 127.00,
"premium": 5.90,
"n": 1000,
"action": "buy",
"expiration": "2021-02-12",
},
]
inputs = {
"stock_price": stock_price,
"start_date": start_date,
"target_date": target_date,
"volatility": volatility,
"interest_rate": interest_rate,
"min_stock": min_stock,
"max_stock": max_stock,
"strategy": strategy,
}
outputs = run_strategy(inputs)
assert outputs.model_dump(
exclude={"data", "inputs"}, exclude_none=True, exclude_defaults=True
) == {
"probability_of_profit": 0.599111819020198,
"expected_profit_if_profitable": 1383.2,
"expected_loss_if_unprofitable": -691.09,
"profit_ranges": [(118.87, 136.15)],
"per_leg_cost": [4600.0, -5900.0],
"strategy_cost": -1300.0,
"minimum_return_in_the_domain": -1300.0000000000146,
"maximum_return_in_the_domain": 3009.999999999999,
"implied_volatility": [0.47300000000000003, 0.419],
"in_the_money_probability": [0.4895105709759477, 0.4805997906939539],
"probability_of_touch": [1.0, 1.0],
"delta": [-0.5216914758915705, 0.5273457614638198],
"gamma": [0.03882722919950356, 0.02669940508461828],
"theta": [0.22727438444823292, -0.15634971608107964],
"vega": [0.09571294014902997, 0.1389462831961853],
"rho": [-0.022202087247849632, 0.046016214466188525],
}