TradePosition#

tradingstrategy.analysis.tradeanalyzer.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__(trades=<factory>, opened_at=None, closed_at=None)#
Parameters:
Return type:

None

Methods

__init__([trades, opened_at, closed_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_trade_count()

How many individual trades was done to manage this position.

is_closed()

is_lose()

is_open()

is_stop_loss()

Was stop loss triggered for this position

is_win()

Did we win this trade.

Attributes

buy_value

buys

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.

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.

position_id

Position id is the same as the opening trade id.

realised_profit

Calculated life-time profit over this position.

realised_profit_percent

Calculated life-time profit over this position.

sell_value

sells

trades

List of all trades done for this position

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

property position_id: int#

Position id is the same as the opening trade id.

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

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

__init__(trades=<factory>, opened_at=None, closed_at=None)#
Parameters:
Return type:

None