TradingPosition#
tradeexecutor.state.position.TradingPosition class.
- class TradingPosition[source]#
Bases:
object
TradingPosition(position_id: int, pair: tradeexecutor.state.identifier.TradingPairIdentifier, opened_at: datetime.datetime, last_pricing_at: datetime.datetime, last_token_price: tradeexecutor.state.types.USDollarAmount, last_reserve_price: tradeexecutor.state.types.USDollarAmount, reserve_currency: tradeexecutor.state.identifier.AssetIdentifier, trades: Dict[int, tradeexecutor.state.trade.TradeExecution] = <factory>, closed_at: Optional[datetime.datetime] = None, frozen_at: Optional[datetime.datetime] = None, last_trade_at: Optional[datetime.datetime] = None, stop_loss: Optional[tradeexecutor.state.types.USDollarAmount] = None, take_profit: Optional[tradeexecutor.state.types.USDollarAmount] = None)
- __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, last_trade_at=None, stop_loss=None, take_profit=None)#
- Parameters
position_id (int) –
pair (TradingPairIdentifier) –
opened_at (datetime) –
last_pricing_at (datetime) –
last_token_price (USDollarAmount) –
last_reserve_price (USDollarAmount) –
reserve_currency (AssetIdentifier) –
trades (Dict[int, TradeExecution]) –
stop_loss (Optional[USDollarAmount]) –
take_profit (Optional[USDollarAmount]) –
- 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
Get the price of the base asset based on the latest valuation.
get_equity_for_position
()get_executed_trades
()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.
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 tied up token quantity in all successfully executed trades.
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
How much % we have made profit so far.
Realised + unrealised profit.
How much money we have received on sells
Calculate the position unrealised profit.
Get the position value using the latest revaluation pricing.
This position represents actual holdings and has executed trades on it.
has_planned_trades
()has_trade
(trade)Check if a trade belongs to this position.
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.
Does this position need to check for stop loss/take profit.
open_trade
(ts, trade_id, quantity, reserve, ...)Open a new trade on position.
schema
(*[, infer_missing, only, exclude, ...])to_dict
([encode_json])to_json
(*[, skipkeys, ensure_ascii, ...])Attributes
When this position was closed
Timestamp when this position was moved to a frozen state
last_trade_at
Trigger a stop loss if this price is reached
Trigger a take profit if this price is reached
Runnint int counter primary key for positions
Trading pair this position is trading
opened_at
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.
- pair: TradingPairIdentifier#
Trading pair this position is trading
- last_token_price: USDollarAmount#
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
- stop_loss: Optional[USDollarAmount] = None#
Trigger a stop loss if this price is reached
- take_profit: Optional[USDollarAmount] = None#
Trigger a take profit if this price is reached
- 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
- 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
- needs_real_time_price()[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 executd.
- 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
- open_trade(ts, trade_id, quantity, reserve, assumed_price, trade_type, reserve_currency, reserve_currency_price)[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
ts (datetime) –
trade_id (int) –
assumed_price (USDollarAmount) –
trade_type (TradeType) –
reserve_currency (AssetIdentifier) –
reserve_currency_price (USDollarAmount) –
- 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_average_price()[source]#
The average price paid for all assets on the long or short side.
- Returns
None if no executed trades
- 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.
- Returns
profit in dollar or None if no opposite trade made
- Return type
- 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_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
- __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, last_trade_at=None, stop_loss=None, take_profit=None)#
- Parameters
position_id (int) –
pair (TradingPairIdentifier) –
opened_at (datetime) –
last_pricing_at (datetime) –
last_token_price (USDollarAmount) –
last_reserve_price (USDollarAmount) –
reserve_currency (AssetIdentifier) –
trades (Dict[int, TradeExecution]) –
stop_loss (Optional[USDollarAmount]) –
take_profit (Optional[USDollarAmount]) –
- Return type
None