alpacahq / alpaca-backtrader-api

Alpaca Trading API integrated with backtrader
https://pypi.org/project/alpaca-backtrader-api/
Apache License 2.0
615 stars 143 forks source link

your connection is rejected while another connection is open under the same account #75

Open JustinGuese opened 4 years ago

JustinGuese commented 4 years ago

Hey, I am running into an error with multiple data feeds, e.g. with the example https://github.com/alpacahq/alpaca-backtrader-api/blob/master/sample/strategy_multiple_datas.py

Error: alpaca_trade_api\stream2.py", line 52, in _connect raise Exception(f"Error while connecting to {self._endpoint}:" Exception: Error while connecting to wss://data.alpaca.markets/stream:your connection is rejected while another connection is open under the same account


It's working with one data feed, and I restarted my computer, tried another account, ... & the same message appears.

Running Windows 10, Python 3.6

My guess is, that it is related to me not having a "full" account because I'm non-US and the datafeed might only allow one feed per paper account?

shlomiku commented 4 years ago

Hi Justin it is not related to your account type alpaca-backtrader opens a websocket to each data you add. there's a branch that I work on to bypass this issue. it is not compelte so I still don't merge it to the master branch but you may try it out (and give me your feedback) install it like this: pip install -U git+https://github.com/alpacahq/alpaca-backtrader-api@shared_websocket_streamer

JustinGuese commented 4 years ago

Perfect! I'll check it out and let you know!

dileshan commented 4 years ago

I'm having the same issue, my strategy uses multiple datas as well. I'm curious on the solution, I assume you can just add another listener to the original connection, this is what I would do using wscat...

dileshan commented 4 years ago

Installing the branch shared_websocket_streamer worked for me running against the samples, although I'll need to wait for the market to open for a true test.

dsqx71 commented 4 years ago

Hi, I'm wondering why the branch has still not been merged yet? I'm wondering if there are any issues with the branch.

shlomiku commented 4 years ago

there's a better solution: https://github.com/shlomikushchi/alpaca-proxy-agent

dsqx71 commented 4 years ago

Hi @shlomikushchi

I'm trying to solve the problem by using Alpaca-proxy-agent, but I encountered more errors.

error while consuming ws messages: server rejected WebSocket connection: HTTP 503

error while consuming ws messages: Error while connecting to wss://data.alpaca.markets/stream:your connection is rejected while another connection is open under the same account

Could you please take a look when you get a chance? Thanks

To reproduce the aforementioned bugs:

Here're the module versions that I was using

The strategies consume multiple tickers datasets.

shlomiku commented 4 years ago

Hi, you are running more than one ws connections still. this is the error you get. I am now making final modifications to this repo to make it compatible with the alpaca-proxy-agent. you can see the changes here: https://github.com/alpacahq/alpaca-backtrader-api/pull/109

in this readme there's a section explaining how to use the proxy agent: https://github.com/alpacahq/alpaca-backtrader-api/blob/make_project_compatible_to_proxy_agent/README.md

also make sure you have the correct branch installed ( not yet released to pypi). you can install it like this: pip install git+https://github.com/alpacahq/alpaca-backtrader-api@make_project_compatible_to_proxy_agent

dsqx71 commented 4 years ago

@shlomikushchi Thanks for the quick fix, it seems to be working. A minor issue is that, sometimes, it gives me the following warning:

error while consuming ws messages: [Errno 110] Connect call failed ('192.168.99.100', 8765)

The code doesn't crash though.

shlomiku commented 4 years ago

Hi, is your docker running in ip 192.168.99.100? what OS are you using? not being able to connect to your own machine is a bit weird.. it's internal networking

dsqx71 commented 4 years ago

Sorry, it's my bad, the default IP is not 192.168.99.100. The OS is Ubuntu 16.04.

After connected to the Websocket, it gives the following log:

connected to: ws://172.17.0.2:8765/stream
connected to: ws://172.17.0.2:8765/stream
code = 1000 (OK), no reason
error while consuming ws messages: consume cancelled

It's not in the regular trading hours when I was testing the code, I guess that's probably the reason behind the error. Anyway, thanks for the hint!

dsqx71 commented 4 years ago

I tested it again in the trading hours and still got the error:

connected to: ws://172.17.0.2:8765/stream
code = 1000 (OK), no reason
error while consuming ws messages: consume cancelled
shlomiku commented 4 years ago

Hi I will be releasing a new version soon. will let you know so you could test it.

dsqx71 commented 4 years ago

I just checked out the latest version of proxy agent 0.50.0. It doesn't work. Got the following error:

error while consuming ws messages: Error while connecting to wss://data.alpaca.markets/stream:your connection is rejected while another connection is open under the same account
error while consuming ws messages: server rejected WebSocket connection: HTTP 503

Edit: sorry, I was using alpaca-backtrader 0.10.1. Never mind, it's a known issue.

vanshtuli commented 3 years ago

Hi @shlomikushchi,

I tried connecting to the alpaca proxy agent as in the pictured attached, but for some reason, it does not connect to the agent and is still fetching data. Can you please tell me what is wrong with my implementation?

image

shlomiku commented 3 years ago

Hi @vanshtuli What version of alpaca-backtrader-api do you have installed?

vanshtuli commented 3 years ago

Hey @shlomikushchi I fixed it. Wasn't an issue with alpaca-backtrader. Had to add an environment variable to redirect my requests to the proxy agent.

Thanks

StefanMarchand commented 3 years ago

Hey @shlomikushchi I'm not sure what I am doing wrong. I have the proxy agent running in docker. I believe I set my environment variable DATA_PROXY_WS=ws://172.17.0.3:8765 but it still connects to:

2021-04-14 14:59:25,972 connected to: wss://paper-api.alpaca.markets/stream 2021-04-14 14:59:26,072 connected to: wss://data.alpaca.markets/stream

I'm on MacOS 10.15.7 alpaca-backtrader-api = 0.13.1 alpaca-trade-api = 0.52.0

-- | -- | --