Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSAFunctorParent.h
1 
2 /* ===================================================================
3 *
4 * T R A D I N G S Y S T E M A P I ™
5 * Copyright © 1999 - 2014 by Peter Ritter ( TradingSystemAPI.com )
6 * A L L R I G H T S R E S E R V E D
7 *
8 * Consult your license regarding permissions and restrictions.
9 * You may obtain a copy of the License at:
10 * http://www.TradingSystemAPI.com/licenses/LICENSE-TSAPI-3.0.html
11 *
12 * ====================================================================
13 */
14 
15 #ifndef TSA_FUNCTOR_PARENT__INCLUDED
16 #define TSA_FUNCTOR_PARENT__INCLUDED
17 
18 #include <vector>
19 #include <memory>
20 
21 #include "TSASeriesTemplate.h"
22 #include "TSAString.h"
23 #include "TSASeriesVariant.h"
24 #include "TSASeriesImp.h"
25 #include "TSAFunctions.h"
26 #include "TSAStrategy.h"
27 #include "TSAFunctorParent.h"
28 
29 namespace tsa {
30  namespace functor {
31 
32  class functor_parent_base : public tsa::object {
33  strategy* m_strategy_ptr = nullptr;
34  public:
35  bool first_bar_completed(void) const { return m_first_bar_is_completed; }
36  void set_first_bar_completed(void) { m_first_bar_is_completed = true; }
37  bool m_first_bar_is_completed = false;
38  public:
39  functor_parent_base(void) { ; }
40  virtual ~functor_parent_base(void){ ; }
41  virtual bool input_data_is_ok(void) { return true; }
42  virtual void evaluate(void) { ; };// { return false; }
43  void set_strategy_ptr(strategy* ptr) { m_strategy_ptr = ptr; }
44  strategy* strategy_ptr(void)const { return m_strategy_ptr; }
45  };
46 
47  /*
48  ** ====================================================
49  ** >>> class functor::parent <<<
50  ** ====================================================
51  */
52 
53  template<typename T>
54  class parent : public functor_parent_base {
55  tsa_declare_testable;
56  private:
57  bool m_out_series_has_data = false;
58 
59  public:
61  std::vector<h_line> m_h_lines;
62  public:
63  variant pane_y_max;
64  variant pane_y_min;
65  bool show_grid;
66  bool dotted_grid;
67  bool config_pane;
68  color grid_color;
70  std::vector<plot_info> m_plots;
71  public:
72  std::vector<series<T>*> m_outputs;
73  date_time m_last_update_timestamp;
74  public:
76  void check_for_duplicate_invocation(void);
78  void create_outputs(size_t num_outputs);
79  void verify_class_member_declaration(void) const;
80  public:
82  parent(void);
84  parent(size_t num_outputs);
86  virtual ~parent(void);
88  operator variant(void) const;
90  operator series<T>& (void) const;
92  operator series_cref<T> (void) const;
94  series_cref<T> output(size_t position) const;
96  void push(T value);
98  void push(size_t position, T value);
100  operator T(void) const;
102  T value(void) const;
104  T value(size_t position) const;
106  size_t output_count(void) const;
107  public:
109  void add_h_line(const h_line& _l);
111  void add_h_line(double _y_value, const color& _color, int _weight);
113  void add_plot(const plot_info& _p);
115  plot_info& get_plot_info(size_t pos);
117  const plot_info& get_plot_info(size_t pos) const;
118  };
119 
120 
121  template<typename T>
122  void parent<T>::check_for_duplicate_invocation(void) {
123 #ifdef TSA_DEBUG
124  date_time strategy_timestamp = strategy_context::strategy_ptr()->bar_timestamp();
125  if(strategy_timestamp == parent<T>::m_last_update_timestamp)
126  throw exception(this, "functor invoked twice on same strategy during_bar; timestamp: "
127  + strategy_timestamp.to_string(), SLOC);
128  parent<T>::m_last_update_timestamp = strategy_timestamp;
129 #endif
130  }
131 
132  template<typename T>
133  void parent<T>::create_outputs(size_t num_outputs) {
134  for(size_t c = 0; c < num_outputs; c++) {
135  auto ptr = new series<T>(this);
136  tsa_assert(ptr->owning_functor_ptr() == this);
137  tsa_assert(!ptr->is_managed());
138  m_outputs.push_back(ptr);
139  m_plots.push_back(plot_info());
140  }
141  }
142 
143  template<typename T>
144  parent<T>::parent(void) {
145  verify_class_member_declaration();
146  create_outputs(1);
147  }
148 
149  template<typename T>
150  parent<T>::parent(size_t num_outputs) {
151  verify_class_member_declaration();
152  create_outputs(num_outputs);
153  }
154 
155  template<typename T>
156  void parent<T>::verify_class_member_declaration(void)const {
157  strategy* strat_ptr = strategy_context::strategy_ptr(true);
158  if(strat_ptr && strat_ptr->state() != strategy_state_type::preparing_managed_code) {
159  throw tsa::exception(this, "'Managed functions' must be invoked as part of strategy::on_prepare(). "
160  " 'Functor objects' must be declared as strategy class members!", SLOC);
161  }
162  }
163 
164  template<typename T>
165  parent<T>::~parent(void) {
166  for(auto& ptr : m_outputs) {
167  delete ptr;
168  }
169  }
170 
171  template<typename T>
172  parent<T>::operator series<T>& (void)const {
173  return *m_outputs[0];
174  }
175 
176  template<typename T>
177  parent<T>::operator series_cref<T> (void)const {
178  return series_cref<T>(*m_outputs[0]);
179  }
180 
181  template<typename T>
182  parent<T>::operator variant(void)const {
183  return value(0);
184  }
185 
186  template<typename T>
187  series_cref<T> parent<T>::output(size_t position)const {
188  series<T>* ser_ptr = m_outputs[position];
189  tsa_assert(ser_ptr->owning_functor_ptr() == this);
190  return series_cref<T>(*m_outputs[position]);
191  }
192 
193  template<typename T>
194  void parent<T>::push(T value) {
195  m_outputs[0]->push(value);
196  m_out_series_has_data = true;
197  }
198 
199  template<typename T>
200  void parent<T>::push(size_t position, T value) {
201  m_outputs[position]->push(value);
202  m_out_series_has_data = true;
203  }
204 
205  template<typename T>
206  parent<T>::operator T(void)const {
207  return value();
208  }
209 
210  template<typename T>
211  T parent<T>::value(void)const {
212 #if TSA_DEBUG
213  if(!m_out_series_has_data || m_outputs[0]->size() == 0) {
214  throw tsa::exception(this, "no output value available; was functor invoked?", SLOC);
215  }
216 #endif
217  return m_outputs[0]->get_at(0);
218  }
219 
220  template<typename T>
221  T parent<T>::value(size_t position)const {
222 #if TSA_DEBUG
223  if(!m_out_series_has_data || m_outputs[0]->size() == 0) {
224  throw tsa::exception(class_name(), "no output value available; was functor invoked?", SLOC);
225  }
226 #endif
227  return m_outputs[position]->get_at(0);
228  }
229 
230  template<typename T>
231  size_t parent<T>::output_count(void)const { return m_outputs.size(); }
232 
233  template<typename T>
234  void parent<T>::add_h_line(const h_line& _l) { m_h_lines.push_back(_l); }
235 
236  template<typename T>
237  void parent<T>::add_h_line(double _y_value, const color& _color, int _weight) {
238  h_line l;
239  l.y_value = _y_value;
240  l.weight = _weight;
241  l.clr = _color;
242  m_h_lines.push_back(l);
243  }
244  template<typename T>
245  void parent<T>::add_plot(const plot_info& _p) { m_plots.push_back(_p); }
246 
247  template<typename T>
248  plot_info& parent<T>::get_plot_info(size_t pos) { return m_plots[pos]; }
249 
250  template<typename T>
251  const plot_info& parent<T>::get_plot_info(size_t pos) const { return m_plots[pos]; }
252 
253  /*
254  ** ====================================================
255  ** >>> class functor::auto_plot <<<
256  ** ====================================================
257  */
258 
260  class auto_plot : public object {
261  public:
262  struct h_line {
263  color clr;
264  double y_value;
265  unsigned weight;
266 
267  h_line(void) : clr(color(200, 200, 200)), y_value(0.0), weight(1) { ; }
268  h_line(const color& _color, double _y_value, int _weight)
269  : clr(_color), y_value(_y_value), weight(_weight) {
270  ;
271  }
272  };
273  typedef std::vector<h_line> HLINEARR;
274  HLINEARR m_h_lines;
275  public:
276  variant pane_y_max;
277  variant pane_y_min;
278  bool show_grid;
279  bool dotted_grid;
280  bool config_pane;
281  color grid_color;
282  struct plot_info {
283  string name;
284  variant period;
285  plot_type type;
286  color color;
287  double value;
288  unsigned weight;
289  double get_value(void) {
290  return value;
291  }
292  void set_value(double _v) {
293  value = _v;
294  }
295  plot_info(void) { ; }
296  };
297  typedef std::vector<plot_info> PLOTARR;
298  PLOTARR m_plots;
299  public:
300  auto_plot(void) { ; }
301  void add_h_line(const h_line& _l) { m_h_lines.push_back(_l); }
302  void add_h_line(double _y_value, const color& _color, int _weight) {
303  h_line l;
304  l.y_value = _y_value;
305  l.weight = _weight;
306  l.clr = _color;
307  m_h_lines.push_back(l);
308  }
309  void add_plot(const plot_info& _p) { m_plots.push_back(_p); }
310  plot_info& get_plot(size_t pos) { return m_plots[pos]; }
311  const plot_info& get_plot(size_t pos) const { return m_plots[pos]; }
312  size_t plot_count(void) const { return m_plots.size(); }
313  };
314  }
315 }
316 
317 #endif
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
#define SLOC
Macro to be passed as argument to Exception constructors. Contains information about the source locat...
Definition: TSAError.h:92
plot_type
Values that represent plot types.
Definition: TSASeriesTemplate.h:149
Parent class.
Definition: TSAFunctorParent.h:260
variant objects can represent values of different types.
Definition: TSAVariant.h:140
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
Represents a color for use in graphics operations.
Definition: TSATypeDef.h:1041