pub struct Xstep<T>{ /* private fields */ }Expand description
Represents a step in a time series with an indexed value at a specific time point.
This struct encapsulates a value at a specific point in a time series, tracking not only the value itself but also its position (index), the time unit it belongs to, and its associated datetime. It’s designed to be used in financial and statistical analysis where tracking values across time is essential.
The generic parameter T allows for different value types while ensuring they can be
incrementally accumulated (via AddAssign) and converted to a Positive type for
calculations that require non-negative values.
§Type Parameters
T- The type of the value being stored, which must implementAddAssign, be convertible toPositive, and beCopy.
§Fields
-
index- An integer representing the position of this step in a sequence. -
value- The actual data value at this time step, of generic typeT. -
time_unit- Defines the time granularity (e.g., day, hour, minute) for this step. -
datetime- The specific expiration date associated with this time step, which can be either a relative number of days or an absolute datetime.
Implementations§
Source§impl<T> Xstep<T>
Implementation for the Xstep<T> struct, which represents a time step in a simulation.
impl<T> Xstep<T>
Implementation for the Xstep<T> struct, which represents a time step in a simulation.
The Xstep<T> struct is designed to handle sequential time steps in financial simulations,
allowing for forward and backward movement in time. The struct maintains an index counter,
a value of type T, a time unit, and an expiration date.
§Type Parameters
T- A type that implementsAddAssign, can be converted intoPositive, and isCopy. This typically represents a numeric value used for calculating time differences.
§Examples
// Create a step with 7 days as the value, using days as the time unit
use optionstratlib_core::model::Positive;
use optionstratlib_core::pos_or_panic;
use optionstratlib_core::model::ExpirationDate;
use optionstratlib_simulation::simulation::steps::Xstep;
use optionstratlib_core::utils::TimeFrame;
let step = Xstep::new(pos_or_panic!(7.0), TimeFrame::Day, ExpirationDate::Days(pos_or_panic!(30.0)));
// Move to the next step (forward in time)
let next_step = step.next();
// Move to the previous step (backward in time)
let prev_step = step.previous();Sourcepub fn new(value: T, time_unit: TimeFrame, datetime: ExpirationDate) -> Self
pub fn new(value: T, time_unit: TimeFrame, datetime: ExpirationDate) -> Self
Creates a new Xstep with the specified value, time unit, and datetime.
§Parameters
value- The step size valuetime_unit- The time unit for this step (e.g., Day, Week, Month)datetime- The expiration date. Stored as-is when the variant isExpirationDate::Days;ExpirationDate::DateTimeis normalized toDaysviaExpirationDate::get_days. If that conversion fails (past date / invalid datetime), the step falls back toExpirationDate::Days(Positive::ZERO)and emitstracing::error!.
§Returns
A new Xstep<T> instance initialized with the provided parameters and index set to 0.
Sourcepub fn index(&self) -> &i32
pub fn index(&self) -> &i32
Returns a reference to the index of the time step.
The index represents the position of this step in a sequence of time steps.
§Returns
&i32- A reference to the index value.
Sourcepub fn step_size_in_time(&self) -> &T
pub fn step_size_in_time(&self) -> &T
Returns a reference to the step size in time units.
This represents the magnitude of this time step in the context of its time frame, such as the number of days, hours, or other time units.
§Returns
&T- A reference to the step size value.
Sourcepub fn time_unit(&self) -> &TimeFrame
pub fn time_unit(&self) -> &TimeFrame
Returns a reference to the time unit associated with this step.
The time unit defines the granularity of the time measurement (e.g., day, hour, minute).
§Returns
&TimeFrame- A reference to the time unit enumeration.
Sourcepub fn datetime(&self) -> &ExpirationDate
pub fn datetime(&self) -> &ExpirationDate
Returns a reference to the datetime associated with this step.
This represents the specific point in time (as an expiration date) that this step corresponds to, which can be either a relative value or an absolute datetime.
§Returns
&ExpirationDate- A reference to the expiration date.
Sourcepub fn days_left(&self) -> Result<Positive, SimulationError>
pub fn days_left(&self) -> Result<Positive, SimulationError>
Calculates the number of days left until the expiry date associated with this step.
This method delegates to the underlying ExpirationDate component to determine
the days remaining until expiration. It provides a consistent way to access time-to-expiry
regardless of how the date was originally specified (as days or as an absolute datetime).
§Returns
Result<Positive, SimulationError>- A positive decimal value representing the number of days until expiration, or an error if the calculation cannot be performed.
§Errors
Returns SimulationError::ExpirationDate when the stored
ExpirationDate cannot be resolved to a non-negative day
count (e.g. the datetime is in the past).
Sourcepub fn next(&self) -> Result<Self, SimulationError>
pub fn next(&self) -> Result<Self, SimulationError>
Generates the next step by reducing the expiration days by the step value.
This method calculates a new Xstep instance with its index incremented by 1,
and the expiration date reduced by the equivalent of value in days.
§Returns
A new Xstep<T> instance with updated index and datetime values.
§Errors
Returns SimulationError::ExpirationReached when the
current step is already at expiration (no forward step is
possible), SimulationError::ExpirationDate when the
underlying ExpirationDate::get_days call fails,
SimulationError::PositiveError when the step cannot be expressed
in days (a custom time unit with no periods in a year, or a step
beyond the Positive range), and SimulationError::StepError
when the step index overflows i32::MAX.
Sourcepub fn previous(&self) -> Result<Self, SimulationError>
pub fn previous(&self) -> Result<Self, SimulationError>
Generates the previous step by increasing the expiration days by the step value.
This method calculates a new Xstep instance with its index decremented by 1,
and the expiration date increased by the equivalent of value in days.
§Returns
A new Xstep<T> instance with updated index and datetime values.
§Errors
Returns SimulationError::ExpirationDate when the underlying
ExpirationDate::get_days call fails,
SimulationError::StepError when the step-size conversion to
Positive fails or when the step index underflows i32::MIN, and
SimulationError::PositiveError when the step cannot be
expressed in days or rewinding the expiration leaves the
representable Positive range.
Trait Implementations§
impl<T> Copy for Xstep<T>
Auto Trait Implementations§
impl<T> Freeze for Xstep<T>where
T: Freeze,
impl<T> RefUnwindSafe for Xstep<T>where
T: RefUnwindSafe,
impl<T> Send for Xstep<T>where
T: Send,
impl<T> Sync for Xstep<T>where
T: Sync,
impl<T> Unpin for Xstep<T>where
T: Unpin,
impl<T> UnsafeUnpin for Xstep<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Xstep<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.