State#

tradeexecutor.state.state.State class.

class State[source]#

Bases: object

The current state of the trading strategy execution.

__init__(created_at=<factory>, cycle=1, name=None, portfolio=<factory>, stats=<factory>, asset_blacklist=<factory>, visualisation=<factory>)#
Parameters
Return type

None

Methods

__init__([created_at, cycle, name, ...])

blacklist_asset(asset)

Add a asset to the blacklist.

create_trade(ts, pair, quantity, reserve, ...)

Creates a request for a new trade.

from_dict(kvs, *[, infer_missing])

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

is_empty()

This state has no open or past trades or reserves.

is_good_pair(pair)

Check if the trading pair is blacklisted.

mark_broadcasted(broadcasted_at, trade)

mark_trade_failed(failed_at, trade)

Unroll the allocated capital.

mark_trade_success(executed_at, trade, ...)

perform_integrity_check()

Check that we are not reusing any trade or position ids and counters are correct.

revalue_positions(ts, valuation_method)

Revalue all open positions in the portfolio.

schema(*[, infer_missing, only, exclude, ...])

start_execution(ts, trade, txid, nonce)

Update our balances and mark the trade execution as started.

start_trades(ts, trades[, max_slippage, ...])

Mark trades ready to go.

to_dict([encode_json])

to_json(*[, skipkeys, ensure_ascii, ...])

update_reserves(new_reserves)

Attributes

cycle

The next cycle.

name

The name of this strategy.

created_at

When this state was created

portfolio

Portfolio of this strategy.

stats

Portfolio and position performance records over time.

asset_blacklist

Assets that the strategy is not allowed to touch, or have failed to trade in the past, resulting to a frozen position.

visualisation

Strategy visualisation and debug messages to show how the strategy is thinking.

created_at: datetime#

When this state was created

Same as when the strategy was launched

cycle: int = 1#

The next cycle.

How many strategy thinking and execution cycles we have completed successfully.

Starts with 1 (no cycles completed)

name: Optional[str] = None#

The name of this strategy. Can be unset. Set when the state is created.

portfolio: Portfolio#

Portfolio of this strategy. Currently only one portfolio per strategy.

stats: Statistics#

Portfolio and position performance records over time.

asset_blacklist: Set[str]#

Assets that the strategy is not allowed to touch, or have failed to trade in the past, resulting to a frozen position. Besides this internal black list, the executor can have other blacklists based on the trading universe and these are not part of the state. The main motivation of this list is to avoid assets that caused a freeze in the future. Key is Ethereum address, lowercased.

visualisation: Visualisation#

Strategy visualisation and debug messages to show how the strategy is thinking.

is_empty()[source]#

This state has no open or past trades or reserves.

Return type

bool

is_good_pair(pair)[source]#

Check if the trading pair is blacklisted.

Parameters

pair (TradingPairIdentifier) –

Return type

bool

create_trade(ts, pair, quantity, reserve, assumed_price, trade_type, reserve_currency, reserve_currency_price, notes=None)[source]#

Creates a request for a new trade.

If there is no open position, marks a position open.

Trade can be opened by knowing how much you want to buy (quantity) or how much cash you have to buy (reserve).

  • To open a spot buy, fill in reseve amount you wish to use for the buy

  • To open a spot sell, fill in quoantity amount you wish to use for the buy, as a negative number

Returns

Tuple position, trade, was a new position created

Parameters
Return type

Tuple[TradingPosition, TradeExecution, bool]

start_execution(ts, trade, txid, nonce)[source]#

Update our balances and mark the trade execution as started.

Called before a transaction is broadcasted.

Parameters
mark_trade_failed(failed_at, trade)[source]#

Unroll the allocated capital.

Parameters
revalue_positions(ts, valuation_method)[source]#

Revalue all open positions in the portfolio.

Reserves are not revalued.

Parameters
blacklist_asset(asset)[source]#

Add a asset to the blacklist.

Parameters

asset (AssetIdentifier) –

perform_integrity_check()[source]#

Check that we are not reusing any trade or position ids and counters are correct.

Raise

Assertion error in the case internal data structures are damaged

start_trades(ts, trades, max_slippage=0.01, underflow_check=False)[source]#

Mark trades ready to go.

Update any internal accounting of capital allocation from reseves to trades.

Sets the execution model specific parameters like max_slippage on the trades.

Parameters
  • max_slippage (float) – The slippage allowed for this trade before it fails in execution. 0.01 is 1%.

  • underflow_check – If true warn us if we do not have enough reserves to perform the trades. This does not consider new reserves released from the closed positions in this cycle.

  • ts (datetime) –

  • trades (List[TradeExecution]) –

__init__(created_at=<factory>, cycle=1, name=None, portfolio=<factory>, stats=<factory>, asset_blacklist=<factory>, visualisation=<factory>)#
Parameters
Return type

None