Rahan
Rahan

Reputation: 29

Websocket API python for FTX

I am having issue getting connected to the websocket api for crypto exchange FTX. The following link is the doc which is not really clear to me as a beginner. https://docs.ftx.com/?python#websocket-api

Below is the code I am using to try and get ticket data.

import pandas as pd 
import numpy as np 
import json 
import hmac 
import time 
import websocket

socket = 'wss://ftx.com/ws/'

api_key = xxxx
secret_key = xxxx

def on_open(ws):
    print('connected')

    ts = int(time.time() * 1000)
    signa = hmac.new(secret_key.encode(), f'{ts}websocket_login'.encode(), 'sha256').hexdigest()
    auth = {'op': 'login', 'args': {'key': api_key,
                                'sign': signa, 
                                'time': ts}}
    ws.send(json.dumps(auth))
    data = {'op': 'subscribe', 'channel': 'ticker', 'market': 'BTC-PERP'}
    ws.send(json.dumps(data))

def on_close(ws):
    print('disconnected')
def on_message(ws,message):
    print('got message')
    json_msg = json.loads(message)
    print(json_msg)

def on_error(ws,error):
    print(error)

ws = websocket.WebSocketApp(socket,on_open=on_open,on_close=on_close,on_message=on_message,on_error=on_error)

 ws.run_forever()

I just cannot connect to it. I an getting the following message:

[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

If someone can enlight me on how it should work. Thank you

Upvotes: 1

Views: 5026

Answers (1)

Gavril Garin
Gavril Garin

Reputation: 31

Of course you are using websocket now. But I think it is a little difference. Try this

https://www.programcreek.com/python/example/84813/websocket.WebSocketApp

Upvotes: 0

Related Questions