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:
- portfolio (Portfolio) – 
- decision_cycle_frequency (DateOffset) – 
 
- Return type:
- None 
 
 - Methods - __init__(portfolio[, decision_cycle_frequency])- Calculate some statistics how our trades went. - Calculate some statistics how our long trades went. - Calculate some statistics how our short trades went. - calculate_summary_statistics([time_bucket, ...])- Calculate some statistics how our trades went. - Calculate some statistics how our trades went. - calculate_time_in_market(state, sorted_positions)- Calculate profit % of all positions, weighted by position size. - Create a timeline feed how we traded over a course of time. - 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 the opened_at timestamp of the first position in the portfolio. - Get the closed_at timestamp of the last position in the portfolio. - Return long positions over all traded assets. - Return open positions over all traded assets. - Return short positions over all traded assets. - Render summary statistics as a JSON table. - Attributes - The portfolio we analysed - All taken positions sorted by the position id, or when they were opened - Decision cycle frequency is needed to calculate some performance metrics like sharpe, etc. - 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:
 
 - get_open_positions()[source]#
- Return open positions over all traded assets. - Positions are sorted by position_id. - Return type:
 
 - get_short_positions()[source]#
- Return short positions over all traded assets. - Positions are sorted by position_id. - Return type:
 
 - get_long_positions()[source]#
- Return long positions over all traded assets. - Positions are sorted by position_id. - Return type:
 
 - 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:
 
 - calculate_short_summary_statistics(time_bucket, state)[source]#
- Calculate some statistics how our short trades went. - Parameters:
- time_bucket (TimeBucket) – Optional, used to display average duration as ‘number of bars’ instead of ‘number of days’. 
- state (State) – Optional, should be specified if user would like to see advanced statistics 
 
- Returns:
- TradeSummary instance 
- Return type:
 
 - calculate_long_summary_statistics(time_bucket, state)[source]#
- Calculate some statistics how our long trades went. - Parameters:
- time_bucket (TimeBucket) – Optional, used to display average duration as ‘number of bars’ instead of ‘number of days’. 
- state (State) – Optional, should be specified if user would like to see advanced statistics 
 
- Returns:
- TradeSummary instance 
- Return type:
 
 - 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:
 
 - 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[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 - HTMLobject 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:
- portfolio (Portfolio) – 
- decision_cycle_frequency (DateOffset) – 
 
- Return type:
- None