TradingPosition#
API documentation for tradeexecutor.state.position.TradingPosition Python class in Trading Strategy framework.
- class TradingPosition[source]#
Bases:
GenericPosition
Represents a single trading position.
Each position trades a single asset
Position is opened when the first trade is made
Position is closed when the last remaining quantity is sold/closed
Position can have its target trigger levels for
take_profit
andstop_loss
Position can have multiple trades and increase or decrease the position exposure
Positions are revalued outside the trades
Trades for the position can have different triggers: rebalance, stop los, etc.
Position can be marked as frozen meaning the automatic system does not how to clean it up
- __init__(position_id, pair, opened_at, last_pricing_at, last_token_price, last_reserve_price, reserve_currency, trades=<factory>, pending_trades=<factory>, expired_trades=<factory>, closed_at=None, frozen_at=None, unfrozen_at=None, last_trade_at=None, portfolio_value_at_open=None, pending_since_at=None, stop_loss=None, take_profit=None, trailing_stop_loss_pct=None, notes=None, balance_updates=<factory>, trigger_updates=<factory>, valuation_updates=<factory>, loan=None, liquidation_price=None)#
- Parameters:
position_id (int) –
pair (TradingPairIdentifier) –
opened_at (datetime) –
last_pricing_at (datetime) –
last_token_price (float) –
last_reserve_price (float) –
reserve_currency (AssetIdentifier) –
trades (Dict[int, TradeExecution]) –
pending_trades (Dict[int, TradeExecution]) –
expired_trades (Dict[int, TradeExecution]) –
balance_updates (Dict[int, BalanceUpdate]) –
trigger_updates (List[TriggerPriceUpdate]) –
valuation_updates (List[ValuationUpdate]) –
liquidation_price (float | None) –
- Return type:
None
Methods
__init__
(position_id, pair, opened_at, ...)add_balance_update_event
(event)Include a new balance update event
add_notes_message
(msg)Add a new message to the notes field.
Calculate the gained interest in tokens.
calculate_quantity_usd_value
(quantity)Calculate value of asset amount using the latest known price.
calculate_value_using_price
(token_price, ...)Calculate the value of this position using the given prices.
There are no tied tokens in this position.
from_dict
(kvs, *[, infer_missing])from_json
(s, *[, parse_float, parse_int, ...])Get the USD value of currently net accrued interest for this position so far.
Return the net from collateral and borrowed interest plus their interest payments.
Get the annualised interest for a credit position.
Get token quantity still availble for the trades in this strategy cycle.
Calculate average buy price.
The average price paid for all assets on the long or short side.
Calculate average buy price.
Iterate over all balance update events.
Get quantity of all balance updates for this position.
Get the amount of outstanding loans we have.
How many units we have bought total
Get the total value of the position when it was bought.
Calculate how much portfolio capital was risk when this position was opened.
How much interest we have claimed from this position and moved back to reserves.
Get the price when the position was closed.
Get the amount of outstanding loans we have.
Get the last recorded and cached market price for the position base asset.
Return class contents for logging.
How long this position was held.
Get equity tied to this position.
get_executed_trades
()Get all trades that have failed in the execution.
Get the first trade for this position.
Return the revert reason why this position is frozen.
Get the assumption of the portfolio asset amount for the account correction / accounting double check.
How to refer this position in log output.
Get the human readable debug dump.
One trading pair may have multiple open positions at the same time.
Get the the last trade for this position.
Get the latest transaction performed for this position.
When did we update this position last time.
get_loan_based_nav
([include_interest, ...])Calculate net asset value (NAV) for a loan based position.
What is the maximum risk of this position.
What is the maximum risk of this position.
Get the largest size of this position over time
get_name
()Get human readable name for this position
The difference in the quantity of assets bought and sold to date.
Get the price when the position was opened.
Get the US-dollar price for a position that does not have any executed price.
Get the original stop loss value when this position was opened.
Get the quantity locked up in market limit trades.
Legacy.
Where to place this position on a profit timeline.
get_quantity
([planned])Get the tied up token quantity in all successfully executed trades.
Get the quanaity of the asset the position at open.
How many asset units this position tolds.
Get the unit name we label the quantity in this position
Calculated life-time profit over this position.
get_realised_profit_usd
([include_interest])Calculates the profit & loss (P&L) that has been 'realised' via two opposing asset transactions in the Position to date.
Get amount of position reduced via in-kind redemptions.
How much interest payments we have made in total.
How many units we have sold total
Get the total value of the position when it was sold.
Calculated life-time profit over this position.
Calculated life-time profit over this position.
Calculated life-time profit over this position, including unrealised PnL.
Get all trades that have been successfully executed and contribute to this position
How much money we have used on buys
Get the total amount of swap fees paid in the position.
get_total_profit_at_timestamp
(timestamp)Get the profit of the position what it was at a certain point of time.
How much % we have made profit so far.
Realised + unrealised profit.
How much money we have received on sells
Get the number of trades in this position.
get_trades_by_strategy_cycle
(timestamp)Get all trades made for this position at a specific time.
Get the reserve currency allocated for trades.
Calculated unrealised PnL for this position.
Get the current profit of this position, minus any netflow.
get_unrealised_profit_usd
([...])Calculate the position unrealised profit.
get_value
([include_interest])Get the current net asset value of this position.
How much the position had value tied after its close.
How much the position had value tied after its open.
This position has stop loss/take profit set.
Do we have legacy / incompatible data issues.
has_buys
()Does is position have any spot buys.
This position represents actual holdings and has executed trades on it.
has_planned_trades
()Does is position have any spot sells.
has_trade
(trade)Check if a trade belongs to this position.
Does this position need to check for a trigger condition.
has_unexecuted_trades
()This position has been closed and does not have any capital tied to it.
This is a trading position for gaining interest by lending out reserve currency.
This position has had a failed trade and can no longer be automatically moved around.
Is this leveraged/loan backed position.
The profit for this trading pair is loan based..
is_long
()Is this position long on the underlying base asset.
is_loss
()This position is currently having non-zero losses.
is_open
()This is an open trading position.
This position is waiting for a condition to trigger to make it open.
This position is currently having non-zero profit.
Did we manage to open this position correctly.
Is any of the position closed.
This position was frozen, but its trades were successfully repaired.
is_short
()Is this position short on the underlying base asset.
is_spot
()Is this a spot market position.
Alias for
is_spot()
.Was this position ended with stop loss trade
Did this position close with stop loss.
Was this position ended with take profit trade
Did this position close with trake profit.
is_test
()The position was opened and closed by perform-test-trade command.
Was this position ended with a trailing stop loss trade.
This position was frozen, but its trades were successfully repaired.
open_trade
(strategy_cycle_at, trade_id, ...)Open a new trade on position.
revalue_base_asset
(last_pricing_at, ...)Update position token prices.
schema
(*[, infer_missing, only, exclude, ...])to_dict
([encode_json])to_json
(*[, skipkeys, ensure_ascii, ...])Attributes
Runnint int counter primary key for positions
Trading pair this position is trading
When this position was opened
When was the last time this position was (re)valued
Last valued price for the base token.
1.0 for stablecoins, unless out of peg, in which case can be 0.99
Which reserve currency we are going to receive when we sell the asset
List of trades taken for this position.
List of trigger trades waiting to be taken trade_id -> Trade map
List of trigger trades that have expried trade_id -> Trade map
When this position was closed
Timestamp when this position was moved to a frozen state.
Timestamp when this position was marked lively again
When this position had a trade last time
Record the portfolio value when the position was opened.
Timestamp when this position was set pending for a market limit.
Trigger a stop loss if this price is reached,
Trigger a take profit if this price is reached
Trailing stop loss.
Human readable notes about this trade
All balance updates that have touched this reserve position.
Trigger updates are stored oldest first.
Trigger updates are stored oldest first.
The loan underlying the position leverage or credit supply.
What is the liquidation price for this position.
- pair: TradingPairIdentifier#
Trading pair this position is trading
- last_token_price: float#
Last valued price for the base token.
There are two ways to receive this
When the position is opened, set to the initial buy price
When the position is revalued, set to the sell price of the position
Note that this might be initially incorrect, if revaluation has not been done yet, because the buy price != sell price.
- reserve_currency: AssetIdentifier#
Which reserve currency we are going to receive when we sell the asset
- trades: Dict[int, TradeExecution]#
List of trades taken for this position. trade_id -> Trade map
- pending_trades: Dict[int, TradeExecution]#
List of trigger trades waiting to be taken trade_id -> Trade map
SEe also old style
stop_loss
,trailing_stop_loss
,trigged_updates
.
- expired_trades: Dict[int, TradeExecution]#
List of trigger trades that have expried trade_id -> Trade map
- closed_at: Optional[datetime]#
When this position was closed
Execution time of the trade or wall-clock time if not available.
- frozen_at: Optional[datetime]#
Timestamp when this position was moved to a frozen state.
This can happen multiple times, so is is the last time when this happened.
See also
unfrozen_at
.
- unfrozen_at: Optional[datetime]#
Timestamp when this position was marked lively again
Set by
tradeexecutor.state.repair
when the position trades are repaired and the position is moved to open or closed list.
- portfolio_value_at_open: Optional[float]#
Record the portfolio value when the position was opened.
This can be later used to analyse the risk of the trades. (“Max value at the risk”)
- pending_since_at: Optional[datetime]#
Timestamp when this position was set pending for a market limit.
Cleared back to None if the position opens.
See also
is_pending()
- stop_loss: Optional[float]#
Trigger a stop loss if this price is reached,
We use mid-price as the trigger price.
Note
This should not be updated directly, but via
tradeexecutor.strategy.pandas_trader.position_manager.update_stop_loss()
.
- take_profit: Optional[float]#
Trigger a take profit if this price is reached
We use mid-price as the trigger price.
- trailing_stop_loss_pct: Optional[float]#
Trailing stop loss.
For details see Trailing stop loss.
Set the trailing stop as the percentage of the market price. This will update
stop_loss
price if the new resultingstop_loss
will be higher as the previous one.Percents as the relative to the the market price, e.g. for 10% trailing stop loss set this value for 0.9.
Calculated as stop_loss = mid_price trailing_stop_loss_pct.
Updated by
tradeexecutor.strategy.stop_loss.check_position_triggers()
. For any updates you can readtrigger_updates
.
- notes: Optional[str]#
Human readable notes about this trade
Special case; not worth to display unless the field is filled in.
May contain multiple newline separated messages
Used to mark test trades from command line.
Used to add log information about frozen and unfrozen positions
- balance_updates: Dict[int, BalanceUpdate]#
All balance updates that have touched this reserve position.
Generated by
tradeexecutor.strategy.sync_model.SyncModel
.BalanceUpdate.id -> BalanceUpdate mapping
- trigger_updates: List[TriggerPriceUpdate]#
Trigger updates are stored oldest first.
There is no record made if there are no trigger updates change. For example, for trailing stop loss, there is no record added, if the price did not move upwards, causing the stop loss level to move.
See also py:attr:pending_trades where new-style partial take profit trades are placed.
- valuation_updates: List[ValuationUpdate]#
Trigger updates are stored oldest first.
See also
last_token_price
andlast_pricing_at
legacy attributes.
- loan: Optional[Loan]#
The loan underlying the position leverage or credit supply.
Applicable for
short/long positions using lending protocols
credit supply (collateral without borrow)
This reflects the latest
tradeexecutor.state.trade.TradeExecution.executed_loan
of a successful trade. This object is updated with accrued interest information from on-chain data outside trades. If the position does not have successfully executed trades yet, this isNone
.
- liquidation_price: float | None#
What is the liquidation price for this position. If the price goes below this, the position is liquidated.
Applicable for
short/long positions using lending protocols
TODO: When this is set and when this is updated.
- get_debug_dump()[source]#
Return class contents for logging.
- Returns:
Indented JSON-like content
- Return type:
- get_human_summary()[source]#
Get the human readable debug dump.
- Returns:
Human readable dict
- Return type:
- is_closed()[source]#
This position has been closed and does not have any capital tied to it.
See also
is_reduced()
:- Return type:
- is_test()[source]#
The position was opened and closed by perform-test-trade command.
The trade and the position should not be counted in the statistics.
- Return type:
- is_frozen()[source]#
This position has had a failed trade and can no longer be automatically moved around.
After the position is unfrozen the flag goes away.
- Return type:
- is_unfrozen()[source]#
This position was frozen, but its trades were successfully repaired.
- Return type:
- is_repaired()[source]#
This position was frozen, but its trades were successfully repaired.
Alias for
is_unfrozen()
.- Return type:
- get_first_trade()[source]#
Get the first trade for this position.
Considers unexecuted trades.
- Return type:
- get_last_trade()[source]#
Get the the last trade for this position.
Considers unexecuted and failed trades.
- Return type:
- is_long()[source]#
Is this position long on the underlying base asset.
We consider the position long if the first trade is buy.
This includes spot buy.
- Return type:
- is_trailing_stop_loss()[source]#
Was this position ended with a trailing stop loss trade.
Position was terminated with a stop loss
Trailing stop loss was set
Trailing stop loss was updated at least once
- Return type:
- is_credit_supply()[source]#
This is a trading position for gaining interest by lending out reserve currency.
- is_pending()[source]#
This position is waiting for a condition to trigger to make it open.
The position is hyphotetical in pending_positions list
Will realise if market limit is reached
- Return type:
- has_executed_trades()[source]#
This position represents actual holdings and has executed trades on it.
This will return false for positions that are still planned or have zero successful trades.
- Return type:
- has_trigger_conditions()[source]#
Does this position need to check for a trigger condition.
stop loss/take profit hardcoded options
other triggers
- Returns:
True if ewe need to check for the triggers in the trigger udpdate
- Return type:
- get_quantity_unit_name()[source]#
Get the unit name we label the quantity in this position
- Return type:
- get_balance_update_events()[source]#
Iterate over all balance update events.
Balance updates describe external events affecting the balance of this position: the update was not triggered by the trade executor itself.
Deposits
Redemptions
Account corrections
Trades are not included here
- Return type:
- get_base_token_balance_update_quantity()[source]#
Get quantity of all balance updates for this position.
How much non-trade events have changed our base token balance
This includes interest events and accounting corrections
- Returns:
How much in-kind redemption events have affected this position.
Decimal zero epsilon noted.
- Return type:
- get_quantity(planned=False)[source]#
Get the tied up token quantity in all successfully executed trades.
Does not account for trades that are currently being executed (in started, or planned state).
Does some fixing for rounding errors in the form of epsilon checks
Accounts for any balance update events (redemptions, interest, accounting corrections)
For interest positions
The underlying principle is calculated as sum of trades e.g. how many deposit or redemption trades we did for Aave reserves
The accrued interest can be read from balance update events
- Parameters:
planned – Include the quantity of the planned trades that are going to be executed on this cycle.
- Returns:
Number of asset units held by this position.
Rounded down to zero if the sum of
- Return type:
- get_redeemed()[source]#
Get amount of position reduced via in-kind redemptions.
Also include any other account corrections
Alias for
get_base_token_balance_update_quantity()
- Returns:
A negative number, the amount of quantiy redeemed from this position
- Return type:
- get_available_trading_quantity(include_pending_trades=False)[source]#
Get token quantity still availble for the trades in this strategy cycle.
This includes
All executed trades
All planned trades for this cycle that have already reduced/increased amounts for this position
This gives you remaining token balance, even if there are some earlier sell orders that have not been executed yet.
- get_opening_price()[source]#
Get the price when the position was opened.
- Returns:
Get the executed opening price of the position.
If the first trade was not success, return 0.0.
- Return type:
- get_current_price()[source]#
Get the last recorded and cached market price for the position base asset.
- Return type:
- get_quantity_old()[source]#
How many asset units this position tolds.
TODO: Remove this
Alias for
get_quantity()
- Return type:
- get_identifier()[source]#
One trading pair may have multiple open positions at the same time.
- Return type:
- get_successful_trades()[source]#
Get all trades that have been successfully executed and contribute to this position
- Return type:
- calculate_value_using_price(token_price, reserve_price, include_interest=True)[source]#
Calculate the value of this position using the given prices.
- get_equity()[source]#
Get equity tied to this position.
TODO: Use
TradingPosition.loan.get_net_asset_value()
for collateral based positions.- Returns:
How much equity we have tied in this position.
TODO: Does not work for collateral positions.
- Return type:
- get_value(include_interest=True)[source]#
Get the current net asset value of this position.
If the position is closed, the value should be zero
Note
This is the planned value, so may be different fr om the executed value.
- Parameters:
include_interest –
Include accrued interest in the valuation.
This will add any interest earned/lost in loans, plus their repayments.
- Returns:
The value of the position if any remaining open amount would be completely closed/unwind.
- Return type:
Calculate net asset value (NAV) for a loan based position.
- get_trades_by_strategy_cycle(timestamp)[source]#
Get all trades made for this position at a specific time.
- Returns:
Iterable of 0….N trades
- Parameters:
timestamp (datetime) –
- Return type:
- get_unexeuted_reserve()[source]#
Get the reserve currency allocated for trades.
Assumes position can only have one reserve currency.
Only spot buys can have unexecuted reserve.
- Returns:
Amount of capital we have allocated in trades that did not correctly execute
- Return type:
- is_properly_opened()[source]#
Did we manage to open this position correctly.
- Returns:
True if the opening trade was correctly executed.
Might also return False for legacy data
- Return type:
- open_trade(strategy_cycle_at, trade_id, quantity, reserve, assumed_price, trade_type, reserve_currency, reserve_currency_price, pair_fee=None, lp_fees_estimated=None, planned_mid_price=None, price_structure=None, slippage_tolerance=None, portfolio_value_at_creation=None, leverage=None, closing=False, planned_collateral_consumption=None, planned_collateral_allocation=None, exchange_name=None, flags=None)[source]#
Open a new trade on position.
Trade can be opened by knowing how much you want to buy (quantity) or how much cash you have to buy (reserve).
- Parameters:
strategy_cycle_at (datetime.datetime | None) –
The strategy cycle timestamp for which this trade was executed.
Might not be available for the accounting corrections done offline.
trade_id (int) – Trade id allocated by the portfolio
quantity (Optional[Decimal]) –
How many units this trade does.
Positive for buys, negative for sells in the spot market.
assumed_price (float) –
The planned execution price.
This is the price we expect to pay per quantity unit after the execution. This is the mid price + any LP fees included.
trade_type (TradeType) – What kind of a trade is this.
reserve_currency (AssetIdentifier) –
Which portfolio reserve we use for this trade.
- param reserve_currency_price:
If the quote token is not USD, then the exchange rate between USD and quote token we assume we have.
Actual exchange rate may depend on the execution.
pair_fee (Optional[float]) – The fee tier from the trading pair / overriden fee.
lp_fees_estimated (Optional[float]) – HOw much we estimate to pay in LP fees (dollar)
planned_mid_price (Optional[float]) – What was the mid-price of the trading pair when we started to plan this trade.
How many reserve units this trade produces/consumes.
I.e. dollar amount for buys/sells.
price_structure (Optional[TradePricing]) –
The full planned price structure for this trade.
The state of the market at the time of planning the trade, and what fees we assumed we are going to get.
slippage_tolerance (Optional[float]) –
Slippage tolerance for this trade.
See
tradeexecutor.state.trade.TradeExecution.slippage_tolerance
for details.portfolio_value_at_creation (Optional[float]) –
Record the portfolio’s value when this posistion was opened.
Will be later used for risk metrics calculations and such.
reserve_currency_price (float) –
- Return type:
- has_trade(trade)[source]#
Check if a trade belongs to this position.
- Parameters:
trade (TradeExecution) –
- can_be_closed()[source]#
There are no tied tokens in this position.
Perform additional check for token amount dust caused by rounding errors.
- Return type:
- get_last_updated_at()[source]#
When did we update this position last time.
What was the last timestamp when this position data was mutated
- Returns:
UTC time
- Return type:
- get_profit_timeline_timestamp()[source]#
Where to place this position on a profit timeline.
If the position is closed, place it at its closing data
- Returns:
UTC time
- Return type:
- get_net_quantity()[source]#
The difference in the quantity of assets bought and sold to date.
Note
To be deprecated. Please use :py:method:`get_quantity` instead.
- Return type:
- get_quantity_at_open()[source]#
Get the quanaity of the asset the position at open.
Include only the first trade that opened the position. Calculate based on the executed price.
- Return type:
- is_reduced()[source]#
Is any of the position closed.
The position is reduced towards close if it contains opposite trades.
See also
is_closed()
.- Return type:
- get_realised_profit_usd(include_interest=True)[source]#
Calculates the profit & loss (P&L) that has been ‘realised’ via two opposing asset transactions in the Position to date.
Profit is calculated as the diff of avg buy and sell price times quantity
Any avg buy and sell contains all fees we have paid in included in the price, so we do not need to add them to profit here
See also
get_realised_profit_percent()
.Always returns zero for frozen positions
Note
This function does not account for in-kind redemptions or any other account corrections. Please use
get_realised_profit_percent()
if possible.
- get_unrealised_profit_usd(include_interest=True, account_redemptions=True)[source]#
Calculate the position unrealised profit.
Calculates the profit & loss (P&L) that has yet to be ‘realised’ in the remaining non-zero quantity of assets, due to the current market price.
- Parameters:
include_interest – The interest accrued on this position is includedin the result
account_redemptions – Any in-kind redemptions are reduced from the net euqantity.
- Returns:
profit in dollar
- Return type:
- get_total_profit_percent()[source]#
How much % we have made profit so far.
TODO: Legacy method. Use
get_unrealised_and_realised_profit_percent()
instead.- Returns:
0 if profit calculation cannot be made yet
- Return type:
- get_total_profit_at_timestamp(timestamp)[source]#
Get the profit of the position what it was at a certain point of time.
Include realised and unrealised profit.
- get_freeze_reason()[source]#
Return the revert reason why this position is frozen.
Get the revert reason of the last blockchain transaction, assumed to be swap, for this trade.
If this position has been unfrozen, then return the last freeze reason.
- get_last_tx_hash()[source]#
Get the latest transaction performed for this position.
It’s the tx of the trade that was made for this position.
TODO: Deprecate
- revalue_base_asset(last_pricing_at, last_token_price)[source]#
Update position token prices.
TODO: Legacy. See
valuation_updates
.
- get_value_at_open()[source]#
How much the position had value tied after its open.
Calculate the value after the first trade.
- Return type:
- get_value_at_close()[source]#
How much the position had value tied after its close.
Calculate the value after the last trade
- Return type:
- get_capital_tied_at_open_pct()[source]#
Calculate 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 cannto be trigged
See also
get_loss_risk_at_open_pct()
.- Returns:
Percent of the portfolio value
- Return type:
- get_loss_risk_at_open()[source]#
What is the maximum risk of this position.
The maximum risk is the amount of portfolio we can lose at one position. It is calculated as position stop loss / position total size. We assume stop losses always trigged perfectly and we do not lose (too much) on the stop loss trigger.
- Returns:
Dollar value of the risked capital
- Return type:
- get_loss_risk_at_open_pct()[source]#
What is the maximum risk of this position.
Risk relative to the portfolio size.
See also
get_loss_risk_at_open_pct()
.- Returns:
Percent of total portfolio value
- Return type:
- get_realised_profit_percent()[source]#
Calculated life-time profit over this position.
Calculate how many percent profit this position made, relative to all trades taken over the life time of the position.
See also
See Profitability calculations for more details.
- Returns:
If the position made 1% profit returns 0.01.
Return
0
if the position profitability cannot be calculated, e.g. due to broken trades.- Return type:
- get_unrealised_and_realised_profit_percent(valuation_price=None, include_unrealised=True)[source]#
Calculated unrealised PnL for this position.
This is an estimation of the profit % assuming the position would be completely closed with the current price.
This function accounts for in-kind redemptions affecting the position
Currently only long positions supportd
See also
get_total_profit_percent()
(don’t use, legacy)
- Parameters:
valuation_price – Valuate the unrealised portion of tokens at this price
include_unrealised – Include the unrealised PnL as the part of the result
- Returns:
The profitability of this position currently.
Return 0.05 for a position that is 5% in profit.
Return 0 if you ask for realised profit and none of the position is yet closed.
- Return type:
- get_unrealised_profit_pct()[source]#
Get the current profit of this position, minus any netflow.
Warning
This function is only tested for spot positions with a single trade. It may fail to calculate other kind of positions.
Calculate based on avg buy and sell
For the unrealised portion, calculate the expected close
See also
- Returns:
Estimated position profit in percent, based on avg trade prices.
E.g. if the position gained 100 USD -> 110 USD return 0.1.
If profit cannot be calculated yet (zero trades executed), return 0.
- Return type:
- get_size_relative_realised_profit_percent()[source]#
Calculated life-time profit over this position.
The profit is scaled to the % of the position size relative to the portfolio to account for max capital allocation for the position.
TODO: This does not work for positions that have capital added over time or strategies that have overlapping positions
See Profitability calculations for more details.
- Returns:
If the 50% aloocation position made 1% profit returns 1.005.
- Return type:
- get_size_relative_unrealised_or_realised_profit_percent()[source]#
Calculated life-time profit over this position, including unrealised PnL.
The profit is scaled to the % of the position size relative to the portfolio to account for max capital allocation for the position.
TODO: This does not work for positions that have capital added over time
TODO: Only correctly support unrealised PnL for spot
- Returns:
If the 50% aloocation position made 1% profit returns 1.005.
- Return type:
- get_size_relative_profit_percent()[source]#
Calculated life-time profit over this position.
Both realised and unrealised profit.
- Returns:
If the position made 1% profit returns 1.01.
- Return type:
- get_duration()[source]#
How long this position was held.
- Returns:
None if the position is still open
- Return type:
datetime.timedelta | None
- get_total_lp_fees_paid()[source]#
Get the total amount of swap fees paid in the position. Includes all trades.
- Return type:
- get_max_size()[source]#
Get the largest size of this position over time
NOTE: This metric doesn’t work for positions with more than 2 trades i.e: positions which have been increased and reduced in size
- Return type:
- get_orignal_stop_loss()[source]#
Get the original stop loss value when this position was opened.
Setting
trailing_stop_loss
will cause stop_loss to be updated. We can still fetch the original stop loss fromtrigger_updates
.
- get_original_planned_price()[source]#
Get the US-dollar price for a position that does not have any executed price.
The position was left in a broken state after the first trade failed to execute
We will still have the price from the first trade we thought we were going to get
Always use
TradingPosition.last_token_price
if available.Applies to spot positions only
- Returns:
PlannedUS dollar spot price of the first trade
- Return type:
- calculate_quantity_usd_value(quantity)[source]#
Calculate value of asset amount using the latest known price.
If we do not have price data, use the first planned trade
- add_notes_message(msg)[source]#
Add a new message to the notes field.
Messages are newline separated.
- Parameters:
msg (str) –
- add_balance_update_event(event)[source]#
Include a new balance update event
- Raises:
BalanceUpdateEventAlreadyAdded – In the case of a duplicate and event id is already used.
- Parameters:
event (BalanceUpdate) –
- calculate_accrued_interest_quantity(asset)[source]#
Calculate the gained interest in tokens.
This is done as the sum of all interest events.
This is also denormalised as position.interest.accrued_interest.
- Parameters:
asset (AssetIdentifier) – aToken/vToken for which we calculate the interest for
- Returns:
Number of quote tokens this position has gained interest.
- Return type:
- get_accrued_interest()[source]#
Get the USD value of currently net accrued interest for this position so far.
Get any unclaimed interest on this position. After position is closed, all remaining accrued interest is claimed.
See
get_accrued_interest_with_repayments()
to account any interest payments.The accrued interest is included as the position accounting item until the position is completely closed
When the position is completed closed, the accured interest tokens are traded and moved to reserves
After position is closed calling get_accrued_interest() should return zero
TODO: This might not work correctly for partially closed positions.
- Returns:
Net interest PnL in USD.
Positive if we have earned interest, negative if we have paid it.
- Return type:
- get_accrued_interest_with_repayments()[source]#
Return the net from collateral and borrowed interest plus their interest payments.
See also
get_accrued_interest()
- Return type:
- get_claimed_interest()[source]#
How much interest we have claimed from this position and moved back to reserves.
See also
get_accrued_interest()
for the life-time interest accumulation.Loan.get_net_asset_value()
for notes about loan interest tracking
- Return type:
- get_repaid_interest()[source]#
How much interest payments we have made in total.
See also
Loan.get_net_asset_value()
for notes about loan interest tracking
- Return type:
- get_held_assets()[source]#
Get the assumption of the portfolio asset amount for the account correction / accounting double check.
- Returns:
(Asset id, amount) iterables
- Return type:
- __init__(position_id, pair, opened_at, last_pricing_at, last_token_price, last_reserve_price, reserve_currency, trades=<factory>, pending_trades=<factory>, expired_trades=<factory>, closed_at=None, frozen_at=None, unfrozen_at=None, last_trade_at=None, portfolio_value_at_open=None, pending_since_at=None, stop_loss=None, take_profit=None, trailing_stop_loss_pct=None, notes=None, balance_updates=<factory>, trigger_updates=<factory>, valuation_updates=<factory>, loan=None, liquidation_price=None)#
- Parameters:
position_id (int) –
pair (TradingPairIdentifier) –
opened_at (datetime) –
last_pricing_at (datetime) –
last_token_price (float) –
last_reserve_price (float) –
reserve_currency (AssetIdentifier) –
trades (Dict[int, TradeExecution]) –
pending_trades (Dict[int, TradeExecution]) –
expired_trades (Dict[int, TradeExecution]) –
balance_updates (Dict[int, BalanceUpdate]) –
trigger_updates (List[TriggerPriceUpdate]) –
valuation_updates (List[ValuationUpdate]) –
liquidation_price (float | None) –
- Return type:
None