Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSCWrapper.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 #ifndef __TSC_WRAPPER_H
14 #define __TSC_WRAPPER_H
15 
16 #include "TSCPlots.h"
17 
18 namespace tsa {
20 
21  class chart_maker;
22  class chart;
23  class plot::plot_base;
24 
28  struct trend_line {
31  double start_price;
33  double end_price;
35  int weight;
36  };
37 
39  struct horizontal_line {
40  double y_value;
42  int weight;
43  };
44 
46 
49  class pane_proxy {
50  friend class chart;
51  private:
52  size_t m_paneIdx;
53  chart* m_pChart;
54  public:
55  //IU
56  pane_proxy(chart&);
58  ~pane_proxy(void);
59  public:
61  void height(size_t height_);
63  size_t height(void)const;
65  void plot_bg(const color& bg_color);
67  void header_bg(const color& bg_color);
69  void show_header(bool show_flag = true);
71  void x_axis_legend(bool show_flag);
73  void background(const color& bg_color);
75  void title(const tsa::string& title);
77  std::string title(void)const;
79  void y_axis_legend(const color& clr);
81  void x_axis_legend(const color& clr);
83  void v_lines(bool enable_flag);
85  void h_lines(bool enable_flag);
87  void dashed_v_lines(bool enable_flag = true);
89  void dashed_h_lines(bool enable_flag = true);
91  void h_line(double y_value, const color& clr = color::white, size_t weight = 1);
93  void line(
95  double start_y_value,
97  double end_y_value,
98  const color& clr = color::white,
99  size_t weight = 1
100  );
102  void y_max(double max);
104  void y_min(double);
105  public://Internal use
106  void ShowTransactions(size_t _plotID, bool b = true);
107  void ShowTransactionPrices(size_t _plotID, bool b = true);
108  void position_as_plot_color(size_t _plotID, const color& longColor = color::auto_color,
109  const color& shortColor = color::auto_color);
110  void ShowTrades(size_t plotID,
111  const color& longColor = color::auto_color, const color& shortColor = color::auto_color);
112  void AxisHLine(double price, const color& = color::white, size_t width = 1);
113 
114  };
115 
117  //IU
118 
119  class graphics_plot_info : public tsa::object, public chart_property_map {
120  public:
121  graphics_plot_info(void) {
122  set_property("VISUALIZE_TRADES", false);
123  set_property("TYPE", "UNDEFINED");
124  }
125  virtual ~graphics_plot_info(void) { ; }
126  };
127 
129 
130  //IU
131  class pane_info : public chart_property_map, public tsa::object {
132  public:
133  std::vector<graphics_plot_info> m_plot_infos;
134  std::vector<horizontal_line> m_CustomHLines;
135  std::vector<horizontal_line> m_AxisHLines;
136  std::vector<trend_line> m_TLines;
137  graphics_plot_info& GetPlotInfo(size_t idx);
138  public:
139  pane_info(void);
140  };
141 
143 
147  class dll_export chart : public tsa::object, public tsa::component {
148  friend class pane_proxy;
149  public:
151  class pane {
152  public:
153  color m_color;
154  size_t m_height_px;
155  size_t m_index;
156  public:
158  pane(void);
160  pane(unsigned height, const color& bg_color);
162  pane(unsigned height);
164  pane(const color& bg_color);
166  ~pane(void);
167  };
168  public:
169  enum ElementType {
170  LINE = 1,
171  DLINE, //discontinuous line
172  AREA,
173  BAR,
174  };
175  private:
176  color_manager m_color_man;
177  std::vector<pane_info> m_pane_infos;
178  tsa::column_defs m_columns;
179  std::vector<tsa::variant> m_Rec;
180  bool m_bFirstPreEval;
181  bool m_bTrackTransactAndPosition;
182  int m_nMaxChartsToSave;
183  int m_nMaxBarsPerChart;
184  std::vector<tsa::transaction> m_tmpTransArr;
185  public:
186  pane_info& get_pane_info(size_t _idx);
187  private:
188  tsa::mem_table m_mem_table; //default - if no series_base
189  tsa::fast::database* m_db_ptr;
190  private:
191  bool m_using_mem_table;
192  int m_width_px;
193  std::string m_title;
194  void prepare_chart_maker(chart_maker& _chartMaker,
195  const tsa::date_time& _start, const tsa::date_time& _end);
196  private: //***TRANSACTIONS***
197  //std::vector<tsa::transaction> m_Transactions;
198  public:
200  chart(void);
201 
202  //IU
203  chart(tsa::series_base& _db);
204 
206  virtual ~chart(void);
207 
210  int max_bars_per_chart(void)const;
211 
214  void max_bars_per_chart(int);
215 
218  int max_charts_to_save(void)const;
219 
221  void max_charts_to_save(int max_num_charts);
222 
224  void set_color(DefaultColorID id, const color& c);
225 
227  void width(int width);
228 
230  int width(void)const;
231 
234  void name(const tsa::string& _s) { m_title = _s; }
235 
237  const std::string& name(void)const { return m_title; }
238 
242  pane_proxy get_pane(size_t _idx);
243 
245  size_t pane_count(void)const;
246 
247  public://internal use
248  const std::string& title(void)const { return m_title; }
249  //IU
250  void title(const tsa::string& _s) { m_title = _s; }
251  pane_proxy operator[](size_t);
252  pane_proxy get_pane_proxy(size_t idx, bool noCreate);
253  //void AddLatestTransactions(std::vector<tsa::transaction>& _transArr);
254  void SetTrackTransactionsAndPosition(bool b = true);
255  public:
257  void save_png(const tsa::os::path& path);
260  void save_png(const tsa::os::path& path,
261  const tsa::date_time& start, const tsa::date_time& end);
268  size_t save_png_book(const tsa::os::path& dir_path, size_t bars_per_page = 0);
269  //IU
270  void print_table(std::ostream&);
271  private: // Internal Use.
272  void save_gif(const tsa::string& fileName);
273  void save_gif(const tsa::string& fileName, const tsa::date_time& start, const tsa::date_time& end);
274  void save_multi_gif(const tsa::string& fileName, size_t nBarsPerPage = 0);
275  private:
276  bool m_columns_defined;
277  bool m_prev_input_was_field_name;
278  size_t m_input_pos;
279  size_t m_value_pos;
280  size_t m_curr_input_pos;
281  size_t m_num_named_fields;//not necessarily same as num items
282  size_t m_num_values;
283  std::string m_most_recent_field_name;
284  std::vector<tsa::type_t> m_types;
285  std::vector<size_t> m_first_column_positions;
286  std::vector<bool> m_input_is_name;
287  tsa::date_time m_timestamp;
288  public:
289  virtual void cmp__evaluate_bar(const date_time&)override;
290  virtual void cmp__finalize(void)override;
291  void reset_counters(void);
292  bool FirstRecordCompletedAndSchemaReset(void)const;
293 
296  chart& operator<<(const chart::pane& pane);
299  chart& operator<<(const char* name);
302  chart& operator<<(const string& name);
305  chart& operator<<(double value);
308  chart& operator<<(int64_t value);
310  chart& operator<<(const plot::plot_base&);
312  chart& operator<<(const functor::auto_plot& functor);
313 
315  template<typename T>
316  chart& operator<<(const functor::parent<T>& _functor) {
317  if(!m_columns_defined) {
318  //------------------------
319  // Register Chart Component
321  component::register_with_strategy(strategy_context::strategy_ptr__null_ok());
322  }
323  //------------------------
324  size_t pane_count = this->pane_count();
325  if(pane_count == 0) {
326  throw exception(class_name(), "chart has no pane", SLOC);
327  }
328  pane_proxy pp = this->get_pane(pane_count - 1);
329  //pane y min max
330  if(_functor.pane_y_max.defined()) {
331  pp.y_max((double)_functor.pane_y_max);
332  }
333  if(_functor.pane_y_min.defined()) {
334  pp.y_min((double)_functor.pane_y_min);
335  }
336  //horizontal lines
337  for(size_t l = 0; l < _functor.m_h_lines.size(); l++) {
338  double y_value = _functor.m_h_lines[l].y_value;
339  color c = _functor.m_h_lines[l].clr;
340  int w = _functor.m_h_lines[l].weight;
341  pp.h_line(y_value, c, w);
342  }
343  }
344  //paint the plots
345  size_t num_plots = _functor.output_count();
346  for(size_t pl = 0; pl < num_plots; pl++) {
347  auto p = _functor.get_plot_info(pl);
348  if(!p.plot_it) {
349  continue;
350  }
351  auto t = p.type;
352 #ifdef _DEBUG
353  /*color c = p.color;
354  int w = p.weight;
355  string n = p.name;
356  double value = p.value;*/
357 #endif
358  if(t == plot_type::line) {
359  *this << plot::line(_functor.value(pl), p.clr, p.weight) << p.name;
360  } else if(t == plot_type::area) {
361  *this << plot::area(_functor.value(pl), p.clr) << p.name;
362  } else if(t == plot_type::bar) {
363  *this << plot::bar(_functor.value(pl), p.clr) << p.name;
364  } else {
365  throw exception(class_name(), "unrecognized plot type", SLOC);
366  }
367  }
368  return *this;
369  }
370 
371  template<typename T>
372  chart& operator<<(const series_tuple<T>& _tuple) {
373  if(!m_columns_defined) {
374  //------------------------
375  // Register Chart Component
377  component::register_with_strategy(strategy_context::strategy_ptr__null_ok());
378  }
379  //------------------------
380  size_t pane_count = this->pane_count();
381  if(pane_count == 0) {
382  throw exception(class_name(), "chart has no pane", SLOC);
383  }
384  pane_proxy pp = get_pane(pane_count - 1);
385  //pane y min max
386  if(_tuple.pane_y_max().defined()) {
387  pp.y_max((double)_tuple.pane_y_max());
388  }
389  if(_tuple.pane_y_min().defined()) {
390  pp.y_min((double)_tuple.pane_y_min());
391  }
392  //horizontal lines
393  auto hl = _tuple.pane_h_lines();
394  for(size_t l = 0; l < hl.size(); l++) {
395  double y_value = hl[l].y_value;
396  color c = hl[l].clr;
397  int w = hl[l].weight;
398  pp.h_line(y_value, c, w);
399  }
400  size_t num_plots = _tuple.size();
401  for (size_t pl = 0; pl < num_plots; pl++) {
402  if (_tuple[pl].empty())
403  throw tsa::exception(class_name(), "tuple has empty series - cannot plot" , SLOC);
404  }
405  }
406  //paint the plots
407  size_t num_plots = _tuple.size();
408  for(size_t pl = 0; pl < num_plots; pl++) {
409  auto p = _tuple[pl].plot_info();
410  if(!p.plot_it) {
411  continue;
412  }
413  auto t = p.type;
414 #ifdef _DEBUG
415  /*color c = p.color;
416  int w = p.weight;
417  string n = p.name;
418  double value = p.value;*/
419 #endif
420  if(t == plot_type::line) {
421  *this << plot::line(_tuple[pl][0], p.clr, p.weight) << p.name;
422  } else if(t == plot_type::area) {
423  *this << plot::area(_tuple[pl][0], p.clr) << p.name;
424  } else if(t == plot_type::bar) {
425  *this << plot::bar(_tuple[pl][0], p.clr) << p.name;
426  } else {
427  throw exception(class_name(), "unrecognized plot type", SLOC);
428  }
429  }
430  return *this;
431  }
432 
433  /* template<typename T>
434  chart& operator<<(const scalar<T>& _scalar){
435  return operator<<(*_scalar.series_ptr());
436  }*/
437 
438  template<typename T>
439  chart& operator<<(const series<T>& _ser) {
440  if(!m_columns_defined) {
441  //------------------------
442  // Register Chart Component
444  component::register_with_strategy(strategy_context::strategy_ptr__null_ok());
445  }
446  //------------------------
447  size_t pane_count = this->pane_count();
448  if(pane_count == 0) {
449  throw exception(class_name(), "chart has no pane", SLOC);
450  }
451  pane_proxy pp = get_pane(pane_count - 1);
452  //pane y min max
453  if(_ser.pane_y_max().defined()) {
454  pp.y_max((double)_ser.pane_y_max());
455  }
456  if(_ser.pane_y_min().defined()) {
457  pp.y_min((double)_ser.pane_y_min());
458  }
459  //horizontal lines
460  auto hl = _ser.pane_h_lines();
461  for(size_t l = 0; l < hl.size(); l++) {
462  double y_value = hl[l].y_value;
463  color c = hl[l].clr;
464  int w = hl[l].weight;
465  pp.h_line(y_value, c, w);
466  }
467 
468  if (_ser.empty())
469  throw exception(class_name(), "Cannot plot an empty series", SLOC);
470  }
471  //paint the plots
472 
473  auto p = _ser.plot_info();
474  tsa_assert(p.plot_it);
475  // if(!p.plot_it) {
476  // return *this;
477  //}
478  auto t = p.type;
479 #ifdef _DEBUG
480  /*color c = p.color;
481  int w = p.weight;
482  string n = p.name;
483  double value = p.value;*/
484 #endif
485 
486 
487  if(t == plot_type::line) {
488  *this << plot::line(_ser[0], p.clr, p.weight) << p.name;
489  } else if(t == plot_type::area) {
490  *this << plot::area(_ser[0], p.clr) << p.name;
491  } else if(t == plot_type::bar) {
492  *this << plot::bar(_ser[0], p.clr) << p.name;
493  } else {
494  throw exception(class_name(), "unrecognized plot type", SLOC);
495  }
496 
497  return *this;
498  }
499  };
500 
502 
503  //}//namespace graphics
504 
505 } //namespace tsa
506 
507 #endif
double max(const series< double > &series, size_t period)
Returns the highest value found in series over given .
Definition: TSAFunctionsScalar.cpp:180
Utility class to manipulate file system paths. This class is intended to fulfil the requirements of t...
Definition: TSAOS.h:36
Class representing a timeseries chart.
Definition: TSCWrapper.h:147
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
Class for managing native timeseries database files. Extremely high speed data access.
Definition: TSADatabase.h:118
color line_color
The line color.
Definition: TSCWrapper.h:41
static const color auto_color
Definition: TSATypeDef.h:1099
void name(const tsa::string &_s)
Sets the chart name which is used to name the chart directory when saving the chart as a chart book...
Definition: TSCWrapper.h:234
static const color white
Definition: TSATypeDef.h:1201
void y_min(double)
Sets the pane minimum y-value to at at most min.
Definition: TSCWrapper.cpp:182
tsa::date_time start_timestamp
Trendline start timestamp.
Definition: TSCWrapper.h:30
void y_max(double max)
Sets the pane maximum y-value to at least max.
Definition: TSCWrapper.cpp:177
Parent class for all plot types.
Definition: TSCPlots.h:30
Defines the columnar structure of a table. Each column has a name, data type and field size...
Definition: TSADataDef.h:88
double start_price
Trendline start price.
Definition: TSCWrapper.h:31
#define SLOC
Macro to be passed as argument to Exception constructors. Contains information about the source locat...
Definition: TSAError.h:92
Plots a &#39;bars&#39; on a chart pane where bars are drawn from the zero line. Bars essentially oscillate ar...
Definition: TSCPlots.h:169
Parent class.
Definition: TSAFunctorParent.h:260
bool is_registered_with_strategy(void) const
Definition: TSAComponent.cpp:50
Class tsa::exception used by most classes of the Trading System API library. The class inherits from ...
Definition: TSAError.h:37
Parent class for many library classes.
Definition: TSATypeDef.h:462
Class that represents a horizontal line to be drawn on a chart.
Definition: TSCWrapper.h:39
color line_color
Trendline color.
Definition: TSCWrapper.h:34
tsa::date_time end_timestamp
Trendline end date_time.
Definition: TSCWrapper.h:32
int weight
The line weight.
Definition: TSCWrapper.h:42
Plots a &#39;line plot&#39; on a chart pane.
Definition: TSCPlots.h:127
Class representing a &#39;pane&#39; on a chart.
Definition: TSCWrapper.h:151
Represents a color for use in graphics operations.
Definition: TSATypeDef.h:1041
const std::string & name(void) const
Returns the chart name.
Definition: TSCWrapper.h:237
A line.
Definition: TSASeriesTemplate.h:136
Proxy class for class chart::pane. To be used for setting pane properties. An instance of this class ...
Definition: TSCWrapper.h:49
double end_price
Trendine end price.
Definition: TSCWrapper.h:33
Plots a series as an &#39;area plot&#39; on a chart pane.
Definition: TSCPlots.h:213
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
void h_line(double y_value, const color &clr=color::white, size_t weight=1)
Adds a horizonal line to the pane, at given y_value, with given clr and weight.
Definition: TSCWrapper.cpp:153
int weight
Trendline weight.
Definition: TSCWrapper.h:35
void register_with_strategy(strategy *)
Definition: TSAComponent.cpp:54
Base class for components such as series_base adaptors.
Definition: TSAComponent.h:34
Class mem_table represents a memory based table. mem_table objects can be used in strategies both for...
Definition: TSAMemTable.h:48
double y_value
The y-value for which the line is drawn.
Definition: TSCWrapper.h:40