SimulatedWallet#

API documentation for tradeexecutor.backtest.simulated_wallet.SimulatedWallet Python class in Trading Strategy framework.

class SimulatedWallet[source]#

Bases: object

A wallet that keeps token balances by ERC-20 address.

  • Simulates different incoming and outgoing tokens from a wallet includive, aToken and vToken interest amounts with rebalance().

  • If a backtest tries to transfer a token it does not have, or does not have enough of it, raise an error

  • Will catch bugs in internal accounting

__init__()[source]#

Methods

__init__()

fetch_nonce_and_tx_hash()

Allocates a dummy nonce for a transaction.

get_all_balances()

Show the status of the wallet as a printable DataFrame.

get_balance(token)

Get on-chain balance of one token.

get_token_symbol(token)

Get the human readable name of token for diagnostics output.

rebase(token, new_amount)

Set rebase token amount.

set_balance(token, amount)

Directly set balance.

update_balance(token, delta[, reason, epsilon])

Change the token balance of some delta.

update_token_info(asset)

Set the token info for a particular ERC-20.

verify_balances(expected[, epsilon])

Check that our simulated balances are what we expect.

Attributes

balances

Raw balances token address -> balance

tokens

token address -> asset info for debug logging

nonce

Start with zero nonce like Ethereum acconts

__init__()[source]#
balances: Dict[str, Decimal]#

Raw balances token address -> balance

tokens: Dict[str, AssetIdentifier]#

token address -> asset info for debug logging

nonce#

Start with zero nonce like Ethereum acconts

update_balance(token, delta, reason=None, epsilon=Decimal('1.00000000000000007154242405462192450852805618492324772617063644020163337700068950653076171875E-18'))[source]#

Change the token balance of some delta.

Check that balance does not go zero.

Parameters:
  • token (str | tradeexecutor.state.identifier.AssetIdentifier) –

    Token we receive or send.

    Give either raw address or asset definition.

    Any asset definion is automatically added to our internal tracking list for diagnostics.

  • delta (Decimal) – The amount of token, human units

  • reason (str) –

    Reason for this change.

    Only used for backtesting diagnostics.

  • epsilon – If the balance goes below this dust threshold, go all the way to zero

set_balance(token, amount)[source]#

Directly set balance.

Parameters:
rebase(token, new_amount)[source]#

Set rebase token amount.

aToken / vToken accrues interest or debt.

Parameters:
get_balance(token)[source]#

Get on-chain balance of one token.

Returns:

Human-readable token balance

Parameters:

token (str | tradeexecutor.state.identifier.AssetIdentifier) –

Return type:

Decimal

fetch_nonce_and_tx_hash()[source]#

Allocates a dummy nonce for a transaction.

Returns:

Tuple (nonce, tx_hash)

Return type:

Tuple[int, str]

get_token_symbol(token)[source]#

Get the human readable name of token for diagnostics output.

Parameters:

token (str | tradeexecutor.state.identifier.AssetIdentifier) –

Return type:

str

update_token_info(asset)[source]#

Set the token info for a particular ERC-20.

This way the wallet has metadata on what token it has and can produce better diagnostics output.

Automatically called by update_balance().

Parameters:

asset (AssetIdentifier) –

get_all_balances()[source]#

Show the status of the wallet as a printable DataFrame.

Example:

print(wallet.get_all_balances())

Output:

                              Balance
Token
USDC                             9500
aUSDC   998.4999999999999999687749774
vWETH  0.3003021039165400376391259260
Return type:

DataFrame

verify_balances(expected, epsilon=0.0001)[source]#

Check that our simulated balances are what we expect.

Returns:

Clean or not, all assets table.

Parameters:

expected (Dict[AssetIdentifier, Decimal]) –

Return type:

Tuple[bool, DataFrame]