Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

state.interface.history should return history at state time during backtest #64

Closed
mariushelf opened this issue Oct 29, 2021 · 5 comments · Fixed by #60
Closed

state.interface.history should return history at state time during backtest #64

mariushelf opened this issue Oct 29, 2021 · 5 comments · Fixed by #60

Comments

@mariushelf
Copy link

Description

state.interface.history should return history at state time during backtest.
At the moment, when called in an init function at the beginning of the backtest, it retrieves history relative to wall time.

Example:

The backtest goes from 2020-01-01 to 2020-12-31. When retrieving 5 days of history I'd expect it to get 2019-12-26 to 2019-12-31.

Instead it gets the last 5 days relative to the day when it is run, e.g., 2021-10-24 - 2021-10-29.

As a workaround I'm using something like this now:

def get_historic_candles_at_state_time(state, symbol, ncandles, resolution):
    current_time = state.time
    start = current_time - ncandles * time_interval_to_seconds(resolution)
    end = current_time
    history = state.interface.history(
        symbol, start_date=start, end_date=end, resolution=resolution
    )
    return history

settings.json

{
  "settings": {
    "account_update_time": 5000,
    "use_sandbox": false,
    "use_sandbox_websockets": false,
    "websocket_buffer_size": 10000,
    "test_connectivity_on_auth": true,

    "coinbase_pro": {
      "cash": "USD"
    },
    "binance": {
      "cash": "EUR",
      "binance_tld": "com"
    },
    "alpaca": {
      "websocket_stream": "iex",
      "cash": "USD"
    }
  }
}

backtest.json (if applicable)

{
  "price_data": {
    "assets": []
  },
  "settings": {
    "use_price": "close",
    "smooth_prices": false,
    "GUI_output": false,
    "show_tickers_with_zero_delta": false,
    "save_initial_account_value": true,
    "show_progress_during_backtest": true,
    "cache_location": "./data/price_caches",
    "continuous_caching": true,
    "resample_account_value_for_metrics": "1d",
    "quote_account_value_in": "USDT",
    "ignore_user_exceptions": false
  }

Platform Info

  • Python version: 3.9
  • Platform: Ubuntu 21.10
@EmersonDove
Copy link
Member

EmersonDove commented Oct 29, 2021

Hmmm I thought we had it working with the backtest times but I must have missed something. Something that may help with your workaround as I patch this is you can use state.time which should give the actual accurate backtest time.

This can be used like this:

state.variables['history'] = state.interface.history(symbol, to=150, return_as='deque', end_date=state.time)['close']

^ Just added the end_date=state.time argument which will push the whole query backwards in time.

Thanks for opening the issue!

@mariushelf
Copy link
Author

Ah nice, thanks for the reply! Didn't know you could combine to and end_time.

I'm using the pypi package, version 1.11.2b0 if that helps.

@EmersonDove EmersonDove linked a pull request Oct 30, 2021 that will close this issue
@EmersonDove
Copy link
Member

EmersonDove commented Oct 30, 2021

Just pushed out a new release. On the backend this is filling in end_date with state.time if an end_date is not already specified. You can update with pip install --upgrade blankly.

@mariushelf
Copy link
Author

It works! 👍 Thanks :)

@bfan1256
Copy link
Contributor

bfan1256 commented Mar 22, 2022

Hey @mariushelf! I know this is really late! Hope you've been well as we've been actively grinding and shooting out updates! Would love for you to join our discord man and try out our new platform

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 a pull request may close this issue.

3 participants