visualise_single_position#

API documentation for tradeexecutor.visual.single_pair.visualise_single_position Python function.

visualise_single_position(state, strategy_universe, position, area_around=100, execution_context=<ExecutionContext backtesting, unspecified engine version>)[source]#

Inspect individual won or lost trade.

  • Give you an overview what failed in the trade

  • We draw the technical indicators if the source data has them (input.visualisation == True).

Example:

from tradeexecutor.utils.list import get_linearly_sampled_items
from tradeexecutor.visual.single_pair import visualise_single_position
from tradeexecutor.utils.profile import profiled

# We are always using interactive charting mode for single position visualisation
setup_charting_and_output(OutputMode.interactive)

state = best_pick.hydrate_state()
portfolio = state.portfolio

pair = strategy_universe.get_pair_by_human_description(trading_pairs[0])  # We examine positions for this trading pair only
examinaned_position_count = 3  # We are linearly sampling this many failing trades for visualisation
area_around_candels = 40  # How many candles before and after entry and exit

all_positions = list(portfolio.get_all_positions())
positions_lost = [p for p in all_positions if p.is_loss()]

print(f"Total lost positions for {pair.get_ticker()} is {len(positions_lost)} / {len(all_positions)}. We are visualising {examinaned_position_count} of these.")
# Take 3 positions equally weighted from the time line

examined_positions = get_linearly_sampled_items(positions_lost, count=examinaned_position_count)

for position in examined_positions:
    position_summary = pd.Series(position.get_human_summary())
    display(position_summary)
    fig = visualise_single_position(
        state=state,
        strategy_universe=strategy_universe,
        position=position,
        area_around=area_around_candels,
    )
    display(fig)
Parameters:
Return type:

Figure