Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/images/collar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/fence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/ladder_strategy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/risk_reversal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 98 additions & 7 deletions examples/options_pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,42 +273,69 @@ fn example_strategy() {
model.price(&put)
);

let otm_put = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put);
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
println!(
"[Collar: {:?}], given stock: {}, put: {}, call: {}",
model.collar(&instrument, &otm_put, &otm_call)(50.0),
instrument.spot,
model.price(&otm_put),
model.price(&otm_call)
);

let atm_put = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Put);
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
let otm_put = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put);
println!(
"[Fence: {:?}], given stock: {}, put: {}, put: {}, call: {}",
model.fence(&instrument, &atm_put, &otm_put, &otm_call)(50.0),
instrument.spot,
model.price(&atm_put),
model.price(&otm_call),
model.price(&otm_put)
);

// [Covered Call: 50.46060396445954], given stock: 50, call: 0.4606039644595379
// [Protective Put: 50.19404262184266], given stock: 50, put: 0.19404262184266008

////////////
/* SIMPLE */

let itm_call = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Call);
let itm_put = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Put);
let itm_call = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Call);
println!(
"[Guts: {:?}], given put: {}, call: {}",
model.guts(&itm_put, &itm_call)(50.0),
model.price(&itm_put),
model.price(&itm_call)
);

let atm_call = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call);
let atm_put = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Put);
let atm_call = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call);
println!(
"[Straddle: {:?}], given put: {}, call: {}",
model.straddle(&atm_put, &atm_call)(50.0),
model.price(&atm_put),
model.price(&atm_call)
);

let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
let otm_put = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put);
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
println!(
"[Strangle: {:?}], given put: {}, call: {}",
model.strangle(&otm_put, &otm_call)(50.0),
model.price(&otm_put),
model.price(&otm_call)
);

// [Guts: 20.604709034251407], given put: 10.310791308307145, call: 10.293917725944262
// [Straddle: 5.971892724319904], given put: 2.923524422096456, call: 3.048368302223448
// [Strangle: 0.654646586302198], given put: 0.19404262184266008, call: 0.4606039644595379
let otm_put = EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put);
let otm_call = EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call);
println!(
"[Risk Reversal: {:?}], given put: {}, call: {}",
model.risk_reversal(&otm_put, &otm_call)(50.0),
model.price(&otm_put),
model.price(&otm_call)
);

///////////////
/* BUTTERFLY */
Expand Down Expand Up @@ -361,13 +388,24 @@ fn example_strategy() {
let long1 = EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call);
let long2 = EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call);
println!(
"[Back Spread: {:?}], given short: {}, long1: {}, given long2: {}",
"[Back Spread: {:?}], given short: {}, long1: {}, long2: {}",
model.back_spread(&short, &long1, &long2)(50.0),
model.price(&short),
model.price(&long1),
model.price(&long2),
);

let long = EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call);
let short1 = EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call);
let short2 = EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call);
println!(
"[Ladder: {:?}], given long: {}, short1: {}, short2: {}",
model.ladder(&long, &short1, &short2)(50.0),
model.price(&short),
model.price(&long1),
model.price(&long2),
);

let front_month = EuropeanOption::new(instrument.clone(), 50.0, 1.0 / 12.0, Call);
let back_month = EuropeanOption::new(instrument.clone(), 50.0, 2.0 / 12.0, Call);
println!(
Expand Down Expand Up @@ -441,6 +479,33 @@ fn example_strategy() {
);
// => Protective Put: examples/images/protective_put.png

let options = vec![
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put),
EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call),
];
let _ = model.plot_strategy_breakdown(
"Collar",
model.collar(&instrument, &options[0], &options[1]),
20.0..80.0,
"examples/images/collar.png",
&options,
);
// => Protective Put: examples/images/collar.png

let options = vec![
EuropeanOption::new(instrument.clone(), 50.0, 1.0, Put),
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put),
EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call),
];
let _ = model.plot_strategy_breakdown(
"Fence",
model.fence(&instrument, &options[0], &options[1], &options[2]),
20.0..80.0,
"examples/images/fence.png",
&options,
);
// => Protective Put: examples/images/fence.png

let options = vec![
EuropeanOption::new(instrument.clone(), 60.0, 1.0, Put),
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Call),
Expand Down Expand Up @@ -481,6 +546,19 @@ fn example_strategy() {
);
// => Strangle: examples/images/strangle_strategy.png

let options = vec![
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Put),
EuropeanOption::new(instrument.clone(), 60.0, 1.0, Call),
];
let _ = model.plot_strategy_breakdown(
"Risk Reversal",
model.risk_reversal(&options[0], &options[1]),
20.0..80.0,
"examples/images/risk_reversal.png",
&options,
);
// => Strangle: examples/images/risk_reversal.png

let options = vec![
EuropeanOption::new(instrument.clone(), 40.0, 1.0, Call),
EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call),
Expand Down Expand Up @@ -576,6 +654,19 @@ fn example_strategy() {
&options,
); // => Back Spread: examples/images/back_spread_strategy.png

let options = vec![
EuropeanOption::new(instrument.clone(), 50.0, 1.0, Call),
EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call),
EuropeanOption::new(instrument.clone(), 55.0, 1.0, Call),
];
let _ = model.plot_strategy_breakdown(
"Ladder",
model.ladder(&options[0], &options[1], &options[2]),
20.0..80.0,
"examples/images/ladder_strategy.png",
&options,
);

let options = vec![
EuropeanOption::new(instrument.clone(), 50.0, 1.0 / 12.0, Call),
EuropeanOption::new(instrument.clone(), 50.0, 2.0 / 12.0, Call),
Expand Down
Loading