alpacahq / Momentum-Trading-Example

An example algorithm for a momentum-based day trading strategy.
657 stars 218 forks source link

TypeError: unhashable type: 'list' #10

Open PushHandle opened 4 years ago

PushHandle commented 4 years ago

I have been getting this error consistently at the end of each day while it is trying to liquidate the day's remaining positions. Any thoughts?

Log below:

Jan 08 12:45:10 alpaca-momentum app/worker.1 Trading over, liquidating remaining position in FVE Jan 08 12:45:10 alpaca-momentum app/worker.1 Task exception was never retrieved Jan 08 12:45:10 alpaca-momentum app/worker.1 future: <Task finished coro=<StreamConn._consume_msg() done, defined at /app/.heroku/python/lib/python3.6/site-packages/alpaca_trade_api/polygon/streamconn.py:99> exception=TypeError("unhashable type: 'list'",)> Jan 08 12:45:10 alpaca-momentum app/worker.1 Traceback (most recent call last): Jan 08 12:45:10 alpaca-momentum app/worker.1 File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_trade_api/polygon/streamconn.py", line 103, in _consume_msg Jan 08 12:45:10 alpaca-momentum app/worker.1 await self._dispatch(data) Jan 08 12:45:10 alpaca-momentum app/worker.1 File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_trade_api/polygon/streamconn.py", line 240, in _dispatch Jan 08 12:45:10 alpaca-momentum app/worker.1 await handler(self, channel, ent) Jan 08 12:45:10 alpaca-momentum app/worker.1 File "algo.py", line 336, in handle_second_bar Jan 08 12:45:10 alpaca-momentum app/worker.1 'AM.{}'.format(symbol) Jan 08 12:45:10 alpaca-momentum app/worker.1 File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_trade_api/polygon/streamconn.py", line 253, in deregister Jan 08 12:45:10 alpaca-momentum app/worker.1 self._handler_symbols.pop(self._handlers[channel_pat], None) Jan 08 12:45:10 alpaca-momentum app/worker.1 TypeError: unhashable type: 'list' Jan 08 14:04:26 alpaca-momentum heroku/worker.1 State changed from up to down Jan 08 14:04:28 alpaca-momentum heroku/worker.1 Stopping all processes with SIGTERM Jan 08 14:04:28 alpaca-momentum heroku/worker.1 Process exited with status 143

sylvanosullivan commented 4 years ago

I haven't spun this up yet, so I can't say for sure. However, I can say that: self._handlers in alpaca_trade_api/polygon/streamconn.py is a dictionary. Attempting to use a list as a key to a dictionary results in this error. We see where this could happen in the pattern (self._handlers[channel_pat], None), if channel_pat is a list. It looks like lines 333-336 in algo.py always pass a list as the argument to streamconn.deregister(): conn.deregister([ 'A.{}'.format(symbol), 'AM.{}'.format(symbol) ])

so this error should always occur.