State#
tradeexecutor.state.state.State class.
- class State[source]#
Bases:
object
The current state of the trading strategy execution.
- __init__(name=None, portfolio=<factory>, stats=<factory>, asset_blacklist=<factory>, visualisation=<factory>)#
- Parameters
portfolio (Portfolio) –
stats (Statistics) –
visualisation (Visualisation) –
- Return type
None
Methods
__init__
([name, portfolio, stats, ...])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, ...)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
The name of this strategy.
Portfolio of this strategy.
Portfolio and position performance records over time.
Assets that the strategy is not allowed to touch, or have failed to trade in the past, resulting to a frozen position.
Strategy visualisation and debug messages to show how the strategy is thinking.
- 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_good_pair(pair)[source]#
Check if the trading pair is blacklisted.
- Parameters
pair (TradingPairIdentifier) –
- Return type
- create_trade(ts, pair, quantity, reserve, assumed_price, trade_type, reserve_currency, reserve_currency_price)[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
ts (datetime) –
pair (TradingPairIdentifier) –
assumed_price (USDollarAmount) –
trade_type (TradeType) –
reserve_currency (AssetIdentifier) –
reserve_currency_price (USDollarAmount) –
- Return type
- start_execution(ts, trade, txid, nonce)[source]#
Update our balances and mark the trade execution as started.
Called before a transaction is broadcasted.
- Parameters
ts (datetime) –
trade (TradeExecution) –
txid (str) –
nonce (int) –
- mark_trade_failed(failed_at, trade)[source]#
Unroll the allocated capital.
- Parameters
failed_at (datetime) –
trade (TradeExecution) –
- revalue_positions(ts, valuation_method)[source]#
Revalue all open positions in the portfolio.
Reserves are not revalued.
- 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__(name=None, portfolio=<factory>, stats=<factory>, asset_blacklist=<factory>, visualisation=<factory>)#
- Parameters
portfolio (Portfolio) –
stats (Statistics) –
visualisation (Visualisation) –
- Return type
None