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
/******************************************************************************
Author: Joaquín Béjar García
Email: jb@taunais.com
Date: 24/12/25
******************************************************************************/
//! # Put/Call Ratio Metrics
//!
//! This module provides traits for computing Put/Call Ratio curves,
//! which are essential for understanding market positioning.
//!
//! ## Overview
//!
//! The Put/Call Ratio is the ratio between the put open interest and the call open
//! interest.
//! It is often used to measure the market sentiment. Generally speaking, traders buying
//! more puts than calls indicates a bearish market. Conversely, traders buying more calls
//! than puts indicates a bullish market.
//!
//! ## Mathematical Background
//!
//! The following common Put/Call Ratio measures are provided:
//!
//! ### Premium Weighted Put/Call Ratio
//! ```text
//! Put/Call Ratio = Put mid price / Call mid price
//! where Put mid price = (Put Bid + Put Ask) / 2 and
//! Call mid price = (Call Bid + Call Ask) / 2
//! ```
//!
//! ## Curve Representation
//!
//! The curve shows Put/Call Ratio across strike prices:
//! - **X-axis**: Strike price
//! - **Y-axis**: Put/Call Ratio
use crateCurve;
use crateCurveError;
/// A trait defining a Put/Call ratio representation.
///
/// The `PutCallRatioCurve` trait is designed to encapsulate the concept of an
/// indicator of whether the investors are placing more bets on prices falling
/// or rising. It has been conceived to be a measure of market sentiment.
/// This trait establishes the foundation for representing and
/// retrieving the put/call ratio in the form of a mathematical curve.
///
/// # Overview
/// Implementors of this trait are required to provide the following methods:
/// - `premium_weighted_pcr`: computes and returns a `Curve` object representing
/// the put/call ratio calculated based on option's premium.
///
/// The `Curve` struct is a mathematical representation of the put/call ratio, where the
/// x-axis typically corresponds to the strike price, and the y-axis corresponds to
/// the put/call ratio.
/// The premium weighted put/call ratio is calculated according to the following formula:
/// ```math
/// \text{premium weighted PCR} = \frac{\text{put mid price}}{\text{call mid price}}
/// ```
///
/// # Usage
/// This trait serves as the basis for constructing and analyzing Put/Call ratio
/// in applications such as:
/// - Financial derivatives modeling
/// - Options pricing engines
/// - Quantitative analysis of market data
///
/// # Required Methods
/// - **`premium_weighted_pcr(&self) -> Curve`**
/// - Computes and returns the premium-based put/call ratio as a `Curve`.
/// - The returned `Ok(Curve)` can be used for graphical representation, numerical
/// analysis, or further mathematical operations, such as interpolation or
/// transformations.
/// - Returns a `CurveError` if it is not possible to calculate the Put/Call Ratio.
///
/// # Integration with Other Modules
/// The `PutCallRatioCurve` trait makes use of the `Curve` struct, defined in the
/// `crate::curves` module. The `Curve` provides the mathematical framework
/// necessary for representing and manipulating the Put/Call ratio data. High-quality
/// precision (via the use of `Decimal` and ordered points) ensures that the
/// output from the `premium_weighted_pcr` methods is reliable and suitable for scientific
/// or financial applications.
///
/// # See Also
/// - [`crate::curves::Curve`]: The fundamental mathematical representation of
/// the Put/Call ratio.
/// - [`crate::curves::Point2D`]: The structure representing individual points
/// in the `Curve`.
///
/// # Examples
/// Users can implement this trait and provide their specific logic for generating
/// a `Curve` corresponding to the put/call ratio. The put/call ratio can be implemented
/// as premium-weighted or open interest based.
///
/// ```rust
/// use std::collections::BTreeSet;
/// use rust_decimal::Decimal;
/// use optionstratlib::curves::Curve;
/// use optionstratlib::error::CurveError;
/// use optionstratlib::metrics::PutCallRatioCurve;
///
/// struct MyPcr;
///
/// impl PutCallRatioCurve for MyPcr {
/// fn premium_weighted_pcr(&self) -> Result<Curve, CurveError> {
/// // Custom logic to build and return a Curve representing the premium
/// // weighted put/call ratio
/// let curve = Curve { points: BTreeSet::new(), x_range: (Decimal::ZERO, Decimal::ZERO) };
/// Ok(curve)
/// }
/// }
/// ```
///
/// This enables integration of user-defined volatility models with the broader
/// ecosystem of mathematical and financial tools that utilize the `Curve` data type.