algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::analytics::TimeIndex Class Reference

The instants a frame's rows correspond to, strictly ascending. More...

#include <algotradelib/src/analytics/time_index.hpp>

Public Types

using Instant = std::chrono::system_clock::time_point
 The instant type of the index.

Public Member Functions

 TimeIndex ()=default
 Constructs an empty index.
 TimeIndex (std::vector< Instant > stamps)
 Constructs an index over a sequence of instants.
std::size_t size () const noexcept
 Returns the number of instants.
bool empty () const noexcept
 Tests whether the index is empty.
const Instant & at (std::size_t position) const
 Returns the instant of one row.
const std::vector< Instant > & stamps () const noexcept
 Returns all instants.
std::optional< std::size_t > find (const Instant &when) const
 Finds the row of an exact instant.
std::optional< std::size_t > asof (const Instant &when) const
 Finds the latest row at or before an instant ("as of" lookup).

Friends

bool operator== (const TimeIndex &lhs, const TimeIndex &rhs)
 Compares two indices instant by instant.
bool operator!= (const TimeIndex &lhs, const TimeIndex &rhs)
 Compares two indices for inequality.

Detailed Description

The instants a frame's rows correspond to, strictly ascending.

Maps an instant to a row position. Lookups (find, asof) are binary searches, meant for asking about one instant at a time.

Definition at line 20 of file time_index.hpp.

Member Typedef Documentation

◆ Instant

using algotradelib::analytics::TimeIndex::Instant = std::chrono::system_clock::time_point

The instant type of the index.

Definition at line 23 of file time_index.hpp.

Constructor & Destructor Documentation

◆ TimeIndex() [1/2]

algotradelib::analytics::TimeIndex::TimeIndex ( )
default

◆ TimeIndex() [2/2]

algotradelib::analytics::TimeIndex::TimeIndex ( std::vector< Instant > stamps)
inlineexplicit

Constructs an index over a sequence of instants.

Parameters
stampsThe instants, one per row, strictly ascending.
Exceptions
std::invalid_argumentif stamps is not strictly ascending.

Definition at line 34 of file time_index.hpp.

Member Function Documentation

◆ asof()

std::optional< std::size_t > algotradelib::analytics::TimeIndex::asof ( const Instant & when) const
inline

Finds the latest row at or before an instant ("as of" lookup).

Never looks forward, so it is safe to use in a backtest.

Parameters
whenThe instant to look up.
Returns
The position of the latest instant not after when, or std::nullopt if when precedes every instant in the index.

Definition at line 97 of file time_index.hpp.

◆ at()

const Instant & algotradelib::analytics::TimeIndex::at ( std::size_t position) const
inline

Returns the instant of one row.

Parameters
positionThe row position.
Returns
The instant at position.
Exceptions
std::out_of_rangeif position is past the end.

Definition at line 61 of file time_index.hpp.

◆ empty()

bool algotradelib::analytics::TimeIndex::empty ( ) const
inlinenoexcept

Tests whether the index is empty.

Returns
true if it holds no instants.

Definition at line 52 of file time_index.hpp.

◆ find()

std::optional< std::size_t > algotradelib::analytics::TimeIndex::find ( const Instant & when) const
inline

Finds the row of an exact instant.

Parameters
whenThe instant to find.
Returns
Its position, or std::nullopt if when is not in the index.

Definition at line 80 of file time_index.hpp.

◆ size()

std::size_t algotradelib::analytics::TimeIndex::size ( ) const
inlinenoexcept

Returns the number of instants.

Returns
The number of rows the index covers.

Definition at line 46 of file time_index.hpp.

Referenced by algotradelib::analytics::Panel::from_bars().

◆ stamps()

const std::vector< Instant > & algotradelib::analytics::TimeIndex::stamps ( ) const
inlinenoexcept

Returns all instants.

Returns
The instants, ascending.

Definition at line 72 of file time_index.hpp.

Referenced by TimeIndex().

◆ operator!=

bool operator!= ( const TimeIndex & lhs,
const TimeIndex & rhs )
friend

Compares two indices for inequality.

Parameters
lhsThe first index.
rhsThe second index.
Returns
true if they differ in any instant.

Definition at line 121 of file time_index.hpp.

◆ operator==

bool operator== ( const TimeIndex & lhs,
const TimeIndex & rhs )
friend

Compares two indices instant by instant.

Parameters
lhsThe first index.
rhsThe second index.
Returns
true if both hold the same instants.

Definition at line 111 of file time_index.hpp.


The documentation for this class was generated from the following file: