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:
position_id (int) –
- Return type:
None
Methods
__init__
(position_id[, trades, opened_at, ...])add_trade
(t)can_trade_close_position
(t)What was the price when the first entry buy for this position was made.
What was the time when the last sell for this position was executd.
Get the largest size of this position over the time
Get the total amount of swap fees paid in the position.
How many individual trades was done to manage this position.
Do we have legacy / incompatible data issues.
is_closed
()is_lose
()is_open
()Was stop loss triggered for this position
Was trake profit triggered for this position
is_win
()Did we win this trade.
Attributes
buy_value
buys
How much portfolio capital was risk when this position was opened.
At what price we exited this position.
Closing the position could be deducted from the trades themselves, but we cache it by hand to speed up processing
How long this position was held.
What is the maximum risk of this position.
Related to loss_risk_at_open_pct This is the related dollar value for maximum risk of the position
At what price we opened this position.
open_quantity
The current value of this open position, with the price at the time of opening.
Closing the position could be deducted from the trades themselves, but we cache it by hand to speed up processing
Position id is the same as the opening trade id.
The total dollar value of the portfolio when the position was opened
Calculated life-time profit over this position.
Calculated life-time profit over this position.
sell_value
sells
Trigger a stop loss if this price is reached,
Position id of the trade Used to be self.trades[0].trade_id
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 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:
- get_last_exit_price()[source]#
What was the time when the last sell for this position was executd.
- Return type:
- property close_price: float#
At what price we exited this position.
Supports only simple enter/exit positions.
- get_trade_count()[source]#
How many individual trades was done to manage this position.
- Return type:
- get_total_lp_fees_paid()[source]#
Get the total amount of swap fees paid in the position. Includes all trades.
- Return type: