Skip to content

Commit 63e0d83

Browse files
47 implement missing strategies (#49)
* Implement collar strategy * Implement fence strategy * Implement ladder strategy * Implement risk reversal strategy
1 parent fe23aad commit 63e0d83

File tree

7 files changed

+267
-64
lines changed

7 files changed

+267
-64
lines changed

examples/images/collar.png

566 KB
Loading

examples/images/fence.png

652 KB
Loading
668 KB
Loading

examples/images/risk_reversal.png

611 KB
Loading

examples/options_pricing.rs

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,42 +273,69 @@ fn example_strategy() {
273273
model.price(&put)
274274
);
275275

276+
let otm_put = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put);
277+
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
278+
println!(
279+
"[Collar: {:?}], given stock: {}, put: {}, call: {}",
280+
model.collar(&instrument, &otm_put, &otm_call)(50.0),
281+
instrument.spot,
282+
model.price(&otm_put),
283+
model.price(&otm_call)
284+
);
285+
286+
let atm_put = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Put);
287+
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
288+
let otm_put = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put);
289+
println!(
290+
"[Fence: {:?}], given stock: {}, put: {}, put: {}, call: {}",
291+
model.fence(&instrument, &atm_put, &otm_put, &otm_call)(50.0),
292+
instrument.spot,
293+
model.price(&atm_put),
294+
model.price(&otm_call),
295+
model.price(&otm_put)
296+
);
297+
276298
// [Covered Call: 50.46060396445954], given stock: 50, call: 0.4606039644595379
277299
// [Protective Put: 50.19404262184266], given stock: 50, put: 0.19404262184266008
278300

279301
////////////
280302
/* SIMPLE */
281303

282-
let itm_call = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Call);
283304
let itm_put = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Put);
305+
let itm_call = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Call);
284306
println!(
285307
"[Guts: {:?}], given put: {}, call: {}",
286308
model.guts(&itm_put, &itm_call)(50.0),
287309
model.price(&itm_put),
288310
model.price(&itm_call)
289311
);
290312

291-
let atm_call = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call);
292313
let atm_put = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Put);
314+
let atm_call = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call);
293315
println!(
294316
"[Straddle: {:?}], given put: {}, call: {}",
295317
model.straddle(&atm_put, &atm_call)(50.0),
296318
model.price(&atm_put),
297319
model.price(&atm_call)
298320
);
299321

300-
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
301322
let otm_put = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put);
323+
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
302324
println!(
303325
"[Strangle: {:?}], given put: {}, call: {}",
304326
model.strangle(&otm_put, &otm_call)(50.0),
305327
model.price(&otm_put),
306328
model.price(&otm_call)
307329
);
308330

309-
// [Guts: 20.604709034251407], given put: 10.310791308307145, call: 10.293917725944262
310-
// [Straddle: 5.971892724319904], given put: 2.923524422096456, call: 3.048368302223448
311-
// [Strangle: 0.654646586302198], given put: 0.19404262184266008, call: 0.4606039644595379
331+
let otm_put = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put);
332+
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
333+
println!(
334+
"[Risk Reversal: {:?}], given put: {}, call: {}",
335+
model.risk_reversal(&otm_put, &otm_call)(50.0),
336+
model.price(&otm_put),
337+
model.price(&otm_call)
338+
);
312339

313340
///////////////
314341
/* BUTTERFLY */
@@ -361,13 +388,24 @@ fn example_strategy() {
361388
let long1 = EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call);
362389
let long2 = EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call);
363390
println!(
364-
"[Back Spread: {:?}], given short: {}, long1: {}, given long2: {}",
391+
"[Back Spread: {:?}], given short: {}, long1: {}, long2: {}",
365392
model.back_spread(&short, &long1, &long2)(50.0),
366393
model.price(&short),
367394
model.price(&long1),
368395
model.price(&long2),
369396
);
370397

398+
let long = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call);
399+
let short1 = EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call);
400+
let short2 = EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call);
401+
println!(
402+
"[Ladder: {:?}], given long: {}, short1: {}, short2: {}",
403+
model.ladder(&long, &short1, &short2)(50.0),
404+
model.price(&short),
405+
model.price(&long1),
406+
model.price(&long2),
407+
);
408+
371409
let front_month = EuropeanOption::new(instrument.clone(), 50.0, 1.0 / 12.0, Call);
372410
let back_month = EuropeanOption::new(instrument.clone(), 50.0, 2.0 / 12.0, Call);
373411
println!(
@@ -441,6 +479,33 @@ fn example_strategy() {
441479
);
442480
// => Protective Put: examples/images/protective_put.png
443481

482+
let options = vec![
483+
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put),
484+
EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call),
485+
];
486+
let _ = model.plot_strategy_breakdown(
487+
"Collar",
488+
model.collar(&instrument, &options[0], &options[1]),
489+
20.0..80.0,
490+
"examples/images/collar.png",
491+
&options,
492+
);
493+
// => Protective Put: examples/images/collar.png
494+
495+
let options = vec![
496+
EuropeanOption::new(instrument.clone(), 50.0, 1.0, Put),
497+
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put),
498+
EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call),
499+
];
500+
let _ = model.plot_strategy_breakdown(
501+
"Fence",
502+
model.fence(&instrument, &options[0], &options[1], &options[2]),
503+
20.0..80.0,
504+
"examples/images/fence.png",
505+
&options,
506+
);
507+
// => Protective Put: examples/images/fence.png
508+
444509
let options = vec![
445510
EuropeanOption::new(instrument.clone(), 60.0, 1.0, Put),
446511
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Call),
@@ -481,6 +546,19 @@ fn example_strategy() {
481546
);
482547
// => Strangle: examples/images/strangle_strategy.png
483548

549+
let options = vec![
550+
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put),
551+
EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call),
552+
];
553+
let _ = model.plot_strategy_breakdown(
554+
"Risk Reversal",
555+
model.risk_reversal(&options[0], &options[1]),
556+
20.0..80.0,
557+
"examples/images/risk_reversal.png",
558+
&options,
559+
);
560+
// => Strangle: examples/images/risk_reversal.png
561+
484562
let options = vec![
485563
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Call),
486564
EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call),
@@ -576,6 +654,19 @@ fn example_strategy() {
576654
&options,
577655
); // => Back Spread: examples/images/back_spread_strategy.png
578656

657+
let options = vec![
658+
EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call),
659+
EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call),
660+
EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call),
661+
];
662+
let _ = model.plot_strategy_breakdown(
663+
"Ladder",
664+
model.ladder(&options[0], &options[1], &options[2]),
665+
20.0..80.0,
666+
"examples/images/ladder_strategy.png",
667+
&options,
668+
);
669+
579670
let options = vec![
580671
EuropeanOption::new(instrument.clone(), 50.0, 1.0 / 12.0, Call),
581672
EuropeanOption::new(instrument.clone(), 50.0, 2.0 / 12.0, Call),

0 commit comments

Comments
 (0)