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
Return type

None

Methods

__init__(position_id, pair, opened_at, ...)

calculate_value_using_price(token_price, ...)

can_be_closed()

There are no tied tokens in this position.

from_dict(kvs, *[, infer_missing])

from_json(s, *[, parse_float, parse_int, ...])

get_average_buy()

Calculate average buy price.

get_average_price()

The average price paid for all assets on the long or short side.

get_average_sell()

Calculate average buy price.

get_buy_quantity()

How many units we have bought total

get_current_price()

Get the price of the base asset based on the latest valuation.

get_equity_for_position()

get_executed_trades()

get_first_trade()

Get the first trade for this position.

get_freeze_reason()

Return the revert reason why this position is frozen.

get_identifier()

One trading pair may have multiple open positions at the same time.

get_last_trade()

Get the the last trade for this position.

get_last_tx_hash()

Get the latest transaction performed for this position.

get_live_quantity()

Get all tied up token quantity.

get_name()

Get human readable name for this position

get_net_quantity()

The difference in the quantity of assets bought and sold to date.

get_opening_price()

Get the price when the position was opened.

get_quantity()

Get the tied up token quantity in all successfully executed trades.

get_quantity_unit_name()

Get the unit name we label the quantity in this position

get_realised_profit_usd()

Calculates the profit & loss (P&L) that has been 'realised' via two opposing asset transactions in the Position to date.

get_sell_quantity()

How many units we have sold total

get_successful_trades()

Get all trades that have been successfully executed and contribute to this position

get_total_bought_usd()

How much money we have used on buys

get_total_profit_percent()

How much % we have made profit so far.

get_total_profit_usd()

Realised + unrealised profit.

get_total_sold_usd()

How much money we have received on sells

get_unrealised_profit_usd()

Calculate the position unrealised profit.

get_value()

Get the position value using the latest revaluation pricing.

has_executed_trades()

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()

is_closed()

This position has been closed and does not have any capital tied to it.

is_frozen()

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.

is_profitable()

This position is currently having non-zero profit.

is_short()

Is this position short on the underlying base asset.

is_stop_loss()

Was this position ended with stop loss trade

is_stop_loss_closed()

Did this position close with stop loss.

is_take_profit()

Was this position ended with take profit trade

is_take_profit_closed()

Did this position close with trake profit.

needs_real_time_price()

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

closed_at

When this position was closed

frozen_at

Timestamp when this position was moved to a frozen state

last_trade_at

stop_loss

Trigger a stop loss if this price is reached

take_profit

Trigger a take profit if this price is reached

position_id

Runnint int counter primary key for positions

pair

Trading pair this position is trading

opened_at

last_pricing_at

When was the last time this position was (re)valued

last_token_price

Last valued price for the base token.

last_reserve_price

reserve_currency

Which reserve currency we are going to receive when we sell the asset

trades

List of trades taken for this position.

position_id: int#

Runnint int counter primary key for positions

pair: TradingPairIdentifier#

Trading pair this position is trading

last_pricing_at: datetime#

When was the last time this position was (re)valued

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

closed_at: Optional[datetime] = None#

When this position was closed

frozen_at: Optional[datetime] = None#

Timestamp when this position was moved to a frozen state

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_open()[source]#

This is an open trading position.

Return type

bool

is_closed()[source]#

This position has been closed and does not have any capital tied to it.

Return type

bool

is_frozen()[source]#

This position has had a failed trade and can no longer be automatically moved around.

Return type

bool

get_first_trade()[source]#

Get the first trade for this position.

Considers unexecuted trades.

Return type

TradeExecution

get_last_trade()[source]#

Get the the last trade for this position.

Considers unexecuted and failed trades.

Return type

TradeExecution

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

bool

is_short()[source]#

Is this position short on the underlying base asset.

Return type

bool

is_stop_loss()[source]#

Was this position ended with stop loss trade

Return type

bool

is_take_profit()[source]#

Was this position ended with take profit trade

Return type

bool

is_profitable()[source]#

This position is currently having non-zero profit.

is_loss()[source]#

This position is currently having non-zero losses.

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

bool

needs_real_time_price()[source]#

Does this position need to check for stop loss/take profit.

Return type

bool

get_name()[source]#

Get human readable name for this position

Return type

str

get_quantity_unit_name()[source]#

Get the unit name we label the quantity in this position

Return type

str

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

Decimal

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

Decimal

get_current_price()[source]#

Get the price of the base asset based on the latest valuation.

Return type

USDollarAmount

get_opening_price()[source]#

Get the price when the position was opened.

Return type

USDollarAmount

get_identifier()[source]#

One trading pair may have multiple open positions at the same time.

Return type

str

get_successful_trades()[source]#

Get all trades that have been successfully executed and contribute to this position

Return type

List[TradeExecution]

get_value()[source]#

Get the position value using the latest revaluation pricing.

Return type

USDollarAmount

is_stop_loss_closed()[source]#

Did this position close with stop loss.

Return type

bool

is_take_profit_closed()[source]#

Did this position close with trake profit.

Return type

bool

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
Return type

TradeExecution

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.

Return type

bool

get_total_bought_usd()[source]#

How much money we have used on buys

Return type

USDollarAmount

get_total_sold_usd()[source]#

How much money we have received on sells

Return type

USDollarAmount

get_buy_quantity()[source]#

How many units we have bought total

Return type

Decimal

get_sell_quantity()[source]#

How many units we have sold total

Return type

Decimal

get_net_quantity()[source]#

The difference in the quantity of assets bought and sold to date.

Return type

Decimal

get_average_buy()[source]#

Calculate average buy price.

Returns

None if no buys

Return type

Optional[USDollarAmount]

get_average_sell()[source]#

Calculate average buy price.

Returns

None if no sells

Return type

Optional[USDollarAmount]

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

Optional[USDollarAmount]

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

Optional[USDollarAmount]

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

USDollarAmount

get_total_profit_usd()[source]#

Realised + unrealised profit.

Return type

USDollarAmount

get_total_profit_percent()[source]#

How much % we have made profit so far.

Returns

0 if profit calculation cannot be made yet

Return type

float

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

str

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

Return type

Optional[str]

__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
Return type

None