-1
from binance.client import Client
from binance import ThreadedWebsocketManager
import pandas as pd

my_api = ""
my_secret = ""

client = Client(api_key=my_api, api_secret=my_secret, tld="com", testnet=True)

twm = ThreadedWebsocketManager(api_key=my_api, api_secret=my_secret)
twm.start()


def simple_bot(msg):
    ''' define how to process incoming WebSocket messages '''

    time = pd.to_datetime(msg["E"], unit="ms")
    price = float(msg["c"])

    print("Time: {} | Price: {}".format(time, price))

    if int(price) % 10 == 0:
        order = client.create_order(symbol="BTCUSDT", side="BUY", type="MARKET", quantity=0.1)
        print("\n" + 50 * "-")
        print("Buy {} BTC for {} USDT".format(order["executedQty"], order["cummulativeQuoteQty"]))
        print(50 * "-" + "\n")

        twm.stop()

twm.start_symbol_ticker_socket(callback=simple_bot, symbol="BTCUSDT")

These are my codes, and when I tried to run these, I got the error like below.

-----ERROR TEXT-----

Exception in thread Thread-1: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py", line 986, in _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 1089, in create_connection transport, protocol = await self._create_connection_transport( File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 1119, in _create_connection_transport await waiter File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/sslproto.py", line 534, in data_received ssldata, appdata = self._sslpipe.feed_ssldata(data) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/sslproto.py", line 188, in feed_ssldata self._sslobj.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 974, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997)

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1009, in _bootstrap_inner self.run() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/threaded_stream.py", line 56, in run self._loop.run_until_complete(self.socket_listener()) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete return future.result() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/threaded_stream.py", line 35, in socket_listener self._client = await AsyncClient.create(loop=self._loop, **self._client_params) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py", line 7258, in create await self.ping() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py", line 7379, in ping return await self._get('ping', version=self.PRIVATE_API_VERSION) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py", line 7344, in _get return await self._request_api('get', path, signed, version, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py", line 7307, in _request_api return await self._request(method, uri, signed, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py", line 7288, in _request async with getattr(self.session, method)(uri, **kwargs) as response: File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/client.py", line 1138, in aenter self._resp = await self._coro File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/client.py", line 535, in _request conn = await self._connector.connect( File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py", line 542, in connect proto = await self._create_connection(req, traces, timeout) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py", line 907, in _create_connection _, proto = await self._create_direct_connection(req, traces, timeout) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py", line 1206, in _create_direct_connection raise last_exc File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection transp, proto = await self._wrap_create_connection( File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py", line 988, in _wrap_create_connection raise ClientConnectorCertificateError(req.connection_key, exc) from exc aiohttp.client_exceptions.ClientConnectorCertificateError: Cannot connect to host api.binance.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997)')]

I can't understand why this is not working. Could you help me!? Thank you!

Kyungyun Lee
  • 115
  • 9

1 Answers1

3

I had this problem and had recently updated to python 3.10 on a Mac.

You need to go into the Applications/WhateverPythonYouAreUsing folder and click on "Install Certificates.command".

It will open a terminal and install certificates.

That fixed it for me.

DarianJ
  • 31
  • 1