Skip to main content

Xstep

Struct Xstep 

Source
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 implement AddAssign, be convertible to Positive, and be Copy.

§Fields

  • index - An integer representing the position of this step in a sequence.

  • value - The actual data value at this time step, of generic type T.

  • 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.

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 implements AddAssign, can be converted into Positive, and is Copy. 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();
Source

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 value
  • time_unit - The time unit for this step (e.g., Day, Week, Month)
  • datetime - The expiration date. Stored as-is when the variant is ExpirationDate::Days; ExpirationDate::DateTime is normalized to Days via ExpirationDate::get_days. If that conversion fails (past date / invalid datetime), the step falls back to ExpirationDate::Days(Positive::ZERO) and emits tracing::error!.
§Returns

A new Xstep<T> instance initialized with the provided parameters and index set to 0.

Source

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.
Source

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.
Source

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.
Source

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.
Source

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).

Source

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.

Source

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§

Source§

impl<T> Clone for Xstep<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for Xstep<T>

Source§

impl<T> Debug for Xstep<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for Xstep<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Serialize for Xstep<T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more