def test_field_bucketing(): """Tests field bucketing by a label """ results_summary = ResultsSummary() df = RandomiseTimeSeries().create_random_time_series(max_points=1000, freq='minute', start='01 Jan 2018', end="01 Jun 2018") df['Col'] = 'something' df_fields = results_summary.field_bucketing(df, metric_name='price', aggregation_metric='sum', aggregate_by_field='Col') assert df_fields.values[0] - df_fields['Col'].sum() < eps # Overwrite first point df['Col'][0] = 'something-else' df_fields = results_summary.field_bucketing(df, metric_name='price', aggregation_metric='mean', aggregate_by_field='Col') # Check the averages match assert df_fields['Col']['something-else'] - df['price'][0] < eps assert df_fields['Col']['something'] - df['price'][1:].mean() < eps
def venue_tca_aggregated_example(): """Example of doing an aggregated TCA computation on a single ticker, and then later calculating the probability distribution function of slippage split by venue (when weighted by executed notional) """ tca_engine = TCAEngineImpl(version=tca_version) tca_request = TCARequest(start_date=start_date, finish_date=finish_date, ticker=ticker, tca_type='aggregated', trade_data_store=trade_data_store, market_data_store=market_data_store, metric_calcs=MetricSlippage()) dict_of_df = tca_engine.calculate_tca(tca_request) summary = ResultsSummary() summary_slippage_df = summary.field_distribution( dict_of_df['trade_df'], metric_name='slippage', aggregate_by_field='venue', pdf_only=True, weighting_field='executed_notional') # Plot PDF of slippage, split up by venue Chart(engine='plotly').plot(summary_slippage_df, style=Style(plotly_plot_mode='offline_html', connect_line_gaps=True))
def test_histogram_generator(): """Test histogram generator for results - in particular for instances where we have only 1 point """ results_summary = ResultsSummary() df = RandomiseTimeSeries().create_random_time_series(max_points=1000, freq='minute', start='01 Jan 2018', end="01 Jun 2018") df_hist, df_pdf = results_summary._create_histogram_distribution(df) assert df_pdf is not None df['Col'] = 'something' df['Col'][0] = 'something-else' df_hist_pdf = results_summary.field_distribution(df, aggregate_by_field='Col', metric_name='price') # Should only have output for 'something' because 'something-else' only has one point, so can't construct distribution from it assert len(df_hist_pdf.columns) == 3 df = RandomiseTimeSeries().create_random_time_series(max_points=1, freq='minute', start='01 Jan 2018', end="01 Jun 2018") df_hist, df_pdf = results_summary._create_histogram_distribution(df) # Should have empty plots because only 1 point from these assert df_hist.empty and df_pdf.empty
def multiple_ticker_tca_aggregated_example(): """Example of how to do TCa analysis on multiple tickers """ tca_engine = TCAEngineImpl(version=tca_version) # Run a TCA computation for multiple tickers, calculating slippage tca_request = TCARequest(start_date=start_date, finish_date=finish_date, ticker=mult_ticker, tca_type='aggregated', trade_data_store=trade_data_store, market_data_store=market_data_store, metric_calcs=MetricSlippage(), reporting_currency='EUR') dict_of_df = tca_engine.calculate_tca(tca_request) trade_df = dict_of_df['trade_df'] # Aggregate some of the results with the ResultsSummary class (we could have done this within the TCARequest) summary = ResultsSummary() # Bucket slippage by ticker and report the average summary_slippage_df = summary.field_bucketing(trade_df, aggregate_by_field='ticker') print(summary_slippage_df) # Bucket slippage by ticker & return the average as weighted by the executed notional in reporting currency # (in this case EUR) summary_slippage_df = summary.field_bucketing(trade_df, aggregate_by_field='venue', weighting_field='executed_notional_in_reporting_currency') print(summary_slippage_df) # Bucket slippage by ticker and report the average summary_slippage_df = summary.field_bucketing(trade_df, aggregate_by_field='venue') print(summary_slippage_df)
def __init__(self, trade_order_list=None, metric_name=None, filter_by=['all'], tag_value_combinations={}, keep_fields=['executed_notional', 'side'], replace_text={}, round_figures_by=1, scalar=1.0, weighting_field=constants.table_weighting_field, exclude_fields_from_avg=[]): self._trade_order_list = trade_order_list self._metric_name = metric_name self._results_summary = ResultsSummary() self._keep_fields = keep_fields self._filter_by = filter_by self._replace_text = replace_text self._round_figures_by = round_figures_by self._weighting_field = weighting_field self._scalar = scalar self._exclude_fields_from_avg = exclude_fields_from_avg self._tag_value_combinations = tag_value_combinations self._trade_order_filter_tag = TradeOrderFilterTag() self._results_form_tag = 'table' self._util_func = UtilFunc() self._time_series_ops = TimeSeriesOps()
def __init__(self, trade_order_list=None, metric_name=None, aggregate_by_field=None, aggregation_metric='mean', tag_value_combinations={}): if not(isinstance(trade_order_list, list)) and trade_order_list is not None: trade_order_list = [trade_order_list] self._trade_order_list = trade_order_list self._metric_name = metric_name self._aggregate_by_field = aggregate_by_field self._aggregation_metric = aggregation_metric self._results_summary = ResultsSummary() self._tag_value_combinations = tag_value_combinations self._trade_order_filter_tag = TradeOrderFilterTag() self._util_func = Mediator.get_util_func() self._time_series_ops = Mediator.get_time_series_ops()
def __init__(self, trade_order_list=None, metric_name=None, aggregate_by_field=None, aggregation_metric='mean', tag_value_combinations={}): self._trade_order_list = trade_order_list self._metric_name = metric_name self._aggregate_by_field = aggregate_by_field self._aggregation_metric = aggregation_metric self._results_summary = ResultsSummary() self._tag_value_combinations = tag_value_combinations self._trade_order_filter_tag = TradeOrderFilterTag() self._util_func = UtilFunc() self._time_series_ops = TimeSeriesOps()
# # See the License for the specific language governing permissions and limitations under the License. # from tcapy.conf.constants import Constants constants = Constants() if __name__ == '__main__': ### Fetch all the trades from SQL Server (irrespective of ticker) and every event-type from tcapy.data.datafactory import DataFactory from tcapy.analysis.tcarequest import TradeRequest from tcapy.analysis.algos.resultssummary import ResultsSummary data_factory = DataFactory() results_summary = ResultsSummary() start_date = '01 Mar 2018' finish_date = '01 Apr 2018' trade_order_type_list = ['trade_df'] query_fields = ['ticker', 'broker_id'] for t in trade_order_type_list: trade_request = TradeRequest(start_date=start_date, finish_date=finish_date, data_store='ms_sql_server', trade_order_type=t) trade_order_df = data_factory.fetch_table(trade_request) query_dict = results_summary.query_trade_order_population(