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

Event loop is already running and websocket question #126

Closed
wjg59701 opened this issue Dec 8, 2019 · 6 comments
Closed

Event loop is already running and websocket question #126

wjg59701 opened this issue Dec 8, 2019 · 6 comments

Comments

@wjg59701
Copy link

wjg59701 commented Dec 8, 2019

Hello, I am trying to run the code example that trades on slight moves in the bid/ask spread, posted here on github.

Using alpaca_trade_api StreamConn object, I get a runtime error "This event loop is already running"

I have also tried directly connecting to the alpaca api just using the websockets library but I need a wss: url and alpaca only provides the https .../stream address.

can someone help and/or point me to a good source/tutorial for establishing multiple websockets and asyncio?

I have a polygon account for streaming data and I can simply connect to that as a websocket but I want to use the alpaca stream 'trade_updates' as well.

@ttt733
Copy link
Contributor

ttt733 commented Jan 29, 2020

The API URL for a direct connection is the same, just with https:// replaced with wss://. I.e., wss://paper-api.alpaca.markets or wss://api.alpaca.markets. So a connection might look like:

conn = tradeapi.StreamConn(
        'X',
        'X',
        'wss://paper-api.alpaca.markets'
    )

@conn.on(r'trade_updates')
async def on_trade_update(conn, channel, data):
    pass

conn.run(['trade_updates'])

Replace the Xs with your API keys and pass with whatever code you want to handle them. If you have trouble connecting in this way, please reopen the issue and let us know more - copy the full error output if possible.

@ttt733 ttt733 closed this as completed Jan 29, 2020
@GitHubEmploy
Copy link

Hello, I tried running this and it started spamming 'ERROR:root:error while consuming ws messages: server rejected WebSocket connection: HTTP 429', and every 3 it said, 'ERROR:root: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'. So now I am trying to figure out how to terminate that connection. This happened when I clicked disconnect instead of terminate.

@shlomiku
Copy link
Contributor

Hi, what do you mean "clicked disconnect" where did you click?
the errors you see caused by the websocket client trying to reconnect to the server

@GitHubEmploy
Copy link

Ok, let me explain in more detail. I use a IDE called PyCharm, and when you stop a program in PyCharm you have 2 options. You can either disconnect from the program, or terminate the program. The main difference is disconnection is where it is still running on your computer, but PyCharm is no longer hosting the program, and terminate is where you stop the program.

@shlomiku
Copy link
Contributor

shlomiku commented Sep 2, 2020

so you need to kill the python process running this code. you can't have more than one connection to the websocket, this is why you get this error.

@nickvertucci
Copy link

@ttt733 Are you able to assist with the momentum template? I just ran into this same error.

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

No branches or pull requests

5 participants