self.get_historical_prices

lumibot.strategies.strategy.Strategy.get_historical_prices(self, asset: Asset | str, length: int, timestep: str = '', timeshift: timedelta = None, quote: Asset = None, exchange: str = None, include_after_hours: bool = True)

Get historical pricing data for a given symbol or asset.

Return data bars for a given symbol or asset. Any number of bars can be return limited by the data available. This is set with ‘length’ in number of bars. Bars may be returned as daily or by minute. And the starting point can be shifted backwards by time or bars.

Parameters:
  • asset (str or Asset) – The symbol string representation (e.g. AAPL, GOOG, …) or asset object. Cryptocurrencies must also specify the quote currency.

  • length (int) – The number of rows (number of timesteps)

  • timestep (str) – Either "minute" for minutes data or "day" for days data default value depends on the data_source (minute for alpaca, day for yahoo, …). If you need, you can specify the width of the bars by adding a number before the timestep (e.g. “5 minutes”, “15 minutes”, “1 day”, “2 weeks”, “1month”, …)

  • timeshift (timedelta) – None by default. If specified indicates the time shift from the present. If backtesting in Pandas, use integer representing number of bars.

  • quote (Asset) – The quote currency for crypto currencies (e.g. USD, USDT, EUR, …). Default is the quote asset for the strategy.

  • exchange (str) – The exchange to pull the historical data from. Default is None (decided based on the broker)

  • include_after_hours (bool) – Whether to include after hours data. Default is True. Currently only works with Interactive Brokers.

Returns:

The bars object with all the historical pricing data. Please check the Entities.Bars object documentation for more details on how to use Bars objects. To get a DataFrame from the Bars object, use bars.df.

Return type:

Bars

Example

Extract 2 rows of SPY data with one day timestep between each row with the latest data being 24h ago (timedelta(days=1)) (in a backtest)

>>> # Get the data for SPY for the last 2 days
>>> bars =  self.get_historical_prices("SPY", 2, "day")
>>> # To get the DataFrame of SPY data
>>> df = bars.df
>>>
>>> # Then, to get the DataFrame of SPY data
>>> df = bars.df
>>> last_ohlc = df.iloc[-1] # Get the last row of the DataFrame (the most recent pricing data we have)
>>> self.log_message(f"Last price of BTC in USD: {last_ohlc['close']}, and the open price was {last_ohlc['open']}")
>>> # Get the data for AAPL for the last 30 minutes
>>> bars =  self.get_historical_prices("AAPL", 30, "minute")
>>>
>>> # Then, to get the DataFrame of SPY data
>>> df = bars.df
>>> last_ohlc = df.iloc[-1] # Get the last row of the DataFrame (the most recent pricing data we have)
>>> self.log_message(f"Last price of BTC in USD: {last_ohlc['close']}, and the open price was {last_ohlc['open']}")
>>> # Get the historical data for an AAPL option for the last 30 minutes
>>> asset = self.create_asset("AAPL", asset_type="option", expiration=datetime.datetime(2020, 1, 1), strike=100, right="call")
>>> bars =  self.get_historical_prices(asset, 30, "minute")
>>>
>>> # Then, to get the DataFrame of SPY data
>>> df = bars.df
>>> last_ohlc = df.iloc[-1] # Get the last row of the DataFrame (the most recent pricing data we have)
>>> self.log_message(f"Last price of BTC in USD: {last_ohlc['close']}, and the open price was {last_ohlc['open']}")
>>> # Get the data for BTC in USD  for the last 2 days
>>> asset_base = self.create(symbol="BTC", asset_type="crypto"),
>>> asset_quote = self.create(symbol="USDT", asset_type="crypto"),
>>>
>>> bars =  self.get_historical_prices(asset_base, 2, "day", quote=asset_quote)
>>>
>>> # Then, to get the DataFrame of SPY data
>>> df = bars.df
>>> last_ohlc = df.iloc[-1] # Get the last row of the DataFrame (the most recent pricing data we have)
>>> self.log_message(f"Last price of BTC in USD: {last_ohlc['close']}, and the open price was {last_ohlc['open']}")