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

Fixing tuple, and 'UTC has no key' errors in examples/FinRL_PaperTrading_Demo.ipynb #1186

Merged
merged 2 commits into from
Mar 20, 2024

Conversation

kurone02
Copy link
Contributor

I have submitted a similar pull request at FinRL-Tutorials. Still, I think it should also be updated here since more people might come here to view the example files, and are unaware of the tutorial repository.


An updated version of the gymnasium package changes the behavior of gymnasium.Env.reset. The return value of the reset method now returns a tuple of observation and info, which causes the tuple error mentioned in #1184 and #1164. Please refer to the gymasium's documentation for more details. This is fixed by ignoring the second value in the returned tuple.

The current version of the exchange-calendars package requires the dates to be timezone native, which causes the "UTC has no key" error. The traceback can be found below. This was resolved by erasing tz=pytz.UTC.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[5], [line 1](vscode-notebook-cell:?execution_count=5&line=1)
----> [1](vscode-notebook-cell:?execution_count=5&line=1) df_erl, cumu_erl = alpaca_history(key=API_KEY, 
      [2](vscode-notebook-cell:?execution_count=5&line=2)                                   secret=API_SECRET, 
      [3](vscode-notebook-cell:?execution_count=5&line=3)                                   url=API_BASE_URL, 
      [4](vscode-notebook-cell:?execution_count=5&line=4)                                   start='2024-03-01', #must be within 1 month
      [5](vscode-notebook-cell:?execution_count=5&line=5)                                   end='2024-03-16') #change the date if error occurs

Cell In[4], [line 13](vscode-notebook-cell:?execution_count=4&line=13)
     [11](vscode-notebook-cell:?execution_count=4&line=11) def alpaca_history(key, secret, url, start, end):
     [12](vscode-notebook-cell:?execution_count=4&line=12)     api = tradeapi.REST(key, secret, url, 'v2')
---> [13](vscode-notebook-cell:?execution_count=4&line=13)     trading_days = get_trading_days(start, end)
     [14](vscode-notebook-cell:?execution_count=4&line=14)     df = pd.DataFrame()
     [15](vscode-notebook-cell:?execution_count=4&line=15)     for day in trading_days:
     [16](vscode-notebook-cell:?execution_count=4&line=16)         #df = df.append(api.get_portfolio_history(date_start = day,timeframe='5Min').df.iloc[:78])

Cell In[4], [line 3](vscode-notebook-cell:?execution_count=4&line=3)
      [1](vscode-notebook-cell:?execution_count=4&line=1) def get_trading_days(start, end):
      [2](vscode-notebook-cell:?execution_count=4&line=2)     nyse = tc.get_calendar('NYSE')
----> [3](vscode-notebook-cell:?execution_count=4&line=3)     df = nyse.sessions_in_range(pd.Timestamp(start,tz=pytz.UTC),
      [4](vscode-notebook-cell:?execution_count=4&line=4)                                 pd.Timestamp(end,tz=pytz.UTC))
      [5](vscode-notebook-cell:?execution_count=4&line=5)     trading_days = []
      [6](vscode-notebook-cell:?execution_count=4&line=6)     for day in df:

File [~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2200](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2200), in ExchangeCalendar.sessions_in_range(self, start, end, _parse)
   [2182](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2182) def sessions_in_range(
   [2183](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2183)     self, start: Date, end: Date, _parse: bool = True
   [2184](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2184) ) -> pd.DatetimeIndex:
   [2185](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2185)     """Return sessions within a given range.
   [2186](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2186) 
   [2187](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2187)     Parameters
   (...)
   [2198](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2198)         Sessions from `start` through `end`.
   [2199](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2199)     """
-> [2200](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2200)     slc = self._get_sessions_slice(start, end, _parse)
   [2201](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2201)     return self.sessions[slc]

File [~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2177](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2177), in ExchangeCalendar._get_sessions_slice(self, start, end, _parse)
   [2175](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2175) def _get_sessions_slice(self, start: Date, end: Date, _parse=True) -> slice:
   [2176](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2176)     """Slice representing a range of sessions."""
-> [2177](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2177)     start, end = self._parse_start_end_dates(start, end, _parse)
   [2178](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2178)     slice_start = self.sessions_nanos.searchsorted(start.value, side="left")
   [2179](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2179)     slice_end = self.sessions_nanos.searchsorted(end.value, side="right")

File [~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2173](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2173), in ExchangeCalendar._parse_start_end_dates(self, start, end, _parse)
   [2171](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2171) if not _parse:
   [2172](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2172)     return start, end
-> [2173](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py:2173) return parse_date(start, "start", self), parse_date(end, "end", self)

File [~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:379](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:379), in parse_date(date, param_name, calendar, raise_oob)
    [375](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:375) ts = parse_timestamp(date, param_name, raise_oob=False, side="left", utc=False)
    [377](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:377) if ts.tz is not None:
    [378](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:378)     raise ValueError(
--> [379](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:379)         f"Parameter `{param_name}` received with timezone defined as '{ts.tz.key}'"
    [380](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:380)         f" although a Date must be timezone naive."
    [381](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:381)     )
    [383](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:383) if not ts == ts.normalize():
    [384](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:384)     raise ValueError(
    [385](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:385)         f"Parameter `{param_name}` parsed as '{ts}' although a Date must have"
    [386](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:386)         f" a time component of 00:00."
    [387](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/kurone02/FinRL/examples/~/miniconda3/envs/trading/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py:387)     )

AttributeError: 'UTC' object has no attribute 'key'

@zhumingpassional
Copy link
Collaborator

thanks for your work in FinRL-Tutorials and this repo.
good.

@zhumingpassional zhumingpassional merged commit 7b11491 into AI4Finance-Foundation:master Mar 20, 2024
1 check failed
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 this pull request may close these issues.

None yet

2 participants