OtherData#

API documentation for tradeexecutor.state.other_data.OtherData Python class in Trading Strategy framework.

class OtherData[source]#

Bases: object

Store custom variables in the backtesting state.

  • For each cycle, you can record custom variables here

  • All historical cycle values are stored

  • All values must be JSON serialisable.

  • You can then read the variables back

  • This can be used in live trade execution as well with care. Because of the underlying infrastructure may crash (blockchain halt, server crash) cycles might be skipped.

Example of storing and loading custom variables:

def decide_trades(input: StrategyInput) -> list[TradeExecution]:
    cycle = input.cycle
    state = input.state

    # Saving values by cycle
    state.other_data.save(cycle, "my_value", 1)
    state.other_data.save(cycle, "my_value_2", [1, 2])
    state.other_data.save(cycle, "my_value_3", {1: 2})

    if cycle >= 2:
        # Loading latest values
        assert state.other_data.load_latest("my_value") == 1
        assert state.other_data.load_latest("my_value_2") == [1, 2]
        assert state.other_data.load_latest("my_value_3") == {1: 2}

    return []

You can also read these variables after the backtest is complete:

result = run_backtest_inline(
    client=None,
    decide_trades=decide_trades,
    create_indicators=create_indicators,
    universe=strategy_universe,
    reserve_currency=ReserveCurrency.usdc,
    engine_version="0.5",
    parameters=StrategyParameters.from_class(Parameters),
    mode=ExecutionMode.unit_testing,
)

# Variables are readable after the backtest
state = result.state
assert len(state.other_data.data.keys()) == 29  # We stored data for 29 decide_trades cycles
assert state.other_data.data[1]["my_value"] == 1      # We can read historic values
__init__(data=<factory>)#
Parameters:

data (dict[int, Any]) –

Return type:

None

Methods

__init__([data])

from_dict(kvs, *[, infer_missing])

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

get_latest_stored_cycle()

Get the cycle for which we have recorded any data.

load_latest(name)

Load the latest named value from the store.

save(cycle, name, value)

Save the value on this cycle.

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

to_dict([encode_json])

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

Attributes

data

Cycle number -> dict mapping

data: dict[int, Any]#

Cycle number -> dict mapping

get_latest_stored_cycle()[source]#

Get the cycle for which we have recorded any data.

Returns:

0 if no data

Return type:

int

save(cycle, name, value)[source]#

Save the value on this cycle.

Parameters:
  • cycle (int) –

  • name (str) –

  • value (Any) –

load_latest(name)[source]#

Load the latest named value from the store.

  • Take the value whatever is the last cycle

Returns:

If the last cycle did not store this var, then return None.

Parameters:

name (str) –

Return type:

Any | None

__init__(data=<factory>)#
Parameters:

data (dict[int, Any]) –

Return type:

None