Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSAMetrics.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_METRICS__INCLUDED
16 #define TSA_METRICS__INCLUDED
17 
18 #include <string>
19 #include <map>
20 
21 #include "TSATypeDef.h"
22 #include "TSAMemTable.h"
23 #include "TSAJSON.h"
24 
25 namespace tsa {
26 
27  class metrics_source;
28  class contract;
29  class strategy;
30 
31  namespace os {
32  class path;
33  }
34 
35  /*
36  ** ====================================================
37  ** >>> class metrics_source <<<
38  ** ====================================================
39  */
40 
44  class dll_export metrics_source {
45  public:
46  virtual variant metric(const std::string& metric_name, trade_grouping gr = all_trades) const = 0;
47  virtual variant get_metric(perf_metric_type, trade_grouping gr = all_trades) const = 0;
48  virtual void daily_cash_flow(mem_table& mt) const = 0;
49  virtual bool is_finalized(void) const = 0;
50 
51  virtual const date_time& metrics_source__begin_timestamp(void) const = 0;
52  virtual const date_time& metrics_source__end_timestamp(void) const = 0;
53  virtual bool is_contract(void) const = 0;
54  virtual std::string metric_source_name(void) const = 0;
55  };
56 
57 
58  class metrics_source;
59  class contract;
60  class strategy;
61 
62  namespace os {
63  class path;
64  }
65 
66  /*
67  ** ====================================================
68  ** >>> class metrics_container <<<
69  ** ====================================================
70  */
71 
76  {
77  protected:
78  std::map<std::string, variant> m_map;
79  public:
80  metrics_container(void);
82  bool operator==(const metrics_container& other);
83  void copy(const metrics_container& other);
84  void operator=(const metrics_container& other);
85  bool is_equal(const metrics_container& other)const;
86  bool verify_equal(const metrics_container& other)const;
87  bool has_metric(const std::string& metric_name)const;
88  bool get_value(const std::string& metric_name, variant& _value)const;
89  const variant& get(const std::string& metric_name)const;
90  template<typename T> T get(const std::string& _metric_name)const {
91  return get(_metric_name).get<T>();
92  }
93  auto_cast_variant get_auto_cast(const std::string& name)const { return auto_cast_variant(get(name)); }
94 
95  variant& operator[](const std::string& key);
96  const variant& operator[](const std::string& key)const;
97  void merge(const metrics_container& other);
98  void update_values_from(const metrics_container& other);
99  void update_value(const std::string& metric_name, const variant& value);
100  void create_if_missing(const std::string& metric_name, const variant& value);
101  void create_if_missing(const std::vector<std::string>& metric_name, const variant& value);
102  bool check_has_all(const std::vector<std::string>& metric_name, const variant& value);
103  void clear(void);
104  size_t size(void)const;
105  void print(std::ostream&)const;
106  public:
107  tsa::json to_json(void)const;
108  void from_json(const json&);
109  std::string to_string(void);
110  void from_string(const std::string&);
111  public:
112  template <class Archive>
113  void serialize(Archive & ar) {
114  ar(m_map);
115  }
116 
117  std::ostream& write(std::ostream&) const;
118  std::istream& read(std::istream&);
119  public:
120 
121  int64_t trade_count(void) const { return get_auto_cast(metric::trade_count); }
122  int64_t loser_count(void) const { return get_auto_cast(metric::loser_count); }
123  int64_t winner_count(void) const { return get_auto_cast(metric::winner_count); }
124  double percent_profitable(void) const { return get_auto_cast(metric::percent_profitable); }
125  double avg_net_trade_pl(void) const { return get_auto_cast(metric::avg_net_trade_pl); }
126  double avg_gross_trade_pl(void) const { return get_auto_cast(metric::avg_gross_trade_pl); }
127  double avg_winner(void) const { return get_auto_cast(metric::avg_winner); }
128  double avg_loser(void) const { return get_auto_cast(metric::avg_loser); }
129  double max_loser(void) const { return get_auto_cast(metric::max_loser); }
130  double max_winner(void) const { return get_auto_cast(metric::max_winner); }
131  int64_t max_consec_winners(void) const { return get_auto_cast(metric::max_consec_winners); }
132  int64_t max_consec_losers(void) const { return get_auto_cast(metric::max_consec_losers); }
133  double sum_pos_excursion(void) const { return get_auto_cast(metric::sum_pos_excursion); }
134  double sum_neg_excursion(void) const { return get_auto_cast(metric::sum_neg_excursion); }
135  double max_pos_excursion(void) const { return get_auto_cast(metric::max_pos_excursion); }
136  double max_neg_excursion(void) const { return get_auto_cast(metric::max_neg_excursion); }
137  double avg_pos_excursion(void) const { return get_auto_cast(metric::avg_pos_excursion); }
138  double avg_neg_excursion(void) const { return get_auto_cast(metric::avg_neg_excursion); }
139  double stddev_trade_pl(void) const { return get_auto_cast(metric::stddev_trade_pl); }
140  double avg_winner_bar_count(void) const { return get_auto_cast(metric::avg_winner_bar_count); }
141  int64_t total_winner_bar_count(void) const { return get_auto_cast(metric::total_winner_bar_count); }
142  int64_t total_loser_bar_count(void) const { return get_auto_cast(metric::total_loser_bar_count); }
143  double avg_loser_bar_count(void) const { return get_auto_cast(metric::avg_loser_bar_count); }
144  double avg_trade_bar_count(void) const { return get_auto_cast(metric::avg_trade_bar_count); }
145  int64_t num_bars_in_trade(void) const { return get_auto_cast(metric::num_bars_in_trade); }
146  int64_t total_num_bars_processed(void) const { return get_auto_cast(metric::total_num_bars_processed); }
147  double percent_bars_in_trade(void) const { return get_auto_cast(metric::percent_bars_in_trade); }
148  double net_profit(void) const { return get_auto_cast(metric::net_profit); }
149  double gross_profit(void) const { return get_auto_cast(metric::gross_profit); }
150  double winners_net(void) const { return get_auto_cast(metric::winners_net); }
151  double losers_net(void) const { return get_auto_cast(metric::losers_net); }
152  double total_commission(void) const { return get_auto_cast(metric::total_commission); }
153  double total_slippage(void) const { return get_auto_cast(metric::total_slippage); }
154  double total_transaction_cost(void) const { return get_auto_cast(metric::total_transaction_cost); }
155  double profit_factor(void) const { return get_auto_cast(metric::profit_factor); }
156  double avg_gain_to_avg_loss_ratio(void) const { return get_auto_cast(metric::avg_gain_to_avg_loss_ratio); }
157  double max_closed_out_runup(void) const { return get_auto_cast(metric::max_closed_out_runup); }
158  double max_closed_out_rundown(void) const { return get_auto_cast(metric::max_closed_out_rundown); }
159  int64_t max_trade_transaction_count(void) const { return get_auto_cast(metric::max_trade_transaction_count); }
160  int64_t max_trade_position_size(void) const { return get_auto_cast(metric::max_trade_position_size); }
161  double avg_trade_transaction_count(void) const { return get_auto_cast(metric::avg_trade_transaction_count); }
162  double avg_trade_position_size(void) const { return get_auto_cast(metric::avg_trade_position_size); }
163  double avg_trade_commission(void) const { return get_auto_cast(metric::avg_trade_commission); }
164  double avg_trade_slippage(void) const { return get_auto_cast(metric::avg_trade_slippage); }
165  double avg_trade_cost(void) const { return get_auto_cast(metric::avg_trade_cost); }
166  double avg_slippage_per_unit(void) const { return get_auto_cast(metric::avg_slippage_per_unit); }
167  double avg_cost_per_unit(void) const { return get_auto_cast(metric::avg_cost_per_unit); }
168  double percent_units_without_slippage(void) const { return get_auto_cast(metric::percent_units_without_slippage); }
169  double avg_net_profit_per_unit(void) const { return get_auto_cast(metric::avg_net_profit_per_unit); }
170  double avg_gross_profit_per_unit(void) const { return get_auto_cast(metric::avg_gross_profit_per_unit); }
171  //double last_trade_is_open(void) const { return get_auto_cast(metric::); }
172 
173 
174  //double in_drawdown(void) const { return get_auto_cast(metric::); }
175  double current_drawdown(void) const { return get_auto_cast(metric::current_drawdown); }
176  int64_t num_drawdowns(void) const { return get_auto_cast(metric::num_drawdowns); }
177  double max_drawdown(void) const { return get_auto_cast(metric::max_drawdown); }
178  int64_t max_drawdown_length(void) const { return get_auto_cast(metric::max_drawdown_length); }
179  double avg_drawdown_length(void) const { return get_auto_cast(metric::avg_drawdown_length); }
180  double avg_drawdown(void) const { return get_auto_cast(metric::avg_drawdown); }
181  double max_account_balance(void) const { return get_auto_cast(metric::max_account_balance); }
182  double min_account_balance(void) const { return get_auto_cast(metric::min_account_balance); }
183 
184  double max_drawdown__eod(void) const { return get_auto_cast(metric::max_drawdown__eod); }
185  int64_t max_drawdown_length__eod(void) const { return get_auto_cast(metric::max_drawdown_length__eod); }
186  double avg_drawdown_length__eod(void) const { return get_auto_cast(metric::avg_drawdown_length__eod); }
187  double avg_drawdown__eod(void) const { return get_auto_cast(metric::avg_drawdown__eod); }
188  int64_t num_drawdowns__eod(void) const { return get_auto_cast(metric::num_drawdowns__eod); }
189 
190  int64_t num_transactions(void) const { return get_auto_cast(metric::num_transactions); }
191  int64_t num_transactions_at_market(void) const { return get_auto_cast(metric::num_transactions_at_market); }
192  int64_t num_transactions_on_stop(void) const { return get_auto_cast(metric::num_transactions_on_stop); }
193  int64_t num_transactions_at_limit(void) const { return get_auto_cast(metric::num_transactions_at_limit); }
194  double avg_transaction_size(void) const { return get_auto_cast(metric::avg_transaction_size); }
195 
196  int64_t num_units_traded(void) const { return get_auto_cast(metric::num_units_traded); }
197  int64_t num_units_traded_without_slippage(void) const { return get_auto_cast(metric::num_units_traded_without_slippage); }
198  int64_t num_units_traded_at_market(void) const { return get_auto_cast(metric::num_units_traded_at_market); }
199  int64_t num_units_traded_on_stop(void) const { return get_auto_cast(metric::num_units_traded_on_stop); }
200  int64_t num_units_traded_at_limit(void) const { return get_auto_cast(metric::num_units_traded_at_limit); }
201  int64_t num_units_traded_other(void) const { return get_auto_cast(metric::num_units_traded_other); }
202 
203  double drawdown_multiplier(void) const { return get_auto_cast(metric::drawdown_multiplier); }
204  double sharpe_ratio(void) const { return get_auto_cast(metric::sharpe_ratio); }
205  double sortino_ratio(void) const { return get_auto_cast(metric::sortino_ratio); }
206  int64_t basket_num_instruments(void) const { return get_auto_cast(metric::basket_num_instruments); }
207  int64_t basket_num_strategies(void) const { return get_auto_cast(metric::basket_num_strategies); }
208  int64_t basket_instrument_num_bars_processed(void) const { return get_auto_cast(metric::basket_instrument_num_bars_processed); }
209  int64_t basket_strategy_num_bars_processed(void) const { return get_auto_cast(metric::basket_strategy_num_bars_processed); }
210  double annual_risk_free_rate_of_return(void) const { return get_auto_cast(metric::annual_risk_free_rate_of_return); }
211  date_time begin_timestamp(void) const { return get_auto_cast(metric::begin_timestamp); }
212  date_time end_timestamp(void) const { return get_auto_cast(metric::end_timestamp); }
213  //duration total_strategy_duration(void) const { return get_auto_cast(metric::total_strategy_duration); }
214  //duration total_instrument_duration(void) const { return get_auto_cast(metric::total_instrument_duration); }
215  std::string source_name(void) const { return get_auto_cast(metric::source_name); }
216  bool source_is_instrument(void) const { return get_auto_cast(metric::source_is_instrument); }
217 
218  };
219 
220 
221  /*
222  ** ====================================================
223  ** >>> class equity_metrics <<<
224  ** ====================================================
225  */
226  struct basic_trade_info {
227  date_time timestamp;
228  double net_profit;
229  double cost;
230  int64_t bars;
231  int64_t units;
232  bool is_long;
233  bool operator==(const basic_trade_info& other)const { return timestamp == other.timestamp; }
234  bool operator<(const basic_trade_info& other) const { return timestamp < other.timestamp; }
235  bool operator<=(const basic_trade_info& other) const { return timestamp <= other.timestamp; }
236  public:
237  template<class Archive>
238  void save(Archive & ar) const {
239  ar.saveBinary((const char*)(this), sizeof(basic_trade_info));
240  }
241  template<class Archive>
242  void load(Archive & ar) {
243  ar.loadBinary((char*)(this), sizeof(basic_trade_info));
244  }
245  };
246 
251  /*
252  ** ====================================================
253  ** >>> class basic_trade_metrics <<<
254  ** ====================================================
255  */
256 
257  // This class retains and aggregates 'basic' trade info
258  // to recalculate difficult metrics after metrics merger
259  class tiny_trade_metrics : public object {
260  std::vector<basic_trade_info> m_trades;
261  metrics_container m_metrics[3];
262  public:
263  tiny_trade_metrics(void);
264  void populate_trades(const std::vector<basic_trade_info>&);
265  void operator=(const tiny_trade_metrics&);
266  bool operator==(const tiny_trade_metrics&);
267  void merge(const tiny_trade_metrics&);
269  protected:
270  struct base_metrics {
271  double sum_net_profit = 0.0;
272  double sum_cost = 0.0;
273  int64_t sum_bars = 0;
274  int64_t num_trades = 0;
275  int64_t sum_units = 0;
276  int64_t winner_count = 0;
277  int64_t loser_count = 0;
278  int64_t sum_winner_bar_count = 0;
279  int64_t sum_loser_bar_count = 0;
280  int64_t max_consec_winners = 0;
281  int64_t max_consec_losers = 0;
282  double max_closed_out_runnup = 0.0;
283  double max_closed_out_rundown = 0.0;
284  std::vector<double> profits_arr;
285  };
286  void calculate_base_metrics(tsa::trade_grouping, base_metrics&);
287  void calculate_metrics(tsa::trade_grouping);
288  public:
289  template <class Archive>
290  void serialize(Archive & ar) {
291  ar(m_metrics[tsa::all_trades]);
292  ar(m_metrics[tsa::long_only]);
293  ar(m_metrics[tsa::short_only]);
294  ar(m_trades);
295  }
296  };
297 
298  /*
299  ** ====================================================
300  ** >>> class daily_equity <<<
301  ** ====================================================
302  */
303  class daily_equity_container
304  : public metrics_container, public object
305  {
306  struct daily_record {
307  tsa::date date;
308  double value;
309  };
310  private:
311  std::map<date, double> m_daily_cash_flows;
312  void merge_daily_cash_flows(const std::map<date, double>&);
313  void calculate_equity_metrics(void);
314  public:
315  daily_equity_container(void);
316  void operator=(const daily_equity_container&);
317  bool operator==(const daily_equity_container&);
318  void merge(const daily_equity_container&);
319  size_t num_cash_flows(void)const;
320  public:
321  void set_daily_cash_flow_series(mem_table& _daily_cash_flow_series);
322  public:
323  double calculate_cumulative_cash_flow(void) const;
324  double average_daily_cash_flow(const std::vector<double>& daily_cash_flows)const;
325  double get_max_drawdown(void)const;
326  void create_cumulative_equity_series(mem_table& _mt);
327  std::vector<daily_record> daily_equity_history(void) const;
328  std::vector<daily_record> daily_cash_flow_history(void) const;
329  std::vector<double> _daily_cash_flow_history(void) const;
330  std::vector<double> _daily_equity_history(void) const;
331  public:
332  void print_daily_cash_flow(std::ostream& stream, bool with_date)const;
333  void print_daily_equity(std::ostream& stream, bool with_date)const;
334  void print_equity_graph(std::ostream&) const;
335  public:
336  template <class Archive>
337  void serialize(Archive & ar) {
338  ar(m_map);
339  ar(m_daily_cash_flows);
340  }
341  };
342 
347  /*
348  ** ====================================================
349  ** >>> class metrics <<<
350  ** ====================================================
351  */
352 
367  class dll_export metrics : public tsa::object {
368  tsa_declare_testable
369  public:
370  static unsigned int STREAM_DELIMITER;
371  private:
372  bool m_is_instrument;
373  std::string m_source_name;
374  size_t m_num_instruments;
375  size_t m_num_strategies;
376  struct sub_metrics {
377  uint64_t trade_count = 0;
378  uint64_t loser_count = 0;
379  uint64_t winner_count = 0;
380  double percent_profitable = 0.0;
381  double avg_net_trade_PL = 0.0;
382  double avg_gross_trade_PL = 0.0;
383  double avg_winner = 0.0;
384  double avg_loser = 0.0;
385  double max_loser = 0.0;
386  double max_winner = 0.0;
387  double stdev_trade_PL = 0.0;
388  double avg_trade_bar_count = 0.0;
389  uint64_t num_bars_in_trade = 0;
390  double percent_bars_in_trade = 0.0;
391  double net_profit = 0.0;
392  double gross_profit = 0.0;
393  double winners_net = 0.0;
394  double losers_net = 0.0;
395  double total_commission = 0.0;
396  double total_slippage = 0.0;
397  double total_transaction_cost = 0.0;
398  double profit_factor = 0.0;
399  double avg_gain_to_avg_loss_ratio = 0.0;
400  uint64_t max_trade_transaction_count = 0;
401  double max_trade_position_size = 0.0;
402  double avg_trade_transaction_count = 0.0;
403  double avg_trade_position_size = 0.0;
404  double avg_trade_cost = 0.0;
405  double avg_trade_commission = 0.0;
406  double avg_trade_slippage = 0.0;
407  double avg_slippage_per_unit = 0.0;
408  double avg_cost_per_unit = 0.0;
409  double percent_units_without_slippage = 0.0;
410  double avg_net_profit_per_unit = 0.0;
411  double avg_gross_profit_per_unit = 0.0;
412  double m_dNumUnitsTraded = 0.0;
413  double m_dNumUnitsTradedWithoutSlippage = 0.0;
414  double m_dNumUnitsTraded_AtMarket = 0.0;
415  double m_dNumUnitsTraded_OnStop = 0.0;
416  double m_dNumUnitsTraded_AtLimit = 0.0;
417  public:
418  sub_metrics(void);
419  void init(void);
420  void copy(const sub_metrics&);
421  bool is_equal(const sub_metrics&);
422  public:
423  void write(out_storage_stream&) const;
424  void read(in_storage_stream&);
425  };
426  sub_metrics m_metrics_all;
427  sub_metrics m_metrics_long;
428  sub_metrics m_metrics_short;
429  static void aggregate_sub_metrics(sub_metrics& aggregate,
430  const sub_metrics&, const sub_metrics&, size_t total_basket_num_bars_processed);
431  private:
432  int64_t total_num_bars_processed = 0;
433  int64_t max_drawdown_length = 0;
434  double avg_drawdown_lengths = 0.0;
435  double avg_drawdown = 0.0;
436  double max_drawdown = 0.0;
437  int64_t num_drawdowns = 0;
438  int64_t m_nNumTransactions = 0;
439  int64_t m_nNumTransactions_AtMarket = 0;
440  int64_t m_nNumTransactions_OnStop = 0;
441  int64_t m_nNumTransactions_AtLimit = 0;
442  duration m_total_duration = 0;
443  date_time m_begin;
444  date_time m_end;
445 
446  //not part of aggregation routines
447  double m_sharpe_ratio = 0.0;
448  double m_sortino_ratio = 0.0;
449  double m_start_capital = 0.0;
450  double m_drawdown_multiplier = 2.0;
451  double m_annual_risk_free_rate_of_return = 0.0;
452  public:
453  std::map<date, double> m_daily_cash_flows;
454  public:
456  struct daily_record {
457  tsa::date date;
458  double value;
459  };
461  std::vector<daily_record> daily_equity_history(void) const;
463  std::vector<daily_record> daily_cash_flow_history(void) const;
465  std::vector<double> _daily_cash_flow_history(void) const;
467  double average_daily_cash_flow(const std::vector<double>& daily_cash_flows)const;
469  std::vector<double> _daily_equity_history() const;
470 
472  double daily_risk_free_return(void)const;
473 
475  double average_trading_days_per_year(void)const;
477  void calculate_equity_metrics(void);
478  void calculate_equity_ratios(void);
480  void print_daily_cash_flow(std::ostream&, bool with_date = false)const;
481  void print_daily_equity(std::ostream&, bool with_date = false)const;
482  protected:
483  void set_sub_metrics(const metrics_source& _s, sub_metrics& _m, trade_grouping _t);
484  void merge_daily_cash_flows(const std::map<date, double>&);
485  static void aggregate_metrics(metrics& combo, const metrics& a, const metrics& b);
486  void calculate_drawdown_metrics(void);
487  void create_cumulative_equity_series(mem_table&);
488  double calculate_cumulative_cash_flow(void) const;
489  protected:
490  static void test__check_performance_aggregation(const metrics&, const metrics&, const metrics&);
491  public:
492 
496  metrics(void);
497  private:
498  metrics(const metrics_source&);
499  metrics(const strategy&);
500  void init(void);
501  public:
506  metrics(const metrics&);
507  public:
511  ~metrics(void);
512  void copy(const metrics&);
513  bool is_equal(const metrics&);
514  void source_name(const std::string& _n);
515  void source_is_instrument(bool _b);
516  std::string source_name(void) const;
517  bool source_is_instrument(void) const;
518  void annual_risk_free_rate_of_return(double annual_rf_ror);
519  double annual_risk_free_rate_of_return(void)const;
520  void drawdown_multiplier(double multipler);
521  double drawdown_multiplier(void)const;
522  public:
523  void write(out_storage_stream&) const;
524  void read(in_storage_stream&);
525  private:
526  public:
527  // Aggregates @b self metrics object with @b other.
528  void merge(const metrics& other);
529  // Aggregates metrics of @b other strategy to @b self metrics instance.
530  void merge(const strategy& other);
531  void init_from_metrics_source(const metrics_source&);
532  void set_strategy_count(size_t);
533  void set_instrument_count(size_t);
534  //void evaluate_equity_metrics(void);
535  protected:
536  void add_as_first(const metrics_source&);
537  protected:
538  void print_equity_graph(std::ostream&) const;
539  public:
546  virtual void daily_cash_flow(mem_table& mt) const;
547  public:
548  virtual const date_time& GetStartDateTime(void) const;
550  date_time GetEndDateTime(void) const;
551  public:
552  /*IU*/
553  size_t num_strategies(void) const;
554  size_t num_instruments(void) const;
555  variant _get_metric(const sub_metrics&, perf_metric_type, trade_grouping) const;
556  variant _get_metric(const sub_metrics&, const std::string&, trade_grouping) const;
557  public:
564  variant get_metric(perf_metric_type metric_type, trade_grouping group = all_trades) const;
565 
567  variant get_metric(const std::string& metric_type, trade_grouping group = all_trades) const;
568 
570  template<typename T>
571  T get(perf_metric_type metric_type, trade_grouping trade_aggr_type = all_trades) const {
572  return get_metric(metric_type, trade_aggr_type).get<T>();
573  }
574  private:
575  size_t GetTradeCount(trade_grouping direction = all_trades) const;
576  size_t GetLoserCount(trade_grouping direction = all_trades) const;
577  size_t GetWinnerCount(trade_grouping direction = all_trades) const;
578  double GetPercentProfitable(trade_grouping direction = all_trades) const;
579  double GetAveNetTradePL(trade_grouping direction = all_trades) const;
580  double GetAveGrossTradePL(trade_grouping direction = all_trades) const;
581  double GetAveWinner(trade_grouping direction = all_trades) const;
582  double GetAveLoser(trade_grouping direction = all_trades) const;
583  double GetMaxLoser(trade_grouping direction = all_trades) const;
584  double GetMaxWinner(trade_grouping direction = all_trades) const;
585  double GetStDevTradePL(trade_grouping direction = all_trades) const;
586  //Bar Count
587  double GetAveTradeBarCount(trade_grouping direction = all_trades) const;
588  size_t GetNumBarsInTrade(trade_grouping direction = all_trades) const;
589  size_t GetNumBarsProcessed() const;
590  double GetPercentBarsInTrade(trade_grouping direction = all_trades) const;
591  // PL
592  double GetNetProfit(trade_grouping direction = all_trades) const;
593  double GetGrossProfit(trade_grouping direction = all_trades) const;
594  double GetWinnersNet(trade_grouping direction = all_trades) const;
595  double GetLosersNet(trade_grouping direction = all_trades) const;
596  double GetTotalCommission(trade_grouping direction = all_trades) const;
597  double GetTotalSlippage(trade_grouping direction = all_trades) const;
598  double GetTotalTransactionCosts(trade_grouping direction = all_trades) const;
599  double GetProfitFactor(trade_grouping direction = all_trades) const;
600  double GetAveGainToAveLossRatio(trade_grouping direction = all_trades) const;
601  //Position Metrics
602  size_t GetMaxTransactionCount(trade_grouping direction = all_trades) const;
603  double GetMaxPositionSize(trade_grouping direction = all_trades) const;
604  double GetAveTransactionCount(trade_grouping direction = all_trades) const;
605  double GetAvePositionSize(trade_grouping direction = all_trades) const;
606  double GetAveCostsPerTrade(trade_grouping direction = all_trades) const;
607  double GetAveCommissionPerTrade(trade_grouping direction = all_trades) const;
608  double GetAveSlippagePerTrade(trade_grouping direction = all_trades) const;
609  double GetAveSlippagePerUnit(trade_grouping direction = all_trades) const;
610  double GetAveCostsPerUnit(trade_grouping direction = all_trades) const;
611  double GetPercentUnitsWithoutSlippage(trade_grouping direction = all_trades) const;
612  double GetAveNetProfitPerUnit(trade_grouping direction = all_trades) const;
613  double GetAveGrossProfitPerUnit(trade_grouping direction = all_trades) const;
614  size_t GetMaxDrawdownLength(void) const;
615  double GetAveDrawdownLength(void) const;
616  double GetAveDrawdown(void) const;
617  double GetMaxDrawdown(void) const;
618  size_t GetNumDrawdowns(void) const;
619  size_t GetNumTransactions(void) const;
620  size_t GetNumTransactions_AtMarket(void) const;
621  size_t GetNumTransactions_OnStop(void) const;
622  size_t GetNumTransactions_AtLimit(void) const;
623  double GetNumUnitsTraded(trade_grouping direction = all_trades) const;
624  double GetNumUnitsTradedWithoutSlippage(trade_grouping direction = all_trades) const;
625  double GetNumUnitsTraded_AtMarket(trade_grouping direction = all_trades) const;
626  double GetNumUnitsTraded_OnStop(trade_grouping direction = all_trades) const;
627  double GetNumUnitsTraded_AtLimit(trade_grouping direction = all_trades) const;
628  double GetSimulationPeriodInYears(void) const;
629  double GetSimulationPeriodInDays(void) const;
630  double total_instrument_period_in_years(void) const;
631  double total_instrument_period_in_days(void) const;
632  public:
633  void save_report_html(const os::path& file_name) const;
634  //void save_report_html_2(const os::path& file_name) const;
635  };
636 
637 
638 
639 
640 
641 
643 
644 }//tsa
645 
646 #endif
647 #pragma once
static const std::string total_commission
The total &#39;commission&#39; booked for trades. (type: double)
Definition: TSATypeDef.h:332
static const std::string avg_drawdown
The value of the average drawdown in the currency in which the securities are traded. (type: double)
Definition: TSATypeDef.h:362
The average gross trade profit / loss (cash amount).
static const std::string avg_transaction_size
The average transaction size in units. (type: double)
Definition: TSATypeDef.h:382
static const std::string avg_slippage_per_unit
The average slippage &#39;per unit&#39; traded. (type: double)
Definition: TSATypeDef.h:346
static const std::string total_loser_bar_count
The number of bars of all losing trades. (type: int64_t)
Definition: TSATypeDef.h:322
static const std::string avg_drawdown__eod
The value of the average drawdown in the currency in which the securities are traded (end of day data...
Definition: TSATypeDef.h:372
static const std::string num_units_traded
The total number of units traded (not &#39;round trip&#39;). (type: int64_t)
Definition: TSATypeDef.h:387
static const std::string percent_bars_in_trade
The percentage of bars during which the strategy had exposure (position not &#39;flat&#39;). (type: double)
Definition: TSATypeDef.h:327
The average net trade profit / loss (cash amount)
The average transaction size in units.
The total number of winning trades.
Utility class to manipulate file system paths. This class is intended to fulfil the requirements of t...
Definition: TSAOS.h:36
The average &#39;per unit&#39; transaction cost (slippage + commission).
Definition: TSAMetrics.h:456
static const std::string num_units_traded_at_limit
The number of units traded using &#39;limit orders&#39;. (type: int64_t)
Definition: TSATypeDef.h:391
static const std::string max_loser
The size of the largest losing trade (cash amount). (type: double)
Definition: TSATypeDef.h:309
static const std::string total_transaction_cost
The total &#39;transaction cost&#39; for all trades. (type: double)
Definition: TSATypeDef.h:334
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
The percent of units filled without &#39;slippage&#39;, via &#39;limit-orders&#39;.
The value of the current drawdown.
static const std::string max_trade_position_size
The maximum position size of any one trade. (type: int64_t)
Definition: TSATypeDef.h:340
static const std::string avg_trade_position_size
The average &#39;maximum position size&#39; of all trades. (type: double)
Definition: TSATypeDef.h:342
static const std::string percent_profitable
The percent of trades profitable. (type: double)
Definition: TSATypeDef.h:304
Bar Log.
Definition: TSATypeDef.h:428
T get(void) const
Returns the objects data. The object&#39;s type must match the template typename. If not, an exception is thrown.
static const std::string sum_pos_excursion
The sum of all positive (advantageous) trade profits before trade was closed out. (type: double) ...
Definition: TSATypeDef.h:313
The number of bars of all losing trades.
Base class for classes that can produce metrics.
Definition: TSAMetrics.h:44
The size of the largest winning trade (cash amount).
static const std::string sharpe_ratio
The sharpe ratio. (type: double)
Definition: TSATypeDef.h:400
The maximum number of consecutive losing trades.
static const std::string total_num_bars_processed
The total number of bars processed by the strategy. (type: int64_t)
Definition: TSATypeDef.h:326
The standard deviation of trade net-profit/loss.
static const std::string sortino_ratio
The sortino ratio. (type: double)
Definition: TSATypeDef.h:401
The ratio of &#39;total winners net&#39; / &#39;total losers net&#39;.
The strategy &#39;gross profit&#39; (before slippage + commission).
perf_metric_type
Values that represent performance metric types.
Definition: TSATypeDef.h:216
static const std::string num_units_traded_at_market
The number of units traded using &#39;market orders&#39;. (type: int64_t)
Definition: TSATypeDef.h:389
The average negative (adverse) trade profit before trade was closed out.
The average slippage &#39;per unit&#39; traded.
static const std::string gross_profit
The strategy &#39;gross profit&#39; (before slippage + commission). (type: double)
Definition: TSATypeDef.h:329
static const std::string winner_count
The total number of winning trades. (type: int64_t)
Definition: TSATypeDef.h:303
The average cost (slippage + commission) per trade.
static const std::string avg_loser_bar_count
The length in bars of the average losing trade. (type: double)
Definition: TSATypeDef.h:323
static const std::string max_neg_excursion
The largest negative (adverse) trade profit before trade was closed out. (type: double) ...
Definition: TSATypeDef.h:316
static const std::string avg_winner
The average net profit (cash amount) of winning trades. (type: double)
Definition: TSATypeDef.h:307
The average gross profit/loss per unit.
The total number of bars on which the strategy had exposure (position not &#39;flat&#39;).
The total number of transactions.
The number of bars of all winning trades.
Base class for classes that can produce metrics.
Definition: TSAMetrics.h:75
static const std::string winners_net
The net profit generated by winning trades. (type: double)
Definition: TSATypeDef.h:330
static const std::string max_closed_out_runup
The highest cummulative gain of sequential winning trades. (type: double)
Definition: TSATypeDef.h:337
structure defining all the metric names. For example, you can pass metric::trade_count as metric_name...
Definition: TSATypeDef.h:297
static const std::string avg_net_profit_per_unit
The average net profit/loss per unit. (type: double)
Definition: TSATypeDef.h:349
static const std::string avg_gain_to_avg_loss_ratio
The ratio of the &#39;average winning trade&#39; / &#39;average losing trade&#39;. (type: double) ...
Definition: TSATypeDef.h:336
static const std::string avg_trade_commission
The average commission per trade. (type: double)
Definition: TSATypeDef.h:343
static const std::string max_account_balance
Not in use.
Definition: TSATypeDef.h:363
static const std::string current_drawdown
The value of the current drawdown. (type: double)
Definition: TSATypeDef.h:357
static const std::string avg_pos_excursion
The average positive (advantageous) trade profit before trade was closed out. (type: double) ...
Definition: TSATypeDef.h:317
Strategy performance metrics.
Definition: TSAMetrics.h:367
static const std::string avg_trade_bar_count
The average trade length in bars. (type: double)
Definition: TSATypeDef.h:324
static const std::string avg_trade_slippage
The average slippage per trade. (type: double)
Definition: TSATypeDef.h:344
static const std::string avg_net_trade_pl
The average net trade profit / loss (cash amount). (type: double)
Definition: TSATypeDef.h:305
The average net profit (cash amount) of winning trades.
The percent of trades profitable.
The length in bars of the average losing trade.
static const std::string stddev_trade_pl
The standard deviation of trade net-profit/loss. (type: double)
Definition: TSATypeDef.h:319
static const std::string avg_trade_cost
The average cost (slippage + commission) per trade. (type: double)
Definition: TSATypeDef.h:345
The average &#39;maximum position size&#39; of all trades.
The net loss generated by losing trades.
static const std::string total_slippage
The total &#39;slippage&#39; booked for trades. Trades resulting from &#39;limit orders&#39; have no slippage...
Definition: TSATypeDef.h:333
Definition: TSAVariant.h:656
The number of units traded using &#39;limit orders&#39;.
static const std::string max_closed_out_rundown
The highest cummulative loss of sequential losing trades. (type: double)
Definition: TSATypeDef.h:338
The ratio of the &#39;average winning trade&#39; / &#39;average losing trade&#39;.
variant objects can represent values of different types.
Definition: TSAVariant.h:140
static const std::string percent_units_without_slippage
The percent of units filled without &#39;slippage&#39;; via &#39;limit-orders&#39;. (type: double) ...
Definition: TSATypeDef.h:348
Represents a duration - the difference between two date_time values.
Definition: TSATime.h:945
static const std::string sum_neg_excursion
The sum of all negative (adverse) trade profit before trade was closed out. (type: double) ...
Definition: TSATypeDef.h:314
static const std::string net_profit
The strategy &#39;net profit&#39; (after slippage + commission). (type: double)
Definition: TSATypeDef.h:328
static const std::string avg_trade_transaction_count
The average number of transactions per trade. (type: double)
Definition: TSATypeDef.h:341
static const std::string min_account_balance
Not in use.
Definition: TSATypeDef.h:364
Parent class for many library classes.
Definition: TSATypeDef.h:462
The value of the largest drawdown.
static const std::string max_drawdown_length
The length in bars of the longest drawdown. (type: int64_t)
Definition: TSATypeDef.h:360
The maximum number of transactions for any one trade.
sref< bool > operator<=(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:91
A date of the Gregorian calendar.
Definition: TSATime.h:119
static const std::string avg_neg_excursion
The average negative (adverse) trade profit before trade was closed out. (type: double) ...
Definition: TSATypeDef.h:318
static const std::string num_transactions
The total number of transactions. (type: int64_t)
Definition: TSATypeDef.h:378
The average drawdown length in bars.
static const std::string num_units_traded_without_slippage
The number of units traded without slippage. (Limit orders do not have slippage). (type: int64_t) ...
Definition: TSATypeDef.h:388
static const std::string max_trade_transaction_count
The maximum number of transactions for any one trade. (type: int64_t)
Definition: TSATypeDef.h:339
static const std::string max_consec_losers
The maximum number of consecutive losing trades. (type: int64_t)
Definition: TSATypeDef.h:312
sref< bool > operator==(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:130
static const std::string num_drawdowns__eod
The total number of drawdowns (end of day data). (type: int64_t)
Definition: TSATypeDef.h:373
The maximum number of consecutive winning trades.
The size of the largest losing trade (cash amount).
The total number of transactions resulting from &#39;stop orders&#39;.
The highest cummulative loss of sequential losing trades.
The total number of trades.
The total number of units traded (not &#39;round trip&#39;).
The value of the average drawdown in the currency in which the securities are traded.
static const std::string max_drawdown_length__eod
The length in bars of the longest drawdown (end of day data). (type: int64_t)
Definition: TSATypeDef.h:370
static const std::string max_drawdown
The value of the largest drawdown. (type: double)
Definition: TSATypeDef.h:359
The average trade length in bars.
The number of units traded without slippage. (Limit orders do not have slippage). ...
static const std::string loser_count
The total number of losing trades. (type: int64_t)
Definition: TSATypeDef.h:302
The average slippage per trade.
The highest cummulative gain of sequential winning trades.
Short trades only.
Definition: TSATypeDef.h:210
static const std::string profit_factor
The ratio of &#39;total winners net&#39; / &#39;total losers net&#39;. (type: double)
Definition: TSATypeDef.h:335
The average net profit (cash amount) of losing trades.
The number of units traded using &#39;stop orders&#39;.
trade_grouping
Trade aggregation types used by reporting functionality.
Definition: TSATypeDef.h:208
The net profit generated by winning trades.
The total number of drawdowns.
The total &#39;slippage&#39; booked for trades. Trades resulting from &#39;limit orders&#39; have no slippage...
static const std::string trade_count
The total number of trades. (type: int64_t)
Definition: TSATypeDef.h:301
static const std::string max_pos_excursion
The largest positive (advantageous) trade profit before trade was closed out. (type: double) ...
Definition: TSATypeDef.h:315
The strategy &#39;net profit&#39; (after slippage + commission).
All trades.
Definition: TSATypeDef.h:211
Long trades only.
Definition: TSATypeDef.h:209
The average positive (advantageous) trade profit before trade was closed out.
static const std::string losers_net
The net loss generated by losing trades. (type: double)
Definition: TSATypeDef.h:331
static const std::string num_transactions_at_limit
The total number of transactions resulting from &#39;limit orders&#39;. (type: int64_t)
Definition: TSATypeDef.h:381
static const std::string max_winner
The size of the largest winning trade (cash amount). (type: double)
Definition: TSATypeDef.h:310
The total number of transactions resulting from &#39;limit orders&#39;.
A class representing JSON objects.
Definition: TSAJSON.h:81
static const std::string total_winner_bar_count
The number of bars of all winning trades. (type: int64_t)
Definition: TSATypeDef.h:321
static const std::string avg_gross_profit_per_unit
The average gross profit/loss per unit. (type: double)
Definition: TSATypeDef.h:350
static const std::string num_drawdowns
The total number of drawdowns. (type: int64_t)
Definition: TSATypeDef.h:358
The total &#39;commission&#39; booked for trades.
Class strategy represents a trading or investment strategy.
Definition: TSAStrategy.h:108
static const std::string max_consec_winners
The maximum number of consecutive winning trades. (type: int64_t)
Definition: TSATypeDef.h:311
static const std::string max_drawdown__eod
The value of the largest drawdown (end of day data). (type: double)
Definition: TSATypeDef.h:369
static const std::string avg_winner_bar_count
The length in bars of the average winning trade. (type: double)
Definition: TSATypeDef.h:320
The length in bars of the longest drawdown.
static const std::string num_transactions_at_market
The total number of transactions resulting from &#39;market orders&#39;. (type: int64_t)
Definition: TSATypeDef.h:379
static const std::string avg_cost_per_unit
The average &#39;per unit&#39; transaction cost (slippage + commission). (type: double)
Definition: TSATypeDef.h:347
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
The total number of transactions resulting from &#39;market orders&#39;.
The average number of transactions per trade.
The length in bars of the average winning trade.
static const std::string num_bars_in_trade
The total number of bars on which the strategy had exposure (position not &#39;flat&#39;). (type: int64_t)
Definition: TSATypeDef.h:325
The percentage of bars during which the strategy had exposure (position not &#39;flat&#39;).
static const std::string avg_gross_trade_pl
The average gross trade profit / loss (cash amount). (type: double)
Definition: TSATypeDef.h:306
sref< bool > operator<(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:78
The average commission per trade.
The largest positive (advantageous) trade profit before trade was closed out.
static const std::string avg_loser
The average net profit (cash amount) of losing trades. (type: double)
Definition: TSATypeDef.h:308
The largest negative (adverse) trade profit before trade was closed out.
The total number of bars processed by the strategy.
static const std::string avg_drawdown_length
The average drawdown length in bars. (type: double)
Definition: TSATypeDef.h:361
The total number of losing trades.
The average net profit/loss per unit.
The maximum position size of any one trade.
Class mem_table represents a memory based table. mem_table objects can be used in strategies both for...
Definition: TSAMemTable.h:48
static const std::string avg_drawdown_length__eod
The average drawdown length in bars (end of day data). (type: double)
Definition: TSATypeDef.h:371
The number of units traded using &#39;market orders&#39;.
The total &#39;transaction cost&#39; for all trades.
static const std::string num_transactions_on_stop
The total number of transactions resulting from &#39;stop orders&#39;. (type: int64_t)
Definition: TSATypeDef.h:380
static const std::string num_units_traded_on_stop
The number of units traded using &#39;stop orders&#39;. (type: int64_t)
Definition: TSATypeDef.h:390