Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
tsa::mem_table Class Reference

Class mem_table represents a memory based table. mem_table objects can be used in strategies both for input and output. Note that memory requirements can grow rapidly. By default, the size of mem_table objects is limited to 2 million records. This limit can be reset via the size_limit(size_t) member. More...

#include <TSAMemTable.h>

Inheritance diagram for tsa::mem_table:
tsa::out_stream_adaptor tsa::in_stream_adaptor tsa::object tsa::appendable tsa::component tsa::component

Public Member Functions

 mem_table (void)
 Constructs an instance with a single field of type float8.
 
 mem_table (int col_count)
 Constructs an instance with field_count fields, all of type float8. More...
 
 mem_table (const mem_table &other)
 Copy Constructor. More...
 
 mem_table (const mem_table &&other)
 Constructor. More...
 
 mem_table (const column_defs &col_def)
 Constructs a new instance with a structure as defined by col_def. More...
 
virtual ~mem_table (void)
 Destructor.
 
virtual void cmp__evaluate_bar (const date_time &) override
 
void columns (const column_defs &data_def)
 Sets the table's schema. The schema defines the type, name, sequence and size (for string fields) of each field. More...
 
class column_defs columns (void) const
 Returns the table's column structure.
 
bool is_equal (const mem_table &other, double fp_allowance=0.0)
 Returns true if both schema and data of self are identical with other. More...
 
virtual void isa__get_price_bar (const date_time &, instrument_price_bar &) override
 Abstract member to be implemented by derived classes. In derived classes, this member should populate priceBar with data corresponding to the current strategy date_time. If no corresponding price data exists for the given strategy date_time, then the most recently available price data may be returned, or else an exception must be thrown. More...
 
virtual date_time isa__get_record_timestamp (void) override
 This member must be overridden, and must return the current record's timestamp which may or may not correspond to the strategy's current timestamp. More...
 
virtual bool isa__get_time_of_next_bar (date_time &nextDateTime, const date_time &currentDateTime) override
 Abstract member which should return the timestamp of the strategy's next price bar (nextDateTime) when given the strategy's currentDateTime. The member should return true if successful. More...
 
virtual bool isa__implements_scheduling (void) const override
 Query if this object isa implements scheduling. More...
 
virtual std::string isa__stream_name (void) const override
 Abstract member. In derived classes should return the data source name (e.g series_base table name). More...
 
void name (const std::string &name)
 Sets the name. More...
 
const std::stringname (void) const
 Returns the table name. More...
 
void operator= (const table &other)
 Assignment operator. More...
 
virtual void osa__append (const date_time &, std::vector< variant > &) override
 Implement this member in any derived class and to append a new record and time_stamp. More...
 
virtual void osa__prepare_stream (const std::string &sourceName, const tsa::column_defs &dd) override
 Implement this member in any derived class. This member is called before any records are passed. Use it for initialization purposes, such as creating a new table in a series_base. More...
 
void size_limit (size_t max)
 Sets the maximum number of records that can be held by self before an exception is thrown. This is to prevent a potential infinite loop from quickly exhausting the computer's memory. More...
 
size_t size_limit (void) const
 Returns the maximum number of records that can be held by self. More...
 
- Public Member Functions inherited from tsa::out_stream_adaptor
virtual ~out_stream_adaptor (void)
 Destructor.
 
virtual void cmp__finalize (void) override
 Override this member in any derived class. It will be invoked when the strategy is finalized. Use it to deallocate memory or close any streams.
 
- Public Member Functions inherited from tsa::component
virtual void cmp__post_evaluate_bar (const date_time &)
 
bool is_registered_with_strategy (void) const
 
void register_with_strategy (strategy *)
 
- Public Member Functions inherited from tsa::in_stream_adaptor
 in_stream_adaptor (void)
 Constructor.
 
 ~in_stream_adaptor (void)
 Destructor.
 
size_t close_field_position (void) const
 Returns the index of the 'close' field. More...
 
