Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# self.get_last_price

lumibot.strategies.strategy.Strategy.get_last_price(self, asset, quote=None, exchange=None, should_use_last_close=True)

  Takes an asset and returns the last known price

  Makes an active call to the market to retrieve the last price. In backtesting will provide the close of the last complete bar.

  Parameters:

    - asset (Asset object or str) – Asset object for which the last closed price will be retrieved.

    - quote (Asset object) – Quote asset object for which the last closed price will be retrieved. This is required for cryptocurrency pairs.

    - exchange (str) – Exchange name for which the last closed price will be retrieved. This is required for some cryptocurrency pairs.

    - should_use_last_close (bool) – If False, it will make Interactive Brokers only return the price of an asset if it has been trading today. Defaults to True.

  Returns:

    Last closed price as either a float or Decimal object.

  Return type:

    Float or Decimal

  Example

      >>> # Will return the last price for the asset
      >>> asset = "SPY"
      >>> last_price = self.get_last_price(asset)
      >>> self.log_message(f"Last price for {asset} is {last_price}")

      >>> # Will return the last price for a crypto asset
      >>> base = Asset(symbol="BTC", asset_type="crypto")
      >>> quote = Asset(symbol="USDT", asset_type="crypto")
      >>> last_price = self.get_last_price(base, quote=quote)
      >>> self.log_message(f"Last price for BTC/USDT is {last_price}")

      >>> # Will return the last price for a crypto asset
      >>> base = Asset(symbol="BTC", asset_type="crypto")
      >>> quote = Asset(symbol="USD", asset_type="forex")
      >>> last_price = self.get_last_price(base, quote=quote)
      >>> self.log_message(f"Last price for BTC/USDT is {last_price}")

      >>> # Will return the last price for a futures asset
      >>> self.base = Asset(
      >>>     symbol="ES",
      >>>     asset_type="future",
      >>>     expiration=date(2022, 12, 16),
      >>> )
      >>> price = self.get_last_price(asset=self.base, exchange="CME")