@@ -44,3 +44,189 @@ Some examples require additional Python packages:
4444- All examples connect to the demo environment by default. To use the live environment, set the ` PROJECTX_ENVIRONMENT ` environment variable to "live".
4545- The examples are designed to be interactive and will prompt for user input when needed.
4646- Make sure your API credentials have the necessary permissions for the actions demonstrated in each example.
47+
48+ ## Running the Examples
49+
50+ To run these examples, you'll need:
51+
52+ 1 . A ProjectX account with API access
53+ 2 . Your username and API key
54+
55+ Set these as environment variables:
56+
57+ ``` bash
58+ export PROJECTX_USERNAME=your_username
59+ export PROJECTX_API_KEY=your_api_key
60+ ```
61+
62+ Then run an example:
63+
64+ ``` bash
65+ python realtime_example.py
66+ ```
67+
68+ ## Troubleshooting Realtime Connections
69+
70+ If you're experiencing issues with realtime connections, follow this troubleshooting guide:
71+
72+ ### Common Issues and Solutions
73+
74+ #### No events being received
75+
76+ If you've subscribed to market events but aren't receiving any callbacks:
77+
78+ 1 . ** Check Authentication** : Ensure your API key is valid and has appropriate permissions.
79+ ``` python
80+ # Verify authentication is working
81+ accounts = client.accounts.get_accounts()
82+ print (accounts) # If this fails, you have an authentication issue
83+ ```
84+
85+ 2 . ** Verify Connection Establishment** : Add debug logging to check connection status.
86+ ``` python
87+ import logging
88+ logging.basicConfig(level = logging.DEBUG )
89+ ```
90+
91+ 3 . ** Check Method Names** : The exact method names must match between client and server.
92+ - SubscribeContractQuotes
93+ - SubscribeContractTrades
94+ - SubscribeContractMarketDepth
95+
96+ 4 . ** Check Contract IDs** : Verify the contract ID is valid and properly formatted.
97+ ``` python
98+ # Get all available contracts
99+ contracts = client.contracts.search_contracts()
100+ valid_ids = [c[" id" ] for c in contracts[" contracts" ]]
101+ print (valid_ids)
102+ ```
103+
104+ 5 . ** Enable Signal-R Debug Mode** : Directly configure the SignalR connection:
105+ ``` python
106+ # Add more debug logging
107+ import signalrcore.hub.errors
108+ signalrcore.hub.errors.HubError.verbose = True
109+ ```
110+
111+ 6 . ** Verify Event Names** : The events from the server must match these exactly:
112+ - GatewayQuote
113+ - GatewayTrade
114+ - GatewayDepth
115+
116+ 7 . ** Check Callback Function Signatures** : Your callbacks should accept 2 parameters:
117+ ``` python
118+ def handle_quote (contract_id : str , data : dict ):
119+ # Both parameters are required
120+ print (f " Contract: { contract_id} , Data: { data} " )
121+ ```
122+
123+ 8 . ** Connection Timing** : Ensure the connection is fully established before subscribing:
124+ ``` python
125+ # Start first, then subscribe
126+ realtime.start()
127+ time.sleep(2 ) # Give it time to connect
128+ market_hub.subscribe_quotes(contract_id, handle_quote)
129+ ```
130+
131+ #### Connection Drops or Errors
132+
133+ If the connection is unstable or drops frequently:
134+
135+ 1 . ** Check Network Stability** : Realtime connections require stable internet.
136+
137+ 2 . ** Implement Reconnect Logic** : Use the built-in reconnect functionality:
138+ ``` python
139+ # The SDK handles reconnection automatically
140+ # You can also manually reconnect
141+ realtime.stop()
142+ time.sleep(1 )
143+ realtime.start() # This will reestablish connections and resubscribe
144+ ```
145+
146+ 3 . ** Check for Credential Expiration** : Ensure your token hasn't expired.
147+ The SDK should handle this automatically, but you can verify:
148+ ``` python
149+ # Force token refresh
150+ client.auth.validate_token()
151+ ```
152+
153+ ### Debugging Tools
154+
155+ 1 . ** Connection Testing Script** :
156+ ``` python
157+ import time
158+ from projectx_sdk import ProjectXClient
159+ from projectx_sdk.realtime import RealtimeService
160+
161+ # Enable verbose logging
162+ import logging
163+ logging.basicConfig(level = logging.DEBUG )
164+
165+ client = ProjectXClient(username = " ..." , api_key = " ..." , environment = " demo" )
166+ realtime = RealtimeService(client)
167+
168+ # Connection state callbacks
169+ def on_connect ():
170+ print (" CONNECTED!" )
171+
172+ def on_disconnect ():
173+ print (" DISCONNECTED!" )
174+
175+ # Manually attach to connection events
176+ realtime.market._connection.on_open(on_connect)
177+ realtime.market._connection.on_close(on_disconnect)
178+
179+ # Start the connection
180+ realtime.start()
181+
182+ # Keep alive
183+ try :
184+ while True :
185+ time.sleep(1 )
186+ except KeyboardInterrupt :
187+ realtime.stop()
188+ ```
189+
190+ 2 . ** Data Structure Inspection** :
191+ ``` python
192+ def debug_callback (contract_id , data ):
193+ import json
194+ print (f " CONTRACT: { contract_id} " )
195+ print (f " DATA: { json.dumps(data, indent = 2 )} " )
196+
197+ market_hub.subscribe_quotes(" CON.F.US.ENQ.H25" , debug_callback)
198+ ```
199+
200+ ### Advanced Troubleshooting
201+
202+ If basic troubleshooting doesn't resolve your issue:
203+
204+ 1 . ** Direct SignalR Connection** : Bypass the SDK abstraction layer:
205+ ``` python
206+ from signalrcore.hub_connection_builder import HubConnectionBuilder
207+
208+ token = client.auth.get_token()
209+ hub_url = " https://gateway-api-demo.s2f.projectx.com/hubs/market"
210+
211+ connection = (
212+ HubConnectionBuilder()
213+ .with_url(f " { hub_url} ?access_token= { token} " )
214+ .with_automatic_reconnect({
215+ " type" : " raw" ,
216+ " keep_alive_interval" : 10 ,
217+ " reconnect_interval" : 5 ,
218+ " max_attempts" : 10 ,
219+ })
220+ .build()
221+ )
222+
223+ connection.on(" GatewayQuote" , lambda contract_id , data :
224+ print (f " QUOTE: { contract_id} - { data} " ))
225+
226+ connection.start()
227+ connection.invoke(" SubscribeContractQuotes" , " CON.F.US.ENQ.H25" )
228+ ```
229+
230+ 2 . ** Network Traffic Analysis** : Use tools like Wireshark to inspect the WebSocket traffic.
231+
232+ 3 . ** Check Server Status** : Verify the ProjectX API status and whether there are any known issues.
0 commit comments