15 #ifndef TSA_METRICS__INCLUDED 16 #define TSA_METRICS__INCLUDED 21 #include "TSATypeDef.h" 22 #include "TSAMemTable.h" 48 virtual void daily_cash_flow(
mem_table& mt)
const = 0;
49 virtual bool is_finalized(
void)
const = 0;
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;
78 std::map<std::string, variant> m_map;
87 bool has_metric(
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>();
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);
104 size_t size(
void)
const;
105 void print(std::ostream&)
const;
108 void from_json(
const json&);
112 template <
class Archive>
113 void serialize(Archive & ar) {
117 std::ostream& write(std::ostream&)
const;
118 std::istream& read(std::istream&);
201 int64_t num_units_traded_other(
void)
const {
return get_auto_cast(metric::num_units_traded_other); }
203 double drawdown_multiplier(
void)
const {
return get_auto_cast(metric::drawdown_multiplier); }
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); }
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); }
226 struct basic_trade_info {
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; }
237 template<
class Archive>
238 void save(Archive & ar)
const {
239 ar.saveBinary((
const char*)(
this),
sizeof(basic_trade_info));
241 template<
class Archive>
242 void load(Archive & ar) {
243 ar.loadBinary((
char*)(
this),
sizeof(basic_trade_info));
259 class tiny_trade_metrics :
public object {
260 std::vector<basic_trade_info> m_trades;
263 tiny_trade_metrics(
void);
264 void populate_trades(
const std::vector<basic_trade_info>&);
265 void operator=(
const tiny_trade_metrics&);
267 void merge(
const tiny_trade_metrics&);
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;
278 int64_t sum_winner_bar_count = 0;
279 int64_t sum_loser_bar_count = 0;
282 double max_closed_out_runnup = 0.0;
284 std::vector<double> profits_arr;
289 template <
class Archive>
290 void serialize(Archive & ar) {
303 class daily_equity_container
306 struct daily_record {
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);
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;
321 void set_daily_cash_flow_series(
mem_table& _daily_cash_flow_series);
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;
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;
336 template <
class Archive>
337 void serialize(Archive & ar) {
339 ar(m_daily_cash_flows);
370 static unsigned int STREAM_DELIMITER;
372 bool m_is_instrument;
374 size_t m_num_instruments;
375 size_t m_num_strategies;
381 double avg_net_trade_PL = 0.0;
382 double avg_gross_trade_PL = 0.0;
387 double stdev_trade_PL = 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;
420 void copy(
const sub_metrics&);
421 bool is_equal(
const sub_metrics&);
423 void write(out_storage_stream&)
const;
424 void read(in_storage_stream&);
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);
434 double avg_drawdown_lengths = 0.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;
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;
453 std::map<date, double> m_daily_cash_flows;
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;
472 double daily_risk_free_return(
void)
const;
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;
484 void merge_daily_cash_flows(
const std::map<date, double>&);
486 void calculate_drawdown_metrics(
void);
487 void create_cumulative_equity_series(
mem_table&);
488 double calculate_cumulative_cash_flow(
void)
const;
515 void source_is_instrument(
bool _b);
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;
523 void write(out_storage_stream&)
const;
524 void read(in_storage_stream&);
528 void merge(
const metrics& other);
532 void set_strategy_count(
size_t);
533 void set_instrument_count(
size_t);
538 void print_equity_graph(std::ostream&)
const;
546 virtual void daily_cash_flow(
mem_table& mt)
const;
548 virtual const date_time& GetStartDateTime(
void)
const;
553 size_t num_strategies(
void)
const;
554 size_t num_instruments(
void)
const;
572 return get_metric(metric_type, trade_aggr_type).
get<T>();
589 size_t GetNumBarsProcessed()
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;
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;
633 void save_report_html(
const os::path& file_name)
const;
static const std::string total_commission
The total 'commission' 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 'per unit' 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 'round trip'). (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 'flat'). (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 'per unit' transaction cost (slippage + commission).
Definition: TSAMetrics.h:456
static const std::string num_units_traded_at_limit
The number of units traded using 'limit orders'. (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 'transaction cost' for all trades. (type: double)
Definition: TSATypeDef.h:334
Namespace for the 'Trading System API' library.
Definition: original1.TSA3Core.cpp:20
The percent of units filled without 'slippage', via 'limit-orders'.
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 'maximum position size' 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'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 'total winners net' / 'total losers net'.
The strategy 'gross profit' (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 'market orders'. (type: int64_t)
Definition: TSATypeDef.h:389
The average negative (adverse) trade profit before trade was closed out.
The average slippage 'per unit' traded.
static const std::string gross_profit
The strategy 'gross profit' (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 'flat').
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 'average winning trade' / 'average losing trade'. (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 'maximum position size' of all trades.
The net loss generated by losing trades.
static const std::string total_slippage
The total 'slippage' booked for trades. Trades resulting from 'limit orders' have no slippage...
Definition: TSATypeDef.h:333
Definition: TSAVariant.h:656
The number of units traded using 'limit orders'.
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 'average winning trade' / 'average losing trade'.
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 'slippage'; via 'limit-orders'. (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 'net profit' (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 'stop orders'.
The highest cummulative loss of sequential losing trades.
The total number of trades.
The total number of units traded (not 'round trip').
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 'total winners net' / 'total losers net'. (type: double)
Definition: TSATypeDef.h:335
The average net profit (cash amount) of losing trades.
The number of units traded using 'stop orders'.
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 'slippage' booked for trades. Trades resulting from 'limit orders' 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 'net profit' (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 'limit orders'. (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 'limit orders'.
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 'commission' 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 'market orders'. (type: int64_t)
Definition: TSATypeDef.h:379
static const std::string avg_cost_per_unit
The average 'per unit' 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 'market orders'.
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 'flat'). (type: int64_t)
Definition: TSATypeDef.h:325
The percentage of bars during which the strategy had exposure (position not 'flat').
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 'market orders'.
The total 'transaction cost' for all trades.
static const std::string num_transactions_on_stop
The total number of transactions resulting from 'stop orders'. (type: int64_t)
Definition: TSATypeDef.h:380
static const std::string num_units_traded_on_stop
The number of units traded using 'stop orders'. (type: int64_t)
Definition: TSATypeDef.h:390