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
25 changes: 15 additions & 10 deletions src/strategies/bull_put_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use super::shared::SpreadStrategy;
use crate::{
ExpirationDate, Options,
chains::{StrategyLegs, chain::OptionChain, utils::OptionDataGroup},
constants::ZERO,
error::{
GreeksError, OperationErrorKind, PricingError,
position::{PositionError, PositionValidationErrorKind},
Expand Down Expand Up @@ -612,18 +611,24 @@ impl Strategies for BullPutSpread {
}
}
fn get_max_loss(&self) -> Result<Positive, StrategyError> {
let width = self.short_put.option.strike_price - self.long_put.option.strike_price;
let max_loss =
(width * self.short_put.option.quantity) - self.get_net_premium_received()?;
if max_loss < ZERO {
Err(StrategyError::ProfitLossError(
let short_strike = self.short_put.option.strike_price.to_dec();
let long_strike = self.long_put.option.strike_price.to_dec();
let width = short_strike - long_strike;
if width < Decimal::ZERO {
return Err(StrategyError::ProfitLossError(
ProfitLossErrorKind::MaxLossError {
reason: "Max loss is negative".to_string(),
reason: "Short put strike must be above long put strike".to_string(),
},
))
} else {
Ok(max_loss)
));
}
let qty = self.short_put.option.quantity.to_dec();
let net_prem = self.get_net_premium_received()?.to_dec();
let max_loss_dec = (width * qty) - net_prem;
Positive::new_decimal(max_loss_dec).map_err(|_| {
StrategyError::ProfitLossError(ProfitLossErrorKind::MaxLossError {
reason: "Max loss is negative".to_string(),
})
})
}
fn get_profit_area(&self) -> Result<Decimal, StrategyError> {
let high = self.get_max_profit().unwrap_or(Positive::ZERO);
Expand Down
37 changes: 22 additions & 15 deletions src/strategies/call_butterfly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,21 @@ impl BreakEvenable for CallButterfly {
fn update_break_even_points(&mut self) -> Result<(), StrategyError> {
self.break_even_points = Vec::new();

self.break_even_points.push(
(self.long_call.option.strike_price
- self.calculate_profit_at(&self.long_call.option.strike_price)?
/ self.long_call.option.quantity)
.round_to(2),
);
let long_strike = self.long_call.option.strike_price.to_dec();
let long_qty = self.long_call.option.quantity.to_dec();
let long_profit = self.calculate_profit_at(&self.long_call.option.strike_price)?;
let candidate_low = long_strike - long_profit / long_qty;
if let Ok(be) = Positive::new_decimal(candidate_low) {
self.break_even_points.push(be.round_to(2));
}

self.break_even_points.push(
(self.short_call_high.option.strike_price
+ self.calculate_profit_at(&self.short_call_high.option.strike_price)?
/ self.short_call_high.option.quantity)
.round_to(2),
);
let short_strike = self.short_call_high.option.strike_price.to_dec();
let short_qty = self.short_call_high.option.quantity.to_dec();
let short_profit = self.calculate_profit_at(&self.short_call_high.option.strike_price)?;
let candidate_high = short_strike + short_profit / short_qty;
if let Ok(be) = Positive::new_decimal(candidate_high) {
self.break_even_points.push(be.round_to(2));
}

self.break_even_points.sort();
Ok(())
Expand Down Expand Up @@ -722,10 +724,15 @@ impl Strategies for CallButterfly {
BreakEvenErrorKind::NoBreakEvenPoints,
));
}
let base_low = break_even[1] - break_even[0];
let be0 = break_even[0].to_dec();
let be1 = break_even[1].to_dec();
let base_low_dec = be1 - be0;
let base_low = Positive::new_decimal(base_low_dec).unwrap_or(Positive::ZERO);
let max_profit = self.get_max_profit().unwrap_or(Positive::ZERO);
let base_high =
self.short_call_high.option.strike_price - self.short_call_low.option.strike_price;
let short_high = self.short_call_high.option.strike_price.to_dec();
let short_low = self.short_call_low.option.strike_price.to_dec();
let base_high_dec = short_high - short_low;
let base_high = Positive::new_decimal(base_high_dec).unwrap_or(Positive::ZERO);
Ok(
Decimal::from_f64((base_low.to_f64() + base_high.to_f64()) * max_profit.to_f64() / 2.0)
.unwrap_or(Decimal::ZERO),
Expand Down
12 changes: 8 additions & 4 deletions src/strategies/long_butterfly_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,17 @@ impl BreakEvenable for LongButterflySpread {
/ self.long_call_high.option.quantity;

if left_net_value <= Decimal::ZERO {
self.break_even_points
.push((self.long_call_low.option.strike_price - left_net_value).round_to(2));
let candidate = self.long_call_low.option.strike_price.to_dec() - left_net_value;
if let Ok(be) = Positive::new_decimal(candidate) {
self.break_even_points.push(be.round_to(2));
}
}

if right_net_value <= Decimal::ZERO {
self.break_even_points
.push((self.long_call_high.option.strike_price + right_net_value).round_to(2));
let candidate = self.long_call_high.option.strike_price.to_dec() + right_net_value;
if let Ok(be) = Positive::new_decimal(candidate) {
self.break_even_points.push(be.round_to(2));
}
}

self.break_even_points.sort();
Expand Down
Loading