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

The most recent observations of a few fields, for a strategy fed one bar at a time. More...

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

Public Types

using Instant = TimeIndex::Instant
 The instant type observations are stamped with.

Public Member Functions

 SeriesBuffer (std::size_t capacity, std::size_t fields=bar_field_count)
 Constructs an empty buffer.
std::size_t capacity () const noexcept
 Returns the most observations the buffer holds.
std::size_t fields () const noexcept
 Returns the number of values per observation.
std::size_t size () const noexcept
 Returns the number of observations held.
bool empty () const noexcept
 Tests whether the buffer is empty.
bool full () const noexcept
 Tests whether the buffer holds capacity() observations.
std::size_t compactions () const noexcept
 Returns how many times the buffer moved its contents to the front of its storage.
void push (Instant when, math::SeriesView row)
 Appends one observation, dropping the oldest if the buffer is full.
void push (Instant when, const std::vector< Scalar > &row)
 Appends one observation, dropping the oldest if the buffer is full.
void push (const Bar &bar)
 Appends one bar, dropping the oldest observation if the buffer is full.
math::SeriesView column (std::size_t field) const
 Returns one field's values, oldest first.
math::SeriesView column (BarField field) const
 Returns one bar field's values, oldest first.
Scalar at (std::size_t position, std::size_t field) const
 Reads one value.
Instant instant (std::size_t position) const
 Returns the instant of one observation.
Instant newest_instant () const
 Returns the instant of the newest observation.
void clear () noexcept
 Discards every observation; the storage is kept.

Detailed Description

The most recent observations of a few fields, for a strategy fed one bar at a time.

Holds up to capacity() observations. Until it is full, each push adds one; after that, each push also drops the oldest. Check full() before computing an indicator whose window is the capacity, so it is never computed from fewer observations than its window.

column() returns the trailing values of one field, oldest first, as one contiguous view of the buffer's own storage. Storage is allocated once at construction and never grows. The trailing statistics themselves are the rolling_* functions in rolling.hpp.

Note
Not thread-safe: use one buffer from one strategy (one strand).

Definition at line 30 of file series_buffer.hpp.

Member Typedef Documentation

◆ Instant

The instant type observations are stamped with.

Definition at line 33 of file series_buffer.hpp.

Constructor & Destructor Documentation

◆ SeriesBuffer()

algotradelib::analytics::SeriesBuffer::SeriesBuffer ( std::size_t capacity,
std::size_t fields = bar_field_count )
inline

Constructs an empty buffer.

Parameters
capacityThe most observations the buffer holds.
fieldsThe number of values per observation; the default holds every BarField.
Exceptions
std::invalid_argumentif capacity or fields is zero.

Definition at line 42 of file series_buffer.hpp.

Member Function Documentation

◆ at()

Scalar algotradelib::analytics::SeriesBuffer::at ( std::size_t position,
std::size_t field ) const
inline

Reads one value.

Parameters
positionThe observation, counting from 0 for the oldest held.
fieldThe field's column position.
Returns
The value.
Exceptions
std::out_of_rangeif position or field is past the end.

Definition at line 199 of file series_buffer.hpp.

◆ capacity()

std::size_t algotradelib::analytics::SeriesBuffer::capacity ( ) const
inlinenoexcept

Returns the most observations the buffer holds.

Returns
The capacity given at construction.

Definition at line 56 of file series_buffer.hpp.

Referenced by SeriesBuffer().

◆ clear()

void algotradelib::analytics::SeriesBuffer::clear ( )
inlinenoexcept

Discards every observation; the storage is kept.

Definition at line 235 of file series_buffer.hpp.

◆ column() [1/2]

math::SeriesView algotradelib::analytics::SeriesBuffer::column ( BarField field) const
inline

Returns one bar field's values, oldest first.

Parameters
fieldThe field.
Returns
A view of size() values, valid until the next push or clear.
Exceptions
std::out_of_rangeif the buffer has fewer columns than field needs.

Definition at line 189 of file series_buffer.hpp.

Referenced by column().

◆ column() [2/2]

math::SeriesView algotradelib::analytics::SeriesBuffer::column ( std::size_t field) const
inline

Returns one field's values, oldest first.

Parameters
fieldThe field's column position.
Returns
A view of size() values in the buffer's storage, valid until the next push or clear.
Exceptions
std::out_of_rangeif field is not below fields().

Definition at line 174 of file series_buffer.hpp.

◆ compactions()

std::size_t algotradelib::analytics::SeriesBuffer::compactions ( ) const
inlinenoexcept

Returns how many times the buffer moved its contents to the front of its storage.

Returns
The compaction count; at most one per capacity() pushes.

Definition at line 86 of file series_buffer.hpp.

◆ empty()

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

Tests whether the buffer is empty.

Returns
true if it holds no observations.

Definition at line 74 of file series_buffer.hpp.

◆ fields()

std::size_t algotradelib::analytics::SeriesBuffer::fields ( ) const
inlinenoexcept

Returns the number of values per observation.

Returns
The field count given at construction.

Definition at line 62 of file series_buffer.hpp.

Referenced by SeriesBuffer().

◆ full()

bool algotradelib::analytics::SeriesBuffer::full ( ) const
inlinenoexcept

Tests whether the buffer holds capacity() observations.

Returns
true once it is full.

Definition at line 80 of file series_buffer.hpp.

◆ instant()

Instant algotradelib::analytics::SeriesBuffer::instant ( std::size_t position) const
inline

Returns the instant of one observation.

Parameters
positionThe observation, counting from 0 for the oldest held.
Returns
Its instant.
Exceptions
std::out_of_rangeif position is past the end.

Definition at line 215 of file series_buffer.hpp.

◆ newest_instant()

Instant algotradelib::analytics::SeriesBuffer::newest_instant ( ) const
inline

Returns the instant of the newest observation.

Returns
The instant of the last push.
Exceptions
std::out_of_rangeif the buffer is empty.

Definition at line 227 of file series_buffer.hpp.

Referenced by push().

◆ push() [1/3]

void algotradelib::analytics::SeriesBuffer::push ( const Bar & bar)
inline

Appends one bar, dropping the oldest observation if the buffer is full.

The bar's fields go to the columns in BarField order; a bar with no open or traded value stores missing() there.

Parameters
barThe bar; its timestamp_ must be later than the newest observation.
Exceptions
std::invalid_argumentif the buffer was not built with bar_field_count fields, or the bar is not later than the newest observation.

Definition at line 147 of file series_buffer.hpp.

◆ push() [2/3]

void algotradelib::analytics::SeriesBuffer::push ( Instant when,
const std::vector< Scalar > & row )
inline

Appends one observation, dropping the oldest if the buffer is full.

Parameters
whenThe observation's instant; must be later than the newest one held.
rowThe observation's values, exactly fields() of them.
Exceptions
std::invalid_argumentif row has the wrong size or when is not later than the newest observation.

Definition at line 129 of file series_buffer.hpp.

◆ push() [3/3]

void algotradelib::analytics::SeriesBuffer::push ( Instant when,
math::SeriesView row )
inline

Appends one observation, dropping the oldest if the buffer is full.

Parameters
whenThe observation's instant; must be later than the newest one held.
rowThe observation's values, exactly fields() of them.
Exceptions
std::invalid_argumentif row has the wrong size or when is not later than the newest observation.

Definition at line 96 of file series_buffer.hpp.

Referenced by push(), and push().

◆ size()

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

Returns the number of observations held.

Returns
A number from 0 to capacity().

Definition at line 68 of file series_buffer.hpp.


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