virtual void cmp__finalize (void) override
 
void current_value (const variant &value)
 Sets the current value. More...
 
bool get_is_strategy_scheduler (void) const
 Returns true. More...
 
size_t high_field_position (void) const
 Returns the index of the 'high' field. More...
 
bool is_connected (void) const
 Returns true if the object state is 'connected'. More...
 
size_t low_field_position (void) const
 Returns the index of the 'low' field. More...
 
size_t open_field_position (void) const
 Returns the index of the 'open' field. More...
 
void set_connected (bool is_connected_flag=true)
 Sets the object state to is_connected_flag. More...
 
void stream_name (const std::string &stream_name)
 Sets the data source_name. More...
 
const std::stringstream_name (void) const
 Returns the data source name. More...
 
virtual void isa__set_forward_shift (size_t)
 
- Public Member Functions inherited from tsa::object
 object (void)
 Constructor.
 
virtual ~object (void)
 Destructor.
 
const char * class_name (void) const
 Returns the class name as returned by the 'Real Time Type Information' (RTTI) mechanism. More...
 
bool has_same_class_as (const object &other) const
 Returns true if self has the same type as other object. More...
 

Record Operations

variantoperator[] (size_t) const
 
virtual class record record (void)
 
void read (size_t index, class record &)
 
virtual void append (const class record &, append_flag_type flag=error_on_duplicate_timestamp)
 
size_t size (void) const
 Returns the number of records in self.
 
bool empty (void) const
 Returns true if table has records. More...
 
date_time first_timestamp (void) const
 Returns the timestamp of the first record. The first timestamp is by default the lowest (oldest). More...
 
date_time last_timestamp (void) const
 Returns the timestamp of the last record. The last timestamp is by default the highest (most recent) record time. More...
 
void clear (void)
 Deletes all records in self. The table's column structure is retained.
 
const variantat (size_t record_pos, size_t col_pos) const
 Returns the value at recordIndex and fieldIndex. More...
 
void update (size_t record_pos, size_t col_pos, const variant &value)
 Sets the value indexed by col_pos and record_pos. The record must already exist!
 
date_time timestamp (size_t record_pos) const
 
void delete_range (const date_time &start, const date_time &end)
 Deletes all records with timestamps between start and end (inclusive).
 
void delete_range (size_t start, size_t end)
 Deletes all records between start index and end index (inclusive).
 
void resize__drop_oldest (size_t newSize)
 Deletes enough records, starting with the oldest, to set the number of records to newSize.
 
void truncate (const date_time &new_max_timestamp)
 
void append (const mem_table &other)
 Appends the records from other to self. For this operation to succeed, both tables must share compatible schemas. Furthermore, there must not be any overlap of record timestamps.
 

Bulk Data

void copy (fast::table &db_table)
 Makes self a copy of db_table.
 
void read_db_table (fast::database &db, const std::string &tableName)
 
void populate_random_records (size_t numRecords, const date_time &start, const duration &ts, bool someAsNIL=true)
 

Column Operations

void reset_column_count (size_t num_cols)
 
void reset_column_type (size_t column_pos, type_t new_type, size_t length=5)
 Resets the column type. More...
 
void rename_column (size_t column_position, const std::string &new_name)
 Renames the column at column_position to new_name. More...
 
void reset_column_properties (size_t column_position, const std::string &new_name, type_t new_type, size_t length=5)
 Resets the column properties of column at given column_position. More...
 
void add_column (const std::string &name, const variant &type_and_default_value, size_t length=5)
 This member adds a new column to self and can be used even when self already contains data which will not be lost. The type_and_default_value argument will be used as template for all values in new column. The length argument is only relevant if the variant passed as type_and_default_value is of type string. More...
 
const std::stringcolumn_name (size_t position) const
 Returns the name of the column at the given position position. More...
 
type_t column_type (size_t position) const
 Returns the type of the column at the given position position. More...
 
size_t column_position (const std::string &column_name) const
 Returns the position of the column with given column_name. More...
 
