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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/******************************************************************************
Author: Joaquín Béjar García
Email: jb@taunais.com
Date: 1/8/24
******************************************************************************/
//! # Options Strategies Module
//!
//! This module contains implementations for various options trading strategies.
//! These strategies combine multiple options contracts to form risk-managed trades
//! suitable for different market conditions. Each strategy provides calculation
//! utilities for profit/loss scenarios and risk assessment.
//!
//! ## Sub-modules
//!
//! - `base`: Provides the base traits and structures for the strategies.
//! - `bear_call_spread`: Implements the Bear Call Spread strategy.
//! - `bear_put_spread`: Implements the Bear Put Spread strategy.
//! - `bull_call_spread`: Implements the Bull Call Spread strategy.
//! - `bull_put_spread`: Implements the Bull Put Spread strategy.
//! - `butterfly_spread`: Implements the Butterfly Spread strategy.
//! - `call_butterfly`: Implements the Call Butterfly strategy.
//! - `collar`: Implements the Collar strategy.
//! - `covered_call`: Implements the Covered Call strategy.
//! - `custom`: Provides utilities for creating custom strategies.
//! - `iron_butterfly`: Implements the Iron Butterfly strategy.
//! - `iron_condor`: Implements the Iron Condor strategy.
//! - `poor_mans_covered_call`: Implements the Poor Man's Covered Call strategy.
//! - `probabilities`: Provides probability calculations for the strategies.
//! - `protective_put`: Implements the Protective Put strategy.
//! - `straddle`: Implements the Straddle strategy.
//! - `strangle`: Implements the Strangle strategy.
//! - `utils`: Provides utility functions for the strategies.
//!
//! ## Usage
//!
//! To use a specific strategy, import the corresponding module and create an instance
//! of the strategy struct. Each strategy provides methods for calculating key metrics
//! such as maximum profit, maximum loss, and breakeven points.
//!
//! Example usage of the Bull Call Spread strategy:
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use tracing::info;
//! use optionstratlib::ExpirationDate;
//! use optionstratlib::strategies::bull_call_spread::BullCallSpread;
//! use positive::Positive;
//! use positive::pos_or_panic;
//! use optionstratlib::strategies::Strategies;
//!
//! let spread = BullCallSpread::new(
//! "SP500".to_string(),
//! pos_or_panic!(5780.0), // underlying_price
//! pos_or_panic!(5750.0), // long_strike_itm
//! pos_or_panic!(5820.0), // short_strike
//! ExpirationDate::Days(Positive::TWO),
//! pos_or_panic!(0.18), // implied_volatility
//! dec!(0.05), // risk_free_rate
//! Positive::ZERO, // dividend_yield
//! Positive::TWO, // long quantity
//! pos_or_panic!(85.04), // premium_long
//! pos_or_panic!(29.85), // premium_short
//! pos_or_panic!(0.78), // open_fee_long
//! pos_or_panic!(0.78), // open_fee_long
//! pos_or_panic!(0.73), // close_fee_long
//! pos_or_panic!(0.73), // close_fee_short
//! );
//!
//! let profit = spread.get_max_profit().unwrap_or(Positive::ZERO);
//! let loss = spread.get_max_loss().unwrap_or(Positive::ZERO);
//! info!("Max Profit: {}, Max Loss: {}", profit, loss);
//! ```
//!
//! Refer to the documentation of each sub-module for more details on the specific
//! strategies and their usage.
//! # Base Module
//!
//! This module provides the base traits and structures for defining and working with
//! options trading strategies. It includes the `StrategyType` enum, `Strategy` struct,
//! and the `Strategies`, `Validable`, and `Optimizable` traits.
//!
//! ## StrategyType
//!
//! The `StrategyType` enum represents different types of trading strategies. Each variant
//! corresponds to a specific strategy, such as `BullCallSpread`, `BearPutSpread`, `IronCondor`, etc.
//!
//! ## Strategy
//!
//! The `Strategy` struct represents a trading strategy. It contains properties such as the strategy's
//! name, type, description, legs (positions), maximum profit, maximum loss, and break-even points.
//!
//! ## Strategies Trait
//!
//! The `Strategies` trait defines the common methods that a trading strategy should implement.
//! It includes methods for adding legs, retrieving legs, calculating break-even points, maximum profit,
//! maximum loss, total cost, and more.
//!
//! ## Validable Trait
//!
//! The `Validable` trait provides a method for validating a trading strategy. Strategies should implement
//! this trait to ensure they are valid before being used.
//!
//! ## Optimizable Trait
//!
//! The `Optimizable` trait extends the `Validable` and `Strategies` traits and adds methods for optimizing
//! a trading strategy. It includes methods for finding the optimal strategy based on different criteria,
//! such as best ratio or best area.
//!
//! ## Usage
//!
//! To define a new trading strategy, create a struct that implements the `Strategies`, `Validable`, and
//! optionally, the `Optimizable` traits. Implement the required methods for each trait based on the specific
//! behavior of your strategy.
//!
//! Example:
//!
//! ```rust
//! use optionstratlib::error::position::PositionError;
//! use optionstratlib::model::position::Position;
//! use positive::Positive;
//! use optionstratlib::strategies::base::{BreakEvenable, Positionable, Strategies, Validable};
//! use optionstratlib::strategies::{BasicAble, Strategable};
//!
//! struct MyStrategy {
//! legs: Vec<Position>,
//! // Other strategy-specific fields
//! }
//!
//! impl Validable for MyStrategy {
//! fn validate(&self) -> bool {
//! true
//! }
//! }
//!
//!
//! impl Positionable for MyStrategy {
//! fn add_position(&mut self, position: &Position) -> Result<(), PositionError> {
//! Ok(self.legs.push(position.clone()))
//! }
//!
//! fn get_positions(&self) -> Result<Vec<&Position>, PositionError> {
//! Ok(self.legs.iter().collect())
//! }
//! }
//!
//! impl BreakEvenable for MyStrategy {}
//!
//!
//! impl BasicAble for MyStrategy {}
//!
//! impl Strategies for MyStrategy {}
//! ```
//! //! Example usage of the Iron Condor strategy:
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use tracing::info;
//! use optionstratlib::ExpirationDate;
//! use optionstratlib::strategies::iron_condor::IronCondor;
//! use positive::Positive;
//! use positive::pos_or_panic;
//! use optionstratlib::strategies::Strategies;
//!
//! let condor = IronCondor::new(
//! "AAPL".to_string(),
//! pos_or_panic!(150.0), // underlying_price
//! pos_or_panic!(155.0), // short_call_strike
//! pos_or_panic!(145.0), // short_put_strike
//! pos_or_panic!(160.0), // long_call_strike
//! pos_or_panic!(140.0), // long_put_strike
//! ExpirationDate::Days(pos_or_panic!(30.0)),
//! pos_or_panic!(0.2), // implied_volatility
//! dec!(0.01), // risk_free_rate
//! pos_or_panic!(0.02), // dividend_yield
//! Positive::ONE, // quantity
//! pos_or_panic!(1.5), // premium_short_call
//! Positive::ONE, // premium_short_put
//! Positive::TWO, // premium_long_call
//! pos_or_panic!(1.8), // premium_long_put
//! pos_or_panic!(5.0), // open_fee
//! pos_or_panic!(5.0), // close_fee
//! );
//!
//! let max_profit = condor.get_max_profit().unwrap_or(Positive::ZERO);
//! let max_loss = condor.get_max_loss().unwrap_or(Positive::ZERO);
//! info!("Max Profit: {}, Max Loss: {}", max_profit, max_loss);
//! ```
//!
//! Refer to the documentation of each sub-module for more details on the specific
//! strategies and their usage.
//!
/// Options trading strategies module collection
///
/// This module provides implementations of various options trading strategies and utility functions
/// for options trading analysis. Each submodule represents a specific strategy or utility.
/// Bear Call Spread strategy implementation
/// Bear Put Spread strategy implementation
/// Internal module for strategy building utilities
/// Bull Call Spread strategy implementation
/// Bull Put Spread strategy implementation
/// Call Butterfly strategy implementation
/// Collar strategy implementation
/// Covered Call strategy implementation
/// Custom strategy implementation and utilities
/// Default implementation for strategies
/// Delta-neutral strategy implementation and utilities
/// The `graph` module provides functionality for creating, managing, and
/// manipulating graph data structures. Common use cases include representing
/// networks, dependency graphs, and other graph-based relationships.
///
/// # Features
/// - Supports various graph representations (e.g., directed, undirected).
/// - Includes methods for traversing graphs (e.g., DFS, BFS).
/// - Provides utilities for adding and removing nodes and edges.
///
/// # Usage
/// To utilize this module, include it in your project and access its functions
/// to build and interact with graph structures:
///
/// Note: Implementations within the `graph` module may depend on specific
/// traits or types relevant to the graph operations.
///
/// For details on available graph types, functionalities, and examples, refer
/// to the corresponding methods and structs within the module.
/// Iron Butterfly strategy implementation
/// Iron Condor strategy implementation
/// Butterfly Spread strategy implementation
/// Long Call strategy implementation
/// Long Put strategy implementation
/// Long Straddle strategy implementation
/// Strangle strategy implementation
/// Macros for options strategies
/// Poor Man's Covered Call strategy implementation
/// Probability calculations for options strategies
/// Protective Put strategy implementation
/// Shared traits for strategy categories
/// Short Call strategy implementation
/// Short Call strategy implementation
/// Short Put strategy implementation
/// Short Straddle strategy implementation
/// Short Strangle strategy implementation
/// Utility functions for options calculations and analysis
pub use ;
pub use BearCallSpread;
pub use BearPutSpread;
pub use StrategyRequest;
pub use StrategyConstructor;
pub use BullCallSpread;
pub use BullPutSpread;
pub use CallButterfly;
pub use Collar;
pub use CoveredCall;
pub use ;
pub use IronButterfly;
pub use IronCondor;
pub use LongButterflySpread;
pub use LongCall;
pub use LongPut;
pub use LongStraddle;
pub use LongStrangle;
pub use PoorMansCoveredCall;
pub use ProtectivePut;
pub use ;
pub use ShortButterflySpread;
pub use ShortCall;
pub use ShortPut;
pub use ShortStraddle;
pub use ShortStrangle;
pub use FindOptimalSide;