Statistics#
API documentation for tradeexecutor.state.statistics.Statistics Python class in Trading Strategy framework.
- class Statistics[source]#
Bases:
object
Statistics for a trade execution state.
We calculate various statistics on the server-side and make them part of the state, so that JS clients can easily display this information.
Statistics are collected over time and more often than trading ticks. We store historical statistics for each position as the part of the state.
- __init__(portfolio=<factory>, positions=<factory>, closed_positions=<factory>, long_short_metrics_latest=None)#
- Parameters:
portfolio (List[PortfolioStatistics]) –
positions (Dict[int, List[PositionStatistics]]) –
closed_positions (Dict[int, FinalPositionStatistics]) –
- Return type:
None
Methods
__init__
([portfolio, positions, ...])add_positions_stats
(position_id, p_stats)Add a new sample to position stats.
from_dict
(kvs, *[, infer_missing])from_json
(s, *[, parse_float, parse_int, ...])get_earliest_portfolio_stats
()Get the time series of portfolio equity.
get_latest_portfolio_stats
()get_latest_position_stats
(position_id)Get the naive rolling PnL percentage.
get_portfolio_statistics_dataframe
(attr_name)Get any of position statistcs value as a columnar data.
get_position_statistics_as_dataframe
(position_id)Convert position statistics history to a Pandas dataframe.
schema
(*[, infer_missing, only, exclude, ...])to_dict
([encode_json])to_json
(*[, skipkeys, ensure_ascii, ...])Attributes
Latest long short metrics
Per portfolio statistics.
Per position statistics.
Per position statistics for closed positions.
- portfolio: List[PortfolioStatistics]#
Per portfolio statistics.
Contains list of statistics for the portfolio over time. The first timestamp is the first entry in the list. Note that now we have only one portfolio per state.
This is calculated in
tradeexecutor.statistics.core.calculate_statistics()
.
- positions: Dict[int, List[PositionStatistics]]#
Per position statistics. We look them up by position id. Each position contains list of statistics for the position over time. The first timestamp is the first entry in the list.
- closed_positions: Dict[int, FinalPositionStatistics]#
Per position statistics for closed positions.
- get_equity_series()[source]#
Get the time series of portfolio equity.
- Returns:
Pandas Series with timestamps as index and equity as values.
- Return type:
Series
- add_positions_stats(position_id, p_stats)[source]#
Add a new sample to position stats.
We cannot use defaultdict() here because we lose defaultdict instance on state serialization.
- Parameters:
position_id (int) –
p_stats (PositionStatistics) –
- get_portfolio_statistics_dataframe(attr_name, resampling_time='D', resampling_method='max')[source]#
Get any of position statistcs value as a columnar data.
Get the daily performance of the portfolio.
Example:
# Create time series of portfolio "total_equity" over its lifetime s = stats.get_portfolio_statistics_dataframe("total_equity")
- Parameters:
attr_name (str) – Which variable we are interested in. E.g. total_equity.
resampling_time (str) – See http://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#resampling
resamping_method – See http://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#resampling
resampling_method (str) –
- Returns:
DataFrame for the value with time as index.
- Return type:
Series
- get_position_statistics_as_dataframe(position_id)[source]#
Convert position statistics history to a Pandas dataframe.
- Returns:
DataFrame object with DateTimeIndex
- Parameters:
position_id (int) –
- Return type:
DataFrame
- get_naive_rolling_pnl_pct()[source]#
Get the naive rolling PnL percentage.
Used to display the PnL on the backtest progress bar.
- Returns:
Profitability -1…inf
- Return type:
- __init__(portfolio=<factory>, positions=<factory>, closed_positions=<factory>, long_short_metrics_latest=None)#
- Parameters:
portfolio (List[PortfolioStatistics]) –
positions (Dict[int, List[PositionStatistics]]) –
closed_positions (Dict[int, FinalPositionStatistics]) –
- Return type:
None