Skip to content

Commit

Permalink
small bug fixes and error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesir committed May 24, 2023
1 parent 8e9ee44 commit dff9d6e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lumibot/data_sources/yahoo_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ def _pull_source_symbol_bars(
last_needed_datetime=self.datetime_end,
)
if data.shape[0] == 0:
raise NoDataFound(self.SOURCE, asset.symbol)
message = (
f"{self.SOURCE} did not return data for symbol {asset}. "
f"Make sure there is no symbol typo or use another data source"
)
logging.error(message)
return None
data = self._append_data(asset, data)

# Get the last minute of self._datetime to get the current bar
Expand Down
19 changes: 17 additions & 2 deletions lumibot/strategies/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ def get_position(self, asset):
Parameters
----------
asset : Asset
asset : Asset or str
Asset object who's traded positions is sought.
Returns
Expand All @@ -945,6 +945,14 @@ def get_position(self, asset):
>>> self.log_message(position.quantity)
"""

# Check if asset is an Asset object or a string
if not (isinstance(asset, Asset) or isinstance(asset, str)):
logger.error(
f"Asset in get_position() must be an Asset object or a string. You entered {asset}."
)
return None

asset = self._set_asset_mapping(asset)
return self.broker.get_tracked_position(self.name, asset)

Expand Down Expand Up @@ -1699,7 +1707,7 @@ def get_last_price(
Parameters
----------
asset : Asset object
asset : Asset object or str
Asset object for which the last closed price will be
retrieved.
quote : Asset object
Expand Down Expand Up @@ -1744,6 +1752,13 @@ def get_last_price(
>>> )
>>> price = self.get_last_price(asset=self.base, exchange="CME")
"""
# Check if the Asset object is a string or Asset object
if not (isinstance(asset, Asset) or isinstance(asset, str)):
logger.error(
f"Asset in get_last_price() must be a string or Asset object. Got {asset} of type {type(asset)}"
)
return None

asset = self._set_asset_mapping(asset)

try:
Expand Down
9 changes: 9 additions & 0 deletions lumibot/tools/yahoo_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def format_df(df, auto_adjust):
def process_df(df, asset_info=None):
df = df.dropna().copy()

# If the df is empty, return it
if df.empty:
return df

if df.index.tzinfo is None:
df.index = df.index.tz_localize(LUMIBOT_DEFAULT_PYTZ)
else:
Expand Down Expand Up @@ -247,6 +251,11 @@ def fetch_symbol_day_data(symbol, caching=True, last_needed_datetime=None):
# Caching is disabled or no previous data found
# or data found not up to date
data = YahooHelper.download_symbol_day_data(symbol)

# Check if the data is empty
if data.empty:
return data

YahooHelper.dump_pickle_file(symbol, DAY_DATA, data)
return data

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def increment_version():

setuptools.setup(
name="lumibot",
version="2.6.0",
version="2.6.1",
author="Robert Grzesik",
author_email="rob@lumiwealth.com",
description="Backtesting and Trading Library, Made by Lumiwealth",
Expand Down

0 comments on commit dff9d6e

Please sign in to comment.