Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSAMemTable.h
1 /* ===================================================================
2 *
3 * T R A D I N G S Y S T E M A P I ™
4 * Copyright © 1999 - 2014 by Peter Ritter ( TradingSystemAPI.com )
5 * A L L R I G H T S R E S E R V E D
6 *
7 * Consult your license regarding permissions and restrictions.
8 * You may obtain a copy of the License at:
9 * http://www.TradingSystemAPI.com/licenses/LICENSE-TSAPI-3.0.html
10 *
11 * ====================================================================
12 */
13 
14 #ifndef TSA_MEM_TABLE__INCLUDED
15 #define TSA_MEM_TABLE__INCLUDED
16 
17 #include <algorithm>
18 #include <ostream>
19 #include <climits>
20 
21 #include "TSADBRecord.h"
22 #include "TSASeriesTemplate.h"
23 #include "TSAStreams.h"
24 #include "TSADataDef.h"
25 
26 namespace tsa {
30  class column_defs;
31  class series_base;
32  namespace fast{
33  class table_imp_ref;
34  }
35 
36  /*
37  ** ====================================================
38  ** >>> class mem_table <<<
39  ** ====================================================
40  */
41 
48  class dll_export mem_table :
49  public out_stream_adaptor,
50  public in_stream_adaptor,
51  public object,
52  public appendable {
53  private:
54  std::vector<variant*> m_records;
55  size_t m_num_fields;
56  size_t m_total_num_fields; //including timestamp
57  mutable size_t m_cursor_position;
58  std::vector<variant> m_append_record;
59  mutable variant* m_curr_record_ptr;
60  std::vector<std::string> m_field_names;
61  std::vector<type_t> m_field_types;
62  std::vector<size_t> m_field_lengths;
63  size_t m_max_record_count;
64  public:
65  static const size_t default_max_records;
66  protected:
67  void Deallocate(void);
68  void Allocate(size_t num_columns);
69  public:
71  mem_table(void);
72 
77  mem_table(int col_count);
78 
83  mem_table(const mem_table& other);
84 
89  mem_table(const mem_table&& other);
90 
91  // internal
92  mem_table(const fast::table_imp_ref&);
93 
98  mem_table(const column_defs& col_def);
99 
101  virtual ~mem_table(void);
102 
107  void operator= (const table& other);
108 
113  void name(const std::string& name);
114 
119  const std::string& name(void) const;
120 
127  void size_limit(size_t max);
128 
133  size_t size_limit(void) const;
134 
141  bool is_equal(const mem_table& other, double fp_allowance = 0.0);
142 
148  void columns(const column_defs& data_def);
149 
151  class column_defs columns(void) const;
152 
153  public:
154  //=============================
157  //=============================
158 
160  variant* operator[](size_t) const;
161 
163  virtual class record record(void); /*class appendable*/
164 
166  void read(size_t index, class record&);
167 
169  virtual void append(const class record&, append_flag_type flag = error_on_duplicate_timestamp); /*class appendable*/
170 
172  size_t size(void) const;
173 
178  bool empty(void) const;
179 
185  date_time first_timestamp(void) const;
186 
192  date_time last_timestamp(void) const;
193 
195  void clear(void);
196 
203  const variant& at(size_t record_pos, size_t col_pos) const;
204 
206  void update(size_t record_pos, size_t col_pos, const variant& value);
207 
208  // Returns the timestamp of the record at @b record_pos.
209  date_time timestamp(size_t record_pos) const;
210 
212  void delete_range(const date_time& start, const date_time& end);
213 
215  void delete_range(size_t start, size_t end);
216 
218  void resize__drop_oldest(size_t newSize);
219 
221  void truncate(const date_time& new_max_timestamp);
222 
225  void append(const mem_table& other);
227 
228  void populate_random_records(size_t num_records);
229  void populate_random_records(size_t num_records, bool some_as_nil);
230 
231  // Turns self into an identical copy of @b other. Copies both its structure and data.
232  void copy_from(const mem_table& other);
233 
234  // Clears @b other, and then populates @b other with all records between @b from and @b to (inclusive)
235  void copy_to(mem_table& other, const date_time& from, const date_time& to);
236  public: //Cursor operations
237 
238  // Positions the cursor on the records at @a index.
239  void cursor__seek(size_t index)const;
240 
241  //
242  bool cursor__seek_next(void)const;
243 
244  //
245  bool cursor__seek_prev(void)const;
246 
247  // Positions the cursor on the last record.
248  void cursor__seek_last(void)const;
249 
250  // Returns the index of the record on which the internal iterator is currently positioned.
251  size_t cursor__position(void) const;
252 
253  // Returns the time of the record on which the cursor is currently positioned.
254  date_time cursor__timestamp(void) const;
255 
256  //
257  void cursor__read_record(date_time& timestamp, std::vector<variant>& rec)const;
258 
259  // Returns the value of the field at @a index, of the record on which the cursor is currently positioned.
260  const variant& cursor__at(size_t fieldIndex) const;
261 
262  // Returns the value of the field with given @a fieldName, of the record on which the cursor is currently positioned.
263  const variant& cursor__at(const std::string& fieldName) const;
264 
265  // Returns true if Cursor position is not nullptr.
266  bool cursor__is_valid(void) const;
267 
268  // Modifies the value of the field at @a fieldIndex for the 'current record'
269  void cursor__set(size_t fieldIndex, const variant& value);
270  date_time cursor__seek(const date_time& target_timestamp)const;
271  seek_result cursor__seek_NEW(const date_time& target_timestamp)const;
272  date_time test__find_timestamp_LOE(const date_time&);
273  public:
274  //=====================
277  //=====================
278  //
280  void copy(fast::table& db_table);
281 
282  // @brief Populates self with data from @b db table with @b tableName.
283  void read_db_table(fast::database& db, const std::string& tableName);
284 
286  void populate_random_records(size_t numRecords, const date_time& start,
287  const duration& ts, bool someAsNIL = true);
288 
290 
291  void populate_random_ohlcv_records(size_t numRecords, const date_time& start,
292  const duration& ts);
293  public:
294  void append_set(size_t fieldIndex, const variant& value);
295  void internal_append(const date_time& timestamp,
296  const std::vector<variant>& record, append_flag_type flag);
297  void Append(const date_time& appendTime);
298  public:
299  //===========================
302  //===========================
303 
304  // Internal: Resets to number of fields to @b numFields, all of type float8. All data will be
305  // lost.
306  void reset_column_count(size_t num_cols);
307 
314  void reset_column_type(size_t column_pos, type_t new_type, size_t length = 5);
315 
321  void rename_column(size_t column_position, const std::string& new_name);
322 
330  void reset_column_properties(size_t column_position, const std::string& new_name, type_t new_type, size_t length = 5);
331 
341  void add_column(const std::string& name, const variant& type_and_default_value, size_t length = 5);
342 
348  const std::string& column_name(size_t position) const;
349 
355  type_t column_type(size_t position) const;
356 
362  size_t column_position(const std::string& column_name) const;
363 
369  bool column_exists(const std::string& column_name) const;
370 
375  virtual size_t column_count(void) const; /*appendable*/
376 
383  size_t column_field_length(size_t column_position) const;
385  public:
386 
387  // Returns true if it is possible to convert all Variants at @a fieldIndex to @a type, in
388  // all rows, without error.
389  bool type_conversion_possible(size_t fieldIndex, type_t type);
390 
391  // Returns the index of the field with given @b fieldname, by initializing @b index.
392  // Will return false if a field with given name does not exist, else true.
393  bool field_index(const std::string& fieldName, size_t& index) const;
394 
395  public: // Internal
396 
397  // @brief Sets all values in @b column_name to @b value. Every record will be affected!
398  void reset_all_values_in_column(const std::string& column_name, const variant& value);
399 
400  // @brief Sets all values in the column indexed by @b fieldIndex to @b value. Every record will be affected!
401  void reset_all_values_in_column(size_t columnIndex, const variant& value);
402 
403  // @brief Iterates through all values in @b column_name, and replaces the value with @b newValue if the
404  // original value was equal to @b toReplace.
405  void replace(const std::string& column_name, const variant& to_replace,const variant& new_value);
406 
407  // @brief Iterates through all values in @b column_name, and deletes all records where the
408  // value is equal to @b deleteValue.
409  size_t delete_where(const std::string& column_name, const variant& deleteValue);
410 
411  // @brief Multiples all values at @b column_index by given @b factor.
412  void multiply_all_values_in_column(size_t columnIndex, double factor);
413 
414  // @brief Adds @b value to all values in @b columnIndex.
415  void add_to_all_values_in_column(size_t columnIndex, double value);
416 
417  // @brief Truncates all string values in all fields of type string to their respective
418  // field size.
419  void fix_string_length(void);
420  public:
421  void purge_deleted_records(void);
422  size_t last_valid_record_index(void) const; //IU Returns the highest valid record index.
423  bool test__curr_record_ptr_is_valid(void) const;
424  variant* get_record_ptr(void) const;
425  void Read(date_time& timestamp, std::vector<variant>& InternalRecord);
426  void _ReadWithTimestampAsFirstValue(std::vector<variant>& InternalRecord);
427  public:
428  std::string to_string(void)const;
429  void from_string(const std::string&);
430 
431  void write_to(std::stringstream&)const;
432  void read_from(std::stringstream&);
433 
434  public: //Internal
435  // Prints table (with borders). @b maxRec can be used to limit the number of records to print.
436  // @b FPPrecision determines the precision of floating point numbers.
437  void print(std::ostream& stream, size_t maxRec = tsa::print_no_limit, size_t FPPrecision = 7);
438  // Prints the table as comma separated values (CSV). @b maxRec can be used to limit
439  // the number of records to print.
440  void csv(std::ostream&, size_t maxRec = tsa::print_no_limit);
441  // internal
442  void print_record(size_t pos, std::ostream&);
443  void print_record_header(std::ostream&);
444 
445  public: // out_stream_adaptor
446  virtual void osa__append(const date_time&, std::vector<variant>&) override;
447  virtual void osa__prepare_stream(const std::string& sourceName,
448  const tsa::column_defs& dd) override;
449 
450  public: // component
451  virtual void cmp__evaluate_bar(const date_time&) override;
452 
453  public: //in_stream_adaptor
454  virtual void isa__set_position(const date_time&) override;
455  virtual bool isa__field_exists(const std::string&) override;
456  virtual size_t isa__field_index(const std::string&) override;
457  virtual type_t isa__field_type(size_t) override;
458  virtual size_t isa__field_count(void) override;
459  virtual const std::string& isa__field_name(size_t _idx) override;
460  virtual date_time isa__get_record_timestamp(void) override;
461  virtual date_time isa__begin(void) override;
462  virtual date_time isa__end(void) override;
463  virtual size_t isa__size(void) override;
464  virtual void isa__on_open(void);
465  virtual variant isa__get_variant(size_t idx) override;
466  virtual double isa__get_double(size_t idx) override;
467  virtual void isa__update_value_at_position(size_t idx) override;
468  virtual std::string isa__stream_name(void) const override;
469  virtual bool isa__get_ohlc_fields_exist(void) override;
470  virtual void isa__get_price_bar(const date_time&, instrument_price_bar&) override;
471  virtual bool isa__get_time_of_next_bar(date_time& nextDateTime, const date_time& currentDateTime) override;
472  virtual bool isa__implements_scheduling(void) const override;
473  public: //Testing
474  bool verify_types_in_column(size_t col_ndex) const;
475  bool verify_types(void) const;
476  bool VerifyColumnDataEqualTo(const std::string& col_name, const variant& data) const;
477  bool VerifyColumnVariantsOfType(size_t col_index, type_t type) const;
478  bool VerifyColumnStringsHaveValidLength(size_t col_index) const;
479  };
481 }//tsa
482 
483 #endif
append_flag_type
Values that represent append flag types.
Definition: TSADBRecord.h:32
Throw an exception when a duplicate timestamp is encountered. This is normally the default behaviour...
Definition: TSADBRecord.h:36
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
const size_t print_no_limit
flag to print without limiting the number of lines printed.
Definition: TSATypeDef.h:78
Class for managing native timeseries database files. Extremely high speed data access.
Definition: TSADatabase.h:118
seek_result
Return value for seek operations on series tables.
Definition: TSATypeDef.h:156
Defines the columnar structure of a table. Each column has a name, data type and field size...
Definition: TSADataDef.h:88
Supports &#39;write&#39;, &#39;update&#39;, &#39;delete&#39;, and &#39;read&#39; operations on series_base tables.
Parent class for &#39;in-stream adaptors&#39;. in_stream object rely on adaptors for access to underlying dat...
Definition: TSAStreams.h:52
Abstract base class for delegates of class out_stream.
Definition: TSAStreams.h:553
variant objects can represent values of different types.
Definition: TSAVariant.h:140
Represents a duration - the difference between two date_time values.
Definition: TSATime.h:945
Replace existing target if it already exists.
Definition: TSAStreams.h:533
_value_types_type
Data type enumeration used throughout the library. Intended to be used via type_t.
Definition: TSATypeDef.h:166
Parent class for many library classes.
Definition: TSATypeDef.h:462
Class representing a database record.
Definition: TSADBRecord.h:52
A data_def object describes the structure of a table.
Represents a database table in fast::database.
Definition: TSADBTable.h:41
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
Represents an object to which a record object can be appended.
Definition: TSADBRecord.h:140
Class mem_table represents a memory based table. mem_table objects can be used in strategies both for...
Definition: TSAMemTable.h:48