bool column_exists (const std::string &column_name) const
 Returns true if a column with a given column_name exists. More...
 
virtual size_t column_count (void) const
 Returns the number of columns in the table's. More...
 
size_t column_field_length (size_t column_position) const
 Returns the size of the column's value field. The return value (in bytes) is only relevant for fields of type_t::string. More...
 

Detailed Description

Class mem_table represents a memory based table. mem_table objects can be used in strategies both for input and output. Note that memory requirements can grow rapidly. By default, the size of mem_table objects is limited to 2 million records. This limit can be reset via the size_limit(size_t) member.

Constructor & Destructor Documentation

tsa::mem_table::mem_table ( int  col_count)

Constructs an instance with field_count fields, all of type float8.

Parameters
col_countNumber of columns.
tsa::mem_table::mem_table ( const mem_table other)

Copy Constructor.

Parameters
otherThe other.
tsa::mem_table::mem_table ( const mem_table &&  other)

Constructor.

Parameters
otherThe other.
tsa::mem_table::mem_table ( const column_defs col_def)

Constructs a new instance with a structure as defined by col_def.

Parameters
col_defThe column definition.

Member Function Documentation

void tsa::mem_table::add_column ( const std::string name,
const variant type_and_default_value,
size_t  length = 5 
)

This member adds a new column to self and can be used even when self already contains data which will not be lost. The type_and_default_value argument will be used as template for all values in new column. The length argument is only relevant if the variant passed as type_and_default_value is of type string.

Parameters
nameThe name.
type_and_default_valueThe type and default value.
lengthThe length.
const variant & tsa::mem_table::at ( size_t  record_pos,
size_t  col_pos 
) const

Returns the value at recordIndex and fieldIndex.

Parameters
record_posZero-based index of records.
col_posZero-based index of columns.
Returns
A variant&
size_t tsa::mem_table::column_count ( void  ) const
virtual

Returns the number of columns in the table's.

Returns
A size_t.

Implements tsa::appendable.

bool tsa::mem_table::column_exists ( const std::string column_name) const

Returns true if a column with a given column_name exists.

Parameters
column_nameName of the column.
Returns
bool.
size_t tsa::mem_table::column_field_length ( size_t  column_position) const

Returns the size of the column's value field. The return value (in bytes) is only relevant for fields of type_t::string.

Parameters
column_positionThe column position.
Returns
A size_t.
const std::string & tsa::mem_table::column_name ( size_t  position) const

Returns the name of the column at the given position position.

Parameters
positionThe position.
Returns
A std::string&
size_t tsa::mem_table::column_position ( const std::string column_name) const

Returns the position of the column with given column_name.

Parameters
column_nameName of the column.
Returns
A size_t.
type_t tsa::mem_table::column_type ( size_t  position) const

Returns the type of the column at the given position position.

Parameters
positionThe position.
Returns
A type_t.
void tsa::mem_table::columns ( const column_defs data_def)

Sets the table's schema. The schema defines the type, name, sequence and size (for string fields) of each field.

Parameters
data_defThe data definition.
bool tsa::mem_table::empty ( void  ) const

Returns true if table has records.

Returns
true if has data.
date_time tsa::mem_table::first_timestamp ( void  ) const

Returns the timestamp of the first record. The first timestamp is by default the lowest (oldest).

Returns
A date_time.
bool tsa::mem_table::is_equal ( const mem_table other,
double  fp_allowance = 0.0 
)

Returns true if both schema and data of self are identical with other.

Parameters
otherThe other.
fp_allowanceThe allowance.
Returns
true if equal, false if not.
void tsa::mem_table::isa__get_price_bar ( const date_time date_time,
instrument_price_bar &  priceBar 
)
overridevirtual

Abstract member to be implemented by derived classes. In derived classes, this member should populate priceBar with data corresponding to the current strategy date_time. If no corresponding price data exists for the given strategy date_time, then the most recently available price data may be returned, or else an exception must be thrown.

