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
/******************************************************************************
use positive::pos_or_panic;
Author: Joaquín Béjar García
Email: jb@taunais.com
Date: 16/8/24
******************************************************************************/
//! # PnL (Profit and Loss) Module
//!
//! This module provides structures and traits for calculating and managing profit and loss (PnL) metrics
//! in financial instruments, particularly options.
//!
//! ## Core Components
//!
//! * `PnL` - Structure representing profit and loss information
//! * `PnLCalculator` - Trait for implementing PnL calculation logic
//!
//! ## Key Features
//!
//! ### PnL Structure
//!
//! The `PnL` structure captures:
//! * Realized profits or losses
//! * Unrealized profits or losses
//! * Initial costs and income
//! * Timestamp of calculation
//!
//! ### PnL Calculator
//!
//! The `PnLCalculator` trait enables:
//! * Real-time PnL calculations based on market prices
//! * PnL calculations at expiration
//! * Custom PnL calculation strategies
//!
//! ## Example Usage
//!
//! ```rust
//! use std::error::Error;
//! use optionstratlib::pnl::utils::{PnL, PnLCalculator};
//! use chrono::{DateTime, Utc};
//! use rust_decimal_macros::dec;
//! use optionstratlib::ExpirationDate;use positive::Positive;
//! use positive::pos_or_panic;
//! use optionstratlib::prelude::PricingError;
//!
//! // Create a new PnL instance
//! let pnl = PnL::new(
//! Some(dec!(100.0)), // Realized PnL
//! Some(dec!(50.0)), // Unrealized PnL
//! pos_or_panic!(25.0), // Initial costs
//! pos_or_panic!(75.0), // Initial income
//! Utc::now(), // Calculation timestamp
//! );
//!
//! // Example implementation of PnLCalculator
//! struct MyOption;
//!
//! impl PnLCalculator for MyOption {
//!
//! fn calculate_pnl(
//! &self,
//! market_price: &Positive,
//! expiration_date: ExpirationDate,
//! _implied_volatility: &Positive,
//! ) -> Result<PnL, PricingError> {
//! Ok(PnL::new(
//! Some(market_price.into()),
//! None,
//! pos_or_panic!(10.0),
//! pos_or_panic!(20.0),
//! expiration_date.get_date()?,
//! ))
//! }
//!
//! fn calculate_pnl_at_expiration(
//! &self,
//! underlying_price: &Positive,
//! ) -> Result<PnL, PricingError> {
//! let underlying_price = underlying_price.to_dec();
//! Ok(PnL::new(
//! Some(underlying_price),
//! None,
//! pos_or_panic!(10.0),
//! pos_or_panic!(20.0),
//! Utc::now(),
//! ))
//! }
//! }
//! ```
//!
//! ## Applications
//!
//! The PnL module is particularly useful for:
//! * Option position tracking
//! * Portfolio management
//! * Risk assessment
//! * Performance monitoring
//!
//! ## Features
//!
//! * Real-time PnL tracking
//! * Expiration value calculations
//! * Cost basis tracking
//! * Income tracking
//! * Timestamp-based calculations
/// * [`model`] - Core data structures for financial analysis and PnL modeling
/// * [`utils`] - Utility functions for data manipulation and calculations
pub use ;
pub use ;
pub use Transaction;
pub use PnL;