#![allow(clippy::indexing_slicing)]
use super::base::{
BreakEvenable, Optimizable, Positionable, Strategable, StrategyBasics, StrategyType, Validable,
};
use crate::chains::OptionData;
use crate::{
ExpirationDate, Options,
chains::{StrategyLegs, chain::OptionChain},
constants::ZERO,
error::{
GreeksError, OperationErrorKind, PricingError,
position::{PositionError, PositionValidationErrorKind},
probability::ProbabilityError,
strategies::{ProfitLossErrorKind, StrategyError},
},
greeks::Greeks,
model::{
ProfitLossRange,
decimal::d_sum,
position::Position,
types::{OptionBasicType, OptionStyle, OptionType, Side},
utils::mean_and_std,
},
pnl::{PnLCalculator, utils::PnL},
pricing::payoff::Profit,
strategies::{
BasicAble, Strategies, StrategyConstructor,
delta_neutral::DeltaNeutrality,
probabilities::{core::ProbabilityAnalysis, utils::VolatilityAdjustment},
utils::{FindOptimalSide, OptimizationCriteria},
},
test_strategy_traits,
};
use chrono::Utc;
use num_traits::FromPrimitive;
use positive::Positive;
use pretty_simple_display::{DebugPretty, DisplaySimple};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use tracing::debug;
use utoipa::ToSchema;
pub(super) const PMCC_DESCRIPTION: &str = "A Poor Man's Covered Call (PMCC) is an options strategy that simulates a covered call \
using long-term equity anticipation securities (LEAPS) instead of the underlying stock. \
It involves buying a long-term in-the-money call option and selling a short-term out-of-the-money call option. \
This strategy aims to generate income while reducing the capital required compared to a traditional covered call.";
#[derive(Clone, DebugPretty, DisplaySimple, Serialize, Deserialize, ToSchema)]
pub struct PoorMansCoveredCall {
pub name: String,
pub kind: StrategyType,
pub description: String,
pub break_even_points: Vec<Positive>,
pub(super) long_call: Position,
pub(super) short_call: Position,
}
impl PoorMansCoveredCall {
#[allow(clippy::too_many_arguments)]
#[inline(never)]
pub fn new(
underlying_symbol: String,
underlying_price: Positive,
long_call_strike: Positive,
short_call_strike: Positive,
long_call_expiration: ExpirationDate,
short_call_expiration: ExpirationDate,
implied_volatility: Positive,
risk_free_rate: Decimal,
dividend_yield: Positive,
quantity: Positive,
premium_long_call: Positive,
premium_short_call: Positive,
open_fee_long_call: Positive,
close_fee_long_call: Positive,
open_fee_short_call: Positive,
close_fee_short_call: Positive,
) -> Result<Self, StrategyError> {
let mut strategy = PoorMansCoveredCall::default();
let long_call_option = Options::new(
OptionType::European,
Side::Long,
underlying_symbol.clone(),
long_call_strike,
long_call_expiration,
implied_volatility,
quantity,
underlying_price,
risk_free_rate,
OptionStyle::Call,
dividend_yield,
None,
);
let long_call = Position::new(
long_call_option,
premium_long_call,
Utc::now(),
open_fee_long_call,
close_fee_long_call,
None,
None,
);
strategy.add_position(&long_call)?;
let short_call_option = Options::new(
OptionType::European,
Side::Short,
underlying_symbol,
short_call_strike,
short_call_expiration,
implied_volatility,
quantity,
underlying_price,
risk_free_rate,
OptionStyle::Call,
dividend_yield,
None,
);
let short_call = Position::new(
short_call_option,
premium_short_call,
Utc::now(),
open_fee_short_call,
close_fee_short_call,
None,
None,
);
strategy.add_position(&short_call)?;
strategy.update_break_even_points()?;
Ok(strategy)
}
}
impl StrategyConstructor for PoorMansCoveredCall {
fn get_strategy(vec_positions: &[Position]) -> Result<Self, StrategyError> {
if vec_positions.len() != 2 {
return Err(StrategyError::OperationError(
OperationErrorKind::InvalidParameters {
operation: "Poor Man's Covered Call get_strategy".to_string(),
reason: "Must have exactly 2 options".to_string(),
},
));
}
let mut sorted_positions = vec_positions.to_vec();
sorted_positions.sort_by(|a, b| {
a.option
.strike_price
.partial_cmp(&b.option.strike_price)
.unwrap_or(std::cmp::Ordering::Equal)
});
let lower_strike_position = &sorted_positions[0];
let higher_strike_position = &sorted_positions[1];
if lower_strike_position.option.option_style != OptionStyle::Call
|| higher_strike_position.option.option_style != OptionStyle::Call
{
return Err(StrategyError::OperationError(
OperationErrorKind::InvalidParameters {
operation: "Poor Man's Covered Call get_strategy".to_string(),
reason: "Options must be calls".to_string(),
},
));
}
if lower_strike_position.option.side != Side::Long
|| higher_strike_position.option.side != Side::Short
{
return Err(StrategyError::OperationError(OperationErrorKind::InvalidParameters {
operation: "Poor Man's Covered Call get_strategy".to_string(),
reason: "Poor Man's Covered Call requires a long lower strike call and a short higher strike call".to_string(),
}));
}
let long_call = Position::new(
lower_strike_position.option.clone(),
lower_strike_position.premium,
Utc::now(),
lower_strike_position.open_fee,
lower_strike_position.close_fee,
lower_strike_position.epic.clone(),
lower_strike_position.extra_fields.clone(),
);
let short_call = Position::new(
higher_strike_position.option.clone(),
higher_strike_position.premium,
Utc::now(),
higher_strike_position.open_fee,
higher_strike_position.close_fee,
higher_strike_position.epic.clone(),
higher_strike_position.extra_fields.clone(),
);
let mut strategy = PoorMansCoveredCall {
name: "Poor Man's Covered Call".to_string(),
kind: StrategyType::PoorMansCoveredCall,
description: PMCC_DESCRIPTION.to_string(),
break_even_points: Vec::new(),
long_call,
short_call,
};
strategy.validate();
strategy.update_break_even_points()?;
Ok(strategy)
}
}
impl BreakEvenable for PoorMansCoveredCall {
fn get_break_even_points(&self) -> Result<&Vec<Positive>, StrategyError> {
Ok(&self.break_even_points)
}
fn update_break_even_points(&mut self) -> Result<(), StrategyError> {
self.break_even_points = Vec::new();
let net_debit = self.get_net_cost()? / self.long_call.option.quantity;
self.break_even_points
.push((self.long_call.option.strike_price + net_debit).round_to(2));
Ok(())
}
}
impl Validable for PoorMansCoveredCall {
fn validate(&self) -> bool {
self.short_call.validate() && self.long_call.validate()
}
}
impl Positionable for PoorMansCoveredCall {
fn add_position(&mut self, position: &Position) -> Result<(), PositionError> {
match (position.option.option_style, position.option.side) {
(OptionStyle::Call, Side::Long) => {
self.long_call = position.clone();
Ok(())
}
(OptionStyle::Call, Side::Short) => {
self.short_call = position.clone();
Ok(())
}
_ => Err(PositionError::invalid_position_style(
position.option.option_style,
"Position is a Put, it is not valid for PoorMansCoveredCall".to_string(),
)),
}
}
fn get_positions(&self) -> Result<Vec<&Position>, PositionError> {
Ok(vec![&self.short_call, &self.long_call])
}
fn get_position(
&mut self,
option_style: &OptionStyle,
side: &Side,
strike: &Positive,
) -> Result<Vec<&mut Position>, PositionError> {
match (side, option_style, strike) {
(_, OptionStyle::Put, _) => Err(PositionError::invalid_position_type(
*side,
"Put is not valid for PoorMansCoveredCall".to_string(),
)),
(Side::Long, OptionStyle::Call, strike)
if *strike == self.long_call.option.strike_price =>
{
Ok(vec![&mut self.long_call])
}
(Side::Short, OptionStyle::Call, strike)
if *strike == self.short_call.option.strike_price =>
{
Ok(vec![&mut self.short_call])
}
_ => Err(PositionError::invalid_position_type(
*side,
"Strike not found in positions".to_string(),
)),
}
}
fn modify_position(&mut self, position: &Position) -> Result<(), PositionError> {
if !position.validate() {
return Err(PositionError::ValidationError(
PositionValidationErrorKind::InvalidPosition {
reason: "Invalid position data".to_string(),
},
));
}
match (
&position.option.side,
&position.option.option_style,
&position.option.strike_price,
) {
(_, OptionStyle::Put, _) => {
return Err(PositionError::invalid_position_type(
position.option.side,
"Put is not valid for PoorMansCoveredCall".to_string(),
));
}
(Side::Long, OptionStyle::Call, strike)
if *strike == self.long_call.option.strike_price =>
{
self.long_call = position.clone();
}
(Side::Short, OptionStyle::Call, strike)
if *strike == self.short_call.option.strike_price =>
{
self.short_call = position.clone();
}
_ => {
return Err(PositionError::invalid_position_type(
position.option.side,
"Strike not found in positions".to_string(),
));
}
}
Ok(())
}
}
impl Strategable for PoorMansCoveredCall {
fn info(&self) -> Result<StrategyBasics, StrategyError> {
Ok(StrategyBasics {
name: self.name.clone(),
kind: self.kind.clone(),
description: self.description.clone(),
})
}
}
impl BasicAble for PoorMansCoveredCall {
fn get_title(&self) -> String {
let strategy_title = format!("{:?} Strategy: ", self.kind);
let leg_titles: Vec<String> = [self.short_call.get_title(), self.long_call.get_title()]
.iter()
.map(|leg| leg.to_string())
.collect();
if leg_titles.is_empty() {
strategy_title
} else {
format!("{}\n\t{}", strategy_title, leg_titles.join("\n\t"))
}
}
fn get_option_basic_type(&self) -> HashSet<OptionBasicType<'_>> {
let mut hash_set = HashSet::new();
let short_call = &self.short_call.option;
let long_call = &self.long_call.option;
hash_set.insert(OptionBasicType {
option_style: &short_call.option_style,
side: &short_call.side,
strike_price: &short_call.strike_price,
expiration_date: &short_call.expiration_date,
});
hash_set.insert(OptionBasicType {
option_style: &long_call.option_style,
side: &long_call.side,
strike_price: &long_call.strike_price,
expiration_date: &long_call.expiration_date,
});
hash_set
}
fn get_implied_volatility(&self) -> HashMap<OptionBasicType<'_>, &Positive> {
let options = [
(
&self.short_call.option,
&self.short_call.option.implied_volatility,
),
(
&self.long_call.option,
&self.long_call.option.implied_volatility,
),
];
options
.into_iter()
.map(|(option, iv)| {
(
OptionBasicType {
option_style: &option.option_style,
side: &option.side,
strike_price: &option.strike_price,
expiration_date: &option.expiration_date,
},
iv,
)
})
.collect()
}
fn get_quantity(&self) -> HashMap<OptionBasicType<'_>, &Positive> {
let options = [
(&self.short_call.option, &self.short_call.option.quantity),
(&self.long_call.option, &self.long_call.option.quantity),
];
options
.into_iter()
.map(|(option, quantity)| {
(
OptionBasicType {
option_style: &option.option_style,
side: &option.side,
strike_price: &option.strike_price,
expiration_date: &option.expiration_date,
},
quantity,
)
})
.collect()
}
fn one_option(&self) -> &Options {
self.short_call.one_option()
}
fn one_option_mut(&mut self) -> &mut Options {
self.short_call.one_option_mut()
}
fn set_expiration_date(
&mut self,
expiration_date: ExpirationDate,
) -> Result<(), StrategyError> {
self.short_call.option.expiration_date = expiration_date;
self.long_call.option.expiration_date = expiration_date;
Ok(())
}
fn set_underlying_price(&mut self, price: &Positive) -> Result<(), StrategyError> {
self.short_call.option.underlying_price = *price;
self.short_call.premium = Positive::new_decimal(
self.short_call
.option
.calculate_price_black_scholes()?
.abs(),
)
.unwrap_or(Positive::ZERO);
self.long_call.option.underlying_price = *price;
self.long_call.premium =
Positive::new_decimal(self.long_call.option.calculate_price_black_scholes()?.abs())
.unwrap_or(Positive::ZERO);
Ok(())
}
fn set_implied_volatility(&mut self, volatility: &Positive) -> Result<(), StrategyError> {
self.short_call.option.implied_volatility = *volatility;
self.long_call.option.implied_volatility = *volatility;
self.short_call.premium = Positive::new_decimal(
self.short_call
.option
.calculate_price_black_scholes()?
.abs(),
)
.unwrap_or(Positive::ZERO);
self.long_call.premium =
Positive::new_decimal(self.long_call.option.calculate_price_black_scholes()?.abs())
.unwrap_or(Positive::ZERO);
Ok(())
}
}
impl Strategies for PoorMansCoveredCall {
fn get_max_profit(&self) -> Result<Positive, StrategyError> {
let profit = self.calculate_profit_at(&self.short_call.option.strike_price)?;
if profit <= Decimal::ZERO {
Err(StrategyError::ProfitLossError(
ProfitLossErrorKind::MaxProfitError {
reason: "Max profit is negative".to_string(),
},
))
} else {
Ok(Positive::new_decimal(profit)?)
}
}
fn get_max_loss(&self) -> Result<Positive, StrategyError> {
let loss = self.calculate_profit_at(&self.long_call.option.strike_price)?;
if loss >= Decimal::ZERO {
Err(StrategyError::ProfitLossError(
ProfitLossErrorKind::MaxLossError {
reason: "Max loss must be negative".to_string(),
},
))
} else {
Ok(Positive::new_decimal(loss.abs()).unwrap_or(Positive::ZERO))
}
}
fn get_profit_area(&self) -> Result<Decimal, StrategyError> {
let base = (self.short_call.option.strike_price
- (self.short_call.option.strike_price
- self.get_max_profit().unwrap_or(Positive::ZERO)))
.to_f64();
let high = self.get_max_profit().unwrap_or(Positive::ZERO).to_f64();
let result = base * high / 200.0;
Decimal::from_f64(result).ok_or_else(|| StrategyError::numeric_conversion(result))
}
fn get_profit_ratio(&self) -> Result<Decimal, StrategyError> {
let result = match (self.get_max_profit(), self.get_max_loss()) {
(Ok(profit), Ok(loss)) => (profit / loss).to_f64() * 100.0,
_ => ZERO,
};
Decimal::from_f64(result).ok_or_else(|| StrategyError::numeric_conversion(result))
}
}
impl Optimizable for PoorMansCoveredCall {
type Strategy = PoorMansCoveredCall;
fn find_optimal(
&mut self,
option_chain: &OptionChain,
side: FindOptimalSide,
criteria: OptimizationCriteria,
) {
let options: Vec<&OptionData> = option_chain.options.iter().collect();
let mut best_value = Decimal::MIN;
for long_call_index in 0..options.len() {
let long_call_option = &options[long_call_index];
for short_call_option in &options[(long_call_index + 1)..] {
debug!(
"Long: {:#?} Short: {:#?}",
long_call_option.strike_price, short_call_option.strike_price
);
if long_call_option.strike_price >= short_call_option.strike_price {
debug!(
"Invalid strike prices long call option: {:#?} short call option: {:#?} ",
long_call_option.strike_price, short_call_option.strike_price
);
continue;
}
if side == FindOptimalSide::Center {
if !self.is_valid_optimal_option(short_call_option, &FindOptimalSide::Upper)
|| !self.is_valid_optimal_option(long_call_option, &FindOptimalSide::Lower)
{
debug!("Invalid option");
continue;
}
} else if !self.is_valid_optimal_option(short_call_option, &side)
|| !self.is_valid_optimal_option(long_call_option, &side)
{
debug!("Invalid option");
continue;
}
let legs = StrategyLegs::TwoLegs {
first: long_call_option,
second: short_call_option,
};
let strategy: PoorMansCoveredCall = match self.create_strategy(option_chain, &legs)
{
Ok(s) => s,
Err(e) => {
tracing::warn!(
error = %e,
"skipping invalid strategy combination"
);
continue;
}
};
if !strategy.validate() {
debug!("Invalid strategy");
continue;
}
let metric = match criteria {
OptimizationCriteria::Ratio => strategy.get_profit_ratio(),
OptimizationCriteria::Area => strategy.get_profit_area(),
};
let current_value = match metric {
Ok(v) => v,
Err(e) => {
tracing::warn!(error = %e, "skipping candidate with unscorable metric");
continue;
}
};
if current_value > best_value {
best_value = current_value;
*self = strategy.clone();
}
}
}
}
fn create_strategy(
&self,
chain: &OptionChain,
legs: &StrategyLegs,
) -> Result<Self::Strategy, StrategyError> {
let (long, short) = match legs {
StrategyLegs::TwoLegs { first, second } => (first, second),
_ => {
return Err(StrategyError::operation_not_supported(
"create_strategy",
"PoorMansCoveredCall requires exactly two legs (TwoLegs)",
));
}
};
let implied_volatility = short.implied_volatility;
if implied_volatility > Positive::ONE {
return Err(StrategyError::invalid_parameters(
"create_strategy",
&format!(
"implied volatility {implied_volatility} exceeds the supported maximum of 1.0"
),
));
}
let long_call_ask = long.call_ask.ok_or_else(|| {
StrategyError::operation_not_supported(
"create_strategy",
"missing call_ask for long call leg",
)
})?;
let short_call_bid = short.call_bid.ok_or_else(|| {
StrategyError::operation_not_supported(
"create_strategy",
"missing call_bid for short call leg",
)
})?;
PoorMansCoveredCall::new(
chain.symbol.clone(),
chain.underlying_price,
long.strike_price,
short.strike_price,
self.long_call.option.expiration_date,
self.short_call.option.expiration_date,
implied_volatility,
self.short_call.option.risk_free_rate,
self.short_call.option.dividend_yield,
self.short_call.option.quantity,
long_call_ask,
short_call_bid,
self.long_call.open_fee,
self.long_call.close_fee,
self.short_call.open_fee,
self.short_call.close_fee,
)
}
}
impl Profit for PoorMansCoveredCall {
fn calculate_profit_at(&self, price: &Positive) -> Result<Decimal, PricingError> {
let price = Some(price);
Ok(d_sum(
&[
self.long_call.pnl_at_expiration(&price)?,
self.short_call.pnl_at_expiration(&price)?,
],
"strategies::poor_mans_covered_call::profit_at",
)?)
}
}
impl ProbabilityAnalysis for PoorMansCoveredCall {
fn get_profit_ranges(&self) -> Result<Vec<ProfitLossRange>, ProbabilityError> {
let break_even_point = self.get_break_even_points()?[0];
let option = &self.short_call.option;
let expiration_date = &option.expiration_date;
let risk_free_rate = option.risk_free_rate;
let (mean_volatility, std_dev) = mean_and_std(vec![
self.short_call.option.implied_volatility,
self.long_call.option.implied_volatility,
]);
let mut profit_range = ProfitLossRange::new(Some(break_even_point), None, Positive::ZERO)?;
profit_range.calculate_probability(
self.get_underlying_price(),
Some(VolatilityAdjustment {
base_volatility: mean_volatility,
std_dev_adjustment: std_dev,
}),
None,
expiration_date,
Some(risk_free_rate),
)?;
Ok(vec![profit_range])
}
fn get_loss_ranges(&self) -> Result<Vec<ProfitLossRange>, ProbabilityError> {
let break_even_point = self.get_break_even_points()?[0];
let option = &self.short_call.option;
let expiration_date = &option.expiration_date;
let risk_free_rate = option.risk_free_rate;
let (mean_volatility, std_dev) = mean_and_std(vec![
self.long_call.option.implied_volatility,
self.short_call.option.implied_volatility,
]);
let mut loss_range = ProfitLossRange::new(None, Some(break_even_point), Positive::ZERO)?;
loss_range.calculate_probability(
self.get_underlying_price(),
Some(VolatilityAdjustment {
base_volatility: mean_volatility,
std_dev_adjustment: std_dev,
}),
None,
expiration_date,
Some(risk_free_rate),
)?;
Ok(vec![loss_range])
}
}
impl Greeks for PoorMansCoveredCall {
fn get_options(&self) -> Result<Vec<&Options>, GreeksError> {
Ok(vec![&self.long_call.option, &self.short_call.option])
}
}
impl DeltaNeutrality for PoorMansCoveredCall {}
impl PnLCalculator for PoorMansCoveredCall {
fn calculate_pnl(
&self,
market_price: &Positive,
expiration_date: ExpirationDate,
implied_volatility: &Positive,
) -> Result<PnL, PricingError> {
Ok(self
.long_call
.calculate_pnl(market_price, expiration_date, implied_volatility)?
+ self
.short_call
.calculate_pnl(market_price, expiration_date, implied_volatility)?)
}
fn calculate_pnl_at_expiration(
&self,
underlying_price: &Positive,
) -> Result<PnL, PricingError> {
Ok(self
.long_call
.calculate_pnl_at_expiration(underlying_price)?
+ self
.short_call
.calculate_pnl_at_expiration(underlying_price)?)
}
}
test_strategy_traits!(PoorMansCoveredCall, test_short_call_implementations);
#[cfg(test)]
mod tests_pmcc_validation {
use super::*;
use positive::pos_or_panic;
use crate::error::position::PositionValidationErrorKind;
use positive::constants::DAYS_IN_A_YEAR;
use rust_decimal_macros::dec;
fn create_basic_strategy() -> PoorMansCoveredCall {
PoorMansCoveredCall::new(
"AAPL".to_string(),
pos_or_panic!(150.0),
pos_or_panic!(140.0),
pos_or_panic!(160.0),
ExpirationDate::Days(DAYS_IN_A_YEAR),
ExpirationDate::Days(pos_or_panic!(30.0)),
pos_or_panic!(0.2),
dec!(0.01),
pos_or_panic!(0.005),
Positive::ONE,
pos_or_panic!(15.0),
pos_or_panic!(5.0),
Positive::ONE,
Positive::ONE,
pos_or_panic!(0.5),
pos_or_panic!(0.5),
)
.unwrap()
}
#[test]
fn test_validate_valid_strategy() {
let strategy = create_basic_strategy();
assert!(strategy.validate());
}
#[test]
fn test_add_leg_long_call() {
let mut strategy = create_basic_strategy();
let option = Options::new(
OptionType::European,
Side::Long,
"AAPL".to_string(),
pos_or_panic!(140.0),
ExpirationDate::Days(DAYS_IN_A_YEAR),
pos_or_panic!(0.2),
Positive::ONE,
pos_or_panic!(150.0),
dec!(0.01),
OptionStyle::Call,
pos_or_panic!(0.005),
None,
);
let position = Position::new(
option,
pos_or_panic!(15.0),
Utc::now(),
Positive::ONE,
Positive::ONE,
None,
None,
);
strategy
.add_position(&position)
.expect("Invalid long call option");
assert_eq!(strategy.long_call, position);
}
#[test]
fn test_add_leg_short_call() {
let mut strategy = create_basic_strategy();
let option = Options::new(
OptionType::European,
Side::Short,
"AAPL".to_string(),
pos_or_panic!(160.0),
ExpirationDate::Days(pos_or_panic!(30.0)),
pos_or_panic!(0.2),
Positive::ONE,
pos_or_panic!(150.0),
dec!(0.01),
OptionStyle::Call,
pos_or_panic!(0.005),
None,
);
let position = Position::new(
option,
pos_or_panic!(5.0),
Utc::now(),
pos_or_panic!(0.5),
pos_or_panic!(0.5),
None,
None,
);
strategy
.add_position(&position)
.expect("Invalid short call option");
assert_eq!(strategy.short_call, position);
}
#[test]
fn test_add_leg_invalid_option() {
let mut strategy = create_basic_strategy();
let option = Options::new(
OptionType::European,
Side::Long,
"AAPL".to_string(),
pos_or_panic!(140.0),
ExpirationDate::Days(DAYS_IN_A_YEAR),
pos_or_panic!(0.2),
Positive::ONE,
pos_or_panic!(150.0),
dec!(0.01),
OptionStyle::Put,
pos_or_panic!(0.005),
None,
);
let position = Position::new(
option,
pos_or_panic!(15.0),
Utc::now(),
Positive::ONE,
Positive::ONE,
None,
None,
);
let err = strategy.add_position(&position).unwrap_err();
assert!(matches!(
err,
PositionError::ValidationError(
PositionValidationErrorKind::IncompatibleStyle {
style: OptionStyle::Put,
reason
}
) if reason == "Position is a Put, it is not valid for PoorMansCoveredCall"
));
}
}
#[cfg(test)]
mod tests_pmcc_optimization {
use super::*;
use positive::{pos_or_panic, spos};
use crate::chains::OptionData;
use positive::constants::DAYS_IN_A_YEAR;
use rust_decimal_macros::dec;
fn create_test_option_chain() -> OptionChain {
let mut chain = OptionChain::new(
"AAPL",
pos_or_panic!(150.0),
"2024-01-01".to_string(),
None,
None,
);
for strike in [140.0, 145.0, 150.0, 155.0, 160.0].iter() {
chain.add_option(
pos_or_panic!(*strike),
spos!(5.0),
spos!(5.2),
spos!(4.8),
spos!(5.0),
pos_or_panic!(0.2),
Some(dec!(0.5)),
None,
None,
spos!(100.0),
Some(50),
None,
);
}
chain
}
fn create_base_strategy() -> PoorMansCoveredCall {
PoorMansCoveredCall::new(
"AAPL".to_string(),
pos_or_panic!(150.0),
pos_or_panic!(140.0),
pos_or_panic!(160.0),
ExpirationDate::Days(DAYS_IN_A_YEAR),
ExpirationDate::Days(pos_or_panic!(30.0)),
pos_or_panic!(0.2),
dec!(0.01),
pos_or_panic!(0.005),
Positive::ONE,
pos_or_panic!(15.0),
pos_or_panic!(5.0),
Positive::ONE,
Positive::ONE,
pos_or_panic!(0.5),
pos_or_panic!(0.5),
)
.unwrap()
}
#[test]
fn test_is_valid_short_option() {
let strategy = create_base_strategy();
let option = OptionData::new(
pos_or_panic!(160.0),
spos!(5.0),
spos!(5.2),
spos!(4.8),
spos!(5.0),
pos_or_panic!(0.2),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
);
assert!(strategy.is_valid_optimal_option(&option, &FindOptimalSide::Upper));
}
#[test]
fn test_is_valid_long_option() {
let strategy = create_base_strategy();
let option = OptionData::new(
pos_or_panic!(140.0),
spos!(5.0),
spos!(5.2),
spos!(4.8),
spos!(5.0),
pos_or_panic!(0.2),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
);
assert!(strategy.is_valid_optimal_option(&option, &FindOptimalSide::Lower));
}
#[test]
fn test_find_optimal_ratio() {
let mut strategy = create_base_strategy();
let chain = create_test_option_chain();
strategy.find_optimal(&chain, FindOptimalSide::All, OptimizationCriteria::Ratio);
assert!(strategy.validate());
}
#[test]
fn test_find_optimal_area() {
let mut strategy = create_base_strategy();
let chain = create_test_option_chain();
strategy.find_optimal(&chain, FindOptimalSide::All, OptimizationCriteria::Area);
assert!(strategy.validate());
}
#[test]
#[should_panic]
fn test_invalid_short_option_zero_underlying() {
let mut strategy = create_base_strategy();
strategy.short_call.option.underlying_price = Positive::ZERO;
let option = OptionData::new(
pos_or_panic!(160.0),
spos!(5.0),
spos!(5.2),
spos!(4.8),
spos!(5.0),
pos_or_panic!(0.2),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
);
assert!(!strategy.is_valid_optimal_option(&option, &FindOptimalSide::Upper));
}
#[test]
fn test_invalid_long_option_zero_underlying() {
let mut strategy = create_base_strategy();
let result = strategy.set_underlying_price(&Positive::ZERO);
assert!(result.is_err());
let option = OptionData::new(
pos_or_panic!(140.0),
spos!(5.0),
spos!(5.2),
spos!(4.8),
spos!(5.0),
pos_or_panic!(0.2),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
);
assert!(!strategy.is_valid_optimal_option(&option, &FindOptimalSide::Lower));
}
}
#[cfg(test)]
mod tests_pmcc_pnl {
use super::*;
use positive::constants::DAYS_IN_A_YEAR;
use num_traits::ToPrimitive;
use positive::pos_or_panic;
use rust_decimal_macros::dec;
fn create_test_strategy() -> PoorMansCoveredCall {
PoorMansCoveredCall::new(
"AAPL".to_string(),
pos_or_panic!(150.0),
pos_or_panic!(140.0),
pos_or_panic!(160.0),
ExpirationDate::Days(DAYS_IN_A_YEAR),
ExpirationDate::Days(pos_or_panic!(30.0)),
pos_or_panic!(0.2),
dec!(0.01),
pos_or_panic!(0.005),
Positive::ONE,
pos_or_panic!(15.0),
pos_or_panic!(5.0),
Positive::ONE,
Positive::ONE,
pos_or_panic!(0.5),
pos_or_panic!(0.5),
)
.unwrap()
}
#[test]
fn test_calculate_profit_at_various_prices() {
let strategy = create_test_strategy();
let profit_below = strategy.calculate_profit_at(&pos_or_panic!(130.0)).unwrap();
assert!(profit_below < Decimal::ZERO);
let profit_middle = strategy.calculate_profit_at(&pos_or_panic!(150.0)).unwrap();
assert!(profit_middle > profit_below);
let profit_short = strategy
.calculate_profit_at(&strategy.short_call.option.strike_price)
.unwrap();
assert_eq!(
profit_short,
strategy.get_max_profit().unwrap_or(Positive::ZERO).to_dec()
);
let profit_above = strategy.calculate_profit_at(&pos_or_panic!(170.0)).unwrap();
assert_eq!(profit_above, profit_above);
}
#[test]
fn test_break_even_point() {
let strategy = create_test_strategy();
assert_eq!(strategy.break_even_points.len(), 1);
let break_even = strategy.break_even_points[0];
let profit_at_be = strategy
.calculate_profit_at(&break_even)
.unwrap()
.to_f64()
.unwrap();
assert!(profit_at_be.abs() < 0.01);
}
#[test]
fn test_net_premium() {
let strategy = create_test_strategy();
let net_premium = strategy.get_net_premium_received().unwrap();
assert_eq!(net_premium, 0.0);
}
#[test]
fn test_max_profit_max_loss_relationship() {
let strategy = create_test_strategy();
assert!(strategy.get_max_profit().unwrap_or(Positive::ZERO) > Positive::ZERO);
assert!(strategy.get_max_loss().unwrap_or(Positive::ZERO) > Positive::ZERO);
assert!(
strategy.get_max_loss().unwrap_or(Positive::ZERO)
> strategy.get_max_profit().unwrap_or(Positive::ZERO)
);
}
}
#[cfg(test)]
mod tests_pmcc_best_area {
use super::*;
use positive::constants::DAYS_IN_A_YEAR;
use num_traits::ToPrimitive;
use positive::pos_or_panic;
use rust_decimal_macros::dec;
fn set_up() -> Result<(PoorMansCoveredCall, OptionChain), crate::error::Error> {
let option_chain =
OptionChain::load_from_json("./examples/Chains/SP500-18-oct-2024-5781.88.json")
.unwrap();
let underlying_price = option_chain.underlying_price;
let strategy = PoorMansCoveredCall::new(
"SP500".to_string(),
underlying_price,
pos_or_panic!(5700.0), pos_or_panic!(5900.0), ExpirationDate::Days(DAYS_IN_A_YEAR),
ExpirationDate::Days(pos_or_panic!(30.0)),
pos_or_panic!(0.2),
dec!(0.01),
pos_or_panic!(0.005),
Positive::ONE,
pos_or_panic!(15.0),
pos_or_panic!(5.0),
Positive::ONE,
Positive::ONE,
pos_or_panic!(0.5),
pos_or_panic!(0.5),
)
.unwrap();
Ok((strategy, option_chain))
}
#[test]
fn test_best_area_all() {
let (mut strategy, option_chain) = set_up().unwrap();
strategy.get_best_area(&option_chain, FindOptimalSide::All);
assert!(strategy.get_profit_area().unwrap().to_f64().unwrap() > 0.0);
assert!(strategy.get_profit_ratio().unwrap().to_f64().unwrap() > 0.0);
assert_eq!(strategy.break_even_points.len(), 1);
assert!(strategy.get_total_cost().unwrap() > Positive::ZERO);
assert!(strategy.get_fees().unwrap().to_f64() > 0.0);
assert!(strategy.long_call.option.strike_price < strategy.short_call.option.strike_price);
}
#[test]
fn test_best_area_upper() {
let (mut strategy, option_chain) = set_up().unwrap();
strategy.get_best_area(&option_chain, FindOptimalSide::Upper);
assert!(strategy.long_call.option.strike_price >= *strategy.get_underlying_price());
assert!(strategy.short_call.option.strike_price > strategy.long_call.option.strike_price);
assert!(strategy.get_profit_area().unwrap().to_f64().unwrap() > 0.0);
assert!(strategy.get_max_profit().unwrap_or(Positive::ZERO) > Positive::ZERO);
}
#[test]
fn test_best_area_lower() {
let (mut strategy, option_chain) = set_up().unwrap();
strategy.get_best_area(&option_chain, FindOptimalSide::Lower);
assert!(strategy.long_call.option.strike_price <= *strategy.get_underlying_price());
assert!(strategy.short_call.option.strike_price > strategy.long_call.option.strike_price);
assert!(strategy.get_profit_area().unwrap().to_f64().unwrap() > 0.0);
assert!(strategy.validate());
}
}
#[cfg(test)]
mod tests_pmcc_best_ratio {
use super::*;
use positive::constants::DAYS_IN_A_YEAR;
use num_traits::ToPrimitive;
use positive::pos_or_panic;
use rust_decimal_macros::dec;
fn set_up() -> Result<(PoorMansCoveredCall, OptionChain), crate::error::Error> {
let option_chain =
OptionChain::load_from_json("./examples/Chains/SP500-18-oct-2024-5781.88.json")
.unwrap();
let underlying_price = option_chain.underlying_price;
let strategy = PoorMansCoveredCall::new(
"SP500".to_string(),
underlying_price,
pos_or_panic!(5700.0),
pos_or_panic!(5900.0),
ExpirationDate::Days(DAYS_IN_A_YEAR),
ExpirationDate::Days(pos_or_panic!(30.0)),
pos_or_panic!(0.2),
dec!(0.01),
pos_or_panic!(0.005),
Positive::ONE,
pos_or_panic!(15.0),
pos_or_panic!(5.0),
Positive::ONE,
Positive::ONE,
pos_or_panic!(0.5),
pos_or_panic!(0.5),
)
.unwrap();
Ok((strategy, option_chain))
}
#[test]
fn test_best_ratio_all() {
let (mut strategy, option_chain) = set_up().unwrap();
strategy.get_best_ratio(&option_chain, FindOptimalSide::All);
assert!(strategy.get_profit_ratio().unwrap().to_f64().unwrap() > 0.0);
assert_eq!(strategy.break_even_points.len(), 1);
assert!(strategy.get_max_profit().unwrap_or(Positive::ZERO) > Positive::ZERO);
assert!(strategy.get_max_loss().unwrap_or(Positive::ZERO) > Positive::ZERO);
assert!(strategy.get_fees().unwrap().to_f64() > 0.0);
}
#[test]
fn test_best_ratio_upper() {
let (mut strategy, option_chain) = set_up().unwrap();
strategy.get_best_ratio(&option_chain, FindOptimalSide::Upper);
assert!(strategy.long_call.option.strike_price >= *strategy.get_underlying_price());
assert!(strategy.short_call.option.strike_price > strategy.long_call.option.strike_price);
assert!(strategy.get_profit_ratio().unwrap().to_f64().unwrap() > 0.0);
assert!(strategy.validate());
}
#[test]
fn test_best_ratio_with_range() {
let (mut strategy, option_chain) = set_up().unwrap();
strategy.get_best_ratio(
&option_chain,
FindOptimalSide::Range(pos_or_panic!(5750.0), pos_or_panic!(5850.0)),
);
assert!(strategy.long_call.option.strike_price >= pos_or_panic!(5750.0));
assert!(strategy.short_call.option.strike_price <= pos_or_panic!(5850.0));
assert!(strategy.get_profit_ratio().unwrap().to_f64().unwrap() > 0.0);
assert!(strategy.validate());
}
}
#[cfg(test)]
mod tests_short_straddle_delta {
use super::*;
use positive::{assert_pos_relative_eq, pos_or_panic};
use crate::assert_decimal_eq;
use crate::model::types::OptionStyle;
use crate::strategies::delta_neutral::DELTA_THRESHOLD;
use crate::strategies::delta_neutral::{DeltaAdjustment, DeltaNeutrality};
use crate::strategies::poor_mans_covered_call::PoorMansCoveredCall;
use rust_decimal_macros::dec;
fn get_strategy(long_strike: Positive, short_strike: Positive) -> PoorMansCoveredCall {
let underlying_price = pos_or_panic!(7138.5);
PoorMansCoveredCall::new(
"CL".to_string(),
underlying_price, long_strike, short_strike, ExpirationDate::Days(pos_or_panic!(45.0)),
ExpirationDate::Days(pos_or_panic!(15.0)),
pos_or_panic!(0.3745), dec!(0.05), Positive::ZERO, Positive::ONE, pos_or_panic!(84.2), pos_or_panic!(353.2), pos_or_panic!(7.01), pos_or_panic!(7.01), pos_or_panic!(7.01), pos_or_panic!(7.01), )
.unwrap()
}
#[test]
fn create_test_short_straddle_reducing_adjustments() {
let strategy = get_strategy(pos_or_panic!(7250.0), pos_or_panic!(7300.0));
let size = dec!(0.0887293);
let delta = pos_or_panic!(0.2168462168831);
let k = pos_or_panic!(7300.0);
assert_decimal_eq!(
strategy.delta_neutrality().unwrap().net_delta,
size,
DELTA_THRESHOLD
);
assert!(!strategy.is_delta_neutral());
let binding = strategy.delta_adjustments().unwrap();
match &binding[1] {
DeltaAdjustment::BuyOptions {
quantity,
strike,
option_style,
side,
} => {
assert_pos_relative_eq!(
*quantity,
delta,
Positive::new_decimal(DELTA_THRESHOLD).unwrap()
);
assert_pos_relative_eq!(
*strike,
k,
Positive::new_decimal(DELTA_THRESHOLD).unwrap()
);
assert_eq!(*option_style, OptionStyle::Call);
assert_eq!(*side, Side::Short);
}
_ => panic!("Invalid suggestion"),
}
let mut option = strategy.short_call.option.clone();
option.quantity = delta;
let delta = option.delta().unwrap();
assert_decimal_eq!(delta, -size, DELTA_THRESHOLD);
assert_decimal_eq!(
delta + strategy.delta_neutrality().unwrap().net_delta,
Decimal::ZERO,
DELTA_THRESHOLD
);
}
#[test]
fn create_test_short_straddle_increasing_adjustments() {
let strategy = get_strategy(pos_or_panic!(7450.0), pos_or_panic!(7250.0));
let size = dec!(-0.028694805);
let delta = pos_or_panic!(0.0689809869957862);
let k = pos_or_panic!(7450.0);
assert_decimal_eq!(
strategy.delta_neutrality().unwrap().net_delta,
size,
DELTA_THRESHOLD
);
assert!(!strategy.is_delta_neutral());
let binding = strategy.delta_adjustments().unwrap();
let suggestion = binding.first().unwrap();
match suggestion {
DeltaAdjustment::BuyOptions {
quantity,
strike,
option_style,
side,
} => {
assert_pos_relative_eq!(
*quantity,
delta,
Positive::new_decimal(DELTA_THRESHOLD).unwrap()
);
assert_pos_relative_eq!(
*strike,
k,
Positive::new_decimal(DELTA_THRESHOLD).unwrap()
);
assert_eq!(*option_style, OptionStyle::Call);
assert_eq!(*side, Side::Long);
}
_ => panic!("Invalid suggestion"),
}
let mut option = strategy.long_call.option.clone();
option.quantity = delta;
let delta = option.delta().unwrap();
assert_decimal_eq!(delta, -size, DELTA_THRESHOLD);
assert_decimal_eq!(
delta + strategy.delta_neutrality().unwrap().net_delta,
Decimal::ZERO,
DELTA_THRESHOLD
);
}
#[test]
fn create_test_short_straddle_no_adjustments() {
let strategy = get_strategy(pos_or_panic!(7379.0), pos_or_panic!(7250.0));
assert_decimal_eq!(
strategy.delta_neutrality().unwrap().net_delta,
Decimal::ZERO,
DELTA_THRESHOLD
);
assert!(strategy.is_delta_neutral());
let suggestion = strategy.delta_adjustments().unwrap();
assert_eq!(suggestion[0], DeltaAdjustment::NoAdjustmentNeeded);
}
}
#[cfg(test)]
mod tests_short_straddle_delta_size {
use super::*;
use positive::{assert_pos_relative_eq, pos_or_panic};
use crate::assert_decimal_eq;
use crate::model::types::OptionStyle;
use crate::strategies::delta_neutral::DELTA_THRESHOLD;
use crate::strategies::delta_neutral::{DeltaAdjustment, DeltaNeutrality};
use crate::strategies::poor_mans_covered_call::PoorMansCoveredCall;
use rust_decimal_macros::dec;
fn get_strategy(long_strike: Positive, short_strike: Positive) -> PoorMansCoveredCall {
let underlying_price = pos_or_panic!(7138.5);
PoorMansCoveredCall::new(
"CL".to_string(),
underlying_price, long_strike, short_strike, ExpirationDate::Days(pos_or_panic!(45.0)),
ExpirationDate::Days(pos_or_panic!(15.0)),
pos_or_panic!(0.3745), dec!(0.05), Positive::ZERO, Positive::TWO, pos_or_panic!(84.2), pos_or_panic!(353.2), pos_or_panic!(7.01), pos_or_panic!(7.01), pos_or_panic!(7.01), pos_or_panic!(7.01), )
.unwrap()
}
#[test]
fn create_test_short_straddle_reducing_adjustments() {
let strategy = get_strategy(pos_or_panic!(7250.1), pos_or_panic!(7300.0));
let size = dec!(0.1773);
let delta = pos_or_panic!(0.4334878994986714);
let k = pos_or_panic!(7300.0);
assert_decimal_eq!(
strategy.delta_neutrality().unwrap().net_delta,
size,
DELTA_THRESHOLD
);
assert!(!strategy.is_delta_neutral());
let binding = strategy.delta_adjustments().unwrap();
match &binding[1] {
DeltaAdjustment::BuyOptions {
quantity,
strike,
option_style,
side,
} => {
assert_pos_relative_eq!(
*quantity,
delta,
Positive::new_decimal(DELTA_THRESHOLD).unwrap()
);
assert_pos_relative_eq!(
*strike,
k,
Positive::new_decimal(DELTA_THRESHOLD).unwrap()
);
assert_eq!(*option_style, OptionStyle::Call);
assert_eq!(*side, Side::Short);
}
_ => panic!("Invalid suggestion"),
}
let mut option = strategy.short_call.option.clone();
option.quantity = delta;
let delta = option.delta().unwrap();
assert_decimal_eq!(delta, -size, DELTA_THRESHOLD);
assert_decimal_eq!(
delta + strategy.delta_neutrality().unwrap().net_delta,
Decimal::ZERO,
DELTA_THRESHOLD
);
}
#[test]
fn create_test_short_straddle_increasing_adjustments() {
let strategy = get_strategy(pos_or_panic!(7450.0), pos_or_panic!(7250.0));
let size = dec!(-0.057389);
let delta = pos_or_panic!(0.1379619739915724);
let k = pos_or_panic!(7450.0);
assert_decimal_eq!(
strategy.delta_neutrality().unwrap().net_delta,
size,
DELTA_THRESHOLD
);
assert!(!strategy.is_delta_neutral());
let binding = strategy.delta_adjustments().unwrap();
let suggestion = binding.first().unwrap();
match suggestion {
DeltaAdjustment::BuyOptions {
quantity,
strike,
option_style,
side,
} => {
assert_pos_relative_eq!(
*quantity,
delta,
Positive::new_decimal(DELTA_THRESHOLD).unwrap()
);
assert_pos_relative_eq!(
*strike,
k,
Positive::new_decimal(DELTA_THRESHOLD).unwrap()
);
assert_eq!(*option_style, OptionStyle::Call);
assert_eq!(*side, Side::Long);
}
_ => panic!("Invalid suggestion"),
}
let mut option = strategy.long_call.option.clone();
option.quantity = delta;
let delta = option.delta().unwrap();
assert_decimal_eq!(delta, -size, DELTA_THRESHOLD);
assert_decimal_eq!(
delta + strategy.delta_neutrality().unwrap().net_delta,
Decimal::ZERO,
DELTA_THRESHOLD
);
}
#[test]
fn create_test_short_straddle_no_adjustments() {
let strategy = get_strategy(pos_or_panic!(7387.5), pos_or_panic!(7255.0));
assert_decimal_eq!(
strategy.delta_neutrality().unwrap().net_delta,
Decimal::ZERO,
DELTA_THRESHOLD
);
assert!(strategy.is_delta_neutral());
let suggestion = strategy.delta_adjustments().unwrap();
assert_eq!(suggestion[0], DeltaAdjustment::NoAdjustmentNeeded);
}
}
#[cfg(test)]
mod tests_poor_mans_covered_call_probability {
use super::*;
use positive::{assert_pos_relative_eq, pos_or_panic};
use crate::strategies::probabilities::utils::PriceTrend;
use rust_decimal_macros::dec;
fn create_test_pmcc() -> PoorMansCoveredCall {
PoorMansCoveredCall::new(
"GOLD".to_string(), pos_or_panic!(2703.3), pos_or_panic!(2600.0), pos_or_panic!(2800.0), ExpirationDate::Days(pos_or_panic!(120.0)), ExpirationDate::Days(pos_or_panic!(30.0)), pos_or_panic!(0.17), dec!(0.05), Positive::ZERO, pos_or_panic!(3.0), pos_or_panic!(154.7), pos_or_panic!(30.8), pos_or_panic!(1.74), pos_or_panic!(1.74), pos_or_panic!(0.85), pos_or_panic!(0.85), )
.unwrap()
}
#[test]
fn test_get_expiration() {
let pmcc = create_test_pmcc();
let expected_dates = [
ExpirationDate::Days(pos_or_panic!(30.0)),
ExpirationDate::Days(pos_or_panic!(120.0)),
];
for date in pmcc.get_expiration().values() {
assert!(expected_dates.contains(date));
}
}
#[test]
fn test_get_risk_free_rate() {
let pmcc = create_test_pmcc();
assert_eq!(
**pmcc.get_risk_free_rate().values().next().unwrap(),
dec!(0.05)
);
}
#[test]
fn test_get_profit_ranges() {
let pmcc = create_test_pmcc();
let result = pmcc.get_profit_ranges();
assert!(result.is_ok());
let ranges = result.unwrap();
assert_eq!(ranges.len(), 1);
let range = &ranges[0];
assert!(range.lower_bound.is_some());
assert!(range.upper_bound.is_none()); assert!(range.probability > Positive::ZERO);
assert!(range.probability <= Positive::ONE);
assert!(range.lower_bound.unwrap() > pmcc.long_call.option.strike_price);
}
#[test]
fn test_get_loss_ranges() {
let pmcc = create_test_pmcc();
let result = pmcc.get_loss_ranges();
assert!(result.is_ok());
let ranges = result.unwrap();
assert_eq!(ranges.len(), 1);
let loss_range = &ranges[0];
assert!(loss_range.lower_bound.is_none()); assert!(loss_range.upper_bound.is_some());
assert!(loss_range.probability > Positive::ZERO);
assert!(loss_range.probability <= Positive::ONE);
}
#[test]
fn test_probability_sum_to_one() {
let pmcc = create_test_pmcc();
let profit_ranges = pmcc.get_profit_ranges().unwrap();
let loss_ranges = pmcc.get_loss_ranges().unwrap();
let total_profit_prob: Positive = profit_ranges.iter().map(|r| r.probability).sum();
let total_loss_prob: Positive = loss_ranges.iter().map(|r| r.probability).sum();
assert_pos_relative_eq!(
total_profit_prob + total_loss_prob,
Positive::ONE,
pos_or_panic!(0.0001)
);
}
#[test]
fn test_break_even_points_validity() {
let pmcc = create_test_pmcc();
let break_even_points = pmcc.get_break_even_points().unwrap();
assert_eq!(break_even_points.len(), 1);
assert!(break_even_points[0] > pmcc.long_call.option.strike_price);
assert!(break_even_points[0] < pmcc.short_call.option.strike_price);
}
#[test]
fn test_with_volatility_adjustment() {
let pmcc = create_test_pmcc();
let vol_adj = Some(VolatilityAdjustment {
base_volatility: pos_or_panic!(0.25),
std_dev_adjustment: pos_or_panic!(0.05),
});
let prob = pmcc.probability_of_profit(vol_adj, None);
assert!(prob.is_ok());
let probability = prob.unwrap();
assert!(probability > Positive::ZERO);
assert!(probability <= Positive::ONE);
}
#[test]
fn test_with_price_trend() {
let pmcc = create_test_pmcc();
let trend = Some(PriceTrend {
drift_rate: 0.1,
confidence: 0.95,
});
let prob = pmcc.probability_of_profit(None, trend);
assert!(prob.is_ok());
let probability = prob.unwrap();
assert!(probability > Positive::ZERO);
assert!(probability <= Positive::ONE);
}
#[test]
fn test_analyze_probabilities() {
let pmcc = create_test_pmcc();
let analysis = pmcc.analyze_probabilities(None, None).unwrap();
assert!(analysis.probability_of_profit > Positive::ZERO);
assert!(analysis.expected_value >= Positive::ZERO);
assert_eq!(analysis.break_even_points.len(), 1);
assert!(analysis.risk_reward_ratio > Positive::ZERO);
}
#[test]
fn test_different_expirations_validity() {
let pmcc = create_test_pmcc();
assert!(match pmcc.short_call.option.expiration_date {
ExpirationDate::Days(short_days) => {
match pmcc.long_call.option.expiration_date {
ExpirationDate::Days(long_days) => short_days < long_days,
_ => false,
}
}
_ => false,
});
}
#[test]
fn test_high_volatility_scenario() {
let pmcc = create_test_pmcc();
let vol_adj = Some(VolatilityAdjustment {
base_volatility: pos_or_panic!(0.7),
std_dev_adjustment: pos_or_panic!(0.05),
});
let analysis = pmcc.analyze_probabilities(vol_adj, None).unwrap();
assert!(analysis.expected_value == Positive::ZERO);
}
#[test]
fn test_extreme_probabilities() {
let pmcc = create_test_pmcc();
let result = pmcc.calculate_extreme_probabilities(None, None);
assert!(result.is_ok());
let (max_profit_prob, max_loss_prob) = result.unwrap();
assert!(max_profit_prob >= Positive::ZERO);
assert!(max_loss_prob >= Positive::ZERO);
assert!(max_profit_prob + max_loss_prob <= Positive::ONE);
}
#[test]
fn test_strike_price_validity() {
let pmcc = create_test_pmcc();
assert!(pmcc.short_call.option.strike_price > pmcc.long_call.option.strike_price);
}
}
#[cfg(test)]
mod tests_poor_mans_covered_call_position_management {
use super::*;
use positive::pos_or_panic;
use crate::error::position::PositionValidationErrorKind;
use crate::model::types::{OptionStyle, Side};
use rust_decimal_macros::dec;
use tracing::error;
fn create_test_short_poor_mans_covered_call() -> PoorMansCoveredCall {
PoorMansCoveredCall::new(
"GOLD".to_string(), pos_or_panic!(2703.3), pos_or_panic!(2600.0), pos_or_panic!(2800.0), ExpirationDate::Days(pos_or_panic!(120.0)), ExpirationDate::Days(pos_or_panic!(30.0)), pos_or_panic!(0.17), dec!(0.05), Positive::ZERO, Positive::TWO, pos_or_panic!(154.7), pos_or_panic!(30.8), pos_or_panic!(1.74), pos_or_panic!(1.74), pos_or_panic!(0.85), pos_or_panic!(0.85), )
.unwrap()
}
#[test]
fn test_short_poor_mans_covered_call_get_position() {
let mut poor_mans_covered_call = create_test_short_poor_mans_covered_call();
let call_position = poor_mans_covered_call.get_position(
&OptionStyle::Call,
&Side::Long,
&pos_or_panic!(2600.0),
);
assert!(call_position.is_ok());
let positions = call_position.unwrap();
assert_eq!(positions.len(), 1);
assert_eq!(positions[0].option.strike_price, pos_or_panic!(2600.0));
assert_eq!(positions[0].option.option_style, OptionStyle::Call);
assert_eq!(positions[0].option.side, Side::Long);
let put_position = poor_mans_covered_call.get_position(
&OptionStyle::Call,
&Side::Short,
&pos_or_panic!(2800.0),
);
assert!(put_position.is_ok());
let positions = put_position.unwrap();
assert_eq!(positions.len(), 1);
assert_eq!(positions[0].option.strike_price, pos_or_panic!(2800.0));
assert_eq!(positions[0].option.option_style, OptionStyle::Call);
assert_eq!(positions[0].option.side, Side::Short);
let invalid_position = poor_mans_covered_call.get_position(
&OptionStyle::Call,
&Side::Short,
&pos_or_panic!(2801.0),
);
assert!(invalid_position.is_err());
match invalid_position {
Err(PositionError::ValidationError(
PositionValidationErrorKind::IncompatibleSide {
position_side: _,
reason,
},
)) => {
assert_eq!(reason, "Strike not found in positions");
}
_ => {
error!("Unexpected error: {:?}", invalid_position);
panic!()
}
}
}
#[test]
fn test_short_poor_mans_covered_call_modify_position() {
let mut poor_mans_covered_call = create_test_short_poor_mans_covered_call();
let mut modified_call = poor_mans_covered_call.short_call.clone();
modified_call.option.quantity = Positive::TWO;
let result = poor_mans_covered_call.modify_position(&modified_call);
assert!(result.is_ok());
assert_eq!(
poor_mans_covered_call.short_call.option.quantity,
Positive::TWO
);
let mut modified_put = poor_mans_covered_call.long_call.clone();
modified_put.option.quantity = Positive::TWO;
let result = poor_mans_covered_call.modify_position(&modified_put);
assert!(result.is_ok());
assert_eq!(
poor_mans_covered_call.long_call.option.quantity,
Positive::TWO
);
let mut invalid_position = poor_mans_covered_call.short_call.clone();
invalid_position.option.strike_price = pos_or_panic!(95.0);
let result = poor_mans_covered_call.modify_position(&invalid_position);
assert!(result.is_err());
match result {
Err(PositionError::ValidationError(kind)) => match kind {
PositionValidationErrorKind::IncompatibleSide {
position_side: _,
reason,
} => {
assert_eq!(reason, "Strike not found in positions");
}
_ => panic!("Expected ValidationError::InvalidPosition"),
},
_ => panic!("Expected ValidationError"),
}
}
}
#[cfg(test)]
mod tests_adjust_option_position {
use super::*;
use positive::pos_or_panic;
use crate::model::types::{OptionStyle, Side};
use rust_decimal_macros::dec;
fn create_test_strategy() -> PoorMansCoveredCall {
PoorMansCoveredCall::new(
"GOLD".to_string(), pos_or_panic!(2703.3), pos_or_panic!(2600.0), pos_or_panic!(2800.0), ExpirationDate::Days(pos_or_panic!(120.0)), ExpirationDate::Days(pos_or_panic!(30.0)), pos_or_panic!(0.17), dec!(0.05), Positive::ZERO, Positive::TWO, pos_or_panic!(154.7), pos_or_panic!(30.8), pos_or_panic!(1.74), pos_or_panic!(1.74), pos_or_panic!(0.85), pos_or_panic!(0.85), )
.unwrap()
}
#[test]
fn test_adjust_existing_call_position() {
let mut strategy = create_test_strategy();
let initial_quantity = strategy.short_call.option.quantity;
let adjustment = Positive::ONE;
let result = strategy.adjust_option_position(
adjustment.to_dec(),
&pos_or_panic!(2800.0),
&OptionStyle::Call,
&Side::Short,
);
assert!(result.is_ok());
assert_eq!(
strategy.short_call.option.quantity,
initial_quantity + adjustment
);
}
#[test]
fn test_adjust_existing_put_position() {
let mut strategy = create_test_strategy();
let initial_quantity = strategy.long_call.option.quantity;
let adjustment = Positive::ONE;
let result = strategy.adjust_option_position(
adjustment.to_dec(),
&pos_or_panic!(2600.0),
&OptionStyle::Call,
&Side::Long,
);
assert!(result.is_ok());
assert_eq!(
strategy.long_call.option.quantity,
initial_quantity + adjustment
);
}
#[test]
fn test_adjust_nonexistent_position() {
let mut strategy = create_test_strategy();
let result = strategy.adjust_option_position(
Decimal::ONE,
&pos_or_panic!(110.0),
&OptionStyle::Put,
&Side::Long,
);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(
err.to_string()
.contains("Put is not valid for PoorMansCoveredCall")
);
}
#[test]
fn test_adjust_with_invalid_strike() {
let mut strategy = create_test_strategy();
let result = strategy.adjust_option_position(
Decimal::ONE,
&Positive::HUNDRED, &OptionStyle::Call,
&Side::Short,
);
assert!(result.is_err());
}
#[test]
fn test_zero_quantity_adjustment() {
let mut strategy = create_test_strategy();
let initial_quantity = strategy.short_call.option.quantity;
let result = strategy.adjust_option_position(
Decimal::ZERO,
&pos_or_panic!(2800.0),
&OptionStyle::Call,
&Side::Short,
);
assert!(result.is_ok());
assert_eq!(strategy.short_call.option.quantity, initial_quantity);
}
}
#[cfg(test)]
mod tests_strategy_constructor {
use super::*;
use positive::pos_or_panic;
use crate::model::utils::create_sample_position;
#[test]
fn test_get_strategy_valid() {
let options = vec![
create_sample_position(
OptionStyle::Call,
Side::Long,
pos_or_panic!(90.0),
Positive::ONE,
pos_or_panic!(95.0),
pos_or_panic!(0.2),
),
create_sample_position(
OptionStyle::Call,
Side::Short,
pos_or_panic!(90.0),
Positive::ONE,
pos_or_panic!(105.0),
pos_or_panic!(0.2),
),
];
let result = PoorMansCoveredCall::get_strategy(&options);
assert!(result.is_ok());
let strategy = result.unwrap();
assert_eq!(strategy.long_call.option.strike_price, pos_or_panic!(95.0));
assert_eq!(
strategy.short_call.option.strike_price,
pos_or_panic!(105.0)
);
}
#[test]
fn test_get_strategy_wrong_number_of_options() {
let options = vec![create_sample_position(
OptionStyle::Call,
Side::Long,
pos_or_panic!(90.0),
Positive::ONE,
pos_or_panic!(95.0),
pos_or_panic!(0.2),
)];
let result = PoorMansCoveredCall::get_strategy(&options);
assert!(matches!(
result,
Err(StrategyError::OperationError(OperationErrorKind::InvalidParameters { operation, reason }))
if operation == "Poor Man's Covered Call get_strategy" && reason == "Must have exactly 2 options"
));
}
#[test]
fn test_get_strategy_wrong_option_style() {
let mut option1 = create_sample_position(
OptionStyle::Call,
Side::Long,
pos_or_panic!(90.0),
Positive::ONE,
pos_or_panic!(95.0),
pos_or_panic!(0.2),
);
option1.option.option_style = OptionStyle::Put;
let option2 = create_sample_position(
OptionStyle::Call,
Side::Short,
pos_or_panic!(90.0),
Positive::ONE,
pos_or_panic!(105.0),
pos_or_panic!(0.2),
);
let options = vec![option1, option2];
let result = PoorMansCoveredCall::get_strategy(&options);
assert!(matches!(
result,
Err(StrategyError::OperationError(OperationErrorKind::InvalidParameters { operation, reason }))
if operation == "Poor Man's Covered Call get_strategy" && reason == "Options must be calls"
));
}
#[test]
fn test_get_strategy_wrong_sides() {
let options = vec![
create_sample_position(
OptionStyle::Call,
Side::Short,
pos_or_panic!(90.0),
Positive::ONE,
pos_or_panic!(95.0),
pos_or_panic!(0.2),
),
create_sample_position(
OptionStyle::Call,
Side::Long,
pos_or_panic!(90.0),
Positive::ONE,
pos_or_panic!(105.0),
pos_or_panic!(0.2),
),
];
let result = PoorMansCoveredCall::get_strategy(&options);
assert!(matches!(
result,
Err(StrategyError::OperationError(OperationErrorKind::InvalidParameters { operation, reason }))
if operation == "Poor Man's Covered Call get_strategy"
&& reason == "Poor Man's Covered Call requires a long lower strike call and a short higher strike call"
));
}
}
#[cfg(test)]
mod tests_poor_mans_covered_call_pnl {
use super::*;
use positive::{assert_pos_relative_eq, pos_or_panic};
use crate::model::utils::create_sample_position;
use rust_decimal_macros::dec;
fn create_test_poor_mans_covered_call() -> Result<PoorMansCoveredCall, StrategyError> {
let long_call = create_sample_position(
OptionStyle::Call,
Side::Long,
Positive::HUNDRED, Positive::ONE, pos_or_panic!(95.0), pos_or_panic!(0.2), );
let short_call = create_sample_position(
OptionStyle::Call,
Side::Short,
Positive::HUNDRED, Positive::ONE, pos_or_panic!(105.0), pos_or_panic!(0.2), );
PoorMansCoveredCall::get_strategy(&[long_call, short_call])
}
#[test]
fn test_calculate_pnl_below_strikes() {
let pmcc = create_test_poor_mans_covered_call().unwrap();
let market_price = pos_or_panic!(90.0); let expiration_date = ExpirationDate::Days(pos_or_panic!(20.0));
let implied_volatility = pos_or_panic!(0.2);
let result = pmcc.calculate_pnl(&market_price, expiration_date, &implied_volatility);
assert!(result.is_ok());
let pnl = result.unwrap();
assert!(pnl.unrealized.is_some());
assert_pos_relative_eq!(pnl.initial_income, pos_or_panic!(5.0), pos_or_panic!(1e-6));
assert_pos_relative_eq!(pnl.initial_costs, pos_or_panic!(7.0), pos_or_panic!(1e-6));
assert!(pnl.unrealized.unwrap() < dec!(0.0)); }
#[test]
fn test_calculate_pnl_between_strikes() {
let pmcc = create_test_poor_mans_covered_call().unwrap();
let market_price = pos_or_panic!(101.0); let expiration_date = ExpirationDate::Days(pos_or_panic!(20.0));
let implied_volatility = pos_or_panic!(0.2);
let result = pmcc.calculate_pnl(&market_price, expiration_date, &implied_volatility);
assert!(result.is_ok());
let pnl = result.unwrap();
assert!(pnl.unrealized.is_some());
assert!(pnl.unrealized.unwrap() > dec!(0.0)); }
#[test]
fn test_calculate_pnl_above_strikes() {
let pmcc = create_test_poor_mans_covered_call().unwrap();
let market_price = pos_or_panic!(110.0); let expiration_date = ExpirationDate::Days(pos_or_panic!(20.0));
let implied_volatility = pos_or_panic!(0.2);
let result = pmcc.calculate_pnl(&market_price, expiration_date, &implied_volatility);
assert!(result.is_ok());
let pnl = result.unwrap();
assert!(pnl.unrealized.is_some());
assert!(pnl.unrealized.unwrap() > dec!(0.0));
assert!(pnl.unrealized.unwrap() < dec!(10.0)); }
#[test]
fn test_calculate_pnl_with_higher_volatility() {
let pmcc = create_test_poor_mans_covered_call().unwrap();
let market_price = pos_or_panic!(105.0);
let expiration_date = ExpirationDate::Days(pos_or_panic!(20.0));
let implied_volatility = pos_or_panic!(0.4);
let result = pmcc.calculate_pnl(&market_price, expiration_date, &implied_volatility);
assert!(result.is_ok());
let pnl = result.unwrap();
assert!(pnl.unrealized.is_some());
assert!(pnl.unrealized.unwrap() > dec!(0.0));
}
}