TradeAnalysis#

API documentation for tradeexecutor.analysis.trade_analyser.TradeAnalysis Python class in Trading Strategy framework.

class TradeAnalysis[source]#

Bases: object

Analysis of trades in a portfolio.

__init__(portfolio, decision_cycle_frequency=<factory>)#
Parameters:
Return type:

None

Methods

__init__(portfolio[, decision_cycle_frequency])

calculate_all_summary_stats_by_side([...])

Calculate some statistics how our trades went.

calculate_long_summary_statistics(...)

Calculate some statistics how our long trades went.

calculate_short_summary_statistics(...)

Calculate some statistics how our short trades went.

calculate_summary_statistics([time_bucket, ...])

Calculate some statistics how our trades went.

calculate_summary_statistics_for_positions(...)

Calculate some statistics how our trades went.

calculate_weighted_average_realised_profit(...)

Calculate profit % of all positions, weighted by position size.

create_timeline()

Create a timeline feed how we traded over a course of time.

get_all_positions()

Return open and closed positions over all traded assets.

get_capital_tied_at_open(position)

Calculate how much capital % was allocated to this position when it was opened.

get_first_opened_at()

Get the opened_at timestamp of the first position in the portfolio.

get_last_closed_at()

Get the closed_at timestamp of the last position in the portfolio.

get_long_positions()

Return long positions over all traded assets.

get_open_positions()

Return open positions over all traded assets.

get_short_positions()

Return short positions over all traded assets.

render_summary_statistics_side_by_side([...])

Render summary statistics as a JSON table.

Attributes

portfolio

The portfolio we analysed

filtered_sorted_positions

All taken positions sorted by the position id, or when they were opened

decision_cycle_frequency

Decision cycle frequency is needed to calculate some performance metrics like sharpe, etc.

portfolio: Portfolio#

The portfolio we analysed

filtered_sorted_positions: list[tradeexecutor.state.position.TradingPosition]#

All taken positions sorted by the position id, or when they were opened

decision_cycle_frequency: DateOffset#

Decision cycle frequency is needed to calculate some performance metrics like sharpe, etc.

If not given assume daily for the legacy compatibilty

get_first_opened_at()[source]#

Get the opened_at timestamp of the first position in the portfolio.

Return type:

Optional[Timestamp]

get_last_closed_at()[source]#

Get the closed_at timestamp of the last position in the portfolio.

Return type:

Optional[Timestamp]

get_all_positions()[source]#

Return open and closed positions over all traded assets.

Positions are sorted by position_id.

Return type:

Iterable[Tuple[int, TradingPosition]]

get_open_positions()[source]#

Return open positions over all traded assets.

Positions are sorted by position_id.

Return type:

Iterable[Tuple[int, TradingPosition]]

get_short_positions()[source]#

Return short positions over all traded assets.

Positions are sorted by position_id.

Return type:

Iterable[Tuple[int, TradingPosition]]

get_long_positions()[source]#

Return long positions over all traded assets.

Positions are sorted by position_id.

Return type:

Iterable[Tuple[int, TradingPosition]]

calculate_summary_statistics(time_bucket=None, state=None)[source]#

Calculate some statistics how our trades went. This is just for overall statistics. For an analysis by overall, long, and short trades, use calculate_all_summary_stats_by_side()

Parameters:
  • time_bucket (Optional[TimeBucket]) – Optional, used to display average duration as ‘number of bars’ instead of ‘number of days’.

  • state – Optional, should be specified if user would like to see advanced statistics

Returns:

TradeSummary instance

Return type:

TradeSummary

calculate_short_summary_statistics(time_bucket, state)[source]#

Calculate some statistics how our short trades went.

Parameters:
  • time_bucket – Optional, used to display average duration as ‘number of bars’ instead of ‘number of days’.

  • state – Optional, should be specified if user would like to see advanced statistics

Returns:

TradeSummary instance

Return type:

TradeSummary

calculate_long_summary_statistics(time_bucket, state)[source]#

Calculate some statistics how our long trades went.

Parameters:
  • time_bucket – Optional, used to display average duration as ‘number of bars’ instead of ‘number of days’.

  • state – Optional, should be specified if user would like to see advanced statistics

Returns:

TradeSummary instance

Return type:

TradeSummary

calculate_summary_statistics_for_positions(time_bucket, state, positions)[source]#

Calculate some statistics how our trades went.

Parameters:
  • time_bucket (Optional[TimeBucket]) – Optional, used to display average duration as ‘number of bars’ instead of ‘number of days’.

  • state – Optional, should be specified if user would like to see advanced statistics

  • positions (Iterable[Tuple[int, TradingPosition]]) –

Returns:

TradeSummary instance

Return type:

TradeSummary

static calculate_weighted_average_realised_profit(positions)[source]#

Calculate profit % of all positions, weighted by position size.

Parameters:

positions (Iterable[Tuple[int, TradingPosition]]) – Iterable of position ids

Returns:

Profit % weighted by position size

calculate_all_summary_stats_by_side(time_bucket=None, state=None, urls=False)[source]#

Calculate some statistics how our trades went. This returns a DataFrame with 3 separate columns for overall, long and short.

For just a single column table for overall statistics, use :py:meth:calculate_summary_statistics() instead.

Parameters:
  • time_bucket (Optional[TimeBucket]) – Optional, used to display average duration as ‘number of bars’ in addition to ‘number of days’.

  • state – Optional, should be specified if user would like to see advanced statistics such as sharpe ratio, sortino ratio, etc.

  • urls – Optional, if True, include an extra column for the urls for each row that link to the relevant glorssary documentation.

Returns:

DataFrame with all the stats for overall, long and short.

Return type:

DataFrame

render_summary_statistics_side_by_side(time_bucket=None, state=None)[source]#

Render summary statistics as a JSON table.

Parameters:
  • time_bucket (Optional[TimeBucket]) – Optional, used to display average duration as ‘number of bars’ in addition to ‘number of days’.

  • state – Optional, should be specified if user would like to see advanced statistics such as sharpe ratio, sortino ratio, etc.

Returns:

Returns a similar table to calcualate_all_summary_stats_by_side, but make it makes row headings clickable, directing user to relevant glossary link and, in this case, returns HTML object instaed of a pandas DataFrame.

Return type:

HTML

static get_capital_tied_at_open(position)[source]#

Calculate how much capital % was allocated to this position when it was opened.

Return type:

float | None

create_timeline()[source]#

Create a timeline feed how we traded over a course of time.

Note: We assume each position has only one enter and exit event, not position increases over the lifetime.

Returns:

DataFrame with timestamp and timeline_event columns

Return type:

DataFrame

__init__(portfolio, decision_cycle_frequency=<factory>)#
Parameters:
Return type:

None