Hide navigation sidebar
Hide table of contents sidebar
Toggle table of contents sidebar

# self.get_historical_prices

strategies.strategy.Strategy.get_historical_prices(self, asset, length, timestep='', timeshift=None, quote=None)

  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. Crypto currencies must specify the market. Use a string with the two coins as follows: ‘ETH/BTC’. Alternatively for cryptos, one can use a tuple with the two asset objects, numerator first, denominator second. ‘(Asset(ETH), Asset(BTC))’

    - 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, …)

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

  Returns

  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_symbol_bars("SPY", 2, "day")
      >>> # To get the DataFrame of SPY data
      >>> bars.df

      >>> # Get the data for AAPL for the last 30 minutes
      >>> bars =  self.get_symbol_bars("AAPL", 30, "minute")
      >>> # To get the DataFrame of AAPL data
      >>> bars.df

      >>> # 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_symbol_bars(asset, 30, "minute")
      >>> # To get the DataFrame of AAPL data
      >>> bars.df

      >>> # 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_symbol_bars("SPY", 2, "day")
      >>> # To get the DataFrame of SPY data
      >>> bars.df