Skip to content

Commit

Permalink
Add code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Oct 17, 2024
1 parent a3508d9 commit c9e1e3b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
13 changes: 3 additions & 10 deletions tests/backtest/test_other_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ def create_indicators(timestamp: datetime.datetime, parameters: StrategyParamete


def decide_trades(input: StrategyInput) -> list[TradeExecution]:
"""Example decide_trades function that opens a position with a trade size limit."""
position_manager = input.get_position_manager()
pair = input.get_default_pair()
cash = input.state.portfolio.get_cash()
timestamp = input.timestamp
"""Example of storing and loading custom variables."""

cycle = input.cycle
state = input.state
Expand Down Expand Up @@ -170,9 +166,6 @@ class Parameters:

# Variables are readable after the backtest
state = result.state
# We stored data for 29 days
assert len(state.other_data.data.keys()) == 29

# We can read historic values
assert state.other_data.data[1]["my_value"] == 1
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

41 changes: 41 additions & 0 deletions tradeexecutor/state/other_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,47 @@ class OtherData:
- 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:
.. code-block:: python
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:
.. code-block::
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
"""

#: Cycle number -> dict mapping
Expand Down

0 comments on commit c9e1e3b

Please sign in to comment.