Topics

Forum Topics not found

Replies

williamfoley285
19 Jul 2024, 07:13 ( Updated at: 22 Jul 2024, 06:25 )

Hello @ poppy playtime chapter 3, I think you can add print statements or logging messages at various points in your code to help debug and track the flow of execution. You can print the received messages, any errors encountered, or other relevant information to aid in troubleshooting.
Here's an updated version of your code with minor modifications for error handling and logging:

python
import websocket
import json
import ssl
import threading
import time

if __name__ == '__main__':
   ws = None
   CLIENT_ID = 'x'
   SECRET = 'xx'
   CTRADER_ID = 'xx'

   # Define the WebSocket endpoint URL
   # websocket_url = "wss://api.ctrader.com/v1/socket"
   websocket_url = "wss://demo.ctraderapi.com:5035"

   # Define your Client ID and Secret
   client_id = CLIENT_ID
   client_secret = SECRET

   # Define the instrument you want to fetch data for
   instrument = "EURUSD"  # Change this to your desired instrument

   # Define the subscription message
   subscribe_message = {
       "op": "subscribe",
       "topic": f"candles.{instrument}.1min",
   }

   # Define the authentication message
   auth_message = {
       "op": "auth",
       "apiKey": client_id,
       "secret": client_secret,
   }

   def on_message(ws, message):
       data = json.loads(message)
       print(data)

       # Check if the message is a candle update
       if data["op"] == "candle":
           candle = data["body"]
           print(f"Received candle: {candle}")

   def on_open(ws):
       print("WebSocket connection opened")
       # Send authentication message when the connection is opened
       ws.send(json.dumps(auth_message))
       # Send the subscribe message to start receiving data
       ws.send(json.dumps(subscribe_message))

   def on_error(ws, error):
       print(f"WebSocket error: {error}")

   def on_close(ws):
       print("WebSocket connection closed")

   ssl_context = ssl.create_default_context()
   # Add your SSL context options and ciphers if necessary

   # Create a WebSocket connection with on_open, on_message, on_error, and on_close callbacks
   ws = websocket.WebSocketApp(
       websocket_url,
       on_message=on_message,
       on_open=on_open,
       on_error=on_error,
       on_close=on_close
   )

   # Run the WebSocket connection in a separate thread
   def run_ws():
       ws.run_forever()

   # Start the WebSocket connection thread
   thread = threading.Thread(target=run_ws)
   thread.start()

   # Keep the main thread alive
   while True:
       try:
           time.sleep(1)
       except KeyboardInterrupt:
           ws.close()
           thread.join()
           break
By incorporating these suggestions and carefully reviewing the WebSocket URL, authentication credentials, and subscription message, you should be able to establish a successful connection and receive data from the WebSocket. Remember to adjust the SSL context options and ciphers if necessary, depending on the server's requirements.


@williamfoley285