Can't connect to websocket API

Viewed 2835

Now I’m working on developing WebSocket to get data (ex.btcusdt) from the website FTX since the ftx.com you can trade the crypto without having to pay fees and I have a minimum budget

so now I want to try out to get some data and making my own bot

but now I’m having a problem with how I connect to the website

since I watch Binance video I was trying the same way but still didn’t get any message from the " wss://ftx.com/ws/ "

I do not quite understand the document they provide

my question is how can I connect to the data stream for example if I want to get the JSON file of BTCUSDT or BULLUSDT

this is the document they provide https://docs.ftx.com/#websocket-api

Thank you

My code

import websocket
SOCKET = "wss://ftx.com/ws/"
def on_open(ws):
    print('opened connection')
def on_close(ws):
    print('closed connection')
def on_message(ws, message):
    print("got message")
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()

This works find with Binance

3 Answers

Using the example code here modified to accept api keys as arguments, here is an example of grabbing ticker data:

if __name__ == '__main__':
    # rest = client.FtxClient(api_key=key, api_secret=secret)
    ws = ws_client.FtxWebsocketClient(api_key=key, api_secret=secret)
    ws.connect()
    for i in range(1, 10):
        print(ws.get_ticker(market='BTC-PERP'))
        time.sleep(1)

As Chev_603 say you can copy the two files into your directory. Then, import the files at the begging of your app and use:

from client import FtxWebsocketClient
from websocket_manager import WebsocketManager

if __name__ == '__main__':
    for i in range(1, 1000):
        print(ws.get_ticker(market='BTC-PERP'))
        time.sleep(0.2)

# your own api_key and api_secret must be written into the client.py lines 20 and 21

JSON message:

import websocket
import json

this = json.dumps({'op': 'subscribe', 'channel': 'trades', 'market': 'BTC- 
PERP'})

def on_open(wsapp):
    wsapp.send(this)

def on_message(wsapp, message):
    print(message)

wsapp = websocket.WebSocketApp("wss://ftx.com/ws/", on_message=on_message, 
on_open=on_open)
wsapp.run_forever()
Related