Skip to content

Thread executor's time_per_step into the trade Account freq (#1846)#2218

Open
genisis0x wants to merge 1 commit into
microsoft:mainfrom
genisis0x:fix/1846-thread-executor-freq-to-account
Open

Thread executor's time_per_step into the trade Account freq (#1846)#2218
genisis0x wants to merge 1 commit into
microsoft:mainfrom
genisis0x:fix/1846-thread-executor-freq-to-account

Conversation

@genisis0x
Copy link
Copy Markdown

Summary

`create_account_instance` always built `Account` with the hardcoded default `freq="day"`. When the user only has intraday data and runs the backtest with a higher-frequency executor (e.g. `time_per_step="60min"`), the benchmark fetch at `Account.init` tries to read day-level data and raises `ValueError: The benchmark [...] does not exist. Please provide the right benchmark` before the executor's `reset_common_infra` gets a chance to call `trade_account.reset(freq=time_per_step)`.

The fix derives the account's freq from the executor's `time_per_step` at the entry point and threads it through:

  • `get_strategy_executor` reads `executor["kwargs"]["time_per_step"]` (or `executor.time_per_step` for an already-built executor) via a new tiny helper `_executor_time_per_step`.
  • `create_account_instance` grows a `freq: str = "day"` parameter and passes it to `Account(freq=...)`.
  • Falls back to the historic `"day"` default when the executor's freq cannot be determined (a Path or opaque object).

No change to the default day-frequency path: callers that don't set `time_per_step`, or that pass a Path-style executor, still get `freq="day"`.

Test plan

  • New `tests/misc/test_backtest_account_freq_threading.py` covers the extraction helper:
    • dict executor with `time_per_step` → returns that value
    • dict executor without it → returns `None` (caller defaults to "day")
    • dict with non-string `time_per_step` is rejected (defensive)
    • executor instance with `time_per_step` attribute → returns it
    • executor instance without the attribute → returns `None`
    • Path/str executor → returns `None`

Fixes #1846

`create_account_instance` always built the Account with the hardcoded
default `freq="day"`. When the user only has intraday data and runs the
backtest with a higher-frequency executor (e.g. 60min), the benchmark
fetch at `Account.__init__` tries to read day-level data and crashes
before the executor's `reset_common_infra` gets a chance to call
`trade_account.reset(freq=time_per_step)`.

Derive the account's freq from the executor's `time_per_step` in
`get_strategy_executor` and pass it through. Falls back to "day" when
the executor's freq cannot be determined (Path / opaque object).

Fixes microsoft#1846
@genisis0x
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

It use default param freq='day' for create Account obj in Backtest.

1 participant