Parameters
date_timeThe date time.
[in,out]priceBarThe price bar.

Implements tsa::in_stream_adaptor.

date_time tsa::mem_table::isa__get_record_timestamp ( void  )
overridevirtual

This member must be overridden, and must return the current record's timestamp which may or may not correspond to the strategy's current timestamp.

Returns
A date_time.

Implements tsa::in_stream_adaptor.

bool tsa::mem_table::isa__get_time_of_next_bar ( date_time nextDateTime,
const date_time currentDateTime 
)
overridevirtual

Abstract member which should return the timestamp of the strategy's next price bar (nextDateTime) when given the strategy's currentDateTime. The member should return true if successful.

This member is important when 'externally scheduling' a strategy. If the derived component is set as TIME_MASTER, then the strategy will retrieve the 'scheduling sequence' via this member.

It is important to properly handle the condition that arises when the strategy date_time reaches the timestamp of last record in the source table, at which point this member must return false.

Scheduling a strategy internal beyond this point won't be possible.

Parameters
[in,out]nextDateTimeThe next date time.
currentDateTimeThe current date time.
Returns
true if it succeeds, false if it fails.

Implements tsa::in_stream_adaptor.

bool tsa::mem_table::isa__implements_scheduling ( void  ) const
overridevirtual

Query if this object isa implements scheduling.

Returns
true if it succeeds, false if it fails.

Implements tsa::in_stream_adaptor.

std::string tsa::mem_table::isa__stream_name ( void  ) const
overridevirtual

Abstract member. In derived classes should return the data source name (e.g series_base table name).

Returns
A std::string.

Implements tsa::in_stream_adaptor.

date_time tsa::mem_table::last_timestamp ( void  ) const

Returns the timestamp of the last record. The last timestamp is by default the highest (most recent) record time.

Returns
A date_time.
void tsa::mem_table::name ( const std::string name)

Sets the name.

Parameters
nameThe name.
const std::string & tsa::mem_table::name ( void  ) const

Returns the table name.

Returns
A std::string&
void tsa::mem_table::operator= ( const table other)

Assignment operator.

Parameters
otherThe other.
void tsa::mem_table::osa__append ( const date_time time_stamp,
std::vector< variant > &  record 
)
overridevirtual

Implement this member in any derived class and to append a new record and time_stamp.

Parameters
time_stampThe time stamp.
[in,out]recordThe record.

Implements tsa::out_stream_adaptor.

void tsa::mem_table::osa__prepare_stream ( const std::string stream_name,
const tsa::column_defs field_list 
)
overridevirtual

Implement this member in any derived class. This member is called before any records are passed. Use it for initialization purposes, such as creating a new table in a series_base.

Parameters
stream_nameName of the stream.
field_listList of fields.

Implements tsa::out_stream_adaptor.

void tsa::mem_table::rename_column ( size_t  column_position,
const std::string new_name 
)

Renames the column at column_position to new_name.

Parameters
column_posThe column position.
new_nameNew name of the column.
void tsa::mem_table::reset_column_properties ( size_t  column_position,
const std::string new_name,
type_t  new_type,
size_t  length = 5 
)

Resets the column properties of column at given column_position.

Parameters
column_indexZero-based index of the column.
new_nameName column name.
new_typeNew column type.
lengthThe column field length.
void tsa::mem_table::reset_column_type ( size_t  column_pos,
type_t  new_type,
size_t  length = 5 
)

Resets the column type.

Parameters
column_posZero-based position of the column.
new_typeThe new type.
lengthThe column field length (for strings).
void tsa::mem_table::size_limit ( size_t  max)

Sets the maximum number of records that can be held by self before an exception is thrown. This is to prevent a potential infinite loop from quickly exhausting the computer's memory.

Parameters
maxThe maximum.
size_t tsa::mem_table::size_limit ( void  ) const

Returns the maximum number of records that can be held by self.

Returns
A size_t.