binance-us / binance-us-api-docs

Official Documentation for the Binance US APIs and Streams
407 stars 170 forks source link

websocket for Binance US #58

Open sanJuan2021 opened 2 years ago

sanJuan2021 commented 2 years ago

I have the most primitive python code to see if I can use the websocket for getting data but it doesn't seem to be working properly. Here's the code:

import websocket, json, pprint

SOCKET = "wss://stream.binance.us:9443/ws/ethusd@kline_1m"

def on_open(ws): print('opened connection')

def on_close(ws): print('closed connection')

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

def on_message(ws, message): print('received message') json_message = json.loads(message) pprint.pprint(json_message)

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

The result is the following: [WinError 10061] No connection could be made because the target machine actively refused it on_close() takes 1 positional argument but 3 were given

LasVegasCoder commented 8 months ago

I noticed that this issue was not resolved, here is a corrected and working codes to pull data from Binance network. This is also working if you're in the USA.

# -*- coding: utf-8 -*-
"""
Created on Wed Oct 18 10:00:37 2023

@author: Prince Adeyemi
"""
import json
import websocket

SOCKET = "wss://stream.binance.us:9443/ws"

def getInfo(symbol):
    global my_request
    my_request = json.dumps({'method':'SUBSCRIBE', 'params':[symbol.lower() +'@ticker'], 'id':1})

def on_open(ws):
    print('opened connection, sending request, please wait...')
    ws.send(my_request)

def on_close(ws):
    print('closed connection')

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

def on_message(ws, message):
    print('Receiving data from binance, please wait ...')
    json_message = json.loads(message)

    print(json_message) 

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

TO use this, just run the function and pass the symbol of the coins as the parameter

To check BITCOIN and USDT, do this.

getInfo('BTCUSDT')

RESULT:

opened connection, sending request, please wait...
Receiving data from binance, please wait ...
{'result': None, 'id': 1}
Receiving data from binance, please wait ...
{'e': '24hrTicker', 'E': 1697651551678, 's': 'BTCUSDT', 'p': '-87.20000000', 'P': '-0.307', 'w': '28482.52883944', 'x': '28432.08000000', 'c': '28350.00000000', 'Q': '0.00338000', 'b': '28342.67000000', 'B': '0.00948000', 'a': '28349.98000000', 'A': '0.04510000', 'o': '28437.20000000', 'h': '28941.84000000', 'l': '28140.00000000', 'v': '203.49885000', 'q': '5796161.86391790', 'O': 1697565151678, 'C': 1697651551678, 'F': 25448633, 'L': 25466705, 'n': 18073}
Receiving data from binance, please wait ...
{'e': '24hrTicker', 'E': 1697651552678, 's': 'BTCUSDT', 'p': '-87.20000000', 'P': '-0.307', 'w': '28482.52883944', 'x': '28432.08000000', 'c': '28350.00000000', 'Q': '0.00338000', 'b': '28342.67000000', 'B': '0.00948000', 'a': '28349.96000000', 'A': '0.04510000', 'o': '28437.20000000', 'h': '28941.84000000', 'l': '28140.00000000', 'v': '203.49885000', 'q': '5796161.86391790', 'O': 1697565152677, 'C': 1697651552677, 'F': 25448633, 'L': 25466705, 'n': 18073}
Receiving data from binance, please wait ...
{'e': '24hrTicker', 'E': 1697651553678, 's': 'BTCUSDT', 'p': '-87.20000000', 'P': '-0.307', 'w': '28482.52883944', 'x': '28432.08000000', 'c': '28350.00000000', 'Q': '0.00338000', 'b': '28342.67000000', 'B': '0.00948000', 'a': '28349.94000000', 'A': '0.04510000', 'o': '28437.20000000', 'h': '28941.84000000', 'l': '28140.00000000', 'v': '203.49885000', 'q': '5796161.86391790', 'O': 1697565153678, 'C': 1697651553678, 'F': 25448633, 'L': 25466705, 'n': 18073}