TradePosition#

tradeexecutor.analysis.trade_analyser.TradePosition Python class in Trading Strategy framework.

class TradePosition[source]#

Bases: object

How a particular asset traded.

Each asset can have multiple entries (buys) and exits (sells)

For a simple strategies there can be only one or two trades per position.

  • Enter (buy)

  • Exit (sell optionally)

__init__(position_id, trades=<factory>, opened_at=None, closed_at=None, portfolio_value_at_open=None, loss_risk_at_open_pct=None, capital_tied_at_open_pct=None, stop_loss=None, maximum_risk=None)#
Parameters:
Return type:

None

Methods

__init__(position_id[, trades, opened_at, ...])

add_trade(t)

can_trade_close_position(t)

get_first_entry_price()

What was the price when the first entry buy for this position was made.

get_last_exit_price()

What was the time when the last sell for this position was executd.

get_max_size()

Get the largest size of this position over the time

get_total_lp_fees_paid()

Get the total amount of swap fees paid in the position.

get_trade_count()

How many individual trades was done to manage this position.

has_bad_data_issues()

Do we have legacy / incompatible data issues.

is_closed()

is_lose()

is_open()

is_stop_loss()

Was stop loss triggered for this position

is_take_profit()

Was trake profit triggered for this position

is_win()

Did we win this trade.

Attributes

buy_value

buys

capital_tied_at_open_pct

How much portfolio capital was risk when this position was opened.

close_price

At what price we exited this position.

closed_at

Closing the position could be deducted from the trades themselves, but we cache it by hand to speed up processing

duration

How long this position was held.

loss_risk_at_open_pct

What is the maximum risk of this position.

maximum_risk

Related to loss_risk_at_open_pct This is the related dollar value for maximum risk of the position

open_price

At what price we opened this position.

open_quantity

open_value

The current value of this open position, with the price at the time of opening.

opened_at

Closing the position could be deducted from the trades themselves, but we cache it by hand to speed up processing

pair_id

Position id is the same as the opening trade id.

portfolio_value_at_open

The total dollar value of the portfolio when the position was opened

realised_profit

Calculated life-time profit over this position.

realised_profit_percent

Calculated life-time profit over this position.

sell_value

sells

stop_loss

Trigger a stop loss if this price is reached,

position_id

Position id of the trade Used to be self.trades[0].trade_id

trades

List of all trades done for this position

position_id: int#

Position id of the trade Used to be self.trades[0].trade_id

trades: List[SpotTrade]#

List of all trades done for this position

opened_at: Optional[Timestamp] = None#

Closing the position could be deducted from the trades themselves, but we cache it by hand to speed up processing

closed_at: Optional[Timestamp] = None#

Closing the position could be deducted from the trades themselves, but we cache it by hand to speed up processing

portfolio_value_at_open: Optional[float] = None#

The total dollar value of the portfolio when the position was opened

loss_risk_at_open_pct: Optional[float] = None#

What is the maximum risk of this position. Risk relative to the portfolio size.

capital_tied_at_open_pct: Optional[float] = None#

How much portfolio capital was risk when this position was opened. This is based on the opening values, any position adjustment after open is ignored Assume capital is tied to the position and we can never release it. Assume no stop loss is used, or it cannot be trigged

stop_loss: Optional[float] = None#

Trigger a stop loss if this price is reached,

We use mid-price as the trigger price.

maximum_risk: Optional[float] = None#

Related to loss_risk_at_open_pct This is the related dollar value for maximum risk of the position

property pair_id: int#

Position id is the same as the opening trade id.

property duration: Optional[timedelta]#

How long this position was held.

Returns:

None if the position is still open

property open_value: float#

The current value of this open position, with the price at the time of opening.

property open_price: float#

At what price we opened this position.

Supports only simple enter/exit positions.

get_first_entry_price()[source]#

What was the price when the first entry buy for this position was made.

Return type:

float

get_last_exit_price()[source]#

What was the time when the last sell for this position was executd.

Return type:

float

property close_price: float#

At what price we exited this position.

Supports only simple enter/exit positions.

property realised_profit: float#

Calculated life-time profit over this position.

property realised_profit_percent: float#

Calculated life-time profit over this position.

is_win()[source]#

Did we win this trade.

is_stop_loss()[source]#

Was stop loss triggered for this position

Return type:

bool

is_take_profit()[source]#

Was trake profit triggered for this position

Return type:

bool

get_max_size()[source]#

Get the largest size of this position over the time

Return type:

float

get_trade_count()[source]#

How many individual trades was done to manage this position.

Return type:

int

get_total_lp_fees_paid()[source]#

Get the total amount of swap fees paid in the position. Includes all trades.

Return type:

int

has_bad_data_issues()[source]#

Do we have legacy / incompatible data issues.

Return type:

bool

__init__(position_id, trades=<factory>, opened_at=None, closed_at=None, portfolio_value_at_open=None, loss_risk_at_open_pct=None, capital_tied_at_open_pct=None, stop_loss=None, maximum_risk=None)#
Parameters:
Return type:

None