ig-python / trading-ig

A lightweight Python wrapper for the IG Markets API
https://trading-ig.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
314 stars 197 forks source link

Stream failing. Cause: Websocket transport not available #340

Open mrxz44 opened 1 day ago

mrxz44 commented 1 day ago

Hi, anyone having issues with stream? I've tried sample 'stream_ig.py' with Live account credentials and looks like there is something with websocket connection.

Creating new v2 session for user 'ex_data_user' at 'https://api.ig.com/gateway/deal'
POST '/session', resp 200
Starting connection with https://apd.marketdatasystems.com
2024-10-21 14:28:51.483802|INFO |lightstreamer.actions|MainThread|serverAddress changed: https://apd.marketdatasystems.com
2024-10-21 14:28:51.483839|INFO |lightstreamer.actions|MainThread|user changed: null
2024-10-21 14:28:51.483855|INFO |lightstreamer.actions|MainThread|password changed
2024-10-21 14:28:51.484009|INFO |lightstreamer.actions|MainThread|Connection requested: details: [serverAddress => https://apd.marketdatasystems.com, libVersion => python_client 2.1.0 build 20231219] options: [requestedMaxBandwidth => BWUnlimited, retryDelay => 4000, firstRetryMaxDelay => 100, sessionRecoveryTimeout => 15000, reverseHeartbeatInterval => 0, stalledTimeout => 2000, reconnectTimeout => 3000, keepaliveInterval => 0, pollingInterval => 0, idleTimeout => 19000, contentLength => 50000000, slowingEnabled => false, serverInstanceAddressIgnored => false, HTTPExtraHeadersOnSessionCreationOnly => false]
2024-10-21 14:28:51.484048|INFO |lightstreamer.session|MainThread|Status: CONNECTING
2024-10-21 14:28:51.484735|INFO |lightstreamer.actions|MainThread|Subscription requested: subId: 1 [mode => MERGE, items => [MARKET:CS.D.BITCOIN.CFD.IP,MARKET:CS.D.EURUSD.MINI.IP,MARKET:IX.D.SPTRD.IFMM.IP], fields => [UPDATE_TIME,BID,OFFER,CHANGE,MARKET_STATE,CHANGE_PCT,HIGH,LOW], requestedSnapshot => SnpYes]
2024-10-21 14:28:51.485194|INFO |lightstreamer.actions|MainThread|Subscription requested: subId: 2 [mode => MERGE, items => [ACCOUNT:AU4YF], fields => [FUNDS,MARGIN,AVAILABLE_TO_DEAL,PNL,EQUITY,EQUITY_USED], requestedSnapshot => SnpYes]
2024-10-21 14:28:51.485375|INFO |lightstreamer.actions|MainThread|Subscription requested: subId: 3 [mode => DISTINCT, items => [TRADE:AU4YF], fields => [CONFIRMS,OPU,WOU], requestedSnapshot => SnpYes]
----------------------HIT CR TO UNSUBSCRIBE AND DISCONNECT----------------------
2024-10-21 14:28:51.688173|WARN |lightstreamer.session|Thread-2 (_hx_local_0)|Websocket suspended
2024-10-21 14:28:51.688272|ERROR|lightstreamer.session|Thread-2 (_hx_local_0)|Retrying connection. Cause: Websocket transport not available

image

dario-weswit commented 1 day ago

Try modifying stream_ig.py and replace "ConsoleLogLevel.INFO" with "ConsoleLogLevel.DEBUG". It should log some details. Note that the DEBUG log is verbose when a connection is in place, hence the change should be reverted once the connection issue is sorted out.

mrxz44 commented 7 hours ago

Thanks @dario-weswit ! looks like the issue with SSL certificate here:

----------------------HIT CR TO UNSUBSCRIBE AND DISCONNECT----------------------
2024-10-22 09:26:16.848797|DEBUG|lightstreamer.stream|LS_IO|WS event: error(Cannot connect to host apd.marketdatasystems.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)')])
Exception: Cannot connect to host apd.marketdatasystems.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)')]
Called from <unknown>._wrap_create_connection (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/connector.py line 1077)
Called from <unknown>._create_direct_connection (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/connector.py line 1312)
Called from <unknown>._create_direct_connection (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/connector.py line 1343)
Called from <unknown>._create_connection (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/connector.py line 1009)
Called from <unknown>.connect (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/connector.py line 556)
Called from <unknown>._request (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/client.py line 661)
Called from <unknown>._ws_connect (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/client.py line 935)
Called from <unknown>.__aenter__ (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/client.py line 1357)
Called from <unknown>._connectAsync (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lightstreamer/client/com_lightstreamer_net.py line 144)
2024-10-22 09:26:16.854070|WARN |lightstreamer.session|Thread-2 (_hx_local_0)|Websocket suspended
2024-10-22 09:26:16.854142|DEBUG|lightstreamer.stream|Thread-2 (_hx_local_0)|WS disposing
2024-10-22 09:26:16.854221|ERROR|lightstreamer.session|Thread-2 (_hx_local_0)|Retrying connection. Cause: Websocket transport not available

I'm on mac m1 pro, is there anything specific to do with ssl?

acarioni commented 5 hours ago

It is recommended to avoid using a self-signed certificate for a public server. However, if you own the certificate, you can install it in the Lightstreamer library with the following steps:

sslcontext = ssl.create_default_context(cafile='cert.crt')
sslcontext.load_cert_chain(certfile='cert.crt', keyfile='cert.key')
LightstreamerClient.setTrustManagerFactory(sslcontext)

In this example, 'cert.crt' is the path to the certificate in PEM format, and 'cert.key' is the path to the private key. See also: Python ssl module and LightstreamerClient.setTrustManagerFactory