TradingPosition#
tradeexecutor.state.position.TradingPosition Python class in Trading Strategy framework.
- class TradingPosition[source]#
Bases:
object
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>, closed_at=None, frozen_at=None, unfrozen_at=None, last_trade_at=None, portfolio_value_at_open=None, stop_loss=None, take_profit=None, notes=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]) –
- Return type:
None
Methods
__init__
(position_id, pair, opened_at, ...)calculate_value_using_price
(token_price, ...)There are no tied tokens in this position.
from_dict
(kvs, *[, infer_missing])from_json
(s, *[, parse_float, parse_int, ...])Calculate average buy price.
The average price paid for all assets on the long or short side.
Calculate average buy price.
How many units we have bought total
Calculate how much portfolio capital was risk when this position was opened.
Get the price of the base asset based on the latest valuation.
How many asset units this position tolds.
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.
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.
Get all tied up token quantity.
What is the maximum risk of this position.
What is the maximum risk of this position.
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 price of the position at open.
Get the tied up token quantity in all successfully executed trades.
Get the quanaity of the asset the position at open.
Get the unit name we label the quantity in this position
Calculates the profit & loss (P&L) that has been 'realised' via two opposing asset transactions in the Position to date.
How many units we have sold total
Get all trades that have been successfully executed and contribute to this position
How much money we have used on buys
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_trades_by_strategy_cycle
(timestamp)Get all trades made for this position at a specific time.
Get the reserve currency allocated for trades.
Calculate the position unrealised profit.
Get the position value using the latest revaluation pricing.
How much the position had value tied after its open.
This position has stop loss/take profit set.
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 stop loss/take profit.
has_unexecuted_trades
()This position has been closed and does not have any capital tied to it.
This position has had a failed trade and can no longer be automatically moved around.
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 currently having non-zero profit.
is_short
()Is this position short on the underlying base asset.
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.
This position was frozen, but its trades were successfully repaired.
open_trade
(strategy_cycle_at, trade_id, ...)Open a new trade on position.
schema
(*[, infer_missing, only, exclude, ...])set_revaluation_data
(last_pricing_at, ...)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.
last_reserve_price
Which reserve currency we are going to receive when we sell the asset
List of trades taken for this position.
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.
Trigger a stop loss if this price is reached,
Trigger a take profit if this price is reached
Human readable notes about this trade
- 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
- 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”)
- stop_loss: Optional[float]#
Trigger a stop loss if this price is reached,
We use mid-price as the trigger price.
- take_profit: Optional[float]#
Trigger a take profit if this price is reached
We use mid-price as the trigger price.
- notes: Optional[str]#
Human readable notes about this trade
Used to mark test trades from command line. Special case; not worth to display unless the field is filled in.
- is_closed()[source]#
This position has been closed and does not have any capital tied to it.
- Return type:
- is_frozen()[source]#
This position has had a failed trade and can no longer be automatically moved around.
- Return type:
- is_unfrozen()[source]#
This position was frozen, but its trades were successfully repaired.
- 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.
- 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 stop loss/take profit.
- Return type:
- get_quantity_unit_name()[source]#
Get the unit name we label the quantity in this position
- Return type:
- get_quantity()[source]#
Get the tied up token quantity in all successfully executed trades.
Does not account for trades that are currently being executed.
Because decimal summ might
- Returns:
Number of asset units held by this position.
Rounded down to zero if the sum of
- Return type:
- get_live_quantity()[source]#
Get all tied up token quantity.
This includes
All executed trades
All planned trades for this cycle
This gives you remaining token balance, even if there are some earlier sell orders that have not been executed yet.
- Return type:
- get_current_price()[source]#
Get the price of the base asset based on the latest valuation.
- 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:
- get_value()[source]#
Get the position value using the latest revaluation pricing.
If the position is closed, the value should be zero.
- Return type:
- 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:
- 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)[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) – The strategy cycle timestamp for which this trade was executed.
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.
reserve_currency_price (float) –
- Return type:
- has_trade(trade)[source]#
Check if a trade belongs to this position.
- Parameters:
trade (TradeExecution) –
- get_net_quantity()[source]#
The difference in the quantity of assets bought and sold to date.
- Return type:
- get_price_at_open()[source]#
Get the price of the position at open.
Include only the first trade that opened the position. Calculate based on the executed price.
- 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:
- get_realised_profit_usd()[source]#
Calculates the profit & loss (P&L) that has been ‘realised’ via two opposing asset transactions in the Position to date.
- get_unrealised_profit_usd()[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.
- Returns:
profit in dollar
- Return type:
- get_total_profit_percent()[source]#
How much % we have made profit so far.
- 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.
- Return type:
- 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
- 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_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:
- __init__(position_id, pair, opened_at, last_pricing_at, last_token_price, last_reserve_price, reserve_currency, trades=<factory>, closed_at=None, frozen_at=None, unfrozen_at=None, last_trade_at=None, portfolio_value_at_open=None, stop_loss=None, take_profit=None, notes=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]) –
- Return type:
None