AssetIdentifier#

API documentation for tradeexecutor.state.identifier.AssetIdentifier Python class in Trading Strategy framework.

class AssetIdentifier[source]#

Bases: object

Identify a blockchain asset for trade execution.

This is pass-by-copy (as opposite to pass-by-reference) asset identifier we use across the persistent state. Because we copy a lot of information about asset, not just its id, this makes data reads and diagnosing problems simpler.

As internal token_ids and pair_ids may be unstable, trading pairs and tokens are explicitly referred by their smart contract addresses when a strategy decision moves to the execution. We duplicate data here to make sure we have a persistent record that helps to diagnose the issues.

Setting custom data:

A custom asset tagging example:

def create_trading_universe(
    ts: datetime.datetime,
    client: Client,
    execution_context: ExecutionContext,
    universe_options: UniverseOptions,
) -> TradingStrategyUniverse:

    assert universe_options.start_at

    pairs = [
        (ChainId.polygon, "uniswap-v3", "WETH", "USDC", 0.0005)
    ]

    dataset = load_partial_data(
        client,
        execution_context=unit_test_execution_context,
        time_bucket=TimeBucket.d1,
        pairs=pairs,
        universe_options=default_universe_options,
        start_at=universe_options.start_at,
        end_at=universe_options.end_at,
    )

    strategy_universe = TradingStrategyUniverse.create_single_pair_universe(dataset)

    # IMPORTANT
    # Warm up must be called before any tags are set
    strategy_universe.warm_up_data()

    # Set custom labels on the token WETH on the trading pair
    weth_usdc = strategy_universe.get_pair_by_human_description(pairs[0])
    weth_usdc.base.set_tags({"L1", "EVM", "bluechip"})

    assert strategy_universe.data_universe.pairs.pair_map is not None, "Cache data structure missing?"

    weth_usdc = strategy_universe.get_pair_by_human_description(pairs[0])
    assert len(weth_usdc.base.get_tags()) > 0

    return strategy_universe

Then how to read tag data and use it in trade decision making:

def decide_trades(input: StrategyInput) -> list[TradeExecution]:
    # Show how to read pair and asset labels in decide_trade()
    for pair in input.strategy_universe.iterate_pairs():
        if "L1" in pair.get_tags():
            # Do some trading logic for L1 tokens only
            pass

    return []
__init__(chain_id, address, token_symbol, decimals, internal_id=None, info_url=None, underlying=None, type=None, liquidation_threshold=None, other_data=<factory>)#
Parameters:
Return type:

None

Methods

__init__(chain_id, address, token_symbol, ...)

convert_to_decimal(raw_amount)

convert_to_raw_amount(amount)

Return any amount in token native units.

from_dict(kvs, *[, infer_missing])

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

get_identifier()

Assets are identified by their smart contract address.

get_pricing_asset()

Get the asset that delivers price for this asset.

get_tags()

Return list of tags associated with this asset.

is_credit()

Is this a credit asset that accrue interest for us

is_debt()

Is this a credit asset that accrue interest for us

is_interest_accruing()

Will this token gain on-chain interest thru rebase

is_stablecoin()

Do we think this asset reprents a stablecoin

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

set_tags(tags)

Set tags for this asset.

to_dict([encode_json])

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

Attributes

checksum_address

Ethereum madness.

info_url

Info page URL for this asset

internal_id

How this asset is referred in the internal database

liquidation_threshold

Aave liquidation threhold for this asset

type

What kind of asset is this

underlying

The underlying asset for aTokens, vTokens and such

chain_id

See https://chainlist.org/

address

Smart contract address of the asset.

token_symbol

The ticker symbol of this token.

decimals

How many tokens this decimals.

other_data

User storeable properties.

chain_id: int#

See https://chainlist.org/

address: str#

Smart contract address of the asset. Always lowercase.

token_symbol: str#

The ticker symbol of this token.

decimals: int#

How many tokens this decimals. Must be always set and non-negative.

internal_id: Optional[int] = None#

How this asset is referred in the internal database

info_url: Optional[str] = None#

Info page URL for this asset

underlying: Optional[AssetIdentifier] = None#

The underlying asset for aTokens, vTokens and such

type: Optional[AssetType] = None#

What kind of asset is this

Legacy data will default to None.

liquidation_threshold: float | None = None#

Aave liquidation threhold for this asset

Set on aTokens that are used as collateral.

other_data: Optional[dict]#

User storeable properties.

You can add any of your own metadata on the assets here.

Be wary of the life cycle of the instances. The life time of the class instances tied to the trading universe that is recreated for every strategy cycle.

See also get_tags().

get_identifier()[source]#

Assets are identified by their smart contract address.

JSON/Human friendly format to give hash keys to assets, in the format chain id-address.

Returns:

JSON friendly hask key

Return type:

str

property checksum_address: HexAddress#

Ethereum madness.

convert_to_raw_amount(amount)[source]#

Return any amount in token native units.

Convert decimal to fixed point integer.

Parameters:

amount (Decimal) –

Return type:

int

is_stablecoin()[source]#

Do we think this asset reprents a stablecoin

Return type:

bool

is_interest_accruing()[source]#

Will this token gain on-chain interest thru rebase

Return type:

bool

is_credit()[source]#

Is this a credit asset that accrue interest for us

Return type:

bool

is_debt()[source]#

Is this a credit asset that accrue interest for us

Return type:

bool

get_pricing_asset()[source]#

Get the asset that delivers price for this asset.

Returns:

If this asset is a derivative of another, then get the underlying, otherwise return self.

Return type:

AssetIdentifier

get_tags()[source]#

Return list of tags associated with this asset.

To set tags:

asset.other_data[“tags”] = {“L1”, “memecoin”}

Returns:

For WETH return e.g. [L1, bluechip]

Return type:

set[str]

set_tags(tags)[source]#

Set tags for this asset.

  • See also get_tags()

  • See also other_data()

  • Must be called in create_trading_universe

  • Be wary of AssetIdentifier life time as it is passed by value, not be reference, so you cannot update instance data after it has been copied to open positions, etc.

  • translate_trading_pair() is the critical method for understanding and managing identifier life times

Parameters:

tags (set[str]) –

__init__(chain_id, address, token_symbol, decimals, internal_id=None, info_url=None, underlying=None, type=None, liquidation_threshold=None, other_data=<factory>)#
Parameters:
Return type:

None