Created
September 11, 2022 14:26
-
-
Save atitawat-pol/d7e5c91bbf1158805f1a17378eb99a3c to your computer and use it in GitHub Desktop.
Real-Time Cryptocurrencies Prices from FTX/Binance using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_connection(): | |
import websocket | |
import json | |
# What to do when opening a new connection | |
def on_open(self): | |
# Prepare sending message | |
sub = json.dumps({ | |
"op": "subscribe", | |
"channel": "trades", | |
"market": "BTC/USD" | |
}) | |
# Send information to the server | |
self.send(sub) | |
print("subscribed") | |
# What to do when receiving a message from server | |
# Our action will be placed here | |
def on_message(self, message): | |
print(message) | |
# What to do when closing a connection | |
def on_close(self): | |
print("### close ###") | |
# websocket logging | |
websocket.enableTrace(True) | |
# websocket object creation | |
wsapp = websocket.WebSocketApp( | |
"wss://ftx.com/ws/", | |
on_open=on_open, | |
on_message=on_message, | |
on_close=on_close | |
) | |
# Run the websocket client | |
wsapp.run_forever() | |
if __name__ == '__main__': | |
test_connection() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment