alpacahq / Momentum-Trading-Example

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

ERROR:root:error while consuming ws messages: cannot set a row with mismatched columns #26

Open ghoshsanjoy78 opened 4 years ago

ghoshsanjoy78 commented 4 years ago

I am trying to run the code. I goes as far as "Watching symbols" But then it keeps hitting this error - ERROR:root:error while consuming ws messages: cannot set a row with mismatched columns What could be the issue and how do I fix it?

jusmark123 commented 4 years ago

The new api version returns an additional column 'vwap' in the historic_agg_v2 call. Add the following just under the call

if 'vwap' in minute_history[symbol]:
             minute_history[symbol].drop('vwap', axis=1, inplace=True)
Gabowatt commented 4 years ago

@jusmark123 I just added this and it eliminated that issue but now it gets stuck on "watching symbols" forever. Have you faced a similar issue?

Change made: for symbol in symbols: minute_history[symbol] = api.polygon.historic_agg_v2( timespan="minute", symbol=symbol, limit=1000, multiplier=1, _from=yesterday.strftime("%Y-%m-%d"), to=today.strftime("%Y-%m-%d") ).df if 'vwap' in minute_history[symbol]: minute_history[symbol].drop('vwap', axis=1, inplace=True) c += 1 print('{}/{}'.format(c, len(symbols))) print('Success.') return minute_history

jusmark123 commented 4 years ago

Not sure, the script waits for until 15 minutes after market to open then gathers symbols to watch. Buying mode takes place for the first hour of execution, then it switches to sell mode for the rest of the day. The issue could be that the buying window was missed so there nothing to sell. So it just watches the symbols taking no action. I set a cron to start the script at 6:30 AM EST

Justin Walker

Sent from my iPhone

On Sep 9, 2020, at 9:55 AM, Gabowatt notifications@github.com wrote:

 @jusmark123 I just added this and it eliminated that issue but now it gets stuck on "watching symbols" forever. Have you faced a similar issue?

Change made: for symbol in symbols: minute_history[symbol] = api.polygon.historic_agg_v2( timespan="minute", symbol=symbol, limit=1000, multiplier=1, _from=yesterday.strftime("%Y-%m-%d"), to=today.strftime("%Y-%m-%d") ).df if 'vwap' in minute_history[symbol]: minute_history[symbol].drop('vwap', axis=1, inplace=True) c += 1 print('{}/{}'.format(c, len(symbols))) print('Success.') return minute_history

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

zenwaffles commented 3 years ago

@Gabowatt @jusmark123 I am having the same problem in which, after fixing the mismatched columns, it just gets stuck on "watching symbols" forever. I have run it for multiple days now, and it never buys. Did either of your figure out why this is or fix it in some way?

nickvertucci commented 3 years ago

Any resolve to this? I have a running code that also stops at watching symbols

GreengageLTD commented 3 years ago

Have the same issue. Made the edit @jusmark123 offered but it still stalls. is it an issue with multiple feeds? Can a dummy column be inserted to match them up? What would that look like?

Getting current ticker data... Success. Tracking 158 symbols. Getting historical data... 1/158 Success. Watching 158 symbols. ERROR:root:error while consuming ws messages: cannot set a row with mismatched columns ^CTraceback (most recent call last): File "/home/primary/.local/lib/python3.6/site-packages/alpaca_trade_api/stream2.py", line 331, in run loop.run_until_complete(self.consume()) File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete return future.result() File "/home/primary/.local/lib/python3.6/site-packages/alpaca_trade_api/stream2.py", line 316, in consume self.data_ws.consume(), File "/home/primary/.local/lib/python3.6/site-packages/alpaca_trade_api/polygon/streamconn.py", line 108, in cons ume await self._consume_task File "/home/primary/.local/lib/python3.6/site-packages/alpaca_trade_api/polygon/streamconn.py", line 114, in _con sume_msg await self._dispatch(data) File "/home/primary/.local/lib/python3.6/site-packages/alpaca_trade_api/polygon/streamconn.py", line 221, in _dis patch await handler(self, channel, ent) File "wm4_v2.txt", line 192, in handle_second_bar minute_history[symbol].loc[ts] = new_data File "/home/primary/.local/lib/python3.6/site-packages/pandas/core/indexing.py", line 670, in setitem iloc._setitem_with_indexer(indexer, value) File "/home/primary/.local/lib/python3.6/site-packages/pandas/core/indexing.py", line 1626, in _setitem_with_inde xer self._setitem_with_indexer_missing(indexer, value) File "/home/primary/.local/lib/python3.6/site-packages/pandas/core/indexing.py", line 1856, in _setitem_with_inde xer_missing raise ValueError("cannot set a row with mismatched columns") ValueError: cannot set a row with mismatched columns During handling of the above exception, another exception occurred: Traceback (most recent call last): File "wm4_v2.txt", line 401, in run(get_tickers(), market_open, market_close) File "wm4_v2.txt", line 372, in run

jusmark123 commented 3 years ago

The error is telling you that the data being added to a dataFrame does have the same columns. You can place a print statement during iterations to find the issue. It shouldn't have anything to do with multiple streams. The trace states that it is happening in the run() function. You can print the DataFrame columns with df.columns.values.tolist() you can even set up a try/except statement that will catch the error so you can print the column. If you are using and IDE like spyder or pycharm then you can debug and step through the code to see your variables including dataFrame structure.

GreengageLTD commented 3 years ago

@jusmark123 Thank you. It looks a lot better today. I appreciate your help.

domnicsavio commented 3 years ago

I'm having the same issue