Websockets: not able to retrieve market data

Hi, Thanks a lot for sharing that.

I am trying to use your code but its throwing syntax errors. Can you please share the python version you are using.

Also a test sample of how to use this code please

Can you please have a look at following gist:

I am able to run the code you shared, update the gist.

How do i communicate with the script, i did not update any credentials and run it just to test, so it must not be working.

can you please point out where i can update my username and password?

I am using the python multiprocessing library

Since I have 2 or more processes running and I would like to be faster than a queue. I use the multiprocessing shared memory types to pass messages between processes.

It’s similar to setting up a stack before calling a function in a language like c/c++.

This is earlier code and should be replaced.

That code above looks like a really early version;
instead of passing md_hb_value to that function; pass in the payload dictionary
inside the payload add keys mda_hb_init and mda_running

payload['mda_hb_init']
payload['mda_running']

def init_md_heartbeat(payload):
     while(not payload['mda_running']):
          wait until you initialize and connect to the websocket; then set payload['mda_hb_init'] = True
     while(payload['mda_running']):
           if(payload['mda_hb_init']):
                    add a try catch block to make sure the websocket connection is still up, tradovate websockets disconnect quite often
                    
                    if your heartbeat interval passed and there's no exception; send a heartbeat and sleep
                    if there's an exception, set payload['mda_hb_init'] to false, re-initialize the websocket

i renamed management package to payload since it’s shorter, u can simplify it further if it suites your needs.
if you have any further questions post below.

1 Like

Hi,

Thank you! for your time and prompt reply.

I have replaced the code snippet in gist websocket.py · GitHub

I have a demo account and i want to monitor orders created and executions.

Can you please share a usage example and where i have to update the credentials.

Thanks again

update, i did not see that code snippet has pseudo code in it :slight_smile:

I am a beginner so if you can please share an example that would be helpful …

you’re going to have to do some problem solving to get going.

look at the file __tv_api.py, there’s a line at the top with these imports:

from my_secrets import APP_ID, APP_VERSION, ACCOUNT, ACCOUNT_P, CID, CLIENT_SECRET, DEVICE_ID

create the file my_secrets.py and place it next to the __tv_api.py file, inside the __tv_api.py file, define those variables; you get those from the Tradovate backend.

I don’t think you get API access on a demo account. Things might have changed, I am not sure.

1 Like

Hi,

your code and questions have been helpful. Can you please share tradovate_auth after redacting the sensitive information off course.

Thanks.

apologies for the delay, what exactly are you looking for?

@Ingo, @Alexander in order for you to receive chart data did you have to register as Data Distributor with CME? The Tradovate tech support keeps on telling me that I need to register. Called CME they are completely clueless why I would have to register. Just contacted CME’s global account management on this matter.

hi ingo I am having the problem you had, but don’t know what you mean you need the contract library.

Thanks in advance.

Does any one know how to terminate the API websocket. I know socket.send() is the heartbeat reply you send. Is the a code that you can send it to close the websocket?

Thanks in advance.

Typically any websocket API will implement socket.close() or something to that effect to terminate the channel manually.

thanks alot, that helped.

I am having another problem. My websocket is open, every thing is working well. When I send an order, it goes through. I can see it, but the socket stops working properly and I get this error. Am I supposed to do something after an order goes through.

Task exception was never retrieved
future: <Task finished name=‘heart_beat’ coro=<heart_beat() done, defined at C:\pythonproject\Tradovate\mdwebsocket.py:23> exception=ConnectionClosedError(‘code = 1006 (connection closed abnormally [internal]), no reason’)>

Can anyone please help me…I have market data subscription. I was able to setup my webscoket to it, but when i tried the quote end point to get a quote I am getting this error.

a[{“s”:200,“i”:2,“d”:{“errorText”:“Symbol is inaccessible”,“errorCode”:“UnknownSymbol”,“mode”:“None”}}]

this is the code:

payload = {“symbol”: symbol}
await websocket.send(f"md/subscribeQuote\n2\n\n{json.dumps(body)}")

Thanks in advance

payload = {“symbol”: symbol}
await websocket.send(f"md/subscribeQuote\n2\n\n{json.dumps(payload)}")

This seems to be the issue. Maybe contact CME? Sorry for the bad news…

thanks for your help. do you you what others having the same problems are doing?

thanks

Hi there! Do you have, by any changed, the complete code/example on how to retrieve market data with Python and websockets? Some links are broken here, and I wanted to try an example and build up from there… thanks so much!

Dear all,

Is there any repository of python examples of all the functions of tradovate api? It would be really helpful for the beginners like me who are familiar just with python.

As of now I am looking for the piece of code to fetch 15 minutes bar historical data of NQ futures.

Thanks,
Sunil

The issue is your uri it should look like this which is in javascript you will have to convert to python.

md/subscribeQuote\n ${qouteId}\n""\n${JSON.stringify({ symbol })}

The symbol must be a stringed object symbol:value