Skip to main content

optionstratlib_core/model/
mod.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 1/8/24
5******************************************************************************/
6
7//! # Model Module
8//!
9//! The core data structures of financial options modeling: option contracts,
10//! positions, legs, trades, payoffs at expiry and checked `Decimal` helpers.
11//! Pricing models, Greeks, probabilities and risk metrics are built on these
12//! types by the layers above; none of them lives here.
13//!
14//! ## Core Components
15//!
16//! * `option` - Implementation of the core Options structure and related functionality
17//! * `position` - Management of financial positions and their properties
18//! * `types` - Essential type definitions and enums
19//! * `utils` - Utility functions for model operations and calculations
20//! * `format` - Display and Debug implementations for model types
21//!
22//! ## Key Features
23//!
24//! ### Options
25//!
26//! Comprehensive implementation of financial options including:
27//!
28//! * Multiple option types (European, American, Asian, etc.)
29//! * Contract validation, time to expiry and payoff at expiry
30//! * Intrinsic value and moneyness
31//!
32//! ### Position Management
33//!
34//! Tools for managing financial positions:
35//!
36//! * Position tracking
37//! * Cost basis calculations
38//! * Profit/Loss analysis
39//! * Break-even calculations
40//! * Fee management
41//!
42//! ### Type System
43//!
44//! Robust type definitions ensuring type safety:
45//!
46//! * `Positive` for non-negative numbers
47//! * `ExpirationDate` handling
48//! * Option styles and types
49//! * Side (Long/Short) definitions
50//!
51//! ### Formatting
52//!
53//! Comprehensive formatting support:
54//!
55//! * Display trait implementations for readable output
56//! * Debug trait implementations for detailed inspection
57//! * Consistent formatting across all types
58//! * Custom format implementations for complex types
59//!
60//! ## Example Usage
61//!
62//! ```rust
63//! use rust_decimal_macros::dec;
64//! use tracing::info;
65//! use optionstratlib_core::model::{ExpirationDate, Options};
66//! use optionstratlib_core::model::types::{ OptionStyle, OptionType, Side};
67//! use positive::pos_or_panic;
68//! use positive::Positive;
69//!
70//! let option = Options::new(
71//!     OptionType::European,
72//!     Side::Long,
73//!     "AAPL".to_string(),
74//!     Positive::HUNDRED,
75//!     ExpirationDate::Days(pos_or_panic!(30.0)),
76//!     pos_or_panic!(0.2),
77//!     Positive::ONE,
78//!     pos_or_panic!(105.0),
79//!     dec!(0.05),
80//!     OptionStyle::Call,
81//!     pos_or_panic!(0.01),
82//!     None,
83//! );
84//!
85//! info!("Option Details: {}", option);
86//! info!("Debug View: {:?}", option);
87//! ```
88
89/// Core utilities for handling decimal numbers in financial calculations.
90pub mod decimal;
91
92/// Formatting utilities for displaying financial data and calculations.
93mod format;
94
95/// The option contract (`Options`): terms, validation, time to expiry and payoff.
96pub mod option;
97
98/// Trading positions (`Position`): an option with its premium, fees, quantity and open date.
99pub mod position;
100
101/// Payoff contracts at expiry: [`payoff::Payoff`], [`payoff::PayoffInfo`] and the
102/// implementation for every [`OptionType`] variant.
103pub mod payoff;
104
105mod positive_ext;
106
107/// Common type definitions used throughout the options strategy library.
108pub mod types;
109
110/// Utility functions supporting various operations across the library.
111pub mod utils;
112
113/// Components for defining and working with chart axes in strategy visualizations.
114mod axis;
115
116mod balance;
117/// Components for defining and working with expiration dates.
118mod expiration;
119/// Components for different types of trading legs (spot, futures, perpetuals).
120pub mod leg;
121mod trade;
122
123pub use axis::BasicAxisTypes;
124pub use balance::*;
125pub use expiration::ExpirationDate;
126pub use expiration::ExpirationDateError;
127pub use expiration::{reject_unrepresentable_expiration, resolve_expiration_date};
128pub use format::{DisplayMoney, UNLIMITED, unlimited_label};
129pub use option::Options;
130pub use position::Position;
131pub use trade::{Trade, TradeAble, TradeStatus, TradeStatusAble, save_trades};
132pub use types::{OptionStyle, OptionType, RainbowType, Side};
133
134/// `Positive`, the non-negative decimal newtype, its error and the
135/// `is_positive::<T>()` type check. Defined by the
136/// standalone `positive` crate and re-exported unchanged: a value obtained
137/// through this path *is* a `positive::Positive` (see the crate-level
138/// "Foundational types" table).
139pub use positive::{Positive, PositiveError, is_